Unit 9 Homework
Unit 9 Homework
public class WorldCup {
public String team1;
public String team2;
public String color;
public WorldCup (String team1, String team2, String color){
this.team1 = team1;
this.team2 = team2;
this.color = color;
}
public String getTeam1(){
return team1;
}
public String getTeam2(){
return team2;
}
public void main(String[] args) {
System.out.println("Team 1: " + this.team1);
System.out.println("Team 2: " + this.team2);
System.out.println("Team Colors: " + this.color);
}
}
public class Argentina extends WorldCup {
public Argentina(String team1, String team2, String color){
super(team1, team2, color);
}
}
public class Portugal extends WorldCup {
public Portugal(String team1, String team2, String color){
super(team1, team2, color);
}
}
Argentina arg = new Argentina("Argentina", "Null", "Blue and White");
Portugal por = new Portugal("Portugal", "Null", "Green and Red");
arg.main(null);
por.main(null);
Inheritance (To do)
- Add a getAge method in the Person super class
- Create a new subclass Student with additional members of your choice to personalize the Student class
- Create a new subclass Teacher with additional members of your choice
- Override the toString method using the @Override to print a Student and teacher object with new members
- Print the student and teacher.
import java.util.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
public class Person {
public String name;
public String birthday;
public Person (String name, String birthday){
this.name = name;
this.birthday = birthday;
}
public String getName(){
return name;
}
public int getAge(){
//LocalDate birthDay = this.birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//return Period.between(birthDay, LocalDate.now()).getYears();
return -1;
}
}
public class Student extends Person {
private int grade;
private double gpa;
public Student (String name, String birthday, int grade, double gpa) {
super(name, birthday);
this.grade = grade;
this.gpa = gpa;
}
public int getGrade(){
return grade;
}
@Override
public String toString(){
return "Student to String: " + name + ", " + birthday + ", gpa: " + gpa ;
}
}
public class Teacher extends Person {
private String subject;
public Teacher (String name, String birthday){
super(name, birthday);
}
@Override
public String toString(){
return "Teacher to String: " + name + ", " + birthday;
}
}
public class Main{
public static void main(String[] args){
Student akhil = new Student("Akhil", "11/01/2004", 12, 4.0);
System.out.println(akhil.toString());
Teacher mortensen = new Teacher("Mr. Mort", "01/01/1920");
System.out.println(mortensen.toString());
}
}
Main.main(null);