Unit 9 HW - Meena Annamalai
2D Arrays
public class worldCup {
public String teamName;
public String headCoach;
public String captain;
public worldCup(String teamName, String headCoach, String captain){
this.teamName = teamName;
this.headCoach = headCoach;
this.captain = captain;
}
public String getTeamInfo(){
String teamInfo = "Team: " + teamName + ", Head Coach: " + headCoach + ", Captain: " + captain;
return teamInfo;
}
}
public class Brazil extends worldCup {
public Brazil(String teamName, String headCoach, String captain) {
super(teamName, headCoach, captain);
}
}
public class US extends worldCup {
public US(String teamName, String headCoach, String captain) {
super(teamName, headCoach, captain);
}
}
public class France extends worldCup {
public France(String teamName, String headCoach, String captain) {
super(teamName, headCoach, captain);
}
}
public class Argentina extends worldCup {
public Argentina(String teamName, String headCoach, String captain) {
super(teamName, headCoach, captain);
}
}
public class Portugal extends worldCup {
public Portugal(String teamName, String headCoach, String captain) {
super(teamName, headCoach, captain);
}
}
public class Tester{
public static void main(String [] args){
Brazil brazilTeam = new Brazil("Brazil", "Tite", "Vacant");
System.out.println(brazilTeam.getTeamInfo());
US usTeam = new US("US", "Gregg Berhalter", "Tyler Adams");
System.out.println(usTeam.getTeamInfo());
France franceTeam = new France("France", "Didier Deschamps", "Hugo Lloris");
System.out.println(franceTeam.getTeamInfo());
Argentina argentinaTeam = new Argentina("Argentina", "Lionel Scaloni ", "Lionel Messi");
System.out.println(argentinaTeam.getTeamInfo());
Portugal portugalTeam = new Portugal("Portugal", "Fernando Santos", "Cristiano Ronaldo");
System.out.println(portugalTeam.getTeamInfo());
}
}
Tester.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 additiona 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.time.LocalDate;
import java.time.Period;
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 dob = LocalDate.parse(birthday);
LocalDate today = LocalDate.now();
return Period.between(dob, today).getYears();
}
}
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 "name: " + name + " bday: " + birthday + " grade: " + grade + " gpa: " + gpa;
}
}
public class Teacher extends Person {
private String subject;
public Teacher (String name, String birthday, String subject) {
super(name, birthday);
this.subject = subject;
}
@Override
public String toString(){
return "name: " + name + " bday: " + birthday + " subject: " + subject;
}
}
public class Tester{
public static void main(String [] args){
Person bob = new Person("bob", "2000-01-01");
System.out.println(bob.name + "'s age: " +bob.getAge());
Student joe = new Student("joe", "2003-01-01", 5, 3.8);
System.out.println(joe.toString());
Teacher bobby = new Teacher("mr. bobby", "1983-01-01", "English");
System.out.println(bobby.toString());
}
}
Tester.main(null);