TAKE HOME EXAMINATION
SEMESTER 7 / 2020
CDOP3203
JAVA PROGRAMMING
MATRICULATION NO : 900307115181001
IDENTITY CARD NO. : 900307115181
PROGRAMME : DIPLOMA IN INFORMATION TECHNOLOGY
TELEPHONE NO. : 01133500911
E-MAIL : [email protected]
LEARNING CENTRE : TERENGGANU LEARNING CENTRE
PART A
1. Inheritance
2. A “subclass” is created when it is derived from “another class”. The class that is derived
from is also known as “superclass”. It’s a hierarchy of objects in which the “subclass inherits
from “superclass”.
3. The correct method to access a priate data from a different class is by creating and using
“getter” and “setter” methods. This is also called “accessor” and “mutator” methods.
4. 5
5. “Method overriding” is an approach that replaces a relatively similar method from the
inherited class in a subclass. Usually, the “overriding method” have the same name, number
and types of parameter values, and return value type as the inherited method.
PART B
Question 1
public class Account {
private int ID;
private double BALANCE;
public Account(int id, double balance ) {
ID = id;
BALANCE = balance;
}
public int getId() {
return ID;
}
public double getBalance () {
return BALANCE;
}
public void setId (int id) {
ID = id;
}
public void withdraw (double amount) {
if (amount >= BALANCE)
BALANCE -= amount;
}
public void deposit (double amount) {
BALANCE += amount;
}
}
Question 2
public class Person {
private String Name;
private String Address;
private String Phone;
private String Email;
public Person () {
Name = "";
Address = "";
Phone = "";
Email = "";
}
public Person (String name, String address, String phone, String email) {
Name = name;
Address = address;
Phone = phone;
Email = email;
}
public String getName() {
return Name;
}
public String getAddress() {
return Address;
}
public String getPhone() {
return Phone;
}
public String getEmail() {
return Email;
}
public void setName(String name) {
Name = name;
}
public void setAddress(String address) {
Address = address;
}
public void setPhone(String phone) {
Phone = phone;
}
public void setEmail(String email) {
Email = email;
}
public void display () {
System.out.println("Name :" + Name);
System.out.println("Address :" + Address);
System.out.println("Phone :" + Phone);
System.out.println("Email :" + Email);
}
}
public class Student extends Person {
private int StudentNo;
public Student () {
super();
StudentNo = "";
}
public Student (String name, String address, String phone, String email, int studentno) {
super(name, address, phone, email);
StudentNo = studentno;
}
public int getStudentNo () {
return StudentNo;
}
public void setStudentNo (int studentno) {
StudentNo = studentno;
}
public void display () {
super.display();
System.out.println("StudentNo :" + StudentNo);
}
public class Employee extends Person {
private double Salary;
private int DepartmentId;
public Employee() {
super();
Salary = 0.00;
DepartmentId = 0;
}
public Employee(String name, String address, String phone, String email, double salary,
int departmentid) {
super(name, address, phone, email);
Salary = salary;
DepartmentId = departmentid;
}
public double getSalary() {
return Salary;
}
public int getDepartmentId() {
return DepartmentId;
}
public void setSalary() {
Salary = salary;
}
public void setDepartmentId() {
DepartmentId = departmentid;
}
public void display () {
super.display();
System.out.println("Salary :" + Salary);
System.out.println("DepartmentId :" + DepartmentId);
}
}