Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person {
- // private instance variables
- private String name, address;
- //Constructs a Person instance with the given name and address
- public Person(String name, String address) {
- this.name = name;
- this.address = address;
- }
- // Getters and Setters
- public String getName() {
- return name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @Override
- public String toString() {
- return name + " ( " + address + " ) ";
- }
- }
- //next class
- public class Student extends Person {
- //private instance variables
- private String program;
- private int year;
- private double fee;
- //constructor
- public Student(String name, String address) {
- super(name, address);
- program = "";
- year = 0;
- fee = 0.00;
- }
- @Override
- public String toString() {
- return "Student: " + super.toString() + ", Program: " + getProgram() + ", Year: " + getYear() + ", Fee: " + getFee();
- }
- //getters and setters
- 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;
- }
- }
- //next class
- public class Staff extends Person {
- //private instance variables
- private String school;
- private double pay;
- //constructor
- public Staff(String name, String address) {
- super(name, address);
- school = "";
- pay = 0.00;
- }
- @Override
- public String toString() {
- return "Staff: " + super.toString() + ", School: " + getSchool() + ", Pay: " + getPay();
- }
- //getters and setters
- 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;
- }
- }
- //test class
- public class testStudentStaff {
- public static void main(String[] args) {
- //to test the student class
- Student s1 = new Student("Jiang Cheng", "05 Yunmeng Rd.");
- s1.setProgram("Information Technology");
- s1.setYear(2023);
- s1.setFee(19000.00);
- System.out.println(s1);
- System.out.println(); //blank print statement to make the output look neater
- Student s2 = new Student("Wei Wuxian", "45 Yiling Ave.");
- s2.setProgram("Theoretical Physics");
- s2.setYear(2022);
- s2.setFee(21000.00);
- System.out.println(s2);
- System.out.println(); //blank print statement to make the output look neater
- //to test the staff class
- Staff st1 = new Staff("Lan Xichen", "111 Gusu Dr.");
- st1.setSchool("Cloud Recesses University");
- st1.setPay(9000.00);
- System.out.println(st1);
- System.out.println(); //blank print statement to make the output look neater
- Staff st2 = new Staff("Lan Qiren", "22 Gusu Dr.");
- st2.setSchool("Cloud Recesses University");
- st2.setPay(12000.00);
- System.out.println(st2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement