1 2 Previous Next 26 Replies Latest reply: Feb 21, 2013 4:27 AM by 962826

# Geometric Object Class

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
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
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
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
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?
• ###### 5. Re: Geometric Object Class
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?
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
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
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
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
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
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
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
988565 wrote:
Should I ...