I am trying to store object pet into an array then I'll make an arraylist out of the array, is there a better way to do this or is there any way
someone can help me make this program work at all? thanks!
Write a program that creates an ArrayList of pets. An item in the list is either a Dog or a Cat, For each pet, enter its name and type('c' for
cat and 'd' for dog). Stop the input when the string stop is entered for the name. After the input is complete, output the name and type for
each pet in the list.
import java.util.*;
public class Chapter13Ex4{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Pet pets = new Pet();
int count =0;
//System.out.println("How many animals would you like to add?");
//int N = scan.nextInt();
boolean finished = false;
Pet[] PetArray = new Pet[100];
while (!finished) {
System.out.println(
"Please enter d to add a dog or c to add a cat ('Stop' to end)");
char choice = scan.next().charAt(0);
switch (choice) {
case 'd':
System.out.println("You want to add a Dog");
addDog();
break;
case 'c':
System.out.println("You want to add a Cat");
addCat();
break;
default:
finished = true;
}
count = count + 1;
}
System.out.println(PetArray[0]);
}
public static void addDog() {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter name: ");
String dName = scan.next();
System.out.println("Please enter weight of dog: ");
double weight = scan.nextDouble();
}
public static void addCat() {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter name: ");
String cName = scan.next();
System.out.println("Please enter the coat color of the cat: ");
String cColor = scan.next();
}
class Dog extends Pet {
private double weight;
public Dog(double weight) {
super();
setType("d");
setWeight(weight);
}
public Dog() {
super();
setType("d");
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
class Cat extends Pet {
private String coatColor;
public Cat() {
super();
setType("c");
}
public Cat(String cName){ //duplicate method HOW DO I CHANGE THIS?
super();
setType("c");
setName(cName);
}
public Cat(String cColor) { //duplicate method HOW DO I CHANGE THIS?
super();
setType("c");
setCoatColor(cColor);
}
public String getCoatColor() {
return coatColor;
}
public void setCoatColor(String coatColor) {
this.coatColor = coatColor;
}
}
}
__________________________________
import java.util.*;
public class Pet {
private String name;
private String type;
private Pet[] pets;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Pet[] getPets() {
return pets;
}
public void setPets(Pet[] pets) {
this.pets = pets;
}
}