Can someone tell me what changes I can make to this program so when it runs it p
ID: 3687746 • Letter: C
Question
Can someone tell me what changes I can make to this program so when it runs it prints the area and volume instead of zeros? I think the ToString is returning the values of the members area and volume of class Rebenar_A04Q1 but I don't know how to change this.
public class Rebenar_A04Q1
{
public double radius;
public double height;
public double area;
public double volume;
public static void main(String[] args)
{
Cone cone1 = new Cone(10,20);
Cone cone2 = new Cone(5,10);
Sphere sphere1 = new Sphere(3);
Sphere sphere2 = new Sphere(5);
System.out.println(cone1);
System.out.println(cone2);
System.out.println(sphere1);
System.out.println(sphere2);
}
public static class Round extends Rebenar_A04Q1
{
private double volume()
{
return (double)(Math.PI * Math.pow(radius,2.0) * height);
}
private double area()
{
return (double)((2*Math.PI*radius*height) + (2*Math.PI*Math.pow(radius,2)));
}
public Round(double radius, double height)
{
this.radius = radius;
this.height = height;
}
}
/////////////////////////////////////////////////////////////////////////////////
public static class Cone extends Rebenar_A04Q1
{
public Cone(double radius, double height)
{
this.radius = radius;
this.height = height;
}
public double area()
{
return (double)(Math.PI*radius*(radius +
Math.sqrt(Math.pow(height,2.0)+Math.pow(height,2.0))));
}
public double volume()
{
return (double)(Math.PI*Math.pow(radius,2)*(height/3));
}
public String toString()
{
return "A Cone of radius: " +radius+ ", area: "+area+", and volume: " +volume+".";
}
}
////////////////////////////////////////////////////////////////////////////////////
public static class Sphere extends Rebenar_A04Q1
{
public Sphere(double radius)
{
this.radius=radius;
}
public double area()
{
return (double)(4*Math.PI*Math.pow(this.radius,2));
}
public double volume()
{
return (double)((4/3)*Math.PI*Math.pow(radius,3));
}
public String toString()
{
return "A Sphere of radius: " +radius+ ", area: "+ area +", and volume: " +volume+".";
}
}
}
Explanation / Answer
The functions of area and volumes are not called correctly in the toString function. Please make the below changes :
1.
change return "A Cone of radius: " +radius+ ", area: "+area+", and volume: " +volume+"."; to
return "A Cone of radius: " +radius+ ", area: "+area()+", and volume: " +volume()+".";
2.
Change return "A Sphere of radius: " +radius+ ", area: "+ area +", and volume: " +volume+"."; to
return "A Sphere of radius: " +radius+ ", area: "+ area() +", and volume: " +volume()+".";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.