Hierarchy of Shape

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

#### Hybrid View

1. Registered User
Join Date
Nov 2006
Posts
5

## Hierarchy of Shape

I have never done this type of java before. I have list the assignment and the code that I worked on. I need somebody to look and see if my shape class is and to help me with the Paint.java and PaintThings.java. I can someone tell me what I need to do in order to get this working. Thanks

In this lab exercise you will develop a class hierarchy of shapes and write a program that computes the amount of paint
needed to paint different objects. The hierarchy will consist of a parent class Shape with three derived classes - Sphere,
Rectangle, and Cylinder. For the purposes of this exercise, the only attribute a shape will have is a name and the method of
interest will be one that computes the area of the shape (surface area in the case of three-dimensional shapes). Do the
following.
1. Write an abstract class Shape with the following properties:
An instance variable shapeName of type String
An abstract method area()
A toString method that returns the name of the shape
2. The file Sphere.java contains a class for a sphere which is a descendant of Shape. A sphere has a radius and its area
(surface area) is given by the formula 4*PI*radius^2. Define similar classes for a rectangle and a cylinder. Both the
Rectangle class and the Cylinder class are descendants of the Shape class. A rectangle is defined by its length and
width and its area is length times width. A cylinder is defined by a radius and height and its area (surface area) is
PI*radius^2*height. Define the toString method in a way similar to that for the Sphere class.
3. The file Paint.java contains a class for a type of paint (which has a "coverage" and a method to compute the amount
of paint needed to paint a shape). Correct the return statement in the amount method so the correct amount will be
returned. Use the fact that the amount of paint needed is the area of the shape divided by the coverage for the paint.
(NOTE: Leave the print statement - it is there for illustration purposes, so you can see the method operating on
different types of Shape objects.)
4. The file PaintThings.java contains a program that computes the amount of paint needed to paint various shapes. A
paint object has been instantiated. Add the following to complete the program:
Instantiate the three shape objects: deck to be a 20 by 35 foot rectangle, bigBall to be a sphere of radius 15, and
tank to be a cylinder of radius 10 and height 30.
Make the appropriate method calls to assign the correct values to the three amount variables.
Run the program and test it. You should see polymorphism in action as the amount method computes the
amount of paint for various shapes.

//*****************************************
// Shape.java
//
// Represents Shape.
//*****************************************

import java.lang.*;
import java.io.*;
import java.util.*;

public abstract class Shape
{

protected String _type;

// Constructor of the shape
protected double _area;

// Return type of shape
public Shape()
{
_type = "Shape";
}

//Return String type
public String type()
{
return _type;
}

//Return the area
public abstract double area();
{
return area;
}
}

//*****************************************
// Sphere.java
//
// Represents a sphere.
//*****************************************
import java.lang.*;

public class Sphere extends Shape
{

//----------------------------------
// Constructor: Sets up the sphere.
//----------------------------------
public Sphere(double r)
{
super("Sphere");
}

//-----------------------------------------
// Returns the surface area of the sphere.
//-----------------------------------------
public double area()
{
}

//-----------------------------------
// Returns the sphere as a String.
//-----------------------------------
public String toString()
{
}

}

//*****************************************
// Rectangle.java
//
// Represents a rectangle.
//*****************************************
import java.lang.*;

public class Rectangle extends Shape {

protected double length;
protected double width;

// Constructor
public Rectangle(double l, double w) {

super("rectangle");
length = l;
width = w;
}

// Calculate area of Rectangle
public double area()
{
return length * width;
}

// Return Rectangle to a String
public String toString() {
return "Center = " + super.toString() +
"; Length = " + length + ", Width = " + width; }

// Return class name
public String getName()
{
return "Rectangle";
}

}

//*****************************************
// Cylinder.java
//
// Represents a cylinder.
//*****************************************

public class Cylinder extends Shape {
private double height;

// constructor
public Cylinder(double h, double r)
{
super(cylinder);
height = h
}

// Calculate area of Cylinder (surface area)
public double area()
{
return 2 * super.area() +
2 * Math.PI * radius * height;
}

// Calculate volume of Cylinder
public double volume()
{
return super.area() * height;
}

// Return Cylinder to a String
public String toString()
{
return super.toString() + "; Height = " + height;
}

// Return the class name
public String getName()
{
return "Cylinder";
}
}

//******************************************************
// 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 ("\nNumber 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));
}
}

2. Registered User
Join Date
Dec 2006
Location
Kerala,India
Posts
51
There are a lot of logical errors and conseptual errors i can see in this code.Bacause of that, the program will not even compile..

I request you to concentrate on the following areas of Object Orientation Thoroughly.

1. Consepts of abstract class and abstract methods. ( In Shape class you have defined area() method as abstract but provided implementation..)
2. Constructor invocation sequence in a class hierarchy.(Shape does not have constructor which accepts a String argument,still you called super with String argument from Rectangle and Sphere.)

some of the errors has occured due to careless ness.

1. method definition in Shape
public abstract double area();
{
return area;
} // see the abstract keyword and semicolon in the end, still implementation..again there is no data member called area, that is _area
2. super Constructor invocation from cylinder
super(cylinder); // there is no variable defined as cylinder. Probably you might have forgotten to provide quotes for cylinder.

Code itself says that, all classes are in different files. Still you didn't make sure that Shape.java is compiled successfully. I suggest you to make sure that each and every independent entity is compiled successfully before you use the same in to another one class. Otherwise, it will be very difficult to trace out the problem when you start working on enterprise applications which has more than 50,000 LOCs.

Study well, work hard.. all the very best..

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise