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 4 of 4

Thread: Trouble using aggregate classes

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble using aggregate classes

    Hey everyone, I'm pretty new with Java and am preparing for a class I'm taking later this summer. I've been doing a lot of self study and have been attempting to get as far as I can in some Java books by myself. Right now I'm working on a carpet calculator problem and I'm a bit stuck. I'm attempting to create a RoomDimension class to set the area of a room, then use a method from that class to establish an argument in a RoomCarpet class which calculates the total cost of of carpeting for the room.

    public class RoomCarpet {
     
        private double carpetCost;
        private double roomSize;
        private RoomDimension yourSize;
     
        // This is what I'm having trouble with 
     
        public RoomCarpet(double cost, double roomsize){ // I'm not sure if I should use double or a RoomDimension object as the parameter
     
            carpetCost = cost;
            yourSize = new RoomDimension(); // I'm not sure what to pass as arguments here
            roomSize = yourSize.getArea();
     
        }
    ...

    and here's the RoomDimension class:

    public class RoomDimension {
     
        private double length;
        private double width;
     
        public RoomDimension(double len, double w){
     
            length = len;
            width = w;
     
        }
     
        public RoomDimension(RoomDimension object2){
     
            length = object2.length;
            width = object2.width;
     
        }
     
        public double getArea(){
     
            return length * width;
     
        }
     
        public String toString(){
     
            String str;
     
            str = "This rooms area is " + getArea();
     
            return str;
        }
     
    }


    I'd appreciate any help I can get. Thanks!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble using aggregate classes

    You could always have both.

    Create a second one that will initialize your current object to the values of the parameter object.

    Use getters and setters, like setLength(), setWidth(), setCost(), etc. You do need a RoomDimension constructor with 0 arguments in order to do that. By default it makes one if there none but doesn't if there are any others.


    public RoomCarpet(double cost, double roomsize){ // I'm not sure if I should use double or a RoomDimension object as the parameter
     
            carpetCost = cost;
            yourSize = new RoomDimension(); // I'm not sure what to pass as arguments here
            roomSize = yourSize.getArea(); // Why are you doing that?  You're changing the parameter's value?  
     
        }
     
    public void setCost(double cost)
    {
    carpetCost = cost;
    }
     
    public double getCost()
    {
    return carpetCost;
    }
     
    public void setRoomSize(double roomSize)
    {
    this.roomSize = roomSize;  // this refers to the RoomCarpet object itself
     
    }
     
    public double getRoomSize()
    {
    return roomSize;
    }
     
    public RoomCarpet(RoomCarpet rc)
    {
    setCost(rc.getCost());
    setRoomSize(rc.getRoomSize());
     
     
    }
    Last edited by javapenguin; June 22nd, 2012 at 11:06 AM.

  3. #3
    Banned
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble using aggregate classes

    this is inheritance or not ?

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trouble using aggregate classes

    No it's not inheritance. If anything, it's composition (or aggregation) hence the thread title.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. New Programming Student, Having Trouble With Abstract Classes/Interfaces
    By bulx0001 in forum Object Oriented Programming
    Replies: 2
    Last Post: February 10th, 2012, 06:24 AM
  3. Hey guys, I'm having a little trouble working with multiple classes.
    By worsewicked in forum Object Oriented Programming
    Replies: 4
    Last Post: January 18th, 2012, 07:03 PM
  4. [SOLVED] having a little trouble
    By XSmoky in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 6th, 2011, 07:48 PM
  5. [SOLVED] Trouble accessing variables from other classes
    By Elementality in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 5th, 2011, 10:50 AM

Tags for this Thread