//Help with Java OOP// So for my assignment we are making a sort of pet tracker
ID: 3774776 • Letter: #
Question
//Help with Java OOP//
So for my assignment we are making a sort of pet tracker and it's the first assignment dealing with this stuff. I kind of understand the concept but not entirely and could use some help. I've run into some errors when trying to compile it. The point of this tracker is to gather the type, age, weight, name, gender and organize it all into a neat display.
Here is my code for the Driver
import java.util.*;
public class _____
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Pet myPet1 = new Pet(); // pet 1
Pet myPet2 = new Pet(); // pet 2
Pet myPet3 = new Pet(); // pet 3
String name; // name of pet
String type; // dog or cat
int age; // age
int petCount = 0; // petcount, dog adds 1 to count, cat doesnt
double weight; // weight in pounds
boolean isMale; // is pet male? true or false
System.out.println("Welcome to the Java Pet Tracker.");
System.out.println("Please enter the type of pet 1 (Dog or Cat): ");
type = input.next();
if (type.equalsIgnoreCase("dog"))
{
petCount++;
}
System.out.println("Please enter the pets name.");
name = input.next();
System.out.println("Please enter the age of pet. ");
age = input.nextInt();
System.out.println("Please enter the weight of pet in pounds (example 5.2): ");
weight = input.nextDouble();
System.out.println("Is pet Male? True or False");
isMale = input.nextBoolean();
myPet1.set(name, age, weight);
myPet1.setType(type);
System.out.println(myPet1.getName());
System.out.println(myPet1.getType());
System.out.println("Please enter the type of pet 2 (Dog or Cat): ");
type = input.next();
if (type.equalsIgnoreCase("dog"))
{
petCount++;
}
System.out.println("Please enter the pets name.");
name = input.next();
System.out.println("Please enter the age of pet. ");
age = input.nextInt();
System.out.println("Please enter the weight of pet in pounds (example 5.2): ");
weight = input.nextDouble();
System.out.println("Is pet Male? True or False");
isMale = input.nextBoolean();
myPet2.set(name, age, weight);
myPet2.setType(type);
System.out.println(myPet2.getName());
System.out.println(myPet2.getType());
System.out.println("Please enter the type of pet 3 (Dog or Cat): ");
type = input.next();
if (type.equalsIgnoreCase("dog"))
{
petCount++;
}
System.out.println("Please enter the pets name.");
name = input.next();
System.out.println("Please enter the age of pet. ");
age = input.nextInt();
System.out.println("Please enter the weight of pet in pounds (example 5.2): ");
weight = input.nextDouble();
System.out.println("Is pet Male? True or False");
isMale = input.nextBoolean();
myPet3.set(name, age, weight);
myPet3.setType(type);
System.out.println(myPet3.getName());
System.out.println(myPet3.getType());
System.out.println("Calculating Pet Population and Stuff");
if ( petCount != 3 || petCount != 0) // if it isn't 3 dogs or 3 cats it knows it is mixed
{
System.out.println("I sure hope them cats and dogs get along!");
}
} // end method
} // end class
Here is the java file that goes with it
public class Pets
{
private String name;
private String type;
boolean isMale;
private int age;
private double weight;
}
public void setName(String name)
{
this.name = name;
}
{
public void setType(String newType)
{
type = newType;
}
public String getType()
{
return type;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public double getWeight()
{
return weight;
}
public void setWeight(double weight)
{
this.weight = weight;
}
public boolean isMale()
{
return isMale;
}
public void setMale(boolean isMale)
{
this.isMale = isMale;
}
public void displayPet()
{
String gender = "";
if (isMale)
{
gender = "Male"
}
else
{
gender = "Female"
}
}
System.out.printf("Type: %s Name: %s Age: %d Weight: %.1f Sex: %s",getType(),getName(),getAge(),getWeight(),gender);
}
When I try to compile the driver it tells my that my 'Pets' class cannot find symbol.
I am somewhat stumped and could use some input and see what changes I need to make.
Explanation / Answer
Pets.java
public class Pets
{
private String name;
private String type;
boolean isMale;
private int age;
private double weight;
String gender;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setType(String newType)
{
type = newType;
}
public String getType()
{
return type;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public double getWeight()
{
return weight;
}
public void setWeight(double weight)
{
this.weight = weight;
}
public boolean isMale()
{
return isMale;
}
public void setMale(boolean isMale)
{
this.isMale = isMale;
}
public String displayPet()
{
String gender = "";
if (isMale)
{
gender = "Male";
}
else
{
gender = "Female";
}
return gender;
}
@Override
public String toString() {
return "Pets# Name=" + name + " Type=" + type + " Age=" + age +" Sex ="+displayPet()+" Weight=" + weight;
}
}
_______________________
Driver.java
import java.util.*;
public class Driver
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Pets myPet1 = new Pets(); // pet 1
Pets myPet2 = new Pets(); // pet 2
Pets myPet3 = new Pets(); // pet 3
String name; // name of pet
String type; // dog or cat
int age; // age
int petCount = 0; // petcount, dog adds 1 to count, cat doesnt
double weight; // weight in pounds
boolean isMale; // is pet male? true or false
System.out.println("Welcome to the Java Pet Tracker.");
System.out.println("Please enter the type of pet 1 (Dog or Cat): ");
type = input.next();
if (type.equalsIgnoreCase("dog"))
{
petCount++;
}
System.out.println("Please enter the pets name.");
name = input.next();
System.out.println("Please enter the age of pet. ");
age = input.nextInt();
System.out.println("Please enter the weight of pet in pounds (example 5.2): ");
weight = input.nextDouble();
System.out.println("Is pet Male? True or False");
isMale = input.nextBoolean();
myPet1.setName(name);
myPet1.setAge(age);
myPet1.setWeight(weight);
myPet1.setType(type);
System.out.println(myPet1.toString());
System.out.println(myPet1.toString());
System.out.println("Please enter the type of pet 2 (Dog or Cat): ");
type = input.next();
if (type.equalsIgnoreCase("dog"))
{
petCount++;
}
System.out.println("Please enter the pets name.");
name = input.next();
System.out.println("Please enter the age of pet. ");
age = input.nextInt();
System.out.println("Please enter the weight of pet in pounds (example 5.2): ");
weight = input.nextDouble();
System.out.println("Is pet Male? True or False");
isMale = input.nextBoolean();
myPet2.setName(name);
myPet2.setAge(age);
myPet2.setWeight(weight);
myPet2.setType(type);
System.out.println(myPet2.toString());
System.out.println("Please enter the type of pet 3 (Dog or Cat): ");
type = input.next();
if (type.equalsIgnoreCase("dog"))
{
petCount++;
}
System.out.println("Please enter the pets name.");
name = input.next();
System.out.println("Please enter the age of pet. ");
age = input.nextInt();
System.out.println("Please enter the weight of pet in pounds (example 5.2): ");
weight = input.nextDouble();
System.out.println("Is pet Male? True or False");
isMale = input.nextBoolean();
myPet3.setName(name);
myPet3.setAge(age);
myPet3.setWeight(weight);
myPet3.setType(type);
System.out.println(myPet3.toString());
System.out.println("Calculating Pet Population and Stuff");
if ( petCount != 3 || petCount != 0) // if it isn't 3 dogs or 3 cats it knows it is mixed
{
System.out.println("I sure hope them cats and dogs get along!");
}
} // end method
} // end class
_______________________
Output:
Welcome to the Java Pet Tracker.
Please enter the type of pet 1 (Dog or Cat):
Dog
Please enter the pets name.
Max
Please enter the age of pet.
2
Please enter the weight of pet in pounds (example 5.2):
8.5
Is pet Male? True or False
true
Pets#
Name=Max
Type=Dog
Age=2
Sex =Female
Weight=8.5
Please enter the type of pet 2 (Dog or Cat):
Cat
Please enter the pets name.
Kitty
Please enter the age of pet.
2
Please enter the weight of pet in pounds (example 5.2):
3.3
Is pet Male? True or False
False
Pets#
Name=Kitty
Type=Cat
Age=2
Sex =Female
Weight=3.3
Please enter the type of pet 3 (Dog or Cat):
Dog
Please enter the pets name.
Honey
Please enter the age of pet.
1
Please enter the weight of pet in pounds (example 5.2):
2.3
Is pet Male? True or False
True
Pets#
Name=Honey
Type=Dog
Age=1
Sex =Female
Weight=2.3
Calculating Pet Population and Stuff
I sure hope them cats and dogs get along!
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.