Object-Oriented Programming Project

Verified

Added on  2019/10/18

|5
|849
|259
Project
AI Summary
This project demonstrates fundamental object-oriented programming principles in Java. It involves creating three classes: Person, Student, and Staff. The Person class serves as a base class, with Student and Staff inheriting from it. Each class has its own attributes and methods, showcasing encapsulation. The Student class includes attributes like program, year, and fee, while the Staff class includes school and pay. A test class instantiates objects of these classes and demonstrates the use of methods, including the toString() method for output. The provided code includes the complete Java source code for each class, along with a test class to demonstrate functionality. The output section shows the expected console output when running the test class.
Document Page
Project Name
University Name , with address
Date
Submitted By –
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Ques 1
package ques1;
public class Person {
private String name;
private String address;
public Person(String name, String address) {
this.name = name;
this.address = address;
}
public Person(){}
@Override
public String toString() {
return "name='" + name + "', address='" + address + "' ";
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
}
Ques 2
package ques2;
import ques1.Person;
public class Staff extends Person {
private String school;
private double pay;
public Staff(String name, String address, String school, double pay) {
super(name, address);
this.school = school;
this.pay = pay;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
this.pay = pay;
}
Document Page
@Override
public String toString() {
return super.toString() +
"school='" + school + '\'' +
", pay=" + pay;
}
}
package ques2;
import ques1.Person;
public class Student extends Person {
private String program;
private int year;
private double fee;
@Override
public String toString() {
return super.toString() + "program='" + program + '\'' +
", year=" + year +
", fee=" + fee +
" ";
}
public String getProgram() {
return program;
}
public void setProgram(String program) {
this.program = program;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getFee() {
return fee;
}
public void setFee(double fee) {
this.fee = fee;
}
public Student(String name, String address, String program, int year, double
fee) {
super(name, address);
this.program = program;
this.year = year;
this.fee = fee;
}
}
Test Class
Document Page
package test;
import ques2.Staff;
import ques2.Student;
public class Test {
public static void main(String args[]) {
// Creating 2 students
Student student1 = new Student("StudentName1", "StudentAddress1",
"Computers", 2018, 23000.5);
Student student2 = new Student("StudentName2", "StudentAddress2",
"Economics", 2016, 25000.75);
// Creating a staff member
Staff staff = new Staff("Staff1", "StaffAddress1", "Science", 500000);
//calling methods from student class
//Student1
System.out.println("Student 1 Details");
System.out.println("Student1 Name : " + student1.getName());// getName()
method from Person Class
System.out.println("Student1 Address : " + student1.getAddress());//
getAddress() method from Person Class
System.out.println("Student1 Program : " + student1.getProgram());
System.out.println("Student1 Year : " + student1.getYear());
System.out.println("Student1 Fee : " + student1.getFee());
System.out.println("To String method : "+student1);
System.out.println("");
//Student2
System.out.println("Student 2 details");
System.out.println("Student2 Name : " + student2.getName());// getName()
method from Person Class
System.out.println("Student2 Address : " + student2.getAddress());//
getAddress() method from Person Class
System.out.println("Student2 Program : " + student2.getProgram());
System.out.println("Student2 Year : " + student2.getYear());
System.out.println("Student2 Fee : " + student2.getFee());
System.out.println("To String method : "+student2);
System.out.println("");
//Staff members
System.out.println("Staff member details");
System.out.println("Staff Member Name : " + staff.getName());// getName()
method from Person Class
System.out.println("Staff Member Address : " + staff.getAddress());//
getAddress() method from Person Class
System.out.println("Staff Member School : " + staff.getSchool());
System.out.println("Staff Member Pay : " + staff.getPay());
System.out.println("To String method : "+staff);
System.out.println("");
}
}
Output
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
chevron_up_icon
1 out of 5
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]