Java homework help please We need a way to add Cylinders to the game. F in the a
ID: 3787038 • Letter: J
Question
Java homework help please
We need a way to add Cylinders to the game. F in the addCylinder method. Notice that we've already provided two fields for you to use to store the Cylinders: a Cylinder array called cylinders and an integer called count. If you need an example of how to add a new object to an array, see textbook chapter 3.1 or the lecture notes from 1/25 We need a way to delete cylinders from the game Fill in the deletecylinder method. It should delete the Cylinder at the end of the cylinders array. Finally, you will fill in the fillCupsInorder method that pours water from the source cylinder to the game's cylinders. Use the example games above to make sure you understand the rules. The method must fill the game cylinders in the order they were added to the game. When you've finished the above parts, you should be able to run CylinderGame to see if it works. There are three test cases provided (see the main method). Each test case gets a game, a source Cylinder, and the expected volumes at the end of the game.Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
############# Cylinder.java ##############
public class Cylinder {
private final double radius;
private final double height;
private double waterHeight;
public Cylinder(double radius, double height){
waterHeight = 0;
if(radius < 0 || height < 0){
this.height = 1;
this.radius = 1;
System.out.println("ERROR Cylinder");
}else{
this.height = height;
this.radius = radius;
}
}
public Cylinder(){
waterHeight = 0;
height = 1;
radius = 1;
}
public double getRadius(){
return radius;
}
public double getHeight(){
return height;
}
public double getVolume(){
return radius*radius*height;
}
public double getWaterVolume(){
return radius*radius*waterHeight;
}
public void setWaterVolume(double volume){
waterHeight = volume/(radius*radius);
}
public String toString(){
final String pi = "u03c0";
return "Cylinder(volume="+getVolume()+pi+", "+
"waterVolume="+getWaterVolume()+pi+")";
}
public void pourWaterFrom(Cylinder other){
// water overflow, then fill this Cylinder completely
if(this.getVolume() >= other.getWaterVolume()){
this.waterHeight = height;
}else{
// finding water height in this cylinder corresponding to
// other water volume
waterHeight = other.getWaterVolume()/(radius*radius);
}
}
public void fillToTop(){
waterHeight = height;
}
}
############## CylinderGame.java #####################
public class CylinderGame {
private Cylinder[] cylinders;
private int count;
public CylinderGame(int maxSize) {
cylinders = new Cylinder[maxSize];
count = 0;
}
public void addCylinder(double r, double h){
// if cylinders array is not full then add at end
if(count != cylinders.length){
cylinders[count] = new Cylinder(r, h);
count = count +1;
}
}
public void deleteCylinder(){
// if there is at least one cylinder in cylinders array then remove last
if(count > 0){
// setting last entry with null
cylinders[count-1] = null;
// decreasing count
count = count -1;
}
}
public void fillCupsInOrder(Cylinder fromCylinder){
double totalFromVolume = fromCylinder.getWaterVolume();
for(int i=0; i<cylinders.length; i++){
cylinders[i].pourWaterFrom(fromCylinder);
if(totalFromVolume >= cylinders[i].getVolume())
totalFromVolume = totalFromVolume - cylinders[i].getVolume();
else
totalFromVolume = 0;
fromCylinder.setWaterVolume(totalFromVolume);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.