Professional Documents
Culture Documents
HCT 216: Programming 2: (4 Marks)
HCT 216: Programming 2: (4 Marks)
Examination
June 2017
Question 1
a) At which line does the object "one" first become eligible for garbage collection?
b) At which line does the object "two" first become eligible for garbage collection?
c) At which line does the object "three" first become eligible for garbage collection?
d) What does the code in Line 12 do ?
[4 marks]
Study the code below and write down the output it will produce if the class is executed.
1 public class Q2 {
2 public static void main(String[] args) {
3 String firstName = "John";
4 String surname = new String("Doe");
5 if (firstName == "John") {
6 System.out.println("Yes name is John");
7 }
8 firstName.concat(" ");
9 firstName.concat(surname);
10 System.out.println(firstName);
11
12 int i = 4;
13 int j = 11;
14 double d = 10.0;
15 System.out.println(j / i);
16 System.out.println(j % i);
17 System.out.println(d / i);
18
19 for (int x=1; x<10; x++) {
20 System.out.println(++x);
21 }
22
23 if (surname == "Doe") {
24 System.out.println("Yes surname is Doe");
25 }
26 }
27 }
[8 marks]
[9 marks]
Question 4
Create a class called "Q4" which, when it is executed, it will do what is requested below. Please note
that you need to read all tasks that the program will do before attempting to answer; so that you
can understand the full context before answering. Full code that can be complied is required.
a) Takes 2 integer arguments, the first one between 0 and 9 inclusive; and the second one
between 20 and 29 inclusive. It must throw the IllegalArgumentException (which is found in
Java in-built package java.lang) if any of the arguments are not within the specified ranges.
[7 marks]
b) Creates an exactly sized array to store the numbers from the first integer to the second
integer, inclusive. [2 marks]
c) Uses a for... loop to store all the numbers into the array, in reverse order, i.e. the biggest will
be on array index 0; and the smallest will be on the last array index. [3 marks]
d) Print-out the array onto the console using a while... loop [2 marks]
e) Sort the array using built-in Java API [1 mark]
f) Print-out the sorted array onto the console using a for ...each loop [2 marks]
g) Print-out the array index on which the number 15 is found within the array [1 mark]
[18 marks]
Carefully study the code of the three classes below and answer the questions that follow
Customer Class
1 package customers;
2 import accounts.BankAccount;
3 public class Customer {
4 private String name;
5
6 public void checkCreditStatus() {
7 }
8
9 public static void main(String[] args) {
10 BankAccount ba = new BankAccount();
11 System.out.println(BankAccount.count);
12 System.out.println(ba.accountNumber);
13 ba.deposit(200.00);
14 }
15 }
BankAccount Class
1 package accounts;
2 public class BankAccount {
3 private static int count;
4 protected String accountNumber;
5
6 public void deposit(double amount) {
7 }
8 }
CurrentAccount Class
1 package accounts;
2 public class CurrentAccount extends BankAccount {
3
4 public void method1() {
5 System.out.println(BankAccount.count);
6 System.out.println(this.accountNumber);
7 this.deposit(200.00);
8 }
9 }
[12 marks]
Question 6
Write code that satisfies the following. Include any necessary import statements. The code should be
in full; and be able to be successfully compiled.
Case study
An online shop allows people to buy various goods online. Customers place orders to buy various
goods, called "sales orders". Each sales order can have many sales order lines; each for a particular
product. Products are represented as "stock items" in the system. Thus an order line shows detail
about the stock item bought (using the stock item code), the quantity and the price. When they buy
for the first time, customers get registered into the system. When a customer is not happy with
products, they return them by post; and also capture another type of order: called a "returns order".
A returns order may also have many order lines exactly like the order lines on a sales order. Thus,
both types of orders have the following common details: customer who placed the order, order
date, total order value and order lines. The "Returns Order" has got the following additional details
unique to it, but are not in the "Sales Order" class. These are: reason for returning goods, name of
manager approving the return, and return charge. The "Sales Order" has only one attribute unique
to it, i.e. the discount amount given.
a) Write code for a Customer class with information including: customer names, address details
(street address & town), mobile number and email. Choose appropriate data types for your
class attributes. Ensure correct abstraction and access protection of class members. Include
getters and setters. [5 marks]
b) Write code for classes that implement:
"Order" and its other 2 types : "Sales Order" and "Returns Order"
The "Order Line"; of which an order can have one or more order lines
c) Write code for a Shop class, with the "main" method that can run. In that main method, do
the following:
instantiate a Customer object and assign some hypothetical values to its
attributes
instantiate a Sales Order object and assign it some hypothetical values to its
attributes
instantiate a Return Order object and assign it some hypothetical values to its
attributes
[8 marks]
[38 marks]
END