This is the class I have to use: /** Class for basic pet data: name, age, and we
ID: 3646443 • Letter: T
Question
This is the class I have to use:/**
Class for basic pet data: name, age, and weight.
*/
public class Pet
{
private String name;
private int age; //in years
private double weight; //in pounds
public Pet () // default constructor
{
name = "No name yet.";
age = 0;
weight = 0;
}
public Pet (String initialName, int initialAge,
double initialWeight)
{
name = initialName;
if ((initialAge < 0) || (initialWeight < 0))
{
System.out.println ("Error: Negative age or weight.");
System.exit (0);
}
else
{
age = initialAge;
weight = initialWeight;
}
}
public void setPet (String newName, int newAge,
double newWeight)
{
name = newName;
if ((newAge < 0) || (newWeight < 0))
{
System.out.println ("Error: Negative age or weight.");
System.exit (0);
}
else
{
age = newAge;
weight = newWeight;
}
}
public Pet (String initialName)
{
name = initialName;
age = 0;
weight = 0;
}
public void setName (String newName)
{
name = newName; //age and weight are unchanged.
}
public Pet (int initialAge)
{
name = "No name yet.";
weight = 0;
if (initialAge < 0)
{
System.out.println ("Error: Negative age.");
System.exit (0);
}
else
age = initialAge;
}
public void setAge (int newAge)
{
if (newAge < 0)
{
System.out.println ("Error: Negative age.");
System.exit (0);
}
else
age = newAge;
//name and weight are unchanged.
}
public Pet (double initialWeight)
{
name = "No name yet";
age = 0;
if (initialWeight < 0)
{
System.out.println ("Error: Negative weight.");
System.exit (0);
}
else
weight = initialWeight;
}
public void setWeight (double newWeight)
{
if (newWeight < 0)
{
System.out.println ("Error: Negative weight.");
System.exit (0);
}
else
weight = newWeight; //name and age are unchanged.
}
public String getName ()
{
return name;
}
public int getAge ()
{
return age;
}
public double getWeight ()
{
return weight;
}
public void writeOutput ()
{
System.out.println ("Name: " + name);
System.out.println ("Age: " + age + " years");
System.out.println ("Weight: " + weight + " pounds");
}
}
And I have to write a program that reads the name, age, and weight of 5 pets and gives me back the max age, min age, max weight, min weight and average of both.
PLEASE PROVIDE FULL PROGRAM IN JAVA LANGUAGE FOR 5 STARS RATING
Explanation / Answer
please rate - thanks
without arrays
import java.util.*;
public class PetTester {
public static void main (String[] args)
{Scanner in=new Scanner(System.in);
Pet p1=new Pet();
Pet p2=new Pet();
Pet p3=new Pet();
Pet p4=new Pet();
Pet p5=new Pet();
getInfo(in,p1,1);
getInfo(in,p2,2);
getInfo(in,p3,3);
getInfo(in,p4,4);
getInfo(in,p5,5);
p1.writeOutput();
p2.writeOutput();
p3.writeOutput();
p4.writeOutput();
p5.writeOutput();
System.out.println("Average age "+(p1.getAge()+p2.getAge()+p3.getAge()+p4.getAge()+p5.getAge())/5.);
System.out.println("Average weight "+(p1.getWeight()+p2.getWeight()+p3.getWeight()+p4.getWeight()+p5.getWeight())/5.);
System.out.println("Youngest "+getMin(p1.getAge(),p2.getAge(),p3.getAge(),p4.getAge(),p5.getAge()));
System.out.println("Oldest "+getMax(p1.getAge(),p2.getAge(),p3.getAge(),p4.getAge(),p5.getAge()));
System.out.println("Lightest "+getMin(p1.getWeight(),p2.getWeight(),p3.getWeight(),p4.getWeight(),p5.getWeight()));
System.out.println("heaviest "+getMax(p1.getWeight(),p2.getWeight(),p3.getWeight(),p4.getWeight(),p5.getWeight()));
}
public static int getMin(int a,int b,int c,int d,int e)
{if(a<b&&a<c&&a<d&&a<e)
return a;
if(b<a&&b<c&&b<d&&b<e)
return b;
if(c<a&&c<b&&c<d&&c<e)
return c;
if(d<a&&d<b&&d<c&&d<e)
return d;
return e;
}
public static double getMin(double a,double b,double c,double d,double e)
{if(a<b&&a<c&&a<d&&a<e)
return a;
if(b<a&&b<c&&b<d&&b<e)
return b;
if(c<a&&c<b&&c<d&&c<e)
return c;
if(d<a&&d<b&&d<c&&d<e)
return d;
return e;
}
public static int getMax(int a,int b,int c,int d,int e)
{if(a>b&&a>c&&a>d&&a>e)
return a;
if(b>a&&b>c&&b>d&&b>e)
return b;
if(c>a&&c>b&&c>d&&c>e)
return c;
if(d>a&&d>b&&d>c&&d>e)
return d;
return e;
}
public static double getMax(double a,double b,double c,double d,double e)
{if(a>b&&a>c&&a>d&&a>e)
return a;
if(b>a&&b>c&&b>d&&b>e)
return b;
if(c>a&&c>b&&c>d&&c>e)
return c;
if(d>a&&d>b&&d>c&&d>e)
return d;
return e;
}
public static void getInfo(Scanner in, Pet p,int n)
{System.out.println("For pet "+n);
System.out.print("Enter pets name: ");
p.setName(in.nextLine());
System.out.print("Enter pets age: ");
p.setAge(in.nextInt());
System.out.print("Enter pets weight: ");
p.setWeight(in.nextDouble());
in.nextLine();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.