Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Unable to calculate and display part of a class

$
0
0
Hi there! Been a lurker for quite some time, but I really need help tonight.
I'm fairly new to Java.
The objective of this program is to obtain the length and width of a room from the user.
Obtain the price per square foot.
Calculate and display the area of the room. (Up to this point I'm fine)
Calculate the total cost by multiplying area * price per square foot.

I've tried different variations of RoomCarpet.java, but it always seems to be that line that sets off an error.
RoomCarpet.java (This is the one I'm having issues with)
public class RoomCarpet{ 
private RoomDimension thisRoomDimension;

private double carpetUnitCost=8;

public void setRoomDimension(RoomDimension thisroomdimension) 
{
this.thisRoomDimension = thisroomdimension;
}

public double getTotal()
{
return thisRoomDimension.getArea() * carpetUnitCost; //This is the line I'm having trouble with
}

public String toString()
{
String str = "The total price is: " + getTotal();
return str;
}
}



The rest of the code if you need it for reference.

RoomDimension.java
public class RoomDimension
{
//Fields
private double rLength;
private double rWidth;

public RoomDimension(double length, double width)
{
rLength = length;
rWidth = width;
}

public RoomDimension() {


}

public double getArea()
{
return rLength * rWidth;
}

public String toString()
{
String str = "The area is: " + getArea();
return str;
}
}



Room.java
import java.util.Scanner;
import java.text.DecimalFormat;

public class Room
{
public static void main(String[] args)
{
//Fields
double length;
double width;


//Set Scanner
Scanner keyboard = new Scanner(System.in);

//Create format
DecimalFormat dollar = new DecimalFormat("#,##0.00");

//Ask user for statistics
System.out.println("What is the length of the room? (Square Feet)");
length = keyboard.nextDouble();

System.out.println("What is the width of the room? (Square Feet)");
width = keyboard.nextDouble();

//Create class objects
RoomDimension area = new RoomDimension(length, width);
RoomCarpet price = new RoomCarpet();

//Display total area and cost
System.out.println(area.toString());
System.out.println(price.toString());
}
}



Thanks!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>