This discussion is archived
1 2 Previous Next 26 Replies Latest reply: Feb 21, 2013 2:27 AM by 962826

# Geometric Object Class

Currently Being Moderated
Hi - a new assignment from a novice programmer. Here it is. My question isn't for the programming answer, but an approach on how to even start this. Thanks!

This problem is based on the GeometricObject class introduced in chapter 13.
Write a Java program to create an ArrayList holding Circle and Rectangle objects. The program will traverse the list adding the area of all objects in the collection. At the end, the program prints the grand total. Test your program with the sample disk data shown below.

This is the sample disk data:

Circle 1
Circle red true 1
Rectangle 10 20
Rectangle blue true 1 5

Hints:
1. Define an ArrayList<GeometricObject> object, call it list.
2. Define the abstract class GeometricObject and its subclasses Circle and Rectangle as indicated in chapter 13 (already discussed in class).
3. For each row in the input file
a. Create a new Circle or Rectangle object.
b. Add the new object to the list.
4. Traverse the list adding each object’s area to an accumulator
5. Print the total area

Observation:
Each line begins consists of several strings. The first string indicates the object’s type (Circle or
Rectangle). In the case of a circle the next token represents either its radius (in case it is numeric) or the
object’s color, followed by its ‘filled’ attribute and finally its radius. Similar strategy is used for Rectangle
objects.
• ###### 1. Re: Geometric Object Class
Currently Being Moderated
1. Define an ArrayList<GeometricObject> object, call it list.
2. Define the abstract class GeometricObject and its subclasses Circle and Rectangle as indicated in chapter 13 (already discussed in class).
3. For each row in the input file
a. Create a new Circle or Rectangle object.
b. Add the new object to the list.
4. Traverse the list adding each object’s area to an accumulator
5. Print the total area
Aside from the fact that steps 1 and 2 are in the wrong order, these hints seem straightforward enough. What is it you're having trouble with?
• ###### 2. Re: Geometric Object Class
Currently Being Moderated
Ok - questions upon starting:

I will have a package with four classes:

1. Driver
2. GeometricObject
3. Circle
4. Rectangle

Does the ArrayList<GeometricObject> go in the Driver file then?
• ###### 3. Re: Geometric Object Class
Currently Being Moderated
Second question - since the disk data has different results for each line, does this mean GeometricObject should just have a property of radius and the subclasses that extend GeometricObject (Circle and Rectangle) should have additional properties? But then how can one sample of Circle just show a radius and then the one below it show color, is filled, and radius?
• ###### 4. Re: Geometric Object Class
Currently Being Moderated
restless8052 wrote:
Does the ArrayList<GeometricObject> go in the Driver file then?
Well if those are the only classes you will have, you can easily see why it wouldn't fit into any of the other classes designwise.
Second question - since the disk data has different results for each line, does this mean GeometricObject should just have a property of radius
Does a rectangle have a radius?
and the subclasses that extend GeometricObject (Circle and Rectangle) should have additional properties? But then how can one sample of Circle just show a radius and then the one below it show color, is filled, and radius?
Think about this the other way around, what are common attributes with both Circle and Rectangle?
• ###### 5. Re: Geometric Object Class
Currently Being Moderated
Kayaman wrote:
restless8052 wrote:
Does the ArrayList<GeometricObject> go in the Driver file then?
Well if those are the only classes you will have, you can easily see why it wouldn't fit into any of the other classes designwise.
Second question - since the disk data has different results for each line, does this mean GeometricObject should just have a property of radius
Does a rectangle have a radius?
and the subclasses that extend GeometricObject (Circle and Rectangle) should have additional properties? But then how can one sample of Circle just show a radius and then the one below it show color, is filled, and radius?
Think about this the other way around, what are common attributes with both Circle and Rectangle?
Good point - no a rectangle does not have a radius. The only thing they both have in common out of the properties are the dimensions. See I told you I was a novice because I am stuck before I can even begin...

Another question I am having - How do I define and "ArrayList<GeometricObject> object, call it list" without knowing the total number of items in the array. Or, because the disk data is only showing 4 items, do i just create it as having 4 items?:
``GeometricObject[] list = new GeometricObject[4];``
• ###### 6. Re: Geometric Object Class
Currently Being Moderated
restless8052 wrote:
Good point - no a rectangle does not have a radius.
The only thing they both have in common out of the properties are the dimensions.
They can both have a color and filled/unfilled properties...
Another question I am having - How do I define and "ArrayList<GeometricObject> object, call it list" without knowing the total number of items in the array. Or, because the disk data is only showing 4 items, do i just create it as having 4 items?:
``GeometricObject[] list = new GeometricObject[4];``
An ArrayList is not an array, it grows as needed.
See http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html for information and usage (don't forget to import the class).
• ###### 7. Re: Geometric Object Class
Currently Being Moderated
Kayaman wrote:
restless8052 wrote:
Good point - no a rectangle does not have a radius.
The only thing they both have in common out of the properties are the dimensions.
They can both have a color and filled/unfilled properties...
Even if the disk data is not showing both color and filled/unfilled properties?

>
Another question I am having - How do I define and "ArrayList<GeometricObject> object, call it list" without knowing the total number of items in the array. Or, because the disk data is only showing 4 items, do i just create it as having 4 items?:
``GeometricObject[] list = new GeometricObject[4];``
An ArrayList is not an array, it grows as needed.
See http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html for information and usage (don't forget to import the class).
Thanks I will take a look at this today.
• ###### 8. Re: Geometric Object Class
Currently Being Moderated
restless8052 wrote:
Even if the disk data is not showing both color and filled/unfilled properties?
But they both can have those properties. If the disk data doesn't have them, they would be empty of course. But both Circle and Rectangle can have them, whereas both Circle and Rectangle can't have a radius.
• ###### 9. Re: Geometric Object Class
Currently Being Moderated
Hello I have the same assignment and I didn't understand how to Define an ArrayList<GeometricObject> object.
Please any hint would be appreciated it and I tried the link you have provided but I didn't help at all!!!
• ###### 10. Re: Geometric Object Class
Currently Being Moderated
How about a beginner's book on Java? Your problem is that you don't know the language properly, you need to learn more. You don't do that in a forum.
• ###### 11. Re: Geometric Object Class
Currently Being Moderated
Should I write as writing any array like
double[] ArrayList = {1, 10, 20}
knowing that
ArrayList[0] = 1;
Arraylist[1] = 10 ;
ArrayList[2] = 20;

... and use the numbers
• ###### 12. Re: Geometric Object Class
Currently Being Moderated
988565 wrote:
Should I ...
You should start your own forum thread instead of ripping focus away from the original poster.
You can easily paste a link to any and all other forum threads that you think might be similar or have relevance toward whatever you are asking.
• ###### 13. Re: Geometric Object Class
Currently Being Moderated
I am ok with sharing this thread if it is the same topic. Afterall, we are both looking for the same answers. Also - we already have Java books - this is our second class in the department structure. You are right, we do not know Java. It is a difficult language to learn, and we are using this forum as a place to ask questions and hopefully get answers or guidance. That's why it is posted in the "New to Java" category.
• ###### 14. Re: Geometric Object Class
Currently Being Moderated
Thank you! Actually I am still trying to do the Array! still no hope! I don't know which Array should I use. I hope we are going to be able to help each other. Since we are both still new to Java language.
Thanks you again.
1 2 Previous Next

#### Legend

• Correct Answers - 10 points
• Helpful Answers - 5 points