/** This code is supposed to take the user input of the length
 and breadth of the room in one class, get the user input of the 
material and cost from the other class and calculate the total cost.*/

import java.util.Scanner;

//My first class
class RoomFlooring
{
private String FloorMaterial;
private double material_cost = 0;

public void setFloorMaterial(String FloorMaterial)
{
System.out.print('\u000C');

// Choosing the floor material
Scanner Floor = new Scanner(System.in);
System.out.println("Choose your floor material: FancyTile or NormalTile: ");

FloorMaterial = Floor.nextLine();
}

 public String getFloorMaterial()
 {
    return FloorMaterial;
}
 // determining the cost according to the floor materials
public void setMaterial_Cost(double material_cost)
 {
 if (FloorMaterial == "FancyTile")
{
    material_cost = 2;
}

else
{
    material_cost = 4;
}

}

public double getMaterial_Cost()
 {
 return material_cost;
}

}
// class to set and get the room dimensions from the user 
class RoomDimensions
{ 

private double length;
private double width;
// getting the length of the room
public void setLength(double length)
{
    Scanner room = new Scanner(System.in);
    System.out.println("Enter the length of the room: ");

    length = room.nextDouble(); 

}
// getting the width of the room
public void setWidth(double width)
{
    Scanner room = new Scanner(System.in);
    System.out.println("Enter the width of the room: ");

    width = room.nextDouble();

}

public double getLength()
{
    return length;
}

public double getWidth()
{
    return width;
}
// calculating the area of the room
public double getArea()
{
    return length*width;
}

}
//Public class that has the main method
public class PriceCalculator
{
 private RoomFlooring room_flooring ;
 private RoomDimensions room_dimensions ;
// passing the data of the above classes to this public class
     public PriceCalculator(RoomFlooring room_flooring, RoomDimensions     room_dimensions)
 {
         this.room_flooring = room_flooring;
     this.room_dimensions = room_dimensions; 
    }

public void main(String[] args)
{

    double final_cost;

  final_cost = room_flooring.getArea() * room_dimensions.getMaterial_Cost();

// I get my error on the above line of the code

    System.out.println(final_cost);
}
}

Recommended Answers

All 4 Replies

I wonder about line 107. room_flooring is called out but I can't find where that method is in that class.

Line 107
getArea is a method in the RoomDimension class, but you try to call it using an instance of RoomFlooring
(and vice-versa for the material costs)

ps
Line 102
your main method is declared as an instance method but it should be static

pps
Java conventions are to use camel case and no underscores for member names, eg roomDimensions, getMaterialCost

commented: If i make my main method static, it says : non-static variable room flooring cannot be referenced from a static context +0

That's right.
The only thing the PriceCalculator class does is to run a test case. You can put all that into the main method. There's no need to create an instance, or to have instance variables. Local variables in the main method will do.

commented: It still gives me the same error message when I put everything in my main method +0

If you really have put it all in the main method then you will have no non-static variables, so you cannot get the same message about non-static variables!. Let's see your latest version of the PriceCalculator class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.