ASSIGNMENT 7
MUHAMMAD MUZZAMMIL BIN JUSOF
1417299
SECTION 5
DR. ALI ALWAN
Q1: What is the output of running class C?
class A {
public A () {
System.out.println(“A’s no-arg constructor is invoked”);
}
}
class B extends A {
}
public class C {
public static void main (String [] args ) {
B b = new B ();
}
}
Answer :
A’s no-arg constructor is invoked
Q2: True/ false
1. When invoking a constructor from a subclass, its superclass’s no-arg constructor is
always invoked.
2. You can override a private method defined in a superclass.
3. You can override a static method defined in a superclass.
4. You can always successfully cast an instance of a subclass to a superclass
5. You can always successfully cast an instance of a superclass to a subclass.
6. A protected method can be accessedby any class in the same package.
7. A protected method can be accessedby its subclasses in any package.
8. A final class can be extended.
9. A final method can be overridden
Answer :
1) False
2) False
3) True
4) True
5) False
6) False
7) True
8) False
9) False
Q3: Explain the difference between method overloading and method overriding.
Answer :
Method overloading is used to increase the readability of the program. Method overriding is
used to provide the specific implementation of the method that is already provided by its super
class. Method overloading is performed within class. Method overriding occurs in two
classes that have IS-A (inheritance) relationship. In case of method overloading, parameter
must be different. In case of method overriding, parameter must be same. Method overloading
is the example of compile time polymorphism. Method overriding is the example of run time
polymorphism. In java, method overloading can't be performed by changing return type of the
method only. Return type can be same or different in method overloading. But you must have
to change the parameter. Return type must be same or covariant in method overriding.
Q4: Show the output of the following code:
public class Test {
public static void main (String [] args) {
new Person().printPerson();
new Student().printPerson();
}
}
class Student extends Person {
@Override
public String getInfo() {
return “Student”;
}
}
class Person {
public String getInfo() {
return “Person”;
}
public void printPerson() {
System.out.println(getInfo());
}
}
Answer :
Person
Student
Q5: What is wrong in the following program?
public class Test {
public static void main (String [ ] args) {
I b = new I(5);
}
}
class H extends I {
public H (int t) {
System.out.println("H's constructor is invoked ");
}
}
class I {
public I (){
}
public I(int k) {
System.out.println("I's construtor is invoked");
}
}
Answer :
public class Test
{
public static void main (String [ ] args) {
I b = new I(5);
}
}
class H extends I {
public H (int t) {
System.out.println("H's constructor is invoked ");
}
}
class I { public I (){
}
public I(int k) {
System.out.println("I's construtor is invoked");
}
}
Q6:
1. Write a Java program for the following scenario:
Suppose we want to monitor the academic performance of students in the Kulliyyah of
ICT. Students consist of undergraduate and graduate students. For eachstudent, we need
to record the student’s name, ten test scores, and the course grade. The program should
store the test scores in an array of type double. The course grade, either “PASS” or “NO
PASS”, is determined by the following formula:
Type of student Grading scheme
Undergraduate PASS if average testscore >=50
Graduate PASS if average testscore >=70
You should provide the following:
a. Identify relevant classes and design a UML class diagram to represent the relationship
between classes using an inheritance hierarchy. You must include relevant attributes and
methods for all the classes.The classes should consist of at least i) overloading constructor
methods, ii) get and set method for each attributes (getName, setName, getTestScore,
setTestScore etc.)
b. Write the complete program based on your design.
Answer :
TestScore
name: String
score: double
+TestScore(name: String, score: double)
+getName(): String
+setName(name: String): void
+getScore(): double
+setScore(score: double): void
public class TestScore
{
String name;
double score;
public TestScore(String name, double score)
{
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
import java.util.*;
public class TestTestScore
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter student name : ");
String name = input.next();
System.out.println("Please enter student level : 1) undergraduate 2) graduate
");
int level = input.nextInt();
System.out.println("Please enter ten score : ");
int score[] = new int[10];
for(int i = 1; i <= score.length; i++)
{
score[i] = input.nextInt();
if(level == 1)
{
if(score[i] >= 50 && score[i] <= 100)
System.out.println("Pass");
else if(score[i] >=0 && score[i] < 50)
System.out.println("Not pass");
}
else if(level == 2)
{
if(score[i] >= 70 && score[i] <= 100)
System.out.println("Pass");
else if(score[i] >=0 && score[i] < 70)
System.out.println("Not pass");
}
else
{
System.out.println("Wrong input!!");
}
}
}
}
Q7: (The Person, Student, Employee, Faculty, and Staff classes). Design a class named
Person and its two subclasses named Student and Employee. Make Faculty and Staff
subclasses of Employee. A person has a name, address, phone number, and email address.
A student has a class status (freshman, sophomore, junior, or senior). Define the status as
a constant. An employee has an office, salary, and date hired. A faculty member has office
hours and a rank. A staff member has a title. Override the toString method in each class
to display the class name and the person’s name.
Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and
invokes their toString() methods.
Answer :
public class Person
{
private String name;
private String address;
private int phone_number;
private String email;
public Person()
{
}
public Person(String name, String address, int phone_number, String email)
{
this.name = name;
this.address = address;
this.phone_number = phone_number;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPhone_number() {
return phone_number;
}
public void setPhone_number(int phone_number) {
this.phone_number = phone_number;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String toString(){
return getClass().getName() + "n" + name;
}
}
public class Student extends Person
{
private final String status;
public Student(String status)
{
this.status = status;
}
public String getStatus() {
return status;
}
}
import java.util.*;
public class Employee extends Person
{
private String office;
private double salary;
private String date;
public Employee()
{
}
public Employee(String office, double salary, String date)
{
this.office = office;
this.salary = salary;
this.date = date;
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
public class Faculty extends Employee
{
private int hours;
private int rank;
public Faculty()
{
}
public Faculty(int hours, int rank)
{
this.hours = hours;
this.rank = rank;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}
public class Staff extends Employee
{
private String title;
public Staff()
{
}
public Staff(String title)
{
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public class TestQ7
{
public static void main(String[] args)
{
Person person = new Person();
Person person1 = new Person("Razak", "Terengganu", 011234567,
"Razak@gmail.com" );
Person student = new Student("junior");
Person employee = new Employee("KL", 12345.95, "02-02-2000" );
Person faculty = new Faculty(9,3);
Person staff = new Staff("Manager");
System.out.println(person.toString() + "n");
System.out.println(student.toString() + "n");
System.out.println(employee.toString() + "n");
System.out.println(faculty.toString() + "n");
System.out.println(staff.toString() + "n");
}
}
Q8: (Subclasses of Account). In lab session4 (Objects and Classes)exercise 2,the Account
class was defined to model a bank account. An account has the properties name, account
number, balance, and methods deposit, withdraw, and get balance. Create two subclasses
for checking and saving accounts. A checking account has an overdraft limit, but a saving
account cannot be overdrawn.
Write a test program that creates objects of Account, SavingsAccount, and
CheckingAccount and invokes their toString() methods.
Answer :
public class Account
{
private String name;
private int accountNo;
private double balance;
public double withdraw;
public double deposit;
public Account()
{
}
public Account(String name, int accountNo, double balance)
{
this.name = name;
this.accountNo = accountNo;
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAccountNo() {
return accountNo;
}
public void setAccountNo(int accountNo) {
this.accountNo = accountNo;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double deposit(double amount)
{
return balance += amount;
}
public double withdraw(double amount)
{
return balance -= amount;
}
public String toString()
{
return "Banking Account Informationn" + "nCustomer Name " + name
+"nAccount Number " + accountNo + "nWithdraw Amount is " + withdraw
+ "nDeposite Amount " + deposit + "nBalance " + balance;
}
}
public class SavingsAccount extends Account
{
SavingsAccount(){ super();}
}
public class CheckingAccount extends Account
{
CheckingAccount(){ super();}
}
public class TestAccount
{
public static void main (String[] args)
{
Account acct1 = new Account ("Mahmud", 123456, 95000.00);
Account acct2 = new Account ("Rosli", 224561, 9500.00);
acct1.withdraw (-100.00);
acct2.deposit (500);
System.out.println (acct1);
System.out.println (acct2);
}
}