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

Thread: Carpet Calculator Problem using aggregation

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Carpet Calculator Problem using aggregation

    Hi this is what i am supposed to do:
    RoomDimension: First, you should create a class named RoomDimension that has two fields: one for the length of the room, and one for the width. The RoomDimension class should have a method that returns the area of the room.

    RoomCarpet : Next you should create a RoomCarpet class that has a RoomDimension object as a field. It should also have a field for the cost of the carpet per square foot. The RoomCarpet class should have a method that returns the total cost of the carpet. Use the UML diagram in figure 9-21 (pg. 599) as a guide to creating these classes.

    RoomPriceDemo : Finally, you will create a RoomPriceDemo application program that asks the user to enter:
    • the dimensions of a room
    • and the price per square foot of the desired carpet
    Your program should then calculate and display the total cost of the carpet.

    here is my code:
    HTML Code:
    package Program_5;
    
    public class RoomDimensions {
    
    private int roomLength=0;
    private int roomWidth=0;
    
    public void setRoomLength(int roomlength) {
    this.roomLength = roomlength;
    }
    
    public void setRoomWidth(int roomwidth) {
    this.roomWidth = roomwidth;
    }
    
    public RoomDimensions() {
    
    
    }
    
    public int getRoomArea() {
    
    
    return roomLength*roomWidth;
    }
    
    }
    Other class
    HTML Code:
    package Program_5;
    
    public class RoomCarpet{ 
    private RoomDimensions thisRoomDimension;
    
    final int carpetUnitCost=8;
    
    public void setRoomDimension(RoomDimensions thisroomdimension) {
    this.thisRoomDimension = thisroomdimension;
    
    }
    
    public int getTotalAMount()
    {
    
    return thisRoomDimension.getRoomArea()* carpetUnitCost;
    
    }
    
    
    }
    Here is my demo:
    HTML Code:
    package Program_5;
    import java.util.Scanner;
    public class RoomPriceDemo {
    
    	
    	public static void main(String[] args) {
    		int roomLength;
    		int roomWidth;
    		//Create Scanner object
    		Scanner keyboard= new Scanner(System.in);
    		System.out.println("Enter The Length Of The Room");
    		roomLength= keyboard.nextInt();
    
    		System.out.println("Enter The Width Of The Room");
    		roomWidth = keyboard.nextInt();
    
    		RoomDimensions roomd = new RoomDimensions();
    		roomd.setRoomLength(roomLength);
    		roomd.setRoomWidth(roomWidth);
    		//instantiating carpet class
    		RoomCarpet room1 = new RoomCarpet();
    		//function call to set dimensions
    		room1.setRoomDimension(roomd);
    		//displaying data
    		System.out.println("Room dimensions: ");
    		System.out.println("Length: " + roomLength + " Width: " + roomWidth + 
    		"Area: " + room1.getRoomArea());
    		System.out.println("Carpet cost:$" + room1.getTotalAMount());
    
    	}
    
    }
    my problem is my room1.getRoomArea()); print statement is not working. everything else seems correct but when i try and run it it gives me an error at the room1.getRoomArea()): method in the System.out.println is underlined red and that is the only problem i have and cant figure out how to fix it


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Carpet Calculator Problem using aggregation

    What is the actual error that the code is giving you?
    Quote Originally Posted by gpelefty90
    method in the System.out.println is underlined red
    Not very useful..

    As a note, you should have a constructors in your RoomDimension class that allows you to begin with giving values to the length and width, as shown below.

    public RoomDimensions(int length, int width)
    {
      roomLength = length;
      roomWidth = width;
    }

    That way for creating a RoomDimensions, you can just
    RoomDimensions aRoomDimension = new RoomDimension(length, width);
    removing the unnecessary method calls. Also, it would be better if your RoomDimensions class was not pluralized..

    You need to be precise about your errors! Generally questions get general answers. Also, highlight your code with Java tags not HTML.

    Recommended Links:
    Java Style Guide: Naming Conventions
    Java Style Guide: Commenting
    Be Precise
    Last edited by Tjstretch; November 1st, 2011 at 02:34 PM.

Similar Threads

  1. Calculator layout problem
    By Choiseymitsu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 08:04 AM
  2. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  3. Aggregation
    By pokuri in forum Object Oriented Programming
    Replies: 1
    Last Post: January 19th, 2011, 08:24 AM
  4. [SOLVED] Another Calculator Problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 4th, 2010, 03:31 PM
  5. Calculator problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 4th, 2010, 01:01 PM