Unit 9 HW/Vocab Integration
Inheritance HW
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
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() {
if (this.birthday != null){
}
return -1;
}
@Override
public String toString() {
return ("Person: " + " name: " + name + " birthday: " + birthday);
}
public class Student extends Person {
private int grade;
private double gpa;
private String job;
public Student (String name, String birthday, int grade, double gpa, String job) {
super(name, birthday);
this.grade = grade;
this.gpa = gpa;
this.job = job;
}
public double getGPA(){
return gpa;
}
public String getJob(){
return job;
}
public int getGrade(){
return grade;
}
@Override
public String toString() {
return ("Student -- " + "name: " + name + " birthday: " + birthday + " grade: " + grade + " gpa: " + gpa + " job: " + job);
}
}}
public class Teacher extends Person {
private int yrsExperience;
private String subject;
public Teacher (String name, String birthday, int yrsExperience, String subject) {
super(name, birthday);
this.yrsExperience = yrsExperience;
this.subject = subject;
}
public int getYrsExp(){
return yrsExperience;
}
public String getSubject(){
return subject;
}
@Override
public String toString() {
return ("Teacher -- " + "name: " + name + " birthday: " + birthday + " Years of Experience: " + yrsExperience + " Subject: " + subject);
}
}
Person Saumya = new Person("Saumya", "2006");
System.out.println(Saumya.toString());
Person Andrea = new Student("Andrea", "2006", 11, 4.0, "Math Tutor");
System.out.println(Andrea.toString());
Person Tom = new Teacher("Tom", "1975", 16, "Earth Science");
System.out.println(Tom.toString());
public class WorldCup {
public String game;
public String team1;
public String team2;
public int score;
public WorldCup (String game, String team1, String team2, int score){
this.game = game;
this.team1=team1;
this.team2=team2;
this.score=score;
}
public String getTeam1(){
return team1;
}
public String getTeam2(){
return team2;
}
public void score() {
this.score += 1;
}
public void stats(){
System.out.println("");
}
public void printInfo() {
System.out.println("Team game: " + this.game);
System.out.println("Team 1: " + this.team1);
System.out.println("Team 2: " + this.team2);
System.out.println("Score: " + this.score);
stats();
}
}
public class Portugal extends WorldCup {
public Portugal(String game, String team1, String team2, int score){
super(game, team1, team2, score);
}
public void score() {
this.score += 1;
}
public void stats(){
System.out.println("Portugal pass accuracy: 71%");
}
}
public class Morocco extends WorldCup {
public Morocco(String game, String team1, String team2, int score){
super(game, team1, team2, score);
}
public void score() {
this.score += 1;
}
public void stats(){
System.out.println("Morocco pass accuracy: 85%");
}
}
WorldCup WorldCup = new WorldCup("Game 1","Portugal", "Morocco", 1);
WorldCup Portugal = new Portugal("Game 1", "Portugal", "Morocco", 0);
WorldCup Morocco = new Morocco("Game 1", "Portugal", "Morocco", 1);
WorldCup.printInfo();
Portugal.printInfo();
Morocco.printInfo();
The extends keyword to create a subclass that inherits from a superclass
The super keyword to call a superclass constructor or method from a subclass
Overriding a method, which involves defining a method in a subclass that has the same signature (name and parameters) as a method in the superclass
Polymorphism, which allows an object of a subclass to be treated as an object of the superclass
Casting, wrapper classes, concatenation, the Math class, compound boolean expressions, truth tables, De Morgan's law, comparing numbers, comparing strings, comparing objects, loops, creating a class, constructor, accessor and mutator methods, static variables and methods, the this keyword, the main method, abstract classes and methods, and standard methods such as toString(), equals(), and hashCode() are not directly related to inheritance in Java.
Big O notation is a measure of the efficiency of an algorithm, not a feature of the Java language.
public class WorldCup {
public static void main(String[] args) {
Team argentina = new Team("Argentina", "Jorge Sampaoli");
Team brazil = new Team("Brazil", "Tite");
Team germany = new Team("Germany", "Joachim Low");
argentina.displayTeamInfo();
brazil.displayTeamInfo();
germany.displayTeamInfo();
argentina.playGame();
brazil.playGame();
germany.playGame();
argentina.displayTeamInfo();
brazil.displayTeamInfo();
germany.displayTeamInfo();
}
}
class Team {
protected String name;
protected String coach;
protected int wins;
public Team(String name, String coach) {
this.name = name;
this.coach = coach;
this.wins = 0;
}
public void displayTeamInfo() {
System.out.println("Team: " + this.name);
System.out.println("Coach: " + this.coach);
System.out.println("Wins: " + this.wins);
}
public void playGame() {
// Simulate a game and increase the number of wins by 1 if the team wins
this.wins++;
}
}
In this program, the Team class represents a team participating in the World Cup. It has three instance variables: name, coach, and wins, which hold the name of the team, the name of the coach, and the number of games the team has won, respectively. The Team class also has a constructor that initializes these instance variables and a displayTeamInfo() method that prints out the team's information.
To create a subclass that represents a specific team, we can use the extends keyword. For example, the following Argentina class extends the Team class and adds a specific logo for the team:
class Argentina extends Team {
private String logo = "https://upload.wikimedia.org/wikipedia/en/thumb/1/1a/Argentina_national_football_team_logo.svg/1200px-Argentina_national_football_team_logo.svg.png";
public Argentina(String name, String coach) {
super(name, coach);
}
public void displayTeamInfo() {
super.displayTeamInfo();
System.out.println("Logo: " + this.logo);
}
}
This Argentina class uses the super keyword to call the Team class' constructor and passes it the name and coach arguments. It also overrides the displayTeamInfo() method from the Team class and adds the team's logo to the information displayed.
To use the Argentina class in the main method of the WorldCup class, we can create an Argentina object and call its methods just like we would call the methods of any other Team object. For example, we can create an Argentina object like this:
Team argentina = new Argentina("Argentina", "Jorge Sampaoli");
And then we can call its methods like this:
Team argentina = new Argentina("Argentina", "Jorge Sampaoli");
argentina.displayTeamInfo();
argentina.playGame();
Because the Argentina class extends the Team class, an Argentina object is considered to be a Team object as well. This allows us to use polymorphism, where we can treat an object of a subclass as an object of the superclass. For example, we can create a Team object that refers to an Argentina object like this:
Team team = new Argentina("Argentina", "Jorge Sampaoli");
Then, when we call the displayTeamInfo() method on the team object, it will call the overridden displayTeamInfo() method in the Argentina class, which will display the team's information as well as the team's logo.