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

Computer Programming Exam

This document appears to be a final exam for a computer programming course. It contains 4 questions testing concepts like output from code snippets, writing a program to calculate parking charges, producing a shape from an input number, and defining a class to calculate carpet costs for a room.

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)
27 views6 pages

Computer Programming Exam

This document appears to be a final exam for a computer programming course. It contains 4 questions testing concepts like output from code snippets, writing a program to calculate parking charges, producing a shape from an input number, and defining a class to calculate carpet costs for a room.

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
First Semester, 2016-2017
ITCS 113 / ITCS 111 / ITCS 103 / ITCS 101
Computer Programming I

Final Exam – Form A

Date: 18th January 2017 Duration: 2 Hours

STUDENT NAME

STUDENT ID # SECTION #

NOTE: THERE ARE (6) PAGES IN THIS TEST


WRITE ONLY ONE SOLUTION FOR EACH QUESTION

Question # MARKS COMMENTS

1 10

2 15

3 15

4 20

TOTAL 60

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

(1) int num = 9;


Output
while (num != 0)
{
System.out.print(num % 2 + "\t"); 1 0 0 1
num /= 2;
}

(2) String desc = "item";


Output
int qty;
double weight, price;
for (int i = 1; i < 4; i++) { item1 5 2.0 2.50
qty = i*5; item2 10 4.0 2.50
weight = i * 2; item3 15 6.0 2.50
price=qty/weight;
System.out.printf("%s%d %4d %6.1f %6.2f \n",
desc, i, qty, weight, price);

(3) public class Example {


public static void main(String[] Output
args) {
Shape s = new Shape("point1"),
t = new Shape("point2"); obejct: point1 5 6
s.output(); obejct: point2 10 30
t.setXY(10,30);
t.output();
Slope is 4.8
System.out.println("Slope is " +
s.slope(t));
}

public class Shape {


private String type;
private int xPos;
private int yPos;
public Shape (String name) {
type = name;
xPos=5;
yPos=6;
}
public void setXY(int x, int y) {
this.xPos = x;
this.yPos = y;
}
public double slope (Shape c) {
return (double) (this.yPos - c.yPos)/(this.xPos - c.xPos);
}
public void output () {
System.out.println ("obejct:"+type+"\t"+xPos+"\t "+yPos);
}
}
Page 2 of 6
Question 2 (15 Points)
A parking garage has two types of customers: members and non-members. During week working days, Sunday to
Thursday, members use the parking for free and non-members pay BD 0.300 per hour. However, during the weekend,
Friday and Saturday, all type of customers pay BD 0.200 per hour.
Write a Java program that prompts the user to enter a day (int) followed by the number of parking hours (double)
and a member code (char), ‘M’ or ‘m’ for members and any other character for non-members. Your program should
then calculate and print the total charges. For the days, use 1 for Sunday, 2 for Monday, etc. Note: For any invalid
day or hours print an appropriate invalid message and exit the program.
Sample Run 1 Sample Run 2 Sample Run 3
Enter day, hours and member code: Enter day, hours and member code: Enter day, hours and member code:
6 4.5 M 2 5.0 M 3 5.25 N
Total charges: BD 0.900 Total charges: BD 0.000 Total charges: BD 1.575

Page 3 of 6
Question 3 (15 Points) SAMPLE INPUT/OUTPUT
Write a program that reads a positive integer number 𝑁 in the range Enter number of lines: -5
0 < 𝑁 ≤ 20. If 𝑁 is outside this range, print an invalid message and ask Invalid input. Try again.
the user to try again. Enter number of lines: 5
After entering 𝑁, the program is required to produce a shape consisting ****1
of *'s and the line number as shown by a sample output for 𝑁 = 5 on ***2
the right hand side. **3
*4
5

Page 4 of 6
Question 4 (15+5 Points)
The answer to this question is divided into two parts. Please write your answer below each part. Part (2) is on the
next page.
Part (1) Create a class called RoomCarpet that has three private instance variables – room length and width in foot
(double) and carpet cost per square foot (double). Additionally, the class should have:
a. A constructor that takes room length, room width and carpet cost per square foot. The constructor sets the
class variables to these values and in case any of these values is negative, set its instance variable to zero.
b. A mutator method (set) and an accessor method (get) for the carpet cost variable only.
c. A method getArea that returns the area of the room. The area of the room is computed by multiplying the
room's length by the room's width.
d. A method getTotalCost that returns the total cost of the carpet. The total cost is computed by multiplying the
room area by the carpet cost per square foot.

Page 5 of 6
Part (2) Write a Java application that asks the user to enter the length and width of a room and the cost per
square foot of the carpeting. Create an object from RoomCarpet class you defined in Part (1) and use it to
display the room area and the total cost of the carpet.

Page 6 of 6

You might also like