Answer: Provided in the explanation section
Step-by-step explanation:
CODE USED :
//weight class
public class Weight
{
//defining private variables pound, ounces and number of ounces in pound which is 16
private int pounds;
private double ounces;
private int numberOfOuncesInPound=16;
//constructor which initializes the weight variable with the coming input
public Weight(int p, double o)
{
this.pounds=p;
this.ounces=o;
}
//checks if the incoming weight object is less than the current object weight
public boolean lessThan(Weight w)
{
//first comparison between pounds of the objects, if current object is less, then return true
if(this.pounds<w.pounds)
return true;
//else if it is less then return false
else if(this.pounds>w.pounds)
return false;
//else if both are equal then we compare the ounces
else
{
//if current object ounces is less than we return true else false
if(this.ounces<w.ounces)
return true;
else
return false;
}
}
//adds incoming weight to the current weight object and then normalize it
public void addTo(Weight w)
{
this.pounds+=w.pounds;
this.ounces+=w.ounces;
normalize();
}
//divide the current weight with factor x which is a parameter of the function
public void divide(int x)
{
this.ounces=toOunces();
this.ounces/=x;
this.pounds=0;
normalize();
}
//returns a string which contains the data in a Particular Format
public String toString()
{
return this.pounds+" lbs "+this.ounces+" oz";
}
//returns weight converted into ounces
private double toOunces()
{
return this.pounds*this.numberOfOuncesInPound+this.ounces;
}
//this function checks if number of ounces is greater than 16 then it normalizes it by converting into pounds
private void normalize()
{
while(this.ounces>this.numberOfOuncesInPound)
{
this.ounces-=this.numberOfOuncesInPound;
this.pounds++;
}
}
}
cheers i hope this helped !!