aspose file tools*
The moose likes Beginning Java and the fly likes array of objects confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array of objects confusion" Watch "array of objects confusion" New topic
Author

array of objects confusion

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have started on this project that calculated the carbon foot print of a family. i have been working on it for a few days now. for the sake of not having to explain what i am trying to do i will simply post the instructions.

5. The program should be written in OOP
format using a tester class.
6. For the data structure, use either an array of
objects or an ArrayList.
7. Create a minimum of five different objects.
8. Your CO2 footprint should account for the
following:
• annual estimate of gasoline used
• annual estimate of electricity used
• annual household waste produced
• annual household waste recycled
• replacement of incandescent bulbs
9. The constructor should include the following parameters:
• annual gasoline used
• average electricity bill and average electricity price
• number of people in home
• recycle paper, plastic, glass, or cans (Booleans)
• number of light bulbs replaced


alright. well i am trying to do that. i have done every thing i know to do, and now im posting as a last resort. im not 100% sure what all my problems are. im aware that they are there though. if anyone can help me figure out what to do to make it work correctly i would greatly appreciate it.



Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

What is the application doing or not doing that you want to change?

Is it compiling?

There's a much easier way to do all that array business.... and keep all the information together. You might want to create a new Family class.... then make a List of Families.


When you do things right, people won't be sure you've done anything at all.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
well i already have two classes. no it definitly doesnt compile. it says i need a constructor. i dont know how to make one. i have worked as far as i kow how to solve.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

I think you might need a new class, Family.

Each family object would have its own information that you could access from an array.... am I making any sense?

http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html

Eric Mission
Greenhorn

Joined: Apr 22, 2009
Posts: 22
I agree with Janeice, I would have made an abstract or concrete class that contains the getters and setters, followed by child classes that would extend from that. Then in my tester class I would create an object that simply calls those methods.

You have too many concepts going on in one class and it becomes confusing. You could do all of this with only 1 (one) for loop.

For example-






Hopefully that will help some. This is the basic concept of OO programming and is extremely powerful in helping a computer scientist in solving real world problems.


to infinity and beyond
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

Here's the key:
9. The constructor should include the following parameters:
• annual gasoline used
• average electricity bill and average electricity price
• number of people in home
• recycle paper, plastic, glass, or cans (Booleans)
• number of light bulbs replaced


So the constructor method will have a declaration like this:
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2
Assuming the assignment requires that you use the supplied CO2FootprintTester your I believe constructor in CO2Footprint should look something like this:

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

I think the OP wrote that tester class....

Why couldn't he make a List of families:



And then iterate through it for the CO2 footprint calculations?




[EDIT]
As for the original code:

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
alright,

thank you guys for all of your valuable feedback. i just have one more thing to ask. i just got an update on the assignment from my teacher and it seems that i may have been making things harder than needed.

2. All of the arrays you have as input into the constructor CO2FootPrint should be primitives: int nbrPeople, double, avgElectBill, etc.
3. I have not gone into detail look at your methods but a cursory look seems you are in the right direction but are trying to use the array parameters. Use simple primitives.

You see, CO2FootPrint is an object that knows its elements that goes into its carbon footprint and can return any value needed concerning its footprint. You create it with its basic knowledge and then it can calculate all of its emmission and waste data for whoever is interested using the getters.


now im really confused because what i thought i had to be doing i changed. can someone give me some feed back on what changes i might need to make to get this back on track?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

I think he's saying each object has primitives as its components (instance variables, and method parameters).

The array part should be an array enclosing each instance of the CO2Footprint class (what I've been calling Family this whole time).

Right now, you're calculating ALL the family's footprints at the same time (by passing an array of values to the method). You should make it so only one calculation is done at a time (by passing only one value at a time).... then if you need more you can run the method again on the next instance in the array of objects.

Work smarter not harder. Make the JVM do the work for you.
nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120
i have been working and i have used you guys advice. i have my first class compiling with no errors, but my second is still a work in progress. im getting a .class expected error on

here is my completed class



and this is the one thats giving me alot of trouble. im still pretty new to objects and im trying to get through my first actual program using them. if someone would help me troubleshoot it i would greatly appreciate it. this code can be overwhelming.

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

You're getting there!

Here's some suggestions to fix what you have:
  • Your instance variables should be private NOT public
  • You should create "setter" and "getter" methods for each of the private instance variables. ex. setPlastic() and getPlastic()


  • Why do you have for loops in each method? The loop should be in the main() method.... I think you're making this harder on yourself -- we may not have been clear.

    The ARRAY holds the CO2Footprint Objects. Once you do that, the easy part is running the calculations on each individual value. :-)

    For example:



    One more thing, why do you have the constructor for the CO2Footprint in both classes? The constructor should only be in the class that its name matches.





















    nathan gibson
    Ranch Hand

    Joined: Sep 16, 2009
    Posts: 120
    i took the constructor out of the tester class, but now when i try to compile i get cannot find symbol - constructor CO2Footprint()

    any ideas?
    Janeice DelVecchio
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1691
        
      12

    Found your problem.

    You made a constructor, but you're not using it....



    Doesn't match the constructor requirements/parameters for:


    It should look like this:



    And the constructor should look like this:



    One other thing to remember.... when you make a constructor that takes parameters, if you WANT a no parameter constructor, you need to make one. The JVM only creates a no parameter constructor if you don't create ANY constructors.

    A little clearer now?
    nathan gibson
    Ranch Hand

    Joined: Sep 16, 2009
    Posts: 120
    i got both of my classes to compile without errors. thank you so much for the help. i wish you could feel my excitement. thankyou for the help once agian.
    Janeice DelVecchio
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1691
        
      12

    YAY!!! Good for you!!



     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: array of objects confusion