[go: up one dir, main page]

0% found this document useful (0 votes)
63 views7 pages

ITCS114 - Final Exam - Key

This document contains the final exam for the Computer Programming II course given on May 29, 2018 from 11:30-13:30. It consists of 4 questions worth a total of 80 marks. Question 1 (12 marks) asks students to find the output of several code snippets. Question 2 (24 marks) asks students to write a class definition. Question 3 (24 marks) asks students to write a program to read and write data from text files. Question 4 has two parts: part A (10 marks) asks students to find the output of code, and part B (10 marks) asks students to write a recursive method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views7 pages

ITCS114 - Final Exam - Key

This document contains the final exam for the Computer Programming II course given on May 29, 2018 from 11:30-13:30. It consists of 4 questions worth a total of 80 marks. Question 1 (12 marks) asks students to find the output of several code snippets. Question 2 (24 marks) asks students to write a class definition. Question 3 (24 marks) asks students to write a program to read and write data from text files. Question 4 has two parts: part A (10 marks) asks students to find the output of code, and part B (10 marks) asks students to write a recursive method.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

University of Bahrain SERIAL

College of Information Technology NO:


Department of Computer Science
Second Semester, 2017-2018
ITCS 114 - Computer Programming II

Final Exam

Date: 29-05-2018 Time: 11:30 – 13:30

Key
STUDENT NAME

STUDENT ID # SECTION #

Question # MARKS COMMENTS

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

public class Item public class itemDemo {


{ public static void main(String [] args) {
private int model; Item [] list = new Item[2];
private double price; Item one = new Item( );
private static int n = 0; one.print( );

public Item( ) { for(int i=0; i<list.length;i++){


model=0; price = 0.0; n+=model; } list[i]=new Item(i,1.5);
list[i].print( ); }
public Item(int m, double p) {
model=m; price=p; n+=model; } one.setItem(7.5);

public void setItem(int m, double p) { list[1].setItem(10,2.5);


model=m; price=p; n+=model; }
one.print( );
public void setItem(double p) { price=p; }
for(int i=0; i<list.length;i++)
public static int getCount ( ) { return n; } list[i].print( );

public void print( ){ }// end of main


System.out.println(model + "-" + price + "-" + n ); } } // end of class
}// end of class

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 class list {

private int[] myList; // 2pts

private int currentSize; // 2pts

public List() // 1 pt
{
myList = new int[100]; // 2 pt
currentSize = 0; // 1 pt
}

public void insert(int x)


{
// total = 11
// header - 1 pt
// check full - 2pts
// check if x exist - 4 pts
// add x to the end of the array - 2pts
// increment currentSize - 2 pt

if (currentSize == myList.length) // or == 100


System.out.println("Cannot insert in a full list");
else
{
boolean found = false;
for (int i = 0; i < myList.length; i++)
if (myList[i] == x)
{
System.out.println(x + " already exists in list");
found = true;
break;
}
if(found==false)
{
myList[currentSize] = x;
currentSize++;
}
}
}

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

}// end of class list

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.

import java.io.PrintWriter; import java.io.FileNotFoundException;


import java.io.IOException; import java.io.File;
import java.util.Scanner;
public class treeDemo{
public static void main(String [] args){

// use try - 2 pts


// use catch - 2 pts
// open input file - 3 pts
// open output file - 3 pts
// use count - 2 pts
// format - 2 pt
// loop through the file - 2 pt
// use split, parse - 2 pt
// if condition for old trees - 1 pt
// write old tree into file - 2 pt
// write count into file - 1 pt
// close the files - 2 pt

try{
Scanner input = new Scanner (new File("treeList.txt"));
PrintWriter output = new PrintWriter("oldTrees.txt");

int count =0;

output.println("Tree Height Age");

while(input.hasNextLine ( )){
String line = input.nextLine();
String [] arr = line.split(",");
double height = Double.parseDouble(arr[0]);
int age = Integer.parseInt(arr[1]);

if(height>120 && age >5)


{
count++;
output.println(count + "." + height + " "+age);
}

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);
}

Question 4 – Part (B) ( 10 points)


Consider the following sequence:
1 𝑖𝑓 𝑛 = 1
𝑠𝑒𝑞𝑢𝑒𝑛𝑐𝑒(𝑛) = {
𝑠𝑒𝑞𝑢𝑒𝑛𝑐𝑒(𝑛 − 1) + 4 𝑖𝑓 𝑛 > 1

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

public static int sequence( int n) {

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

You might also like