Unit 6 Assignment Grading Information: This Program is due on Date Specified . C
ID: 3778035 • Letter: U
Question
Unit 6 Assignment
Grading Information: This Program is due on Date Specified.
Comments are REQUIRED; flow charts and pseudocode are NOT REQUIRED.
Directions Points
The file must be called <LastInitialFirstInitialUnit6.java> (driver)
<LastInitialFirstInitialPets.java> (handles variables and methods for the Pet Class)
Proper coding conventions required the first letter of the class start with a capital
letter and the first letter of each additional word start with a capital letter.
Only submit the .java files needed to make the program run. Do not submit the
.class files or any other files.
5%
Style Components
Include properly formatted prologue, comments, indenting, and other style elements as
shown in Chapter 2 starting page 64 and Appendix 5 page 881-892.
5%
Topics covered in chapter
Topics with * are covered in this assignment. Ensure you use every item listed below with
an * in your completed assignment.
*Object Oriented Programming
*Driver Class
Calling Objects, this reference
*Instance Variables
Tracing
UML
*Local Variables
*The return statement
*Argument Passing
*Specialized Methods – *Accessor, *Mutator, and Boolean Methods
Problem Solving with Simulation (optional)
Basic Guidelines
You will be making a very basic Pet Tracker. You will be tracking type, name, age,
etc and outputting some information at the end.
LiFiUnit6.java (driver)
Provide a driver class that has the following items:
• Creates 3 instances of your LiFiUnit6Pets class. You may use a loop but it is
not required.
60%
• Gets input for first type, name as String. Notice in the example how the
prompts change once the name has been entered.
• Get input for age as integer.
• Get input for weight as double.
• Get input for isMale as Boolean.
• For the 1st pet, use the default constructor and use proper mutator methods to
set all variables.
• For the 2nd pet, use a single parameter constructor that accepts type as an
argument and use proper mutator methods for all other values.
• For the 3rd pet, use a constructor that accepts all values as arguments.
• Create a class method (in your driver), that counts the number of dogs and the
number of cats. It is recommended you create one method called countPets
that accepts the pet instance, type, and 0 as a constant for number. This
method should return the number of the pet set to it. This method is required
but how it works is up to you.
• After counting the pets, if you have 1 or more dogs and 1 or more cats, output
“I sure hope the cats and dogs get along”.
• Call displayPet method from your Pets class.
LiFiPets.java
Provide a pets class that has accessor/mutator methods for all variables pertaining to
the pets above.
• Create a method called displayPet and output the values for all pets, see
sample below. Ensure to output “Male” or “Female” based on isMale. Cal lthis
method for each pet.
Sample output is provided below. Be sure to mimic it exactly except for values
entered.
30%
NOTE: Complete your activity and upload to assignment.
Total Percentage 100%
Sample
Notice how once the name is input, the name is used in the
following prompts.
The below example has 2 dogs and 1 cat so notice the output
about hoping the pets get along.
Welcome to the Java Pet Tracker.
Please enter the type of Pet #1 (Dog or Cat): Dog
Please enter the name of Pet #1: Max
Please enter the age of Max: 3
Please enter the weight of Max in pounds (example 5.2): 85.2
Is Max Male (true/false): true
Please enter the type of Pet #2 (Dog or Cat): Dog
Please enter the name of Pet #2: Buddy
Please enter the age of Buddy: 4
Please enter the weight of Buddy in pounds (example 5.2): 75.6
Is Buddy Male (true/false): true
Please enter the type of Pet #3 (Dog or Cat): Cat
Please enter the name of Pet #3: CiCi
Please enter the age of CiCi: 10
Please enter the weight of CiCi in pounds (example 5.2): 10.6
Is CiCi Male (true/false): false
Calculating Pet Population and Stuff
I sure hope them cats and dogs get along!
Pet Registration Information:
Type: Dog
Name: Max
Age: 3
Weight: 85.2
Sex: Male
Type: Dog
Name: Buddy
Age: 4
Weight: 75.6
Sex: Male
Type: Cat
Name: CiCi
Age: 10
Weight: 10.6
Sex: Female
The below examples is of all Cat’s so notice no statement
about hoping they get along.
Welcome to the Java Pet Tracker.
Please enter the type of Pet #1 (Dog or Cat): Cat
Please enter the name of Pet #1: CiCi
Please enter the age of CiCi: 10
Please enter the weight of CiCi in pounds (example 5.2): 10.7
Is CiCi Male (true/false): false
Please enter the type of Pet #2 (Dog or Cat): Cat
Please enter the name of Pet #2: Zeus
Please enter the age of Zeus: 2
Please enter the weight of Zeus in pounds (example 5.2): 7.6
Is Zeus Male (true/false): true
Please enter the type of Pet #3 (Dog or Cat): Cat
Please enter the name of Pet #3: Strawberry
Please enter the age of Strawberry: 2
Please enter the weight of Strawberry in pounds (example 5.2): 11.6
Is Strawberry Male (true/false): false
Calculating Pet Population and Stuff
Pet Registration Information:
Type: Cat
Name: CiCi
Age: 10
Weight: 10.7
Sex: Female
Type: Cat
Name: Zeus
Age: 2
Weight: 7.6
Sex: Male
Type: Cat
Name: Strawberry
Age: 2
Weight: 11.6
Sex: Female
Explanation / Answer
Here is the code for 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 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);
}
} // end
And the code for PetsDriver.java is:
import java.util.*;
public class PetsDriver
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
Pets myPet1 = new Pets(); // pet 1
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.");
for(int i=0; i<3; i++){ // loop to repeat for the 3 pets
System.out.println("Please enter the type of pet "+(i+1)+" (Dog or Cat): ");
type = input.next();
if (type.equalsIgnoreCase("dog"))
{
petCount++; //if dog +1 to count
}
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("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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.