I need to create Shape is an abstract class & Drawable as an interface
My goal is to create class hierarchy as,
No code duplication.
Only one counter implemented for counting all the shapes (objects of the type Circle, GraphicCircle, Rectangle, GraphicRectangle, any Shape subclasses that may be added in the future).
Only one counter implemented for counting all the Circle (and its subclass) objects.
Only one counter implemented for counting all the Rectangle (and its subclass) objects.
Shape class has no knowledge of any of its subclasses.
As beginner, first created Shape abstract class and one Circle, code complied without any error.
When I tried to run via >java console got an error as "Exception in thread "main" java.lang.NullPointerException at AbsInhTest.main(AbsInhTest.java:15)"
Please review below code and help in fixing error and implementing proper hierarchy with above validations
When tested via html getting error as below,
Java Plug-in 1.6.0_13
Using JRE version 1.6.0_13 Java HotSpot Client VM
User home directory = C:\Documents and Settings\java
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.lang.ClassCastException: AbsInhTest cannot be cast to java.applet.Applet
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassCastException: AbsInhTest cannot be cast to java.applet.Applet
Shape
public abstract class Shape extends Object{
private int x;
private int y;
public double getArea()
{
return 0.0;
}
public double getVolume()
{
return 0.0;
}
public abstract String getName();
}
Circle
public class Circle extends Shape{
private double radius;
public Circle()
{
}
//Constructor
public Circle (double x, double y, double radiusValue)
{
//super (x,y);
setRadius(radiusValue);
}
//Set Radius
public void setRadius(double radiusValue)
{
radius = (radiusValue < 0.0 ? 0.0 : radiusValue);
}
//Return Radius
public double getRadius()
{
return radius;
}
//Calcluate and return diameter
public double getDiameter()
{
return 2 * getRadius();
}
//Calcluate and return circumference
public double getCircumference()
{
return Math.PI * getDiameter();
}
public double getArea()
{
return Math.PI * getRadius() * getRadius();
}
public String getName()
{
return "Circle";
}
//override toString to return String representation of Circle
public String toString()
{
return "Center = " + super.toString() + "; Raidus = " + getRadius();
}
}
AbsInhTest
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import java.applet.Applet;
import java.awt.Graphics;
public class AbsInhTest{
public static void main(String args[]){
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
Circle circle = new Circle( 22, 8, 3.5 );
String output = circle.getName() + ": " +circle + "\n";
Shape arrayOfShapes[] = new Shape[ 2 ];
arrayOfShapes[ 0 ] = circle;
for (int i = 0; i < arrayOfShapes.length; i++) {
output += "\n\n" + arrayOfShapes[ i ].getName() + ": " +
arrayOfShapes[ i ].toString() + "\nArea = " +
twoDigits.format(arrayOfShapes[ i ].getArea() )+ "\nVolume = " +
twoDigits.format(arrayOfShapes[ i ].getArea() );
}
JOptionPane.showMessageDialog( null, output );
System.exit( 0 );
}
}
Thanks for your time and help
This post has been edited by oops_guy: 01 May 2009 - 08:56 AM

New Topic/Question
Reply




MultiQuote



|