3
Question 1: Refactor the following 2 programs to make them more OO compliant: (you can ignore any imported packages) ________________________________________________________________________________ ____ public class Fibonacci { int startingNum, current, prev, prevprev; public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(“Please your starting Fibonacci number:”); StartingNum = input.nextInt(); prev = StartingNum – 1; prevprev = prev -1; for(int i = 0; i < 20; i++) { // Loop exactly 20 times current = prev + prevprev; // Next number is sum of previous two System.out.print(current + " "); // Print it out prevprev = prev; // First previous becomes 2nd previous prev = current; // And current number becomes previous } System.out.println(); // Terminate the line, and flush output } } (Five marks) ________________________________________________________________________________ ______ public class MyClass { public static void main(String[] args) { Scanner userinput = new Scanner(System.in); System.out.println("Welcome to Java Calculator"); System.out.print("Enter First Number:"); int num1 = userinput.nextInt(); System.out.print("Enter Second Number:"); int num2= userinput.nextInt(); System.out.print("Enter operation to perform (+,-,x,/):"); String operation= userinput.next(); if (operation.equals("+")) System.out.println(num1 + num2); else if (operation.equals("-")) System.out.println(num1 - num2);

OODJ-MockTest-1

Embed Size (px)

DESCRIPTION

fbdd

Citation preview

Page 1: OODJ-MockTest-1

Question 1: Refactor the following 2 programs to make them more OO compliant: (you can ignore any imported packages)

____________________________________________________________________________________public class Fibonacci { int startingNum, current, prev, prevprev;

public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(“Please your starting Fibonacci number:”); StartingNum = input.nextInt();

prev = StartingNum – 1;prevprev = prev -1;

for(int i = 0; i < 20; i++) { // Loop exactly 20 times current = prev + prevprev; // Next number is sum of previous two System.out.print(current + " "); // Print it out prevprev = prev; // First previous becomes 2nd previous prev = current; // And current number becomes previous } System.out.println(); // Terminate the line, and flush output }}

(Five marks)______________________________________________________________________________________

public class MyClass {

public static void main(String[] args) {Scanner userinput = new Scanner(System.in);System.out.println("Welcome to Java Calculator");

System.out.print("Enter First Number:");int num1 = userinput.nextInt();

System.out.print("Enter Second Number:");int num2= userinput.nextInt();

System.out.print("Enter operation to perform (+,-,x,/):");String operation= userinput.next();

if (operation.equals("+"))System.out.println(num1 + num2);

else if (operation.equals("-"))System.out.println(num1 - num2);

else if (operation.equals("x"))System.out.println(num1 * num2);

else if (operation.equals("/"))System.out.println(num1 / num2);

elseSystem.out.println("The operation is not valid.");

} } (5 marks)______________________________________________________________________________________

Page 2: OODJ-MockTest-1

Question 2: Draw a Use Case diagram for the following scenario:

2(a) Draw a Use Case for the above code (MyClass) (4 marks)

2(b) APHotel is provides short term accommodation for families/parents visiting their children at APU. The hotel operates on a reservation basis but does try and keep a few rooms free in case any visitors turn up to reception without a reservation. Guests use the online system to make reservations, as well as to add additional packages such as tours, spa sessions etc

The Hotel tries to provide guests with as much flexibility as possible, therefore guests can use the Hotel's website to extend their stay provided this is done at least one day in advance. Alternatively, guests can also reduce the number of days they would like to stay by using the same system. (4 marks)

Question 3: Create a class diagram Using the following classes and relationship types:

Classes Order Customer Account NormalOrder Company Payment Product PriorityOrder ProductInformation OrderStatus Individual Delivery

relationships association aggregation composition generalisation

(7 marks)