I am having trouble with the fill method 5. [40 points] Use the following UML di
ID: 3732584 • Letter: I
Question
I am having trouble with the fill method
5. [40 points] Use the following UML diagram and the partially completed code for the class Bottle Complete the codee a. [10 points] Add the properties b. [10 points] Implement the getter for the property filled. c. [10 points] Finish the constructor Bottle(double, String). Make it verify that the unit is "oz" or "cc", and sets it to "oz if not. d. [10 points] Finish the method fill (double amount) The parameter amount represents the amount of liquid to be added to the bottle. The property filled represents the amount of liquid already in the bottle. Make the method verify that the bottle will not overflow. The bottle can be filled to capacity but no more public class Bottle[ //Properties go here. public Bottle (double size, string unit)t //Finish this constructor Bottle size: double filled : double uni:String public Bottle (double size)t this (size, "oz"); Bottle( Bottle(size : double) Bottle(size: double, unit: String) public Bottle O this (16.0, "oz"; public double getsize ) freturn size; ) public String getUnit ) freturn unit; ) //Implement a getter for filled here. + fill(amoun: double) + getSize): double + getFilled) double t getUnit String public void fill (double amount) t //Finish this method here.Explanation / Answer
Bottle.java
public class Bottle {
//Declaring instance variables
private double size;
private double filled;
private String unit;
//Zero argumented constructor
public Bottle() {
this(16.0, "oz");
}
//Parameterized constructor
public Bottle(double size) {
this(size, "oz");
}
//Parameterized constructor
public Bottle(double size, String unit) {
this.size = size;
this.unit = unit;
}
//This method will fill the bottle
public void fill(double amount)
{
if(filled<=size)
{
if(filled+amount>size)
{
filled+=size-filled;
System.out.println("** Bottle is full **");
}
else
{
filled+=amount;
}
}
else
{
System.out.println("** Bottle is Full **");
}
}
// getters and setters
public double getSize() {
return size;
}
public void setSize(double size) {
this.size = size;
}
public double getFilled() {
return filled;
}
public void setFilled(double filled) {
this.filled = filled;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
_____________________
Test.java
public class Test {
public static void main(String[] args) {
//creating an instance of Bottle class
Bottle b=new Bottle();
System.out.println("___ Calling Fill() Method ___");
b.fill(3);
System.out.println("Filled Amount :"+b.getFilled());
System.out.println("___ Calling Fill() Method ___");
b.fill(6);
System.out.println("Filled Amount :"+b.getFilled());
System.out.println("___ Calling Fill() Method ___");
b.fill(6);
System.out.println("Filled Amount :"+b.getFilled());
System.out.println("___ Calling Fill() Method ___");
b.fill(6);
System.out.println("Filled Amount :"+b.getFilled());
System.out.println("___ Calling Fill() Method ___");
b.fill(6);
System.out.println("Filled Amount :"+b.getFilled());
}
}
____________________
Output:
___ Calling Fill() Method ___
Filled Amount :3.0
___ Calling Fill() Method ___
Filled Amount :9.0
___ Calling Fill() Method ___
Filled Amount :15.0
___ Calling Fill() Method ___
** Bottle is full **
Filled Amount :16.0
___ Calling Fill() Method ___
** Bottle is full **
Filled Amount :16.0
_____________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.