Week 12: Sample Questions: Review This Subject
Week 12: Sample Questions: Review This Subject
Week 12: Sample Questions: Review This Subject
2. Which of the following methods can you use to compile and run a Java source file
“Hello.java” that contains a main() method ?
1
4. Which of the following Java code fragments compile correctly?
a. String x = 2;
b. String s = "Hello World"
c. String myName; myname = "Harry";
d. String myName; myName = "George";
Line 1 a. Method
Line 2 b. Constructor
2
6. Consider the code below:
FILE: Message.java
public class Message {
String message;
FILE: Printer.java
public class Printer {
public static void main(String args[]) {
Message msg1 = new Message("A");
Message msg2 = new Message("B");
System.out.println(msg1);
System.out.println(msg2);
msg1 = msg2;
System.out.println(msg1);
System.out.println(msg2);
msg1.setMessage("C");
System.out.println(msg1);
System.out.println(msg2);
System.out.println();
}
}
3
Use 30 minutes to do the 3 sample (part C) questions.
PART C: Write code
1. The class Department has the attributes deptName(String), numberOfPersons (int).
• Write code for a class that represents the Department with fields deptName and
numberOfPersons. The class must have a suitable constructor with parameters. The
class must also have a toString() method which formats the content of the object as
a printable String.
• Write a main( ) that creates one object of the Department class and calls the
toString() method to print this object.
class Account
{
private int accountNo; // account number
private String owner; // primary owner
private double balance; // account balance
protected String accountType; // "SINGLE"
public Account( int n, String ow, double b)
{
accountNo = n;
balance = b;
owner = ow;
accountType = "SINGLE";
}
balance = bal;
}
4
public int getAccountNumber()
{
return accountNo;
}
a. Write another class JointAccount that inherits from class Account. This new class
should have one instance variable - a name of the joint owner. The account number,
balance and account type are kept in the superclass, however account type ought to
be accessible from the derived class and have to be set to “JOINT”. You can change
account type to protected or hide the field to achieve this.
b. Write method toString() that overrides the method in the super class. It displays the
same information as the initial toString() method in the superclass, but with the
addition of the name of the co-owner.
5
3. The following program is to provide a user interface to calculate the interest. First, a
JLabel object is displayed with the message "Enter Balance: ", then the balance (e.g.
10000) should be entered to the balance field (via a JTextField). Second a JLabel
object is displayed with the message "Enter Interest Rate %: ", then the rate (e.g. 5.5)
should be entered to the rate field. Finally, the "Calculate Interest" button is clicked
and the result (e.g. "Interest earn: 550") is shown in the output field (via a JTextField).
You can find more questions from Lecture Notes and Lab exercises
Compile and run the three programs you written in Part C above