Tools: VSCode | Source Code: GitHub
The Dog
program models two dogs with ages and provides functionality to set, display, and compare their ages. The main objective is to offer methods for assigning ages to two dogs, displaying their ages based on user input, and determining if the dogs share the same age. It uses constructors to initialize the dogs and includes methods to interact with the user and perform comparisons.
The Dog
class contains two private instance variables: age1
and age2
, which store the ages of the two dogs. It provides two constructors: one to initialize the first dog’s age and another to initialize the second dog’s age. The reason for having two constructors is to offer flexibility in how the dog objects are created. The first constructor, Dog(int yas1)
, is used when only the first dog's age needs to be provided, while the second constructor, Dog(int yas2, boolean isdogsecond)
, allows the user to set the second dog's age. This design ensures that the program can handle both scenarios depending on which dog’s age is being set.
The two constructors are important for flexibility. The first constructor only requires an age for the first dog, while the second one allows the age for the second dog to be set, with the additional unused boolean parameter (isdogsecond
) potentially available for future program expansion or additional logic. By providing both constructors, the program can cater to different needs, whether it's creating a single dog or both dogs with their respective ages.
The Dog
class includes setter and getter methods for both dogs' ages. The setAge1(int dogage1)
and setAge2(int dogage2)
methods assign values to age1
and age2
, while the getAge1()
and getAge2()
methods return the values of age1
and age2
. These methods ensure encapsulation by allowing age values to be modified and retrieved in a controlled manner, providing good object-oriented design practices.
The toString()
method interacts with the user to display the age of the dog they choose. The program asks the user to input 1
or 2
to select which dog’s age they want to see. Based on the user's input, the program displays the corresponding dog's age. If the input is invalid, the method returns "Invalid dog choice." This method provides a user-friendly way to interact with the program, ensuring flexibility and responsiveness to user input.
The equalsAge()
method compares the ages of the two dogs and now returns a boolean
value instead of printing a message. If the dogs have the same age, the method returns true
; otherwise, it returns false
. This change to returning a boolean provides more utility by allowing the result of the comparison to be used in further logic or displayed by the calling code, making the method more versatile for future use cases.
The Dogtester
class tests the functionality of the Dog
class. In the main
method, two Dog
objects are created: fido
with an age of 5 and yeller
with an age of 7. The toString()
method is called on both objects to display their ages, and the equalsAge()
method is called on fido
to check if its age matches yeller
's age. Since their ages differ, the result of the equalsAge()
method will be false
, and it is printed to the console.
The change from a void
method to a boolean
return type in equalsAge()
improves the program by allowing the age comparison result to be used programmatically. Instead of simply printing a message, the method now returns a boolean, enabling more flexible handling of the comparison results in future code implementations.
The reason for having two constructors in the Dog
class is to support different scenarios: initializing one dog or both dogs. The first constructor is used when only one dog’s age is provided, and the second constructor is for initializing both dogs. By providing these constructors, the program is more adaptable to various use cases, such as when only one dog’s age needs to be set, or when both dogs' ages are known at the time of object creation.
Having two constructors allows for better control and flexibility in how dog objects are instantiated, making the program more robust and easier to expand in the future. This kind of constructor overloading is a common object-oriented programming technique that simplifies object creation and makes code more maintainable.
import java.util.Scanner;
public class Dog {
private int age1;
private int age2;
public Dog(int yas1) {
setAge1(yas1);
}
public Dog(int yas2, boolean isdogsecond) {
setAge2(yas2);
}
public void setAge1(int dogage1) {
age1 = dogage1;
}
public int getAge1() {
return age1;
}
public void setAge2(int dogage2) {
age2 = dogage2;
}
public int getAge2() {
return age2;
}
public String toString() {
Scanner whichdog = new Scanner(System.in);
System.out.println("Which dog age (1 or 2):");
int dogdog = whichdog.nextInt();
if (dogdog == 1) {
return "Dog 1's age is " + getAge1();
} else if (dogdog == 2) {
return "Dog 2's age is " + getAge2();
} else {
return "Invalid dog choice";
}
}
public boolean equalsAge() {
return age1 == age2;
}
}
public class DogTester {
public static void main(String[] args) {
Dog fido = new Dog(5);
System.out.println(fido.toString());
Dog yeller = new Dog(7, true);
System.out.println(yeller.toString());
System.out.println(fido.equalsAge());
}
}