PrQ PrQ - 7 months ago 15
Java Question

Calling another Method from object-Java

I'm stumped. I created a method called "makeBed" in the (BedRoom) class that calls another method in the (Bed) class called "make". But when I try to call the method via bedroom.makeBed(); it does not work. I put a // in the part i'm trying to get to work.

Main

public class Main {
Wall wall1 = new Wall("West");
Wall wall2 = new Wall("East");
Wall wall3 = new Wall("South");
Wall wall4 = new Wall("North");

Ceiling ceiling = new Ceiling(12, 55);

Bed bed = new Bed("Modern", 4, 3, 2, 1);

Lamp lamp = new Lamp("classic", false, 75);

BedRoom bedroom = new BedRoom("test",wall1,wall2,wall3,wall4,ceiling,bed,lamp);

bedroom.makeBed(); //bedroom.makeBed(); does not work
//It does not show the public method "makeBed" in the BedRoom class
}


BedRoom Class

public class BedRoom {
private String name;
private Wall wall1;
private Wall wall2;
private Wall wall3;
private Wall wall4;
private Ceiling ceiling;
private Bed bed;
private Lamp lamp;

public BedRoom(String name, Wall wall1, Wall wall2, Wall wall3, Wall wall4, Ceiling ceiling, Bed bed, Lamp lamp) {
this.name = name;
this.wall1 = wall1;
this.wall2 = wall2;
this.wall3 = wall3;
this.wall4 = wall4;
this.ceiling = ceiling;
this.bed = bed;
this.lamp = lamp;
}

public Lamp getLamp(){
return this.lamp;
}

public void makeBed(){ //This is the method I'm trying to access
System.out.println("Bedroom -> Making bed");
bed.make();
}

}


Bed Class

public class Bed {
private String style;
private int pillows;
private int height;
private int sheets;
private int quilt;

public Bed(String style, int pillows, int height, int sheets, int quilt) {
this.style = style;
this.pillows = pillows;
this.height = height;
this.sheets = sheets;
this.quilt = quilt;
}

public void make(){
System.out.println("Bed -< Making"); //Method I'm trying to call
}

public String getStyle() {
return style;
}

public int getPillows() {
return pillows;
}

public int getHeight() {
return height;
}

public int getSheets() {
return sheets;
}

public int getQuilt() {
return quilt;
}
}

Answer

I want to say this is it... You don't have your code wrapped into a method so it not compiling right.

public class Main {
public static void main(String[] args) {
Wall wall1 = new Wall("West");
Wall wall2 = new Wall("East");
Wall wall3 = new Wall("South");
Wall wall4 = new Wall("North");

Ceiling ceiling = new Ceiling(12, 55);

Bed bed = new Bed("Modern", 4, 3, 2, 1);

Lamp lamp = new Lamp("classic", false, 75);

BedRoom bedroom = new BedRoom("test",wall1,wall2,wall3,wall4,ceiling,bed,lamp);

bedroom.makeBed(); //bedroom.makeBed(); does not work 
//It does not show the public method "makeBed" in the BedRoom class
}
}

Maybe that will work.

This is on the assumption that THIS is your class you want to initilize the whole script from, if not change public static void main(String[] args){} into public Main() {}