Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey guys I\'ve been doing lab assignments in Java now, but this task, I struggle

ID: 3639615 • Letter: H

Question

Hey guys

I've been doing lab assignments in Java now, but this task, I struggle a bit to solve. I've seen lots of examples but I get nowhere then will have to ask if anyone knows how this task can be solved.

Task:

---------------------------------------------------------------------------------------------------------------------------

Sphere.java , Paint.java and PaintThings.java class are here:

//*****************************************
// Sphere.java
//
// Represents a sphere.
//*****************************************
public class Sphere extends Shape
{
private double radius; //radius in feet
//----------------------------------
// Constructor: Sets up the sphere.
//----------------------------------
public Sphere(double r)
{
super("Sphere");
radius = r;
}
//-----------------------------------------
// Returns the surface area of the sphere.
//-----------------------------------------
public double area()
{
return 4*Math.PI*radius*radius;
}
//-----------------------------------
// Returns the sphere as a String.
//-----------------------------------
public String toString()
{
return super.toString() + " of radius " + radius;
}
}

//*****************************************************
// Paint.java
//
// Represents a type of paint that has a fixed area
// covered by a gallon. All measurements are in feet.
// *****************************************************
public class Paint
{
private double coverage; //number of square feet per gallon
//-----------------------------------------
// Constructor: Sets up the paint object.
//-----------------------------------------
public Paint(double c)
{
coverage = c;
}
//---------------------------------------------------
// Returns the amount of paint (number of gallons)
// needed to paint the shape given as the parameter.
//---------------------------------------------------
public double amount(Shape s)
{
System.out.println ("Computing amount for " + s);
return 0;

}

//***********************************************************
// PaintThings.java
//
// Computes the amount of paint needed to paint various
// things. Uses the amount method of the paint class which
// takes any Shape as a parameter.
//***********************************************************
import java.text.DecimalFormat;
public class PaintThings
{
//-----------------------------------------
// Creates some shapes and a Paint object
// and prints the amount of paint needed
// to paint each shape.
//-----------------------------------------
public static void main (String[] args)
{
final double COVERAGE = 350;
Paint paint = new Paint(COVERAGE);
Rectangle deck;
Sphere bigBall;
Cylinder tank;
double deckAmt, ballAmt, tankAmt;
// Instantiate the three shapes to paint
// Compute the amount of paint needed for each shape
// Print the amount of paint for each.
DecimalFormat fmt = new DecimalFormat("0.#");
System.out.println (" Number of gallons of paint needed...");
System.out.println ("Deck " + fmt.format(deckAmt));
System.out.println ("Big Ball " + fmt.format(ballAmt));
System.out.println ("Tank " + fmt.format(tankAmt));
}
}

I have tried me on this classes in the task:

Making the Rectangle,Cylinder and Shape class

-------------------------------------------

public abstract class Shape
{
String name;
double area;

}

---------------------------------------------

public class Cylinder extends Shape
{
private double radius;
private double height;


public double area()
{
return PI*radius^2*height;
}
}

---------------------------------------------

public class Rectangle extends Shape
{
private double height, width;

public Rectangle(double _height, double _width)
{
name = "Rectangle";
height = _height;
width = _width;
}

public void Area()
{
area = height * width;
}
}

Explanation / Answer

public abstract class Shape
{
private String shapeName;

public Shape(String shapeName)
{
this.shapeName = shapeName;
}

public abstract double area();

public String toString()
{
return shapeName;
}
}

With the definition of Shape, we can now fill in the Paint class:

public class Paint
{
...
public double amount(Shape s)
{
return s.area()/coverage;
{

}

Here are two more classes of shapes:


public class Rectangle extends Shape
{
private double length, width;

public Rectangle(double length, double width)
{
super("rectangle");
this.length = length;
this.width = width;
}

public double area()
{
return length*width;
}
}

public class Cylinder extends Shape
{
private double radius, height;

public Cylinder(double radius, double height)
{
super("cylinder");
this.radius = radius;
this.height = height;
}

public double area()
{
return Math.PI*radius*2*height + 2*Math.PI*radius*radius;
}
}