A veterinary clinic is writing software to be used when tracking the animals the
ID: 3799600 • Letter: A
Question
A veterinary clinic is writing software to be used when tracking the animals they treat. One of the classes they need will be used to process a dog’s weight.
Write a JAVA class Weight that represents a weight in pounds and ounces (both integers).
Note: there are 16 ounces per pound.
1) Determine the necessary private instance variables for the class.
2) Constructors (constructors do not need to validate input)
1. Write a constructor that builds a weight with given pounds and ounces.
2.Write a constructor that builds a weight with pounds only (use zero for the ounces).
3. Write a constructor that takes no arguments and builds a weight of one pound zero ounces.
4. Write a copy constructor that will make a copy of an existing weight.
3) Methods
1. Write a method that will retrieve the pounds for a weight.
2. Write a method that will retrieve the ounces for a weight.
3. Write a method that will give a String representation of the pounds and ounces of a weight (ex: “5 lbs. 2 oz.”).
4. Write a method that can be used to change the pounds of a weight.
5. Write a method that can be used to change the ounces of a weight.
6. Write a method to add a number of ounces to a weight.
7. Write a method to subtract a given percentage from a weight.
8. Write a method that will determine whether or not a weight is heavy (greater than 92 and ½ pounds).
9.Write a method that will determine whether or not two weights are equal (have the same pounds and ounces).
10.Write a method that will calculate and return the total weight in ounces.
Explanation / Answer
Please find the required program below: Please find the comments against each line for the description:
class Weight {
//instance variables
int pounds;
int ounces;
//constructor
public Weight(int pounds, int ounces) {
this.pounds = pounds;
this.ounces = ounces;
}
//constructor
public Weight(int pounds) {
this.pounds = pounds;
this.ounces = 0;
}
//constructor
public Weight() {
this.pounds = 1;
this.ounces = 0;
}
//copy constructor
public Weight(Weight w) {
this.pounds = w.pounds;
this.ounces = w.ounces;
}
//getters and setters
public int getPounds() {
return pounds;
}
public void setPounds(int pounds) {
this.pounds = pounds;
}
public int getOunces() {
return ounces;
}
public void setOunces(int ounces) {
this.ounces = ounces;
}
//string representation
@Override
public String toString() {
return pounds + " lbs. " + ounces + " oz.";
}
//add ounces to the weight
public void addOunces(int ounces) {
int o = this.ounces + ounces;
if(o >= 16){
this.pounds = (o/16);
this.ounces = o%16;
}
}
//subtract percentage from the weight
public void substract(int percent) {
int o = (pounds * 16) + ounces;
int newo = o - (percent/100 * ounces);
this.pounds = (newo/16);
this.ounces = newo%16;
}
//check whether is heavy or not
public boolean isHeavy() {
if(pounds == 92 && ounces >= 50)
return true;
else if(pounds > 92)
return true;
else
return false;
}
//to compare with other weight
public boolean equals(Weight other) {
if (ounces != other.ounces)
return false;
if (pounds != other.pounds)
return false;
return true;
}
//get Total Weight In Ounce
public int getTotalWeightInOunce() {
return (pounds * 16) + ounces;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.