Abstract class & Interface

Abstract class & Drawable Interface

Page 1 of 1

4 Replies - 1897 Views - Last Post: 01 May 2009 - 11:56 AM Rate Topic: -----

#1 oops_guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-April 09

Abstract class & Interface

Posted 30 April 2009 - 12:52 PM

Hi,

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


Is This A Good Question/Topic? 0
  • +

Replies To: Abstract class & Interface

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Abstract class & Interface

Posted 30 April 2009 - 06:33 PM

And how do you execute this Application ?

> java AbsInhTest

at the console prompt ?
Was This Post Helpful? 0
  • +
  • -

#3 oops_guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-April 09

Re: Abstract class & Interface

Posted 01 May 2009 - 08:54 AM

Yes, that's correct.

Tried executing at console prompt
> java AbsInhTest

Got below error
"Exception in thread "main" java.lang.NullPointerException at AbsInhTest.main(AbsInhTest.java:15)"

Then tried using html did not work instead got big list errors which I did not understand about list of errors .

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 oops_guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-April 09

Re: Abstract class & Interface

Posted 01 May 2009 - 11:34 AM

FYI..

Found the error in my code created an array of size 2, but only put one object in it. The second element was empty. Added Circle circle2 = new Circle( 11, 4, 7 ); it worked via >java console.

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 );
   Circle circle2 = new Circle( 11, 4, 7 );

   String output = circle.getName() + ": " +circle + "\n";

   Shape arrayOfShapes[] = new Shape[ 2 ];
   arrayOfShapes[ 0 ] = circle;
   arrayOfShapes[ 1 ] = circle2;
   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 );
   }
  }



Well, still stuck with html error...
Was This Post Helpful? 0
  • +
  • -

#5 oops_guy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 30-April 09

Re: Abstract class & Interface

Posted 01 May 2009 - 11:56 AM

Hi Guys,

I have created below program it works fine.
Need to added new features as GraphicCircle c1,GraphicCircle c2, GraphicRectangle r1 and GraphicRectangle r2

Can anybody please suggest on how to add above features into below code?

Thanks
// Using abstract methods and classes.
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
}

class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}

class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}

class Circle extends Triangle {
Circle(double a, double b,double radius) {
super(a, b);
}

// override area for circle
  //Return Radius
	 public double getRadius(){
		  return dim1;
	 }
 //Calcluate and return diameter
	 public double getDiameter(){
		   return 2 * getRadius();
	 }
 //Calcluate and return circumference
	 public double getCircumference(){
			return Math.PI * getDiameter();
	 }
	 public double area(){ 
		 System.out.println("Inside Area for Circle.");
			  return Math.PI * getRadius() * getRadius();
	 }
}

class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Circle c = new Circle( 22, 8, 1 );
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = c;
System.out.println("Area is " + figref.area());
 }
}



Thanks for your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1