5 Replies - 2181 Views - Last Post: 27 July 2007 - 05:48 PM Rate Topic: -----

#1 yogi_bear_79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-July 07

More help w/abstract classes, interfaces & inheritence

Post icon  Posted 27 July 2007 - 03:55 PM

This is my last lab of the class! I am pretty close on this one, however, I am stuck. I know my problem resides in my circle class or at least the brunt of it. Any nudge in the right direction would be most helpful. I can't guarantee it will get me across the line, but again your tutorage is most welcome, thanks in advance!!!




Class TestCircle will not compile, but that should be because of problems in the other files. Unless there is a typo, this is from the lesson plan.

import java.text.DecimalFormat;

public class TestCircle
{
	public static void main(String[] args)
	{
		DecimalFormat showTwoDecimals = new DecimalFormat( "0.00" );
		Circle c1 = new Circle(2.5, 22, 44);
		double newRadius = 6.0;

		System.out.println("This shape is a " + c1.getName() +
			   			   "\nlocated at " + c1.getLocation() +
			   			   "\nRadius is " + c1.getRadius());

		System.out.println("The shape area is: "
							 + showTwoDecimals.format(c1.area())+"\n");
		System.out.println("The shape perimiter (circumference) is: " +
							showTwoDecimals.format(c1.perimeter())+"\n");
		System.out.println("After changing the radius to " + newRadius);
							c1.setRadius(newRadius);
		System.out.println(", it is a "+ c1 + "nArea is "
							+ showTwoDecimals.format(c1.area())+"\n");
		System.out.println("The shape perimiter (circumference) is: " +
							showTwoDecimals.format(c1.perimeter())+"\n");
   }
}




Circle class will not compile, these are the errors:
.\Point.java:6: Shape(java.lang.String) in Shape cannot be applied to ()
{
^
.\Point.java:12: Shape(java.lang.String) in Shape cannot be applied to ()
{
^
public abstract class Circle extends Point implements Shape2D
{
   protected double radius;

   public Circle()
   {
	  setRadius( 0 );
   }

   public Circle( double circleRadius, int x, int y)
   {
		this.radius=circleRadius;
   }

   // Get radius of Circle
   public double getRadius()
   {
   		return radius;
   }

   // Set radius of Circle
   public void setRadius(double r)
   {
   		radius = ( r >= 0 ? r : 0 );
   }

   // Calculate area of Circle
   public double area()
   {
   		return Math.PI * radius * radius;
   }

   // convert the Circle to a String
   public String toString()
   {
		return "Circle with center at (" + x + ", " + y + ")" +
			  " and radius = " + radius;
   }

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

   // return the center location
   public String getLocation()
   {
	return "X = " + x + ", Y = " + y;
   }
}



Point Class will not compile, the two errors are (the same as the circle class):
.\Point.java:6: Shape(java.lang.String) in Shape cannot be applied to ()
{
^
.\Point.java:12: Shape(java.lang.String) in Shape cannot be applied to ()
{
^

public abstract class Point extends Shape
{
	protected int x, y;

   	public Point()
   	{
	  this.x = 0;
	  this.y = 0;
   	}

   	public Point( int xCoordinate, int yCoordinate )
   	{
	  this.x = xCoordinate;
	  this.y = yCoordinate;
   	}

}



Shape Class (Will Compile)

public abstract class Shape
{
	public String name;

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

	public abstract String getName();
	public abstract String getLocation();
}



Shape2D interface (Will Compile)

public interface Shape2D
{
  public double getArea();
  public double getPerimiter();
}


Is This A Good Question/Topic? 0
  • +

Replies To: More help w/abstract classes, interfaces & inheritence

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: More help w/abstract classes, interfaces & inheritence

Posted 27 July 2007 - 04:26 PM

Hi I give you a hint in the previous post, and I'll give you again:
try
public abstract class Point extends Shape
{
	protected int x, y;

	   public Point()
	   {
	  super("This shape is a point");
	  this.x = 0;
	  this.y = 0;
	   }

	   public Point( int xCoordinate, int yCoordinate )
	   {
	  super("This shape is a point");
	  this.x = xCoordinate;
	  this.y = yCoordinate;
	   }

}


And according to that example do something similar with the other classes, the test program should be considered at the end after the problems with the other classes have been solved.
Was This Post Helpful? 0
  • +
  • -

#3 yogi_bear_79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-July 07

Re: More help w/abstract classes, interfaces & inheritence

Posted 27 July 2007 - 04:50 PM

Thanks, your contributions are much appriciated. All of my classes will now compile, all that is except the test class. It's errors are:

\TestCircle.java:8: Circle is abstract; cannot be instantiated
Circle c1 = new Circle(2.5, 22, 44);
^
\TestCircle.java:18: cannot resolve symbol
symbol : method perimeter ()
location: class Circle
showTwoDecimals.format(c1.perimeter())+"\n");
\TestCircle.java:24: cannot resolve symbol
symbol : method perimeter ()
location: class Circle
showTwoDecimals.format(c1.perimeter())+"\n");


I tried to use your hint in my other classes, but it seemed to stop them from compiling. I realize that doesn't mean they are correct without it, but I have to start somewhere. I realize there isn't a perimiter method in the circle class, but I am unsure how to implement it from Shape2D. Also whenever I remove the abstract from circle then it won't compile so I am not sure what to do with the error
^
import java.text.DecimalFormat;

public class TestCircle
{
	public static void main(String[] args)
	{
		DecimalFormat showTwoDecimals = new DecimalFormat( "0.00" );
		Circle c1 = new Circle(2.5, 22, 44);
		double newRadius = 6.0;

		System.out.println("This shape is a " + c1.getName() +
			   			   "\nlocated at " + c1.getLocation() +
			   			   "\nRadius is " + c1.getRadius());

		System.out.println("The shape area is: "
							 + showTwoDecimals.format(c1.area())+"\n");
		System.out.println("The shape perimiter (circumference) is: " +
							showTwoDecimals.format(c1.perimeter())+"\n");
		System.out.println("After changing the radius to " + newRadius);
							c1.setRadius(newRadius);
		System.out.println(", it is a "+ c1 + "nArea is "
							+ showTwoDecimals.format(c1.area())+"\n");
		System.out.println("The shape perimiter (circumference) is: " +
							showTwoDecimals.format(c1.perimeter())+"\n");
   }
}




public abstract class Circle extends Point implements Shape2D
{
   protected double radius;
   private Point center;

   public Circle()
   {
	   setRadius(0);
   }

   public Circle( double circleRadius, int x, int y)
   {
		this.radius=circleRadius;
   }

   // Get radius of Circle
   public double getRadius()
   {
   		return radius;
   }

   // Set radius of Circle
   public void setRadius(double r)
   {
   		radius = ( r >= 0 ? r : 0 );
   }

   // Calculate area of Circle
   public double area()
   {
   		return Math.PI * radius * radius;
   }

   // convert the Circle to a String
   public String toString()
   {
		return "Circle with center at (" + x + ", " + y + ")" +
			  " and radius = " + radius;
   }

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

   // return the center location
   public String getLocation()
   {
	return "X = " + x + ", Y = " + y;
   }
}


Was This Post Helpful? 0
  • +
  • -

#4 yogi_bear_79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-July 07

Re: More help w/abstract classes, interfaces & inheritence

Posted 27 July 2007 - 04:59 PM

I made some more changes to the Circle class, same errors on the test class though

public abstract class Circle extends Point implements Shape2D
{
   protected double radius;
   private Point center;

   public Circle()
   {
	   setRadius(0);
   }

   public Circle( double circleRadius, int x, int y)
   {
		super( x, y);
		setRadius( circleRadius );

   }

   // Get radius of Circle
   public double getRadius()
   {
   		return radius;
   }

   // Set radius of Circle
   public void setRadius(double r)
   {
   		radius = ( r >= 0 ? r : 0 );
   }

   // Calculate area of Circle
   public double area()
   {
   		return Math.PI * radius * radius;
   }

   // convert the Circle to a String
   public String toString()
   {
		return "Circle with center at (" + x + ", " + y + ")" +
			  " and radius = " + radius;
   }

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

   // return the center location
   public String getLocation()
   {
	return "X = " + x + ", Y = " + y;
   }
}


Was This Post Helpful? 0
  • +
  • -

#5 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: More help w/abstract classes, interfaces & inheritence

Posted 27 July 2007 - 05:14 PM

Hi I made changes like I deleted abstract from Circle because you can't instantiate an object from an abstract in your test program, so after that I remove the implements Shape2D from Circle and put it in Point, also I override those functions in the Shape2D interface in the Circle class, and instead of perimiter() I used getPerimiter() function in the main program so all in all here are the codes:

Shape2D
public interface Shape2D
{
  public double getArea();
  public double getPerimiter();
}


Shape
public abstract class Shape
{
	public String name;

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

	public abstract String getName();
	public abstract String getLocation();
}


Point
public abstract class Point extends Shape implements Shape2D
{
	protected int x, y;

	   public Point()
	   {
	  super("This shape is a point");
	  this.x = 0;
	  this.y = 0;
	   }

	   public Point( int xCoordinate, int yCoordinate )
	   {
	  super("This shape is a point");
	  this.x = xCoordinate;
	  this.y = yCoordinate;
	   }

}


Circle

public class Circle extends Point 
{
   protected double radius;

   public Circle()
   {
	  setRadius( 0 );
   }


	public double getArea()
	{
		return area();
	}
	public double getPerimiter()
	{
		return 2*radius*Math.PI;
	}
   public Circle( double circleRadius, int x, int y)
   {
		this.radius=circleRadius;
   }

   // Get radius of Circle
   public double getRadius()
   {
		   return radius;
   }

   // Set radius of Circle
   public void setRadius(double r)
   {
		   radius = ( r >= 0 ? r : 0 );
   }

   // Calculate area of Circle
   public double area()
   {
		   return Math.PI * radius * radius;
   }

   // convert the Circle to a String
   public String toString()
   {
		return "Circle with center at (" + x + ", " + y + ")" +
			  " and radius = " + radius;
   }

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

   // return the center location
   public String getLocation()
   {
	return "X = " + x + ", Y = " + y;
   }
}


TestCircle
import java.text.DecimalFormat;

public class TestCircle
{
	public static void main(String[] args)
	{
		DecimalFormat showTwoDecimals = new DecimalFormat( "0.00" );
		Circle c1 = new Circle(2.5, 22, 44);
		double newRadius = 6.0;

		System.out.println("This shape is a " + c1.getName() +
							  "\nlocated at " + c1.getLocation() +
							  "\nRadius is " + c1.getRadius());

		System.out.println("The shape area is: "
							 + showTwoDecimals.format(c1.area())+"\n");
		System.out.println("The shape perimiter (circumference) is: " +
							showTwoDecimals.format(c1.getPerimiter())+"\n");
		System.out.println("After changing the radius to " + newRadius);
							c1.setRadius(newRadius);
		System.out.println(", it is a "+ c1 + "nArea is "
							+ showTwoDecimals.format(c1.area())+"\n");
		System.out.println("The shape perimiter (circumference) is: " +
							showTwoDecimals.format(c1.getPerimiter())+"\n");
   }
}


This post has been edited by PennyBoki: 27 July 2007 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#6 yogi_bear_79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-July 07

Re: More help w/abstract classes, interfaces & inheritence

Posted 27 July 2007 - 05:48 PM

Thank you very much for your paitence. I completley overlooked overiding those functions. I had it in my head that somehow (not sure how) that I had to tell circle to get the function from Shape2D. Thank you so much for your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1