Question

public class CylindricalTank {    private String idTag;           // Unique String identifying the tank...

public class CylindricalTank {

   private String idTag;           // Unique String identifying the tank
   private double radius;           // The non-negative radius of the base of the tank in meters
   private double height;           // The non-negative height of the tank in meters
   private double liquidLevel;       // The current height of the liquid in the tank in meters

   /**
   * CylindricalTank General Constructor
   */
   public CylindricalTank(String tag, double radius, double height, double liquidLevel) {
       super();
       this.idTag = tag;
       this.radius = radius;
       this.height = height;
       this.liquidLevel = liquidLevel;
   }

   /**
   * Exercise #1
   * Copy Constructor creates a tank with the same properties as the parameter tank.
   */
   public CylindricalTank(CylindricalTank t) {
       // YOUR CODE HERE
       this.idTag = t.getIdTag();
       this.radius = t.getRadius();
       this.height = t.getHeight();
       this.liquidLevel = t.getLiquidLevel();
   }

   // Getters
   public String getIdTag() { return idTag; }
   public double getRadius() { return radius; }
   public double getHeight() { return height; }
   public double getLiquidLevel() { return liquidLevel; }

   // Setters
   public void setRadius(double radius) { this.radius = radius; }
   public void setHeight(double height) { this.height = height; }
   public void setLiquidLevel(double liquidLevel) { this.liquidLevel = liquidLevel; }

   // Instance Methods

   /**
   * Returns true if both the target and parameter tanks have the same id
   */
   public boolean equals(Object t2) {
       if (t2 instanceof CylindricalTank) {
           CylindricalTank ct = (CylindricalTank) t2;
           return this.getIdTag().equals(ct.getIdTag());
       }
       return false;
   }

   /**
   * Returns the Cylindrical Tank as a string.
   */
   public String toString() {
       return "CylindricalTank[id=" + this.getIdTag() + "]";
   }

   /**
   * Returns the maximum volume of liquid that the tank can hold in cubic meters.
   */
   public double getCapacity() {
       return Math.PI * this.radius * this.radius * this.height;
   }

   /**
   * Returns the current volume of liquid that the tank holds in cubic meters.
   */
   public double getLiquidVolume() {
       return Math.PI * this.radius * this.radius * this.liquidLevel;
   }

   /**
   * Exercise #2
   * Returns the current volume of liquid that the tank can hold in gallons.
   *
   * Hint: 1 Cubic Meter is equivalent to 264 US Gallons.
   */
   public double getLiquidVolumeInGallons() {
       // YOUR CODE HERE
       double currentVolume = getLiquidVolume();
       double gallons = 264;
       double result = currentVolume*gallons;
       return result;
   }
  
   /**
   * Exercise #3
   * Compares the capacity (volume) of the target tank and the parameter tank.
   * Returns 0 if they have the same capacity, 1 if the target tank has larger capacity
   * and -1 otherwise.
   */
   public int compareTo(CylindricalTank t) {
       // YOUR CODE HERE
       if (this.getLiquidVolume() == t.getLiquidVolume()) {
           return 0;
       }
       else if (this.getLiquidVolume() < t.getLiquidVolume()) {
           return -1;
       }
       else
           return 1;
   }

   /**
   * Exercise #4
   * Modifies the target tank and add to it as much of the volume of liquid specified
   * by the cubicMeters parameters as possible. If the tank cannot hold all the liquid then it
   * should become full and the remainder of the liquid should be simply ignored.
   *
   * Note: This method must return the instance object (this).
   */
   public CylindricalTank add(double cubicMeters) {
       // YOUR CODE HERE
       this.liquidLevel += cubicMeters;
       if (this.getLiquidLevel() > this.getCapacity()) {
           this.liquidLevel = this.height;
       }
      
       return this;// Leave return as is as the method should return target object
   }

  
   /**
   * Exercise #5
   * Returns a string indicating the percent of its volume that the tank is full ignoring
   * any fractional part. For instance if the tank is exactly half way full the method should return the
   * String "50% Full". If the tank is exactly one third full the method should return the
   * String "33% Full". If the tank is empty the method should return the String "Empty".
   * HINT: To convert a double to an int you can cast it as follows (int)5.3 yields 5.
   */
   public String getPercentFilled() {
       // YOUR CODE HERE
       return ;
   }
}

J Unit

public class CylindricalTank {

   private String idTag;           // Unique String identifying the tank
   private double radius;           // The non-negative radius of the base of the tank in meters
   private double height;           // The non-negative height of the tank in meters
   private double liquidLevel;       // The current height of the liquid in the tank in meters

   /**
   * CylindricalTank General Constructor
   */
   public CylindricalTank(String tag, double radius, double height, double liquidLevel) {
       super();
       this.idTag = tag;
       this.radius = radius;
       this.height = height;
       this.liquidLevel = liquidLevel;
   }

   /**
   * Exercise #1
   * Copy Constructor creates a tank with the same properties as the parameter tank.
   */
   public CylindricalTank(CylindricalTank t) {
       // YOUR CODE HERE
       this.idTag = t.getIdTag();
       this.radius = t.getRadius();
       this.height = t.getHeight();
       this.liquidLevel = t.getLiquidLevel();
   }

   // Getters
   public String getIdTag() { return idTag; }
   public double getRadius() { return radius; }
   public double getHeight() { return height; }
   public double getLiquidLevel() { return liquidLevel; }

   // Setters
   public void setRadius(double radius) { this.radius = radius; }
   public void setHeight(double height) { this.height = height; }
   public void setLiquidLevel(double liquidLevel) { this.liquidLevel = liquidLevel; }

   // Instance Methods

   /**
   * Returns true if both the target and parameter tanks have the same id
   */
   public boolean equals(Object t2) {
       if (t2 instanceof CylindricalTank) {
           CylindricalTank ct = (CylindricalTank) t2;
           return this.getIdTag().equals(ct.getIdTag());
       }
       return false;
   }

   /**
   * Returns the Cylindrical Tank as a string.
   */
   public String toString() {
       return "CylindricalTank[id=" + this.getIdTag() + "]";
   }

   /**
   * Returns the maximum volume of liquid that the tank can hold in cubic meters.
   */
   public double getCapacity() {
       return Math.PI * this.radius * this.radius * this.height;
   }

   /**
   * Returns the current volume of liquid that the tank holds in cubic meters.
   */
   public double getLiquidVolume() {
       return Math.PI * this.radius * this.radius * this.liquidLevel;
   }

   /**
   * Exercise #2
   * Returns the current volume of liquid that the tank can hold in gallons.
   *
   * Hint: 1 Cubic Meter is equivalent to 264 US Gallons.
   */
   public double getLiquidVolumeInGallons() {
       // YOUR CODE HERE
       double currentVolume = getLiquidVolume();
       double gallons = 264;
       double result = currentVolume*gallons;
       return result;
   }
  
   /**
   * Exercise #3
   * Compares the capacity (volume) of the target tank and the parameter tank.
   * Returns 0 if they have the same capacity, 1 if the target tank has larger capacity
   * and -1 otherwise.
   */
   public int compareTo(CylindricalTank t) {
       // YOUR CODE HERE
       if (this.getLiquidVolume() == t.getLiquidVolume()) {
           return 0;
       }
       else if (this.getLiquidVolume() < t.getLiquidVolume()) {
           return -1;
       }
       else
           return 1;
   }

   /**
   * Exercise #4
   * Modifies the target tank and add to it as much of the volume of liquid specified
   * by the cubicMeters parameters as possible. If the tank cannot hold all the liquid then it
   * should become full and the remainder of the liquid should be simply ignored.
   *
   * Note: This method must return the instance object (this).
   */
   public CylindricalTank add(double cubicMeters) {
       // YOUR CODE HERE
       this.liquidLevel += cubicMeters;
       if (this.getLiquidLevel() > this.getCapacity()) {
           this.liquidLevel = this.height;
       }
      
       return this;// Leave return as is as the method should return target object
   }

  
   /**
   * Exercise #5
   * Returns a string indicating the percent of its volume that the tank is full ignoring
   * any fractional part. For instance if the tank is exactly half way full the method should return the
   * String "50% Full". If the tank is exactly one third full the method should return the
   * String "33% Full". If the tank is empty the method should return the String "Empty".
   * HINT: To convert a double to an int you can cast it as follows (int)5.3 yields 5.
   */
   public String getPercentFilled() {
       // YOUR CODE HERE
       return ;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I fixed the code in add() method and also implemented the getPercentFilled() method. Please do rate the answer if it helped. Thank you.

public class CylindricalTank {

private String idTag; // Unique String identifying the tank
private double radius; // The non-negative radius of the base of the tank in meters
private double height; // The non-negative height of the tank in meters
private double liquidLevel; // The current height of the liquid in the tank in meters

/**
* CylindricalTank General Constructor
*/
public CylindricalTank(String tag, double radius, double height, double liquidLevel) {
super();
this.idTag = tag;
this.radius = radius;
this.height = height;
this.liquidLevel = liquidLevel;
}

/**
* Exercise #1
* Copy Constructor creates a tank with the same properties as the parameter tank.
*/
public CylindricalTank(CylindricalTank t) {
// YOUR CODE HERE
this.idTag = t.getIdTag();
this.radius = t.getRadius();
this.height = t.getHeight();
this.liquidLevel = t.getLiquidLevel();
}

// Getters
public String getIdTag() { return idTag; }
public double getRadius() { return radius; }
public double getHeight() { return height; }
public double getLiquidLevel() { return liquidLevel; }

// Setters
public void setRadius(double radius) { this.radius = radius; }
public void setHeight(double height) { this.height = height; }
public void setLiquidLevel(double liquidLevel) { this.liquidLevel = liquidLevel; }

// Instance Methods

/**
* Returns true if both the target and parameter tanks have the same id
*/
public boolean equals(Object t2) {
if (t2 instanceof CylindricalTank) {
CylindricalTank ct = (CylindricalTank) t2;
return this.getIdTag().equals(ct.getIdTag());
}
return false;
}

/**
* Returns the Cylindrical Tank as a string.
*/
public String toString() {
return "CylindricalTank[id=" + this.getIdTag() + "]";
}

/**
* Returns the maximum volume of liquid that the tank can hold in cubic meters.
*/
public double getCapacity() {
return Math.PI * this.radius * this.radius * this.height;
}

/**
* Returns the current volume of liquid that the tank holds in cubic meters.
*/
public double getLiquidVolume() {
return Math.PI * this.radius * this.radius * this.liquidLevel;
}

/**
* Exercise #2
* Returns the current volume of liquid that the tank can hold in gallons.
*
* Hint: 1 Cubic Meter is equivalent to 264 US Gallons.
*/
public double getLiquidVolumeInGallons() {
// YOUR CODE HERE
double currentVolume = getLiquidVolume();
double gallons = 264;
double result = currentVolume*gallons;
return result;
}
  
/**
* Exercise #3
* Compares the capacity (volume) of the target tank and the parameter tank.
* Returns 0 if they have the same capacity, 1 if the target tank has larger capacity
* and -1 otherwise.
*/
public int compareTo(CylindricalTank t) {
// YOUR CODE HERE
if (this.getLiquidVolume() == t.getLiquidVolume()) {
return 0;
}
else if (this.getLiquidVolume() < t.getLiquidVolume()) {
return -1;
}
else
return 1;
}

/**
* Exercise #4
* Modifies the target tank and add to it as much of the volume of liquid specified
* by the cubicMeters parameters as possible. If the tank cannot hold all the liquid then it
* should become full and the remainder of the liquid should be simply ignored.
*
* Note: This method must return the instance object (this).
*/
public CylindricalTank add(double cubicMeters) {
double vol = getLiquidVolume() + cubicMeters;
if(vol >= getCapacity()) {
   liquidLevel = height;
}
else {
   liquidLevel = vol / (Math.PI * getRadius() * getRadius());
}
  
return this;// Leave return as is as the method should return target object
}

  
/**
* Exercise #5
* Returns a string indicating the percent of its volume that the tank is full ignoring
* any fractional part. For instance if the tank is exactly half way full the method should return the
* String "50% Full". If the tank is exactly one third full the method should return the
* String "33% Full". If the tank is empty the method should return the String "Empty".
* HINT: To convert a double to an int you can cast it as follows (int)5.3 yields 5.
*/
public String getPercentFilled() {
   int per = (int)(getLiquidLevel() * 100.0 / getCapacity());
     
   if(per == 0) {
       return "Empty";
   }
   else {
       return per + "% Full";
   }
}
}

Add a comment
Know the answer?
Add Answer to:
public class CylindricalTank {    private String idTag;           // Unique String identifying the tank...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public...

    Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public class InterfaceRunner { public static void main(String[] args) { ArrayList<GeometricSolid> shapes = new ArrayList<>(); shapes.add(new Sphere(10)); shapes.add(new Sphere(1)); shapes.add(new Cylinder(1, 5)); shapes.add(new Cylinder(10, 20)); shapes.add(new RightCircularCone(1, 5)); shapes.add(new RightCircularCone(10, 20)); /* * Notice that the array list holds different kinds of objects * but each one is a GeometricSolid because it implements * the interface. * */ for (GeometricSolid shape : shapes) { System.out.printf("%.2f%n",shape.volume());...

  • write a completed program (included the main) for the Q: add an equals method to each...

    write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...

  • Provided code Animal.java: public class Animal {    private String type;    private double age;   ...

    Provided code Animal.java: public class Animal {    private String type;    private double age;       public Animal(String aT, double anA)    {        this.type = aT;        if(anA >= 0)        {            this.age = anA;        }    }    public String getType()    {        return this.type;    }    public double getAge()    {        return this.age;    } } Provided code Zoo.java: public class Zoo {...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • public class Car {    /* four private instance variables*/        private String make;   ...

    public class Car {    /* four private instance variables*/        private String make;        private String model;        private int mileage ;        private int year;        //        /* four argument constructor for the instance variables.*/        public Car(String make) {            super();        }        public Car(String make, String model, int year, int mileage) {        super();        this.make = make;        this.model...

  • HospitalEmployee Inheritance help please. import java.text.NumberFormat; public class HospitalEmployee {    private String empName;    private...

    HospitalEmployee Inheritance help please. import java.text.NumberFormat; public class HospitalEmployee {    private String empName;    private int empNumber;    private double hoursWorked;    private double payRate;       private static int hospitalEmployeeCount = 0;    //-----------------------------------------------------------------    // Sets up this hospital employee with default information.    //-----------------------------------------------------------------    public HospitalEmployee()    { empName = "Chris Smith"; empNumber = 9999; hoursWorked = 0; payRate =0;               hospitalEmployeeCount++;    }       //overloaded constructor.    public HospitalEmployee(String eName,...

  • please help me add on this java code to run public class CarHwMain public static void...

    please help me add on this java code to run public class CarHwMain public static void main(String args 1/ Construct two new cars and one used car for the simulation Cari carl = new Car W"My New Mazda", 24.5, 16.0); Car car2 = new Cart My New Ford" 20.5, 15.0) Cari car) - new Cari ("My Used Caddie", 15.5, 16.5, 5.5, 1/ ADD CODE to completely fill the tanks in the new cars and off the used cars ton //...

  • import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {       ...

    import java.util.Scanner; public class MPGMain {    public static void main(String[] args)    {        Scanner input = new Scanner(System.in);               Mileage mileage = new Mileage();               System.out.println("Enter your miles: ");        mileage.setMiles(input.nextDouble());               System.out.println("Enter your gallons: ");        mileage.setGallons(input.nextDouble());               System.out.printf("MPG : %.2f",mileage.getMPG());           } } public class Mileage {    private double miles;    private double gallons;    public double getMiles()...

  • Question 19 Given the following class: public class Swapper ( private int x; private String y...

    Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT