SlideShare ist ein Scribd-Unternehmen logo
1 von 11
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);
}
}

Weitere ähnliche Inhalte

Was ist angesagt?

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 

Was ist angesagt? (20)

Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String FormattingPython Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String Formatting
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, Loops
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Chat Room System using Java Swing
Chat Room System using Java SwingChat Room System using Java Swing
Chat Room System using Java Swing
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
This pointer
This pointerThis pointer
This pointer
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Constructor
ConstructorConstructor
Constructor
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Loops
LoopsLoops
Loops
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
java graphics
java graphicsjava graphics
java graphics
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
F# for C# Programmers
F# for C# ProgrammersF# for C# Programmers
F# for C# Programmers
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Gym Management System User Manual
Gym Management System User ManualGym Management System User Manual
Gym Management System User Manual
 

Ähnlich wie Assignment 7

Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
irshadkumar3
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
shatha00
 
My code is not matching up with the results.The output for the cod.pdf
My code is not matching up with the results.The output for the cod.pdfMy code is not matching up with the results.The output for the cod.pdf
My code is not matching up with the results.The output for the cod.pdf
solimankellymattwe60
 

Ähnlich wie Assignment 7 (20)

11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
11slide
11slide11slide
11slide
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
Oop
OopOop
Oop
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Simple class and object examples in java
Simple class and object examples in javaSimple class and object examples in java
Simple class and object examples in java
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
My code is not matching up with the results.The output for the cod.pdf
My code is not matching up with the results.The output for the cod.pdfMy code is not matching up with the results.The output for the cod.pdf
My code is not matching up with the results.The output for the cod.pdf
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Core java oop
Core java oopCore java oop
Core java oop
 
Design pattern
Design patternDesign pattern
Design pattern
 

Mehr von IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Heaps
HeapsHeaps
Heaps
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 

Kürzlich hochgeladen

如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
avy6anjnd
 
一比一原版(Greenwich毕业证书)格林威治大学毕业证如何办理
一比一原版(Greenwich毕业证书)格林威治大学毕业证如何办理一比一原版(Greenwich毕业证书)格林威治大学毕业证如何办理
一比一原版(Greenwich毕业证书)格林威治大学毕业证如何办理
bd2c5966a56d
 
一比一原版伯明翰城市大学毕业证成绩单留信学历认证
一比一原版伯明翰城市大学毕业证成绩单留信学历认证一比一原版伯明翰城市大学毕业证成绩单留信学历认证
一比一原版伯明翰城市大学毕业证成绩单留信学历认证
62qaf0hi
 
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
opyff
 
John Deere Tractors 5415 Diagnostic Repair Service Manual.pdf
John Deere Tractors 5415 Diagnostic Repair Service Manual.pdfJohn Deere Tractors 5415 Diagnostic Repair Service Manual.pdf
John Deere Tractors 5415 Diagnostic Repair Service Manual.pdf
Excavator
 
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
ezgenuh
 
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
wsppdmt
 
Top profile Call Girls In dharamshala [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In dharamshala [ 7014168258 ] Call Me For Genuine Mode...Top profile Call Girls In dharamshala [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In dharamshala [ 7014168258 ] Call Me For Genuine Mode...
gajnagarg
 
CELLULAR RESPIRATION. Helpful slides for
CELLULAR RESPIRATION. Helpful slides forCELLULAR RESPIRATION. Helpful slides for
CELLULAR RESPIRATION. Helpful slides for
euphemism22
 
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE AbudhabiAbortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills in Kuwait Cytotec pills in Kuwait
 

Kürzlich hochgeladen (20)

Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best ServiceMuslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
Muslim Call Girls Churchgate WhatsApp +91-9930687706, Best Service
 
Stacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
Stacey+= Dubai Calls Girls O525547819 Call Girls In DubaiStacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
Stacey+= Dubai Calls Girls O525547819 Call Girls In Dubai
 
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVESEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
SEM 922 MOTOR GRADER PARTS LIST, ALL WHEEL DRIVE
 
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
如何办理伦敦商学院毕业证(LBS毕业证)毕业证成绩单原版一比一
 
一比一原版(Greenwich毕业证书)格林威治大学毕业证如何办理
一比一原版(Greenwich毕业证书)格林威治大学毕业证如何办理一比一原版(Greenwich毕业证书)格林威治大学毕业证如何办理
一比一原版(Greenwich毕业证书)格林威治大学毕业证如何办理
 
一比一原版伯明翰城市大学毕业证成绩单留信学历认证
一比一原版伯明翰城市大学毕业证成绩单留信学历认证一比一原版伯明翰城市大学毕业证成绩单留信学历认证
一比一原版伯明翰城市大学毕业证成绩单留信学历认证
 
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
 
Effortless Driving Experience Premier Mercedes Sprinter Suspension Service
Effortless Driving Experience Premier Mercedes Sprinter Suspension ServiceEffortless Driving Experience Premier Mercedes Sprinter Suspension Service
Effortless Driving Experience Premier Mercedes Sprinter Suspension Service
 
John Deere Tractors 5415 Diagnostic Repair Service Manual.pdf
John Deere Tractors 5415 Diagnostic Repair Service Manual.pdfJohn Deere Tractors 5415 Diagnostic Repair Service Manual.pdf
John Deere Tractors 5415 Diagnostic Repair Service Manual.pdf
 
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
一比一原版(UdeM学位证书)蒙特利尔大学毕业证学历认证怎样办
 
Vip Begusarai Escorts Service Girl ^ 9332606886, WhatsApp Anytime Begusarai
Vip Begusarai Escorts Service Girl ^ 9332606886, WhatsApp Anytime BegusaraiVip Begusarai Escorts Service Girl ^ 9332606886, WhatsApp Anytime Begusarai
Vip Begusarai Escorts Service Girl ^ 9332606886, WhatsApp Anytime Begusarai
 
Washim Call Girls 📞9332606886 Call Girls in Washim Escorts service book now C...
Washim Call Girls 📞9332606886 Call Girls in Washim Escorts service book now C...Washim Call Girls 📞9332606886 Call Girls in Washim Escorts service book now C...
Washim Call Girls 📞9332606886 Call Girls in Washim Escorts service book now C...
 
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
 
Top profile Call Girls In dharamshala [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In dharamshala [ 7014168258 ] Call Me For Genuine Mode...Top profile Call Girls In dharamshala [ 7014168258 ] Call Me For Genuine Mode...
Top profile Call Girls In dharamshala [ 7014168258 ] Call Me For Genuine Mode...
 
CELLULAR RESPIRATION. Helpful slides for
CELLULAR RESPIRATION. Helpful slides forCELLULAR RESPIRATION. Helpful slides for
CELLULAR RESPIRATION. Helpful slides for
 
Changodar Call Girls Book Now 7737669865 Top Class Escort Service Available
Changodar Call Girls Book Now 7737669865 Top Class Escort Service AvailableChangodar Call Girls Book Now 7737669865 Top Class Escort Service Available
Changodar Call Girls Book Now 7737669865 Top Class Escort Service Available
 
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE AbudhabiAbortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
Abortion pills Dubai (+918133066128) Cytotec 200mg pills UAE Abudhabi
 
Premium Call Girls Nagpur Call Girls (Adult Only) 💯Call Us 🔝 6378878445 🔝 💃 E...
Premium Call Girls Nagpur Call Girls (Adult Only) 💯Call Us 🔝 6378878445 🔝 💃 E...Premium Call Girls Nagpur Call Girls (Adult Only) 💯Call Us 🔝 6378878445 🔝 💃 E...
Premium Call Girls Nagpur Call Girls (Adult Only) 💯Call Us 🔝 6378878445 🔝 💃 E...
 
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
 
John deere 7200r 7230R 7260R Problems Repair Manual
John deere 7200r 7230R 7260R Problems Repair ManualJohn deere 7200r 7230R 7260R Problems Repair Manual
John deere 7200r 7230R 7260R Problems Repair Manual
 

Assignment 7

  • 1. 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
  • 2. 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) {
  • 3. 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
  • 4. 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; } }
  • 5. 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!!"); } } } }
  • 6. 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;
  • 7. } 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; }
  • 8. 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; } }
  • 9. 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"); } }
  • 10. 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; }
  • 11. 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); } }