BCS 210 OOP Exam Marking Guide
BCS 210 OOP Exam Marking Guide
S N D EM
EAND 1E
co2YDepmed
(University of Choice)
MASINDE MULIRO UNIVERSITY OF
SCIENCE AND TECHNOLOGY
(MMUST)
MAIN CAMPUS
UNIVERSITY EXAMINATIONS
2018/2019 ACADEMIC YEAR
SECOND YEAR FIRST SEMESTER EXAMINATIONS
TIME: 2 Hours
8 System.out.println(l )
93
103 [5 Marks]
the pattern
Write a complete Java class that displays
c)
33
555
7777
99999 assigns grades
gets the best score, and then
a program that
reads student scores,
d) Write
[6 Marks)
based on the following scheme:
PROGRAMMING II
BCS 210/BIT 210: OBJECT ORIENTED
BCS 210/ BIT 219
eWrite a program that displays numbers in a triangular pattern, as shown in Figure below.
The number of lines in the
display changes to fit the window as the window resizes[6 Marks]
Exergise15 3 ID
5
5 6 7
6 6 7 8
4 6 6 7 s9
23 4 5 67 8 9 10
1 2 3 4 6 6 7 8 9 1011
W i t e a program that prompts for a n integer value for radius, computes area of a circle using
a constant Pl, and displays the area computed. Use InputDialogBox for prompting the value
following formula:
[8 Marks]
-b + V# 4ac - V - 4ac
2a
and r= 2a
b-4ac is called the discriminant of the quadratic equation. If it is positive, the equation has
two real roots. If it is zero, the equation has one root. If it is negative, the equation has no
real roots. Write a program that prompts the user to enter values for a, b, and c and displays
the result based on the discriminant. If the discriminant is positive, display two roots. If the
discriminant is 0, display one root. Otherwise, display "The equation has no real roots".
Note you can use Math.pow(x, 0.5) to compute Here are some sample runs. [8 Marks]
er
Enter a, b, c 103
Theroots are-0381966 and2.6180B
Enter a, b, e 12 0 1
The root is 1
Enter a, C 123
he equation has ng real roots
I/public Shape(){
public Shape (Date date, String color, String Nane)
public Rectangle 0
final double
pi
private int radius;3.142;
=
Teturn pi *
radius *
radius;
private int
getRadius ()
return radius;
1 Class B extends A
b) Every bank offers a checking account. Derive the class checkingAccount from the class
bankAccount (designed in part (a). This class inherits members to store the account number
account typically receives
and the balance from the base class. A customer with checking
a
(5 Marks)
the class savingsAccount from the class
offers savings account. Derive
c) Every bank
a
ORIENTED PROCRAMMING II
BCS 210/BIT 210: OBJECT
BCS 210/ BIT 210
and the balance from the base class. A customer with a savings account typically receives
nterest, makes deposits, and withdraws money. In addition to the operations inherited from
the base class, this class should provide the following operations: set interest rate, retrieve
interest rate, post interest, withdraw (override the method of the base class), and print
account information. NB: Add appropriate constructors. (4 Marks)
d) Write a program to test your classes designed in parts (6) and (©). (3 Marks)
Question Three (15 Marks)
a) Design a class named Account that contains:
i. A private int data field named id for the account (default 0).
i. A private double data field named balance for the account (default 0.
ii. A private double data field named annuallnterestRate that stores the current interest
rate (default 0). Assume all accounts have the same interest rate.
iv. A private Date data field named dateCreated that stores the date when the account
was created
Y. A no-arg constructor that
creates a default account.
V1. A constructor that creates an
vii. The accessor and mutator
account with the specified id and initial balance
methods for id, balance, and annuallnterestRate.
viii. The accessor method for dateCreated.
ix. A method named getMonthlylnterestRate) that returns the monthly interest rate.
X. A method named withdraw that withdraws a
account.
specified amount from the
xi. A method named
deposit that deposits a specified amount to the account.
Required:
Draw the UML diagram for the class. Implement the class. Write a test program that
creates an Account object with an account ID of 1122, a balance of $20,000, and an
annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500,, use the
deposit method to deposit $3,000, and print the balance, the monthly interest, and the
date when this account was created.
[12marks]
b) What is the printout of the following code? [3 Marks]
public class Foo {
private boolean x;
public static void main(String[] args) {
Foo foo new Foo0;
System.out.println(foo.x);
the
are equired to write a Java program that calculates Ñ of this student using
formula
where
5 Marks)
int m;
int n;
int nin)
public C(int mIn,
m=mIn;
n = nIn;
public int ml ()
return mtn
public class
extends DomesticAnimal. .
class FarmAnimal
publi
class HousePet extends DomesticAnimal. ..
public FarmAnimal
class Cow extends
. . .
public
extends FarmAnimal
public c l a s s Goat
. .
extends Cow
public class DairyCow
.
NB: The student could use different approach, check student code
keenly to see if the question is logically answered
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR| Page 8
(d) Best score [6 Marks]
1 import java.util.Scanner;
2 import java.util.Arrays; // for getting best score
3 public class BestScore {
4 public static void main(String[] args) {
5 int students;
6 int[][] scores;
7 System.out.print("Enter the number of students: ");
8 Scanner s = new Scanner(System.in);
9 students = s.nextInt();
10 scores = new int[students][4];
11 System.out.print("Enter 4 scores: ");
12 String line; //read the four scores on a single line
13 Scanner ss = new Scanner(System.in);
14 line = ss.nextLine(); //read values on a single line
separated by whitespace
15 String numbers[] = line.split(" "); //create an array of
string-numbers
16 //now convert this numbers and throw them in the 2D array
17 int k = 0; //track students
18 int c = 0; //track scores
19 int[] marks;
20 marks = new int[4];
21 int t = 0;
22 for(String nums : numbers) //for each string in numbers
23 { //convert to integer
24 int num = Integer.parseInt(nums);
25 marks[t] = num; //temp holder for getting best score
26 scores[k][c] = num;
27 k++;
28 c++;
29 t++;
30 }
31 Arrays.sort(marks); //sort array in ascending order
32 //after sorting, the best score is the last value in the
array
33 int best = marks[marks.length - 1]; //grab the best score
34 c = 0; //for display, start recounting scores
35 char grade = ’F’;
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR| Page 9
36 for(int i = 0; i < scores.length; i++) //for each student
37 {
38 if( scores[i][c] >= best - 10)
39 grade = ’A’;
40 else if (scores[i][c] >= best - 20)
41 grade = ’B’;
42 else if (scores[i][c] >= best - 30)
43 grade = ’C’;
44 else if (scores[i][c] >= best - 40)
45 grade = ’D’;
46 System.out.println("Student " + i + " score is " +
scores[i][c] + " and grade is " + grade);
47 c++;
48 }
49 }
50 }
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 10
(f) Area of circle using InputDialogBox [4 Marks]
1 import javax.swing.JOptionPane;
2 public class AreaCircle {
3
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 11
QUESTION TWO [15 MARKS]
(a) Quadratic roots [8 Marks]
1 import java.util.Scanner;
2 public class Quadratic {
3 public static void main(String[] args) {
4 double[] vals;
5 vals = new double[3];
6 Scanner s = new Scanner(System.in);
7 System.out.print("Enter a, b, c : ");
8 String line = s.nextLine(); //read the three inputs on
single line
9 //split the inputs and convert each to double
10 String[] inputs = line.split(" ");
11 for(int i = 0; i < 3; i++)
12 {
13 double num = Double.parseDouble(inputs[i]);
14 vals[i] = num;
15 }
16 //calculate the discriminant
17 double delta = (vals[1] * vals[1]) - (4 * vals[0] * vals
[2]);
18 double r1 = (-vals[1] + Math.pow(delta, 0.5))/(2 * vals
[0]);
19 double r2 = (-vals[1] - Math.pow(delta, 0.5))/(2 * vals
[0]);
20 if(delta == 0)
21 {
22 System.out.println("The root is " + r1);
23 }
24 else if(delta > 0)
25 {
26 System.out.println("The roots are " + r1 + " and " +
r2);
27 }
28 else
29 System.out.println("The equation has no real roots");
30 }
31 }
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 12
(b) (i) Writing definition of Constructor at line 8 [3 Marks]
1 public Shape(Date date, String color, String Name)
2 {
3 dateCreated = date;
4 this.color = color;
5 name = Name;
6 }
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 13
QUESTION THREE [15 MARKS]
(a) BankAccount class [3 Marks]
1 class BankAccount {
2 private int accountNo;
3 private double balance;
4
5 public BankAccount(){
6
7 }
8 public BankAccount(int no, double bal){
9 accountNo = no;
10 balance = bal;
11 }
12 public void setAcNumber(int n)
13 {
14 accountNo = n;
15 }
16 public void setBalance(double b)
17 {
18 balance = b;
19 }
20 public int getAcNumber(){
21 return accountNo;
22 }
23 public double getBalance(){
24 return balance;
25 }
26 public void withdraw(double amount){
27 balance = balance - amount;
28 }
29 public void deposit(double amount){
30 balance = balance + amount;
31 }
32 public void display(){
33 System.out.print("Account Number = " + getAcNumber() +
34 "\nBalance = " + getBalance());
35 }
36 }
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 14
(b) Checking Account [5 Marks]
1 class CheckingAccount extends BankAccount {
2 private double interestRate;
3 private double minBalance;
4 private double serviceCharges;
5
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 15
(c) Savings Account [4 Marks]
1 class SavingsAccount extends BankAccount {
2 private double rate;
3
4 public SavingsAccount(){
5 }
6 public void setRate(double r){
7 rate = r;
8 }
9 public double getRate(){
10 return rate;
11 }
12 public double postInterest(){
13 return rate * getBalance();
14 }
15 @Override
16 public void withdraw(double amount){
17 setBalance(getBalance() - amount);
18 }
19 @Override
20 public void display(){
21 System.out.print("Account Number = " + getAcNumber() +
22 "\nBalance = " + getBalance() +
23 "\nInterest Rate = " + getRate());
24 }
25 }
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 16
QUESTION FOUR [15 MARKS]
(a) Draw UML. Implement the class. Write test program [12 Marks]
Account
int id
double balance
double annualInterestRate
Date dateCreated
Account()
Account(int, double)
setID(int):void
setBalance(double):void
setInterest(double):void
getID(): int
getBalance(): double
getInterest(): double
getDateCreated(): String/Date
getMonthlyInterestRate(): double
withdraw(double): void
deposit(double): void
9 public Account() {
10
11 }
12 public Account(int id, double bal){
13 this.id = id;
14 balance = bal;
15 }
16 public void setID(int i) {
17 id = i;
18 }
19 public void setBalance(double b){
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 17
20 balance = b;
21 }
22 public void setInterest(double r) {
23 annualInterestRate = r;
24 }
25 public int getID(){
26 return id;
27 }
28 public double getBalance(){
29 return balance;
30 }public double getInterest(){
31 return annualInterestRate;
32 }
33 public String getDateCreated(){
34 return dateCreated.toString();
35 }
36 public double getMonthlyInterestRate(){
37 return annualInterestRate / 12;
38 }
39 public void withdraw(double amount){
40 balance = balance - amount;
41 }
42 public void deposit(double amount){
43 balance = balance + amount;
44 }
45 }
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 18
11 System.out.println("Date Created = " + ac.getDateCreated
());
12 }
13 }
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 19
(b) Subclass of C named B [5 Marks]
1 public class B extends C {
2 public int m1() {
3 return m - n;
4 }
5 }
6 //Since m and n have default access modifier, they can
7 //accessed from within the same package assuming the
8 //two classes are in the same package. If the two
9 //classes are not in the same package, then m and n
10 //should either have protected or public access modifiers
|BCS 210 Object Oriented Programming, SCI, MMUST 2018. Main Examination. Dr. AR|Page 20