0% found this document useful (0 votes)
11 views8 pages

ModelA CS CS141 2 18

This is the model answ
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)
11 views8 pages

ModelA CS CS141 2 18

This is the model answ
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/ 8

Al Imam Mohammad Ibn Saud Islamic University

College of Computer and Information Sciences


Computer Science Department
Course Title: Computer Programming 2
Course Code: CS141
Course Dr Albathan, Dr Aljammaz, Dr Alsulaiman,
Instructors: Dr Shahin, Dr Sriti, Ms Alawwad, Ms Aldayel,
Ms Aljaloud, Ms Almajed, Ms Almedlej,
Ms Alqefari
Exam: Final Exam
Semester: Spring 2018
Date: 16/08/1439 - 02/05/2018
Duration: 2 hours
Marks: 40
Privileges: ☐Open Book ☐Open Notes
☐Calculator Permitted ☐Laptop Permitted

Student Name: MODEL ANSWER

Student ID:
Section No.:

Instructions:

1. Answer 4 questions; there are 4 questions in 8 pages.


2. Write your name on each page of the exam paper.
3. Write your answers directly on the question sheets. Use the ends of the question pages
for rough work or if you need extra space for your answer.
4. If information appears to be missing from a question, make a reasonable assumption,
state your assumption, and proceed.
5. No questions will be answered by the invigilator(s) during the exam period.

Official Use Only


Question Student Marks Question Marks

1 10
2 10
3 10
4 10
Total 40

Page 1 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Question 1: To be answered in (40) Minutes [ ] / 10 Marks

There is only one error in each of the following codes. Identify and underline the error, and
explain the cause of each error.
a) abstract class A {
//Class A Members
}

class B {
//Class B Members
}

class C extends A, B {
//Class C Members
}
Answer: a class cannot extend more than one class. //2 marks

b) public class A {
public A(){
System.out.println(1);
super();
System.out.println(2);
}
}
Answer: super() must be the first statement in the constructor. //2 marks

c) interface X
{
void methodX();
}

class Y implements X
{
private void methodX()
{
System.out.println("Method X");
}
}
Answer: The visibility of methodX() has been reduced to private while implementing it in
the class //2 marks

Page 2 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


d) public class ExceptionHandling
{
public static void main(String[] args)
{
try
{
//This statement throws NumberFormatException
//(an indirect subclass of RuntimeException)
int i = Integer.parseInt("abc");
}
catch(Exception ex)
{
System.out.println("...");
}
catch(NumberFormatException ex)
{
System.out.println("...");
}
}
}

Answer: This block is unreachable (compile time error) because the exception is already
caught by above catch block //2 marks

e) public class GenericBox{


// Private variable
private E content;

// Constructor
public GenericBox(E content) {
this.content = content;
}

public E getContent() {
return content;
}

public void setContent(E content) {


this.content = content;
}
}

Answer: The type parameter <E> is not declared after the class name //2 marks

Page 3 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Question 2: To be answered in (30) Minutes [ ] / 10 Marks

a) Create a checked exception class called DeniedStudentException with two constructors: the first
constructor has no parameters and the second takes a message string.

Answer a): (3 marks)

public class DeniedStudentException extends Exception //1 mark


{
public DeniedStudentException() { } //1 mark

public DeniedStudentException(String message) { //1 mark


super(message);
}
}

Page 4 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


b) Write a class called Student that has 2 instance variables which are course number of hours taught
and student number of absences (hours). In addition, the class has one constructor that initializes
the instance variables. The constructor throws a DeniedStudentException when the number of
absences exceeds 15% of the course number of hours.

Answer b): (7 marks)

public class Student //1 mark


{
private int courseCount; //0.5 mark
private int absenceCount; //0.5 mark

public Student(int courseCount, int absenceCount) //1 mark


throws DeniedStudentException //1 mark
{
if (absenceCount * 100 / courseCount > 15) //1 mark
throw new DeniedStudentException("Student absent more
than 15%."); //1 mark

this.courseCount = courseCount; //0.5 mark


this.absenceCount = absenceCount; //0.5 mark
}
}

Page 5 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Question 3: To be answered in (30) Minutes [ ] / 10 Marks

Write a generic method computeCost that takes an array of generic type which must implements the
following Item interface.

public interface Item {


double getPrice();
}

computeCost computes and returns the sum of all items price after multiplying each item’s price by
1.05 except if:
• the item is a reference to a Food class, or
• the item’s price is less than 100.0

public static <T extends Item> double computeCost( T[] items ) //2 mark
{
double sum = 0 ; //1 mark

for(T i: items) // 1 mark


{
double price = ((Item)i).getPrice(); // 1 mark

if (! (i instanceof Food) && price > 100.0) // 2 marks


{
price += price * 0.05; //1 mark //or: price *= 1.05;
}

sum += price; //1 mark


}

return sum; //1 mark


}

Page 6 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Question 4: To be answered in (30) Minutes [ ] / 20 Marks

a) Write the code for the flowing GUI specifications: (3 marks)

• Set the width and height of a window to 300 and 70, respectively.

setSize(300,70);

• Create a text field with default text “0” and number of columns is 5

private JTextField text = new JTextField("0", 5);

• Set the layout for a container to FlowLayout

setLayout(new FlowLayout());

b) The following figure represents a “Plus/Minus Counters” application user interface:

Complete the following code skeleton to reproduce the same interface using Java Swing library

components (you can reuse the answers of the question (a)).

Page 7 of 8

Imam University | CCIS | Doc. No. 006-02-20170316


Answer: (7 marks)

import javax.swing.*;
import java.awt.*;

// declare the UI class


public class CounterUI extends JFrame { // 0.5 mark

// Declare & instantiate components


private JButton bPlus = new JButton("+"); // 1 mark
private JButton bMinus = new JButton("-"); // 1 mark
private JTextField text = new JTextField("0", 5);

// Declare constructor
public CounterUI() {
// set layout
setLayout(new FlowLayout());

// add components to the interface


add(bMinus); // 0.5 mark
add(text); // 0.5 mark
add(bPlus); // 0.5 mark

// set other window's properties


setSize(300,70);
setTitle("Plus/Minus Counters"); // 1 mark
setVisible(true); // 1 mark
}

public static void main(String[] args) {


// instantiate the interface
new CounterUI(); // 1 mark
}
}

Page 8 of 8

Imam University | CCIS | Doc. No. 006-02-20170316

You might also like