Hi i\'m currently taking an introduction to Java class. If anyone could help exp
ID: 3603514 • Letter: H
Question
Hi i'm currently taking an introduction to Java class. If anyone could help explain to me what is wrong with my program in a easy way not to dificult. I'm having hard time calcualting the average for my five pets...Thanks !
package pp1_c00305169;
import java.util.Scanner;
public class Pp1_C00305169 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
//Declare array of Objects
Pets[] choices = new Pets[2];
//populate array
for (int i = 0; i < choices.length; i++) {
choices[i] = new Pets();
}
String name, type, breed, owner;
double age = 0, weight = 0;
for (Pets choice : choices) {
System.out.print("Enter Pet name: ");
name = reader.nextLine();
choice.setName(name);
System.out.print("Enter Pet Type (dog or cat): ");
type = reader.nextLine();
choice.setType(type);
System.out.print("Enter Pet Breed: ");
breed = reader.nextLine();
choice.setBreed(breed);
System.out.print("Enter Pet Age: ");
age = reader.nextDouble();
choice.setAge(age);
System.out.print("Enter Pet Weight: ");
weight = reader.nextDouble();
choice.setWeight(weight);
reader.nextLine();
System.out.print("Enter Pet Owner: ");
owner = reader.nextLine();
choice.setOwner(owner);
System.out.println();
}
for (Pets choice : choices) {
System.out.println(" " + choice.getName() + " type: " + choice.getType() + " breed: "
+ choice.getBreed() + " age: " + choice.getAge() + " weight: " + choice.getWeight() + " owner: "
+ choice.getOwner() + " ");
}
//call function
getWeight(choices);
averages(choices);
//need to demonstrate string
}
public static void getWeight(Pets[] choices){
Scanner reader = new Scanner(System.in);
for (Pets choice : choices) {
System.out.println("Does " + choice.getName()
+ " weight " + choice.getWeight() + "lbs.? (y/n): ");
String ba = reader.nextLine();
choice.getName();
choice.getWeight();
if (ba.equals("y")) {
System.out.println();
} else if (ba.equals("n")) {
System.out.println("Enter correct pet weight: ");
choice.setWeight(Double.parseDouble(reader.nextLine()));
}
}
}
public static void averages(Pets[] choices){
//get Average weight and age
double avg_weight = 1;
double avg_age = 1;
double weight;
double age;
//Calc Average ages and weights
for (Pets choice : choices)
{
avg_weight = choice.getWeight();
avg_age = choice.getAge();
}
// avg_weight /= choices.length;
// avg_age /= choices.length;
System.out.println("The average dog is " + (avg_age / (choices.length)) + " years old");
System.out.println("The average cat is " + (avg_age / (choices.length)) + " years old");
System.out.println("The average dog weights " + (avg_weight / (choices.length)) + " lbs.");
System.out.println("The average cat weights " + (avg_weight / (choices.length)) + " lbs.");
}
}
----------------------------------------------------------------------------------------------------------------
package pp1_c00305169;
public class Pets {
//Default Constructor
private String name = "na";
private String type = "ba";
private String breed = "ca";
private String owner = "da";
private double age; //in years
private double weight; //in pounds
public Pets() {
}
public Pets(String name, String type, String breed, double age, double weight, String owner) {
this.name = name;
this.type = type;
this.breed = breed;
this.age = age;
this.weight = weight;
this.owner = owner;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getBreed() {
return breed;
}
public double getAge() {
return age;
}
public double getWeight() {
return weight;
}
public String getOwner() {
return owner;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void setAge(Double age) {
this.age = age;
checkWeight(age);
}
public void setWeight(Double weight) {
this.weight = weight;
checkWeight(weight);
}
public void setOwner(String owner) {
this.owner = owner;
}
public void checkWeight(double checkWeight) {
if (checkWeight > 0) {
weight = checkWeight;
}
}
@Override
public String toString() {
return "The avg dog is " + this.age + "years old" + " " +
"The avg cat is " + this.age + "years old" + " " +
"The avg do weights " + this.weight + "lbs." + " " +
"The avg cat weights " + this.weight + "lbs.";
}
}
Explanation / Answer
Pet.java
public class Pet {
//Declaring instance variables
private String name = "na";
private String type = "ba";
private String breed = "ca";
private String owner = "da";
private double age; // in years
private double weight; // in pounds
// Default Constructor
public Pet() {}
//Parameterized constructor
public Pet(String name, String type, String breed, double age,
double weight, String owner) {
this.name = name;
this.type = type;
this.breed = breed;
this.age = age;
this.weight = weight;
this.owner = owner;
}
//getters and setters
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getBreed() {
return breed;
}
public double getAge() {
return age;
}
public double getWeight() {
return weight;
}
public String getOwner() {
return owner;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void setAge(Double age) {
if (age < 0)
this.age = 0;
else
this.age = age;
}
public void setWeight(Double weight) {
if (weight < 0)
this.weight = 0;
else
this.weight = weight;
}
public void setOwner(String owner) {
this.owner = owner;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Name=" + name + ", Type=" + type + ", Breed=" + breed + ", Owner=" + owner + ", Age=" + age + ", Weight=" + weight;
}
}
__________________
Pp1_C00305169.java
import java.util.Scanner;
public class Pp1_C00305169 {
static Pet pets[]=null;
public static void main(String[] args) {
//Declaring variables
String name ,type ,breed ,owner;
double age, weight;
pets=new Pet[5];
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Geetting each pet information and storing in Pet class array
for(int i=0;i<pets.length;i++)
{
System.out.println("__Pet#"+(i+1)+"__");
System.out.print("Enter Name:");
name=sc.nextLine();
System.out.print("Enter Type:");
type=sc.nextLine();
System.out.print("Enter Breed:");
breed=sc.nextLine();
System.out.print("Enter Owner:");
owner=sc.nextLine();
System.out.print("Enter Age:");
age=sc.nextDouble();
System.out.print("Enter Weight:");
weight=sc.nextDouble();
sc.nextLine();
//Creating an Pet class Obejct by passing user entered inputs
pets[i]=new Pet(name, type, breed, age, weight, owner);
}
System.out.println("___Displaying Pets Info___");
//Displaying each Pet info
for(int i=0;i<pets.length;i++)
{
System.out.println(pets[i]);
}
//Calling the Methods
double avgAge=calAverageAge();
double avgWeight=calAverageWeight();
//Displaying the average weight and Age
System.out.println("The Average Age of "+pets.length+" is :"+avgAge);
System.out.println("The Average Weight of "+pets.length+" is :"+avgWeight);
}
//This method will calculate the Average Weight
private static double calAverageWeight() {
double tot=0.0,avg=0.0;
for(int i=0;i<pets.length;i++)
{
tot+=pets[i].getAge();
}
return tot/pets.length;
}
//This method will calculate the Average Age
private static double calAverageAge() {
double tot=0.0,avg=0.0;
for(int i=0;i<pets.length;i++)
{
tot+=pets[i].getWeight();
}
return tot/pets.length;
}
}
____________________
Output:
__Pet#1__
Enter Name:Max
Enter Type:xxxx
Enter Breed:yyyy
Enter Owner:John
Enter Age:3
Enter Weight:8
__Pet#2__
Enter Name:Mikcy
Enter Type:xzx
Enter Breed:xsx
Enter Owner:Sachin
Enter Age:4
Enter Weight:12
__Pet#3__
Enter Name:Tommy
Enter Type:xxzx
Enter Breed:xyzx
Enter Owner:Rick
Enter Age:2
Enter Weight:6
__Pet#4__
Enter Name:Honey
Enter Type:xsxs
Enter Breed:xzxz
Enter Owner:Rob
Enter Age:5
Enter Weight:6
__Pet#5__
Enter Name:Leo
Enter Type:CSXX
Enter Breed:DCDD
Enter Owner:Rahul
Enter Age:5
Enter Weight:15
___Displaying Pets Info___
Name=Max, Type=xxxx, Breed=yyyy, Owner=John, Age=3.0, Weight=8.0
Name=Mikcy, Type=xzx, Breed=xsx, Owner=Sachin, Age=4.0, Weight=12.0
Name=Tommy, Type=xxzx, Breed=xyzx, Owner=Rick, Age=2.0, Weight=6.0
Name=Honey, Type=xsxs, Breed=xzxz, Owner=Rob, Age=5.0, Weight=6.0
Name=Leo, Type=CSXX, Breed=DCDD, Owner=Rahul, Age=5.0, Weight=15.0
The Average Age of 5 is :9.4
The Average Weight of 5 is :3.8
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.