Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Help with Abstract Extended Classes

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with Abstract Extended Classes

    Hey Everyone,
    I am taking my first java class right now(although I am not new to coding, I've taken many c and C++ classes). I am working on an assignment and I have run into some trouble. The assignment is to create an abstract class that has a bunch of extended classes(main class shape with extended classes such as circle, rectangle, etc...). I have created the classes I believe and I was creating a demo program to test/debug it and i have run into a error i can't seem to figure out.
    Below is my main Shape class, my extended Circle class, and my ShapeDemo program.

    Main Shape class
    ~~~~~~~~~~~~~~
    package CS249Mazur;
     
    public abstract class Shape
    {
    	public double Area;
    	public double Perimeter;
     
    	public Shape()
    	{
    		Area = this.getArea();
    		Perimeter = this.getPerimeter();
    	}
     
    	public Shape(Shape original)
    	{
    		Area = original.Area;
    		Perimeter = original.Perimeter;
    	}
     
    	public String toString()
    	{
    		return ("Area = "+this.Area+"\nPerimeter = "+this.Perimeter);
    	}
     
    	public boolean equals(Shape otherShape)
    	{
    		return (this.Area == otherShape.Area);
    	}
     
    	public int compareTo(Shape otherShape)
    	{
    		if (this.Area < otherShape.Area)
    			return -1;
    		else if(this.Area > otherShape.Area)
    			return 1;
    		else
    			return 0;
    	}
     
    	public abstract double getArea();
    	public abstract double getPerimeter();
    }

    Extended Circle class
    ~~~~~~~~~~~~~~~~~~
    package CS249Mazur;
     
    public abstract class Circle extends Shape
    {
    	public double radius;
     
    	public Circle(double theRadius)
    	{
    		super();
    		if(theRadius > 0)
    			radius = theRadius;
    		else
    		{
    			System.out.println("Fatal Error: Illegal Radius amount entered.");
    			System.exit(0);
    		}
    	}
     
    	public Circle(Circle original)
    	{
    		super(original);
    		radius = original.radius;
    	}
     
    	public double getArea()
    	{
    		return 3.141592*radius*radius;
    	}
     
    	public double getPerimeter()
    	{
    		return 2-3.141592*radius;
    	}
     
    	public double getRadius()
    	{
    		return this.radius;
    	}
     
    	public void setRadius(double r)
    	{
    		this.radius = r;
    	}
     
    	public String toString()
    	{
    		return (super.toString()+"\nRadius = "+this.radius);
    	}
     
    }

    Shape Demo program
    ~~~~~~~~~~~~~~~~~
     package CS249Mazur;
     
    public abstract class ShapeDemo
    {
    	Shape array[];
    	double rad=0;
     
    	//ask user for shape
    	//figure out what shape they entered is
    	//Based on what they entered ask for the parts needed
     
    	rad=5;
    	Circle o1;
    	array[0]=o1;
    	o1.setRadius(rad);
     
    	//move to next spot in the array
    	//and get the next shape to enter into the array
    }

    The demo program is not what I will be using for the actual program just something simple to test if my classes worked. I am getting an error on the line where i create the double rad. some kind of syntax error. saying: Syntax error on token ";", { expected after this token.

    Any and all help would be appreciated!
    Thanks!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with Abstract Extended Classes

    Please review the Announcement topic at the topic of each sub-forum to learn how to post your code in code or highlighting tags. What you've posted is hard to read.

    However, it was readable in the email version, where I saw that the ShapeDemo class has no methods. It has statements, but no methods, like a main() method from which to begin the program's execution.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Abstract Extended Classes

    Quote Originally Posted by GregBrannon View Post
    Please review the Announcement topic at the topic of each sub-forum to learn how to post your code in code or highlighting tags. What you've posted is hard to read.

    However, it was readable in the email version, where I saw that the ShapeDemo class has no methods. It has statements, but no methods, like a main() method from which to begin the program's execution.

    Doesn't the ShapeDemo Program gets its Methods from the Classes? Do i need some type of Include statement?

    If it helps, i am using Eclipse IDE to do my coding.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with Abstract Extended Classes

    The ShapeDemo class has statements - lines that are expected to execute, like:

    array[0]=o1;

    that must be inside a method.

    I would expect there to be a main() method somewhere, and I don't see one. Post your exact error messages, copied and pasted into a post, rather than your interpretation of them.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Abstract Extended Classes

    Oh wow, can't believe i forgot a main method. lol.
    So i Added that in and this is the new ShapeDemo class:

    package CS249Mazur;

    public abstract class ShapeDemo
    {
    	Shape array[];
    	double rad=0;
     
    	//ask user for shape
    	//figure out what shape they entered is
    	//Based on what they entered ask for the parts needed
    	 public void Main(String [ ] args)
    	 {
    		 rad=5;
    		 Circle o1;
    		 o1 = new Circle(rad);
    		 array[0]=o1;
    	 }
    	//move to next spot in array
    	//and get the next shape to enter into the array
    }

    The error i am getting now is "Cannot instantiate the type Circle", on the line "o1 = new Circle(rad);"
    I use eclipse IDE to run the program so that is how i get the error messages.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with Abstract Extended Classes

    Abstract classes cannot be instantiated. Does Circle need to be abstract? ShapeDemo shouldn't be abstract.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Abstract Extended Classes

    Quote Originally Posted by GregBrannon View Post
    Abstract classes cannot be instantiated. Does Circle need to be abstract? ShapeDemo shouldn't be abstract.
    I am new to Java so I am still learning. I am not sure what has to be labeled abstract or not. My assignment was to make an abstract class Shape. Does that me the Main shape classes that uses the getArea() method that is part of it's extended class is the only class that has to be labeled abstract?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with Abstract Extended Classes

    You should find reading this helpful. Most of the classes you'll write will be concrete (non-Abstract). When you're writing an Abstract class or an interface, you'll have specific reasons for doing so.

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Abstract Extended Classes

    This is where I am at with the program. Thanks to all your help i got it farther(my silly mistakes).

    package CS249Mazur;
     
    public class ShapeDemo
    {
     
     
     
    	//ask user for shape
    	//figure out what shape they entered is
    	//Based on what they entered ask for the parts needed
    	 public static void main(String [ ] args)
    	 {
    		 double rad=0;
    		 Shape array[] = null;
    		 rad=5;
    		 Circle o1;
    		 o1 = new Circle(rad);
    		 array[1]=o1;
     
    		 System.out.println(o1.toString());
    	 }
    	//move to next spot in array
    	//and get the next shape to enter into the array
    }

    Here is the error I am getting:
    Exception in thread "main" java.lang.NullPointerException
    at CS249Mazur.ShapeDemo.main(ShapeDemo.java:19)

    I am trying to make an array that can hold a bunch of Shapes that will be created. But I am having an issue with the Array part.

    Any Suggestions?

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with Abstract Extended Classes

    I would do it something like:

    Shape[] shapes = new Shape[NUMBER_OF_SHAPES];

    where NUMBER_OF_SHAPES is a previously defined constant. This:

    Shape shapes[], and this:

    Shape[] shapes

    are both allowed, but the second is preferred. I also believe it's easier to read as "an array of Shape objects, "shapes".

    That page I pointed you two is just one of thousands that cover topics like this one. You should practice finding and using those pages and gain confidence in using them to answer these kind of questions.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    jishrm (October 15th, 2013)

  12. #11
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help with Abstract Extended Classes

    Your error is coming from the line before that.
    Shape array[];

    The two brackets should come after the type, not the identifier and I do believe you have to instantiate arrays if I remember correctly.

  13. The Following User Says Thank You to Parranoia For This Useful Post:

    jishrm (October 17th, 2013)

  14. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,104
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with Abstract Extended Classes

    The two brackets should come after the type, not the identifier . . .
    I discussed that and noted that it's a preferred construction but either is acceptable.
    . . and I do believe you have to instantiate arrays if I remember correctly.
    Ya . . . I think I covered that too, if I understand your meaning.

Similar Threads

  1. Abstract Classes?
    By jean28 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2013, 07:13 AM
  2. what is the use of interfaces and abstract classes?
    By sagar474 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 18th, 2011, 03:34 PM
  3. [SOLVED] abstract classes, can you explain?
    By Scotty in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 5th, 2011, 08:01 PM
  4. Issue with abstract classes
    By zaphod2003 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2011, 02:25 AM
  5. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 07:15 AM

Tags for this Thread