Advertisement
thotfrnk

ex4-2.java

Jun 7th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. public class Person {
  2.     // private instance variables
  3.     private String name, address;
  4.  
  5.     //Constructs a Person instance with the given name and address
  6.     public Person(String name, String address) {
  7.         this.name = name;
  8.         this.address = address;
  9.     }
  10.  
  11.     // Getters and Setters
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public String getAddress() {
  16.         return address;
  17.     }
  18.     public void setAddress(String address) {
  19.         this.address = address;
  20.     }
  21.  
  22.     @Override
  23.     public String toString() {
  24.         return name + " ( " + address + " ) ";
  25.     }
  26. }
  27.  
  28. //next class
  29.  
  30. public class Student extends Person {
  31.  
  32.     //private instance variables
  33.     private String program;
  34.     private int year;
  35.     private double fee;
  36.  
  37.     //constructor
  38.     public Student(String name, String address) {
  39.         super(name, address);
  40.         program = "";
  41.         year = 0;
  42.         fee = 0.00;
  43.     }
  44.  
  45.     @Override
  46.     public String toString() {
  47.         return "Student: " + super.toString() + ", Program: " + getProgram() + ", Year: " + getYear() + ", Fee: " + getFee();
  48.     }
  49.  
  50.     //getters and setters
  51.     public String getProgram() {
  52.         return program;
  53.     }
  54.  
  55.     public void setProgram(String program) {
  56.         this.program = program;
  57.     }
  58.  
  59.     public int getYear() {
  60.         return year;
  61.     }
  62.  
  63.     public void setYear(int year) {
  64.         this.year = year;
  65.     }
  66.  
  67.     public double getFee() {
  68.         return fee;
  69.     }
  70.  
  71.     public void setFee(double fee) {
  72.         this.fee = fee;
  73.     }
  74. }
  75.  
  76. //next class
  77.  
  78. public class Staff extends Person {
  79.  
  80.     //private instance variables
  81.     private String school;
  82.     private double pay;
  83.  
  84.     //constructor
  85.     public Staff(String name, String address) {
  86.         super(name, address);
  87.  
  88.         school = "";
  89.         pay = 0.00;
  90.     }
  91.  
  92.  
  93.     @Override
  94.     public String toString() {
  95.         return "Staff: " + super.toString() + ", School: " + getSchool() + ", Pay: " + getPay();
  96.     }
  97.  
  98.     //getters and setters
  99.     public String getSchool() {
  100.         return school;
  101.     }
  102.  
  103.     public void setSchool(String school) {
  104.         this.school = school;
  105.     }
  106.  
  107.     public double getPay() {
  108.         return pay;
  109.     }
  110.  
  111.     public void setPay(double pay) {
  112.         this.pay = pay;
  113.     }
  114. }
  115.  
  116. //test class
  117.  
  118. public class testStudentStaff {
  119.     public static void main(String[] args) {
  120.  
  121.         //to test the student class
  122.         Student s1 = new Student("Jiang Cheng", "05 Yunmeng Rd.");
  123.         s1.setProgram("Information Technology");
  124.         s1.setYear(2023);
  125.         s1.setFee(19000.00);
  126.  
  127.         System.out.println(s1);
  128.  
  129.         System.out.println(); //blank print statement to make the output look neater
  130.  
  131.         Student s2 = new Student("Wei Wuxian", "45 Yiling Ave.");
  132.         s2.setProgram("Theoretical Physics");
  133.         s2.setYear(2022);
  134.         s2.setFee(21000.00);
  135.  
  136.         System.out.println(s2);
  137.  
  138.         System.out.println(); //blank print statement to make the output look neater
  139.  
  140.         //to test the staff class
  141.         Staff st1 = new Staff("Lan Xichen", "111 Gusu Dr.");
  142.         st1.setSchool("Cloud Recesses University");
  143.         st1.setPay(9000.00);
  144.  
  145.         System.out.println(st1);
  146.  
  147.         System.out.println(); //blank print statement to make the output look neater
  148.  
  149.         Staff st2 = new Staff("Lan Qiren", "22 Gusu Dr.");
  150.         st2.setSchool("Cloud Recesses University");
  151.         st2.setPay(12000.00);
  152.  
  153.         System.out.println(st2);
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement