It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Similar documents
1 Short Answer (10 Points Each)

1 Short Answer (7 Points Each)

1 Short Answer (10 Points Each)

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

1 Short Answer (10 Points Each)

1 Short Answer (5 Points Each)

1 Short Answer (10 Points Each)

1 Definitions & Short Answer (5 Points Each)

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

1 Short Answer (2 Points Each)

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

1 Short Answer (15 Points Each)

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

5. What is a block statement? A block statement is a segment of code between {}.

Question: Total Points: Score:

Midterm Examination (MTA)

Esc101 Mid Semester Exam - II

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Arrays. Eng. Mohammed Abdualal

Intro to Programming in Java Practice Midterm

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Repetition, Looping. While Loop

Objectives of the lesson

System.out.printf("Please, enter the value of the base : \n"); base =input.nextint();

CIS November 14, 2017

CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CS141 Programming Assignment #10

1 Short Answer (15 Points Each)

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CS111: PROGRAMMING LANGUAGE II

1 Definitions & Short Answer (4 Points Each)

University of Palestine. Mid Exam Total Grade: 100

LAB 7. Objectives: Navin Sridhar D 8 54

1 Short Answer (5 Points Each)

Computer programming Code exercises [1D Array]

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Intermediate Programming

CSE 1223: Exam II Autumn 2016

Control Structures: if and while A C S L E C T U R E 4

Last Class. While loops Infinite loops Loop counters Iterations

3/18/2015. Chapter 19 Sorting and Searching SELECTION SORT SORTING AN ARRAY OF INTEGERS SORTING AN ARRAY OF INTEGERS FILE SELECTIONSORTER.

Data Structures COE 312 ExamII Preparation Exercises

Problems with simple variables

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Question: Total Points: Score:

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Introduction to Computer Science Unit 2. Exercises

Introduction to Computer Science Midterm 3 Fall, Points

Controls Structure for Repetition

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 25, 2016

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Top-Down Program Development

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Object Oriented Programming. Java-Lecture 6 - Arrays

CS 101 Spring 2007 Midterm 2 Name: ID:

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Chapter 6 Lab Classes and Objects

Instructor: Yu Wang 11/16/2012

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

To keep track of this new wrinkle we need some new variables at the Class level:

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

Chapter 6 Lab Classes and Objects

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Lab Assignment Three

COS 126 General Computer Science Spring Written Exam 1

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

For example, when your method is added to the class with the main method below, the output should be:

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

St. Edmund Preparatory High School Brooklyn, NY

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Loops. CSE 114, Computer Science 1 Stony Brook University

Programming with Java

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Lesson 7 Part 2 Flags

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Assignment2013 Please use this document only for verifying if your programs are right. Do not blindly copy paste and waste your time.

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

H212 Introduction to Software Systems Honors

PROGRAMMING FUNDAMENTALS

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

CS170 Introduction to Computer Science Midterm 2

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Fundamentals of Programming Data Types & Methods

Transcription:

COSC 117 Exam 3 Key Fall 2012 Part 1: Definitions & Short Answer (3 Points Each) 1. A method in a class that has no return type and the same name as the class is called what? How is this type of method called from the main function? It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff(); 2. What is the difference between a private class member and a public class member? All member variables should be which type? Public members can be accessed from outside the class and private members can only be accessed from inside the class. All data members should be private. 3. Write a declaration that will create a one-dimensional integer array that will hold 20 items. Then write a segment of code that will put the numbers 3, 5, 1, -2, and 4 in the first 5 entries of the array and the numbers 100, 200, 300, in the remainder of the array, using a loop. int[] D = new int[20]; D[0] = 3; D[1] = 5; D[2] = 1; D[3] = -2; D[4] = 4; for (int i = 0; i < 15; i++) D[i + 5] = 100 * i; 4. Write a declaration that will create a two-dimensional array of doubles that has 10 rows and 23 columns. double[][] A = new double[10][23]; 5. Write a function header that will bring in as a parameter a two-dimensional array of doubles and return a one-dimensional array of integers. public static int[] dosomething(double[][] A) 1

Part 2: Program Traces (15 Points Each) 1. For each of the program inputs below write the output of the program. import java.util.scanner; public class Exam3Trace1 { public static void PrintArray(int[] Arr) { for (int i = 0; i < Arr.length; i++) { System.out.print(Arr[i] + " "); public static void main(string[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Input n: "); int n = keyboard.nextint(); int[] A = new int[n]; System.out.print("Input elements: "); for (int i = 0; i < n; i++) A[i] = keyboard.nextint(); PrintArray(A); int x = 2; for (int i = 0; i < n; i++){ int y = x*x % n; int temp = A[x]; A[x] = A[y]; A[y] = temp; x = y; (a) (b) (c) PrintArray(A); Input n: 4 Input elements: 1 2 3 4 1 2 3 4 3 2 1 4 Input n: 5 Input elements: 1 2 3 4 5 1 2 3 4 5 1 3 5 4 2 Input n: 6 Input elements: 1 2 3 4 5 6 1 2 3 4 5 6 1 2 5 4 3 6 2

2. For each of the program inputs below write the output of the program. import java.util.scanner; public class Exam3Trace2 { public static void doone() { char[][] pic = new char[6][6]; for (int i = 0; i < 6; i++) for (int j = 0; j < 6; j++) { if (i == j i == 0 i == 5) pic[i][j] = '*'; else pic[i][j] = '.'; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) System.out.print(pic[i][j]); public static void dotwo(int n) { int[] A = new int[n]; for (int i = 0; i < n; i++) { A[i] = i + 1; for (int j = 0; j < 3; j++) { int temp = A[0]; for (int i = 1; i < n; i++) { A[i - 1] = A[i]; A[A.length - 1] = temp; for (int i = 0; i < n; i++) System.out.print(A[i] + " "); public static void dothree(int n, int k) { char[][] pic = new char[6][6]; for (int i = 0; i < 6; i++) for (int j = 0; j < 6; j++) { if (i == n j == k) pic[i][j] = '*'; else pic[i][j] = '.'; for (int i = 0; i < 6; i++) { for (int j = 0; j < 6; j++) System.out.print(pic[i][j]); 3

public static void main(string[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Input: "); for (int i = 0; i < 3; i++) { switch (keyboard.nextint()) { case 1: doone(); break; case 2: dotwo(keyboard.nextint()); break; case 3: dothree(keyboard.nextint(), keyboard.nextint()); break; default: doone(); (a) Input: 2 10 1 3 2 3 4 5 6 7 8 9 10 1 2 3 ******.*.....*......*. ****** ****** 4

3. Write the output of the program in the box on the next page. MyStuff.java: public class MyStuff { private int num1; private double num2; public int num3; public MyStuff() { num1 = 3; num2 = 7.5; num3 = 5; public MyStuff(int a, int b, double c) { num1 = a; num2 = c; num3 = b; public void setall(int a, int b, double c) { num1 = a; num2 = c; num3 = b; public void copy(mystuff m) { num1 = m.num1; num2 = m.num2; num3 = m.num3; public void printmystuff() { System.out.println(num1 + " " + num2 + " " + num3); public int num1num2() { return num1 * (int) num2; public int num1num3() { return num1 * num3; public void setnum1(int a) { num1 = a; public void setnum2(double a) { num2 = a; public void setnum3(int a) { num3 = a; public boolean isequal(mystuff m) { boolean retval = false; if (num1 == m.num1 && num2 == m.num2 && num3 == m.num3) retval = true; public boolean isgreater(mystuff m) { if (num1 > m.num1) return true; else if (num1 == m.num1) { if (num2 > m.num2) return true; else if (num2 == m.num2) { if (num3 > m.num3) return true; return false; public void mixitup() { num2 = (num1 + num2) / 2.0; num1 = 2 * num1 + --num3; num3 += 3; return retval; 5

Main Program: public class Exam3Trace3 { public static void printbar(){ System.out.println(""); m.setnum3(25); for (int i = 0; i < 3; i++) A[i].printMyStuff(); public static void main(string[] args) { MyStuff m = new MyStuff(); MyStuff n = new MyStuff(-2, 4, 8.2); MyStuff k = new MyStuff(2, 1, 5.3); n.copy(k); System.out.println(n == k); m.printmystuff(); n.printmystuff(); k.printmystuff(); m.num3 = 4; n.setnum1(3); m.printmystuff(); n.printmystuff(); System.out.println(m.isEqual(n)); System.out.println(n.isGreater(m)); k.mixitup(); k.printmystuff(); m.setall(7, 2, 5); n.setall(1, 2, 3.7); k.setall(4, 2, 3.7); MyStuff[] A = new MyStuff[3]; A[0] = k; A[1] = m; A[2] = n; for (int i = 0; i < 2; i++){ if (A[0].isGreater(A[1])){ MyStuff temp = A[0]; A[0] = A[1]; A[1] = temp; 3 7.5 5-2 8.2 4 2 5.3 1 3 7.5 4 3 8.2 4 false true 4 3.65 3 1 3.7 2 4 3.7 2 7 5.0 2 4 3.7 2 7 5.0 2 1 3.7 2 4 3.7 2 7 5.0 25 1 3.7 2 false if (A[1].isGreater(A[2])){ MyStuff temp = A[1]; A[1] = A[2]; A[2] = temp; for (int i = 0; i < 3; i++) A[i].printMyStuff(); A[0] = k; A[1] = m; A[2] = n; for (int i = 0; i < 3; i++) A[i].printMyStuff(); 6

Part 3: Coding (10 Points Each) 1. Write a function that takes a one-dimensional array of doubles as its only parameter and returns the average of the elements in the array. If the array has no elements in it the function should return 0. public static double avg(double[] A) { if (A.length == 0) return 0; double sum = 0; for (int i = 0; i < A.length; i++) sum += A[i]; return sum / A.length; 2. Write a function that takes a one-dimensional array of integers as its only parameter and uses the selection sort to sort the array. Recall that the idea behind the selection sort is to find the largest element in the array and interchange it with the entry in the last position. Then find the largest entry in the array up to the next to last position and interchange it with the entry in the next to last position. and so on until the array is sorted. public static void selectionsort(int[] A) { for (int lastplace = A.length - 1; lastplace > 0; lastplace--) { int maxloc = 0; for (int j = 1; j <= lastplace; j++) if (A[j] > A[maxLoc]) maxloc = j; int temp = A[maxLoc]; A[maxLoc] = A[lastPlace]; A[lastPlace] = temp; 7

3. Write a function that will take in a two-dimensional array of integers as its only parameter and return another two-dimensional array of integers that is the transpose of the array. The transpose of an array is where the rows are columns and the columns are the rows. That is, row 1 of the array is column 1 of the transpose, row 2 of the array is column 2 of the transpose, and so on. public static int[][] transpose(int[][] A) { int[][] T = new int[a[0].length][a.length]; for (int i = 0; i < A.length; i++) for (int j = 0; j < A[0].length; j++) T[j][i] = A[i][j]; return T; 4. Write a function that will take in a two-dimensional array of integers as its only parameter and return a one-dimensional array of integers that holds the column sums of the input array. public static int[] columnsum(int[][] A) { int[] T = new int[a[0].length]; for (int j = 0; j < A[0].length; j++) { int sum = 0; for (int i = 0; i < A.length; i++) sum += A[i][j]; T[j] = sum; return T; 5. Write a function that takes a one-dimensional array of integers and an integer target value and does a binary search for the target value in the array. The return value is to be the position of the target value in the array and -1 if the target value is not in the array. static int binarysearch(int[] A, int N) { int lowestpossibleloc = 0; int highestpossibleloc = A.length - 1; while (highestpossibleloc >= lowestpossibleloc) { int middle = (lowestpossibleloc + highestpossibleloc) / 2; if (A[middle] == N) { return middle; else if (A[middle] > N) { highestpossibleloc = middle - 1; else { lowestpossibleloc = middle + 1; return -1; 8