0% found this document useful (0 votes)
76 views6 pages

ITCS 111 Final S2 2016 - 2017 - Key

This document contains a final exam for a computer programming course. The exam consists of multiple choice and coding questions testing concepts like loops, conditionals, classes, methods and string manipulation. It provides sample inputs, outputs and code snippets as part of the questions.

Uploaded by

Fatima Abdullah
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)
76 views6 pages

ITCS 111 Final S2 2016 - 2017 - Key

This document contains a final exam for a computer programming course. The exam consists of multiple choice and coding questions testing concepts like loops, conditionals, classes, methods and string manipulation. It provides sample inputs, outputs and code snippets as part of the questions.

Uploaded by

Fatima Abdullah
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/ 6

SERIAL University of Bahrain

College of Information Technology


Department of Computer Science
Second Semester, 2016-2017
ITCS 113 / ITCS 111 / ITCS 103
Computer Programming I

Final Exam – Form A

Date: 13th June 2017 Duration: 2 Hours

STUDENT NAME

STUDENT ID # SECTION #

Page 1 of 6
Question 1 (10 points)
What is the output of the following codes?

(1) int i = 34;


double d = 76.7362; Output
String s = "The fox has fox tail";
System.out.println(3 + 12 / i + d); 79.7362
System.out.printf("%8.2f\n", d);
System.out.println("length = " + s.length()); 3476.74
System.out.println("index = " + s.indexOf("fox"));
System.out.println("char = " + s.charAt(4));
length = 20
System.out.println(s.substring(4,8)+ "with" + index = 4
s.substring(15));
System.out.println(s.replace('x','g').toUpperCase()); last index = 12
String s1 = s.substring(4,7);
if (s1.equals("FOX")) char = f
System.out.println("YES");
else fox with tail
System.out.println("NO"); THE FOG HAS FOG TAIL
NO

(2) int x = 0, y = 0;
for(int i=1; i<=6; i++) Output
{
if (x > y) i=3
System.out.println("i = " + i);
if (i % 2 == 0) i=5
x += i;
else
x = 12, y = 9
y += i;
}
System.out.println("x = " + x + ", y = " + y);

(3) public class Example {


public static void main(String[] args) { Output
MyClass m1 = new MyClass();
m1.a = 3
MyClass m2 = new MyClass();
int x = m1.getNum(); m1.a = 8
System.out.println("m1.a = " + x); m2.a = -2
m2.setNum(x-5); compute = 6
if (m2.isValid())
m1.setNum(7);
else
m1.setNum(8);
System.out.println("m1.a = " + m1.getNum());
System.out.println("m2.a = " + m2.getNum());
int y = m1.compute(m2);
System.out.println("compute = " + y);
}

public class MyClass {


private int a = 3;
public void setNum(int a) { this.a = a; }
public int getNum() { return this.a; }
public boolean isValid() { return this.a > 0; }
public int compute(MyClass b) { return this.a+b.getNum(); }
}

Page 2 of 6
Question 2 (15 Points) SAMPLE INPUT/OUTPUT
Write a Java program that will ask the user for the number of rooms (including How many rooms in the house: -5
kitchens and bathrooms) in a house. Then the user enters the area for each Invalid input. Please try again.
room in square meters. Your program should compute the total area and the
net charges. The table below determines the net charges according to the total How many rooms in the house: 5
Enter area of room 1: 6.35
area. Display the total area and net charges formatted to two decimal places. Enter area of room 2: 12.0
Total Area in Square Meters Charge in BD Enter area of room 3: 19.0
Enter area of room 4: 29.4
Less than or equal to 60 20 Enter area of room 5: 40.6
Greater than 60 and Less or equal to 110 25 plus 10% of area
Total area is 107.35 Square Meters.
Greater than 110 30 plus 15% of area
Painting cost is BD 35.74
Note: In case the user entered negative or zero for the number of rooms, then
display an invalid message and ask the user to try again.

import java.util.Scanner;
public class Question2 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);

double roomArea,totalArea,totalCharge=0;
int numOfRooms;

totalArea = 0;
System.out.print("How many rooms in the house: ");
numOfRooms = kbd.nextInt();
while (numOfRooms <=0)
{
System.out.println("Invalid input. Plaese try again.");
numOfRooms = kbd.nextInt();
}

for(int i=1; i<= numOfRooms; i++)


{
System.out.print("Enter area of room " + i + ": ");
roomArea = kbd.nextDouble();
totalArea += roomArea;
}

if(totalArea <= 60)


totalCharge = 20;
else if(totalArea > 60 && totalArea <= 110)
totalCharge = 25 + totalArea*0.10;
else if(totalArea > 110)
totalCharge = 30 + totalArea*0.15;

System.out.printf("\nTotal area is %.2f Square Meters.\n", totalArea);


System.out.printf("Painting cost is BD %.2f\n", totalCharge);

}
}

Page 3 of 6
Question 3 (15 Points) SAMPLE INPUT/OUTPUT
Write a program that reads a positive integer number 𝑁 in the Enter number of lines: 25
range 0 < 𝑁 ≤ 19. If 𝑁 is outside this range, print an invalid message Invalid input. Try again.
and ask the user to try again. Enter number of lines: 5
After entering 𝑁, the program is required to produce a table of 𝑁 lines 11111
and 𝑁 columns. Each row consists of: 00000
1. All ‘1’ when the row number is odd. 11111
00000
2. All ‘0’ when the row number is even.
11111

public class Question3 {


public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);

System.out.print("Enter number of lines: ");


int lines = kbd.nextInt();
while (lines <=0 || lines > 19)
{
System.out.println("Invalid input. Plaese try again.");
System.out.print("Enter number of lines: ");
lines = kbd.nextInt();
}

char ch; for(int i=1; i<= lines; i++)


for(int i=1; i<= lines; i++) {
{ for(int j=1; j<= lines; j++)
if (i%2 == 1) ch = '1'; {
else ch = '0'; if (i%2 == 1)
for(int j=1; j<= lines; j++) System.out.print("1");
System.out.print(ch); else
System.out.println(); System.out.print("0");
} }
System.out.println();
}

Page 4 of 6
Question 4 (13+7 Points)
This question is divided into two parts. Please write your answer below each part.
Part (2) is on the next page.
Part (1) Define a class with the following specification:
a. Create a class called Employee that has four instance variables – a (String) first name, a (double) payRate, a
(double) normalHours, and a (double) extraHours.
b. Make the instance variable payRate private and provide a mutator method (set) and an accessor method
(get) to access this variable. In set method, if the supplied pay rate (parameter) is negative print an invalid
message and do not change the instance variable payRate.
c. A method calcPay() to calculate the employee pay by multiplying payRate by normalHours added to it the pay
for extraHours. For every extra hour the pay rate is doubled. This method must return the pay as a result.
d. A method print() that displays the normal and extra working hours and the calculated pay amount formatted
to two decimal places as shown below.
Sample Output:
Employee Name: Ahmed
Normal Hours Worked: 140.00
Extra Hours Worked: 20.00
Hourly Rate: BD 2.50
Pay Amount: BD 450.00

public class Employee {


String name;
double normalHours, extraHours;
private double payRate;

public double getPayRate() {


return payRate;
}

public void setPayRate(double payRate) {


this.payRate = payRate;
}

public double calcPay()


{
return this.normalHours * this.payRate +
this.extraHours * this.payRate * 2;
}

public void print()


{
System.out.printf("Employee Name: %s\n", this.name);
System.out.printf("Normal Hours Worked: %.2f\n", this.normalHours);
System.out.printf("Extra Hours Worked: %.2f\n", this.extraHours);
System.out.printf("Hourly Rate: %.2f\n", this.payRate);
System.out.printf("Pay Amopunt: %.2f\n", this.calcPay());
}
}
Page 5 of 6
Part (2) Write a Java application that asks the user to enter the employee name, pay rate, normal hours
worked, and extra hours worked. Create an object of type Employee class you defined in Part (1) and set its
values according to the input data. Use it to calculate the pay amount and print the data.

public class Question4 {


public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);

String name;
double rate, normalHours, extraHours;

System.out.print("First name: ");


name = kbd.next();
System.out.print("Normal Hours: ");
normalHours = kbd.nextDouble();
System.out.print("Extra Hours: ");
extraHours = kbd.nextDouble();
System.out.print("Hourly rate: ");
rate = kbd.nextDouble();

Employee emp = new Employee();

emp.name = name;
emp.normalHours = normalHours;
emp.extraHours = extraHours;
emp.setPayRate(rate);

emp.print();
}
}

Page 6 of 6

You might also like