CodeHS Java9.2.6 Students Part 1 StudentTester.java code example

Example 1: CodeHS Java9.2.6 Students Part 1 StudentTester.java

public class StudentTester
{
    public static void main(String[] args)
    {
        
        /**
         * Create a student in the class of 2020
         */
         Student bill = new Student("bill", 2020);
         
         /**
         * Create a student athlete in the class of 2022
         * that is eligible and plays soccer.
         */
         StudentAthlete robert = new StudentAthlete("robert", 2022, "soccer", true);
         
         // Print out both objects
    }
}

Example 2: CodeHS Java9.2.6 Students Part 2 Student.java

public class Student
{
    private String name;
    private int classYear;
   
   // Constructor goes here
   public Student(String name, int year){
       this.name = name;
       this.classYear = year;
   }
   
    public String getName(){
       return name;
    }
   
    public int getClassYear(){
       return classYear;
    }
   
    public String toString(){
        return name + ", class of " + classYear;
    }
}

Example 3: CodeHS Java 9.2.6 Students Part 3 StudentAthlete.java

public class StudentAthlete extends Student
{
    private String sport;
    private boolean eligible;
    
   public StudentAthlete(String name, int classYear, String sport, boolean eligible){
       super(name, classYear);
       this.sport = sport;
       this.eligible = eligible;
   }
   
   
   public String getSport(){
       return sport;
   }
   
   public boolean isEligible(){
       return eligible;
   }
    
    @Override
    public String toString(){
        return super.getName() + ", class of " + super.getClassYear() +
            ", plays " + sport;
    }
}

Tags:

Java Example