0% found this document useful (0 votes)
48 views

ITCS114 Final Key

Here is the output of the program: 3 2 * 2 Part (B) : Explain the output The program takes two integer parameters x and y. It first prints the values of x and y on one line separated by a space (3 2). Then it checks if y equals 1 using an if statement. Since y does equal 1, the code block inside the if statement is executed - it prints a * on a new line. Finally, the method returns the value of y, which is 1. So the return value printed will be 2.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

ITCS114 Final Key

Here is the output of the program: 3 2 * 2 Part (B) : Explain the output The program takes two integer parameters x and y. It first prints the values of x and y on one line separated by a space (3 2). Then it checks if y equals 1 using an if statement. Since y does equal 1, the code block inside the if statement is executed - it prints a * on a new line. Finally, the method returns the value of y, which is 1. So the return value printed will be 2.
Copyright
© © All Rights Reserved
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
First Semester, 2019-2020
ITCS 114 - Computer Programming II

Final Exam

Date: 07-01-2019 Time: 11:30 – 13:30

KEY
STUDENT NAME

STUDENT ID # 2 0 SECTION #

Question # MARKS COMMENTS

1 10

2 – Part (A) 14

2- Part (B) 18

3 20

4 – Part (A) 10

4 – Part (B) 8

TOTAL 80

Page 1 of 7
Question 1:

public class myClass{ public class testDemo{


private int x1; public static void main(String [ ]args){
private static int x2 = 10; myClass A = new myClass(3);
myClass [] list = new myClass[2];
public myClass(int n) A.output();
{ x1=n; }
for(int i=0; i <list.length; i++)
public int sum(int n) {
{ x2+=n; return x2;} list[i] = new myClass(i);
list[i].output();
public int sum( ) }
{ x2+=x1; return x2; }
int R = 0;
public void output ( )
{ System.out.println(x1+"-"+x2); } try{
int x = list[0].sum( );
public static int getX2() int y = list[1].sum(A.getX2());
{return x2;} if (x>y)
throw new Exception ("More");
}// end of class
System.out.println(x+","+y);
R = x+y;
}//end of try
catch (Exception e){
System .out.println(e.getMessage());
R++;
}// end of catch
System.out.println("R="+R);
} // end of main
}// end of class

OUTPUT

// each line 2 pt
3-10
0-10
1-10
10,20
R=30

Page 2 of 7
Question 2 – Part(A):
Write a Java program that defines a two dimensional array named numbers of size N X M of type integer.
N and M values should be entered by the user. The program should read the data from the keyboard into the
array. The program should then find and display the rows containing two consecutive zeros (i.e. two zeros
coming after each other in the same row).
Sample Input/output
Enter # Rows , # Cols: 4 5
Enter Values for Row 0: 1 2 3 0 5
Enter Values for Row 1: 1 0 0 3 8
Enter Values for Row 2: 0 0 0 0 4
Enter Values for Row 3: 1 0 1 0 5
Rows with two consecutive zeros:
Row = 1
Row = 2
import java.util.Scanner;
public class arrayTwo {
public static void main(String [] args){

// read M and N - 2 pts


// Create array - 2 pts
// read values - 3 pts
// check two consecutive zeros - 5

Scanner input = new Scanner (System.in);

System.out.println("Enter # Rows , # Cols:");


int N = input.nextInt();
int M = input.nextInt();

int numbers[][] = new int[N][M];

for(int i=0; i<N; i++)


{
System.out.println("Enter Values for Row " + i);
for(int j=0; j<M; j++)
numbers[i][j] = input.nextInt();
}

System.out.println("Rows with two consecutive zeros:");


for(int i=0; i<N; i++)
{

for(int j=0; j<M-1; j++)


if(numbers[i][j] == 0 && numbers[i][j+1]==0)
{
System.out.println("Row = "+ i);
break;
}
}
}//end of main
}// end of class

Page 3 of 7
Question 2 – Part(B):
Write the definition of a class named shoppingList that include the following private members:
- list: an array of type String to hold the names of items in the list.
- numItems: an integer to represent the number of items in the list.
Additionally, the class should include the following public methods:
1. A default constructor to initialize numItems to zero and create list as an array of size 10.
2. A constructor with an integer parameter (m) to initialize numItems to zero and create list as an array
of size (m).
3. A method named addItem that takes a string (y). The method should add (y) to the end of the list array
and increment numItems if it is not full.
4. A method named replace that takes two strings (M, N). The method should replace all of occurrences
of (M) in the list array by (N).
5. A method named print to print the items in the list array.
import java.util.Scanner;
public class shoppingList {

private String [] list; // 2 pts


private int numItems; // 2 pts

public shoppingList(){ // 3 pts


list = new String [10]; numItems=0; }

public shoppingList(int m){ // 3 pts


list = new String [m]; numItems=0; }

public void addItem(String y){ // 4 pts


if(numItems == list.length)
System.out.println("Full");
else
{ list[numItems]=y;
numItems++;
}
}

public void replace (String M, String N){ // 4 pts


// The method should replace all of occurrences of (M) in the list array by (N).
for(int i = 0; i< numItems ; i++)
if (list[i].equals(M))
list[i] = N;
}

public void print ( ){


for(int i = 0; i< numItems ; i++)
System.out.print (list[i]+" ");
}

}// end of class

Page 4 of 7
Question 3 :
Write a Java program to read from a file named “input.txt”, which includes the following:
- The first line shows the average price of all items in the file
- The file then contains a unknown number of items. Each item has a name and price separated by
comma, as the sample below.
The program should write into another file named “output.txt” the following:
- Items (name, price) with price above average.
- Total number of items in the input file and number of items with price above average.
Format your output file as the sample below.
input.txt output.txt
Average=8.275 Items above average price
Chair, 12.500 Chair, 12.500
Table, 10.00 Table, 10.00
Desk, 55.750 Desk, 55.750
Lamp, 4.00 …..
Glass 0.500 ……
.... # Items in the input file = 100
.... # Items above average = 13

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


import java.io.IOException; import java.io.File;
import java.util.Scanner;
public class itemDemo{
public static void main(String [] args){
try{ // 2 pts
Scanner input = new Scanner (new File("input.txt")); // 2 pts
PrintWriter output = new PrintWriter ("output.txt"); // 2 pts

// find avg = 2 pts


String line = input.nextLine();
String []arr = line.split("=");
double avg = Double.parseDouble(arr[1]);

// loop for items 2 pts


// use split, check price 2 pts
// output 1 pt
// counters 2 pts
output.println("Items above average price:");
int count =0, total = 0;;
while(input.hasNextLine()){
line = input.nextLine();
String [] item = line.split(",");
double price = Double.parseDouble(item[1]);
total++;

if (price > avg){


output.println(line);
count++;
}
}
// output counters 1 pts
// close files 2 pts
output.println("# Items in the input file = " + total);
output.println("# Items above aaverage = " + count);
input.close();

Page 5 of 7
output.close();

}// end of try

catch(IOException e){ // 2 pts


System.out.println("Exepion" + e.getMessage());

} // end of main
}// end of class

Page 6 of 7
Question 4
Part (A) :
Find the output of the following program
public static int test (int x, int y)
{ Output
System.out.println(x+" "+y);
if ( y == 1){ // each line 2 pts
System.out.println("*"); 3 2
return y; 1 1
} *
else { 3 1
y--; 2
int r = test(x-2, y);
System.out.println(x+" "+y);
return (r+1);
}
}

public static void main(String [] args){


int x = test(3,2);
System.out.println(x);
}

Part (B):
Write a recursive function int power (int base, int exponent) to return baseexponent, i.e. to return the result
of raising the base number to the exponent power.
For example, power (3, 4) = (3)4 = 3 * 3 * 3 * 3 = 81
Assume that exponent is any integer number greater than or equal to Zero.
Hints: (base)0 = 1, (0)exponent = 0

public static int power (int base, int exponent)


{

if (exponent == 0) /* Base Case: 2 points */


return 1;
else if (base == 0 ) /* 2 Point, as this could be omitted */
return 0;
else
return base * power(base, exponent - 1);
/* recursive call: 4 points */

Page 7 of 7

You might also like