Question
Given the driver class PetsDemo.java and the (mostly) complete abstract Pet class, create a Cat class and a Dog class to extend Pet, and provide
Given the driver class PetsDemo.java and the (mostly) complete abstract Pet class, create a Cat class and a Dog class to extend Pet, and provide concrete methods for the abstract getAgeInHumanYears(). The Pet class itself also has a getAge() method that needs to be finished before it will work correctly.
Notice that we're making use of the java.time package to make objects like dob (date of birth) to represent a date, or age to model a period of time. Make sure to use the API documentation so that you understand those data types.
LocalDateDemo code:
package lab3;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.util.Scanner;
public class Localdatedemo {
// check out the Java API to see more about how to use the Period and LocalDate classes
// https://docs.oracle.com/en/java/javase/11/docs/api/index.html
public static void main(String[] args) {
LocalDate today = LocalDate.now(); // Date today
LocalDate bday = LocalDate.of(1972, Month.MAY, 3); // Birth date (using enum type for month)
Period difference = Period.between(bday, today); // Amount of time
System.out.println(difference.getDays());
System.out.println(difference.getMonths());
System.out.println(difference.getYears());
System.out.println(bday.toString());
Scanner input = new Scanner(System.in);
System.out.print("Month: ");
int month = input.nextInt();
System.out.print("Day: ");
int day = input.nextInt();
System.out.print("Year: ");
int year = input.nextInt();
LocalDate bornOn = LocalDate.of(year, month, day);
Period age = Period.between(bornOn, today);
System.out.println("Age is " + age.getYears() + " years.");
input.close();
}
}
Pet class code:
package lab3;
import java.time.LocalDate;
import java.time.Period;
public abstract class Pet {
// Fields
private String name;
private LocalDate dob;
// Constructor
public Pet (String n, int y, int m, int d) {
this.name = n;
this.dob = LocalDate.of(y, m, d);
}
// Methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setDob(int y, int m, int d) {
this.dob = LocalDate.of(y, m, d);
}
public int getAge() {
//TODO: Return the pet's age in number of actual years
return 0;
}
abstract public int getAgeInHumanYears();
// TODO: provide concrete methods by creating Cat and Dog classes to extend Pet.
// https://www.akc.org/expert-advice/health/how-to-calculate-dog-years-to-human-years/
// https://www.purina.co.uk/cats/key-life-stages/ageing/cats-age-in-human-years
}
Pet Demo code:
package lab3;
public class PetDemo {
public static void main(String[] args) {
// You will fix this by creating Cat and Dog classes.
// No need to modify this file.
Pet pet1 = new Dog("Chuki", 2016, 03, 03);
Pet pet2 = new Cat("Aspen", 2009, 11, 20);
System.out.println(pet1.getName() + " is " + pet1.getAgeInHumanYears() + " in human years.");
System.out.println(pet2.getName() + " is " + pet2.getAgeInHumanYears() + " in human years.");
pet2.setName("Old Aspy");
System.out.println(pet2.getName() + " is actually " + pet2.getAge() + " years old.");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started