ITCS114 - Final Exam - Key
ITCS114 - Final Exam - Key
Final Exam
Key
STUDENT NAME
STUDENT ID # SECTION #
1 12
2 24
3 24
4 – Part (A) 10
4 – Part (B) 10
TOTAL 80
Page 1 of 7
Question 1 ( 12 ) :
Find the output of the following programs
Output
Each line 2 pts
0-0.0-0
0-1.5-0
1-1.5-1
0-7.5-11
0-1.5-11
10-2.5-11
Page 2 of 7
Question 2 (24 Points)
Write the definition of a class named list that include the following private members:
1) myList: an array of type integer,
2) currentSize: to represent the current number of integers stored in myList, initially the array is
empty (i.e currentSize=0),
Additionally, the class should include the following public methods:
1) A default constructor. The constructor should initialize currentSize to zero and create myList as an
array of size 100.
2) A method named insert that takes an integer (x) as a parameter. The method should add (x) to
the end of myList and increment currentSize if:
1. The array is not full, and
2. (x) does not exist already in myList (duplication is not allowed).
3) A method named print to output the integers stored in myList.
public List() // 1 pt
{
myList = new int[100]; // 2 pt
currentSize = 0; // 1 pt
}
Page 3 of 7
public void print() // 1 pt
{
System.out.println("myList = ");
for (int i = 0; i < currentSize; i++) // 1 pt
System.out.println (myList[i]); // 1 pt
Page 4 of 7
Question 3 (24 Points)
Write a java program to read from a file called “treeList.txt”. The file includes tree height, and tree age for
unknown number of trees. The file use a comma separated file format, as the sample below. The program
should write old trees only into an output file called “oldTrees.txt”. A tree is considered old if its height is
above 120 and its age is greater than 5 years. Finally print number of old trees at the end of the output file
and format the output file to be similar to the given sample.
treeList.txt oldTrees.txt
15.000,5 Tree Height Age
130.500,7 1. 130.500 7
140.000,6 2. 140.000 6
150.800,3
100.200,8 Number of old trees = 2.
try{
Scanner input = new Scanner (new File("treeList.txt"));
PrintWriter output = new PrintWriter("oldTrees.txt");
while(input.hasNextLine ( )){
String line = input.nextLine();
String [] arr = line.split(",");
double height = Double.parseDouble(arr[0]);
int age = Integer.parseInt(arr[1]);
Page 5 of 7
}
output.println("Number of old trees = "+count);
input.close();
output.close();
} // end of try
catch(IOException e){
System.out.println("Error"+e.getMessage());
}
} // end of main
}// end of class
Page 6 of 7
Question 4 – Part (A) - (10 Points)
Find the output of the following program
public static int find (int [] arr1,int [] arr2, int start)
{ Output
if(start == arr1.length) { output - Each line 2 pt
System.out.println("#");
return 0; 10,10
}
else if (arr1[start] == arr2[start]) { 20,40
System.out.println(arr1[start]+","+arr2[start]);
return 1+ find(arr1,arr2,start+1); 30,30
} #
else { 2
System.out.println(arr1[start]+","+arr2[start]);
return find(arr1,arr2,start+1);
}
} // end of find method
public static void main(String [] args)
{
int [] arr1 = {10,20,30}; int [] arr2 = {10,40,30};
int R = find (arr1, arr2, 0); System.out.println(R);
}
Write a recursive method that takes a positive integer n as a parameter and returns the nth term of this
sequence. For example:
If n = 1, the method should return 1; If n = 2, the method should return 5; If n = 3, the method should return
9
if(n==1) // 2 pts
return 1; // 2 pts
else // 1 pts
return 4 + sequence (n-1);
// return - 1 pt
// 4 - 1 pt
// + - 1 pt
// sequence (n-1) - 2 pts
Page 7 of 7