/Help with Java OOP// So for my assignment we are making a sort of pet tracker a
ID: 3775658 • 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. Example:
Name:Kitty
Type:Dog
Age: 3
Weight: 6.3
Gender: Male
For all 3 pets.
Pet File
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 void displayPet()
{
String gender = "";
if (isMale)
{
gender = "Male";
}
else
{
gender = "Female";
}
}
System.out.printf(
"Type = %s, Name = %s, Age = %d, Weight = %.1f, Sex = %s ", this.type, this.name, this.age, this.weight, this.isMale);
}
Driver file (I tired to set up a 'For' loop but I'm not this is my first time working with this so I was somewhat stumped.)
import java.util.*;
public class Driver
{
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.setName(name);
myPet1.setAge(age);
myPet1.setWeight(weight);
myPet1.setType(type);
System.out.println(myPet1.getName());
System.out.println(myPet1.getAge());
System.out.println(myPet1.getWeight());
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.setName(name);
myPet2.setAge(age);
myPet2.setWeight(weight);
myPet2.setType(type);
System.out.println(myPet2.getName());
System.out.println(myPet2.getAge());
System.out.println(myPet2.getWeight());
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.setName(name);
myPet3.setAge(age);
myPet3.setWeight(weight);
myPet3.setType(type);
System.out.println(myPet3.getName());
System.out.println(myPet3.getAge());
System.out.println(myPet3.getWeight());
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
____________________________________________________________
When I try to compile it, I get this error on the printf line
error: <identifier> expected
error: ')' expected
error: as of release 8, 'this' is allowed as the parameter name for the receiver type only, which has to be the first parameter
error: ';' expected
_________________________________________________________________
Explanation / Answer
import java.util.*;
import java.util.List;
class Pet
{
String name;
String type;
boolean isMale;
int age;
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 void displayPet()
{
String gender = "";
if (isMale)
{
gender = "Male";
}
else
{
gender = "Female";
}
}
public void PrintValues(){
// System.out.printf("Type = %s, Name = %s, Age = %d, Weight = %.1f, Sex = %s ", this.type, this.name, this.age, this.weight, this.isMale);
System.out.println("Type = ");
}
}
public class Driver
{
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.setName(name);
myPet1.setAge(age);
myPet1.setWeight(weight);
myPet1.setType(type);
System.out.println(myPet1.getName());
System.out.println(myPet1.getAge());
System.out.println(myPet1.getWeight());
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.setName(name);
myPet2.setAge(age);
myPet2.setWeight(weight);
myPet2.setType(type);
System.out.println(myPet2.getName());
System.out.println(myPet2.getAge());
System.out.println(myPet2.getWeight());
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.setName(name);
myPet3.setAge(age);
myPet3.setWeight(weight);
myPet3.setType(type);
System.out.println(myPet3.getName());
System.out.println(myPet3.getAge());
System.out.println(myPet3.getWeight());
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
If you want you can add for loop also
replace those 3 functions instead of below code
for(int i=0; i<3; i++){
System.out.println("Please enter the type of pet "+(i+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.getName());
System.out.println(myPet1.getAge());
System.out.println(myPet1.getWeight());
System.out.println(myPet1.getType());
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.