Java 學習記錄 33 — 代碼挑戰 10

張小雄
4 min readFeb 1, 2021

--

題目:

Directions:

this is an exercise in Class Composition. to complete the exercise, you must create five classes with associated member variables and methods. The five classes should be created as follows:

Create a class and name it Lamp. Inside this class should be declared three member variables: style of type String, battery of type boolean, and globRating of type int. All variables should be marked private. A constructor needs to be created which accepts the three member variables as parameters.

In addition, four methods should also be created: turnOn() has no return type and should print a message that the lamp is being turned on; getStyle() returns the lamp style; isBattery returns the value of battery; and getGlobRating() returns the globRating of the lamp.

Create a class and name it Bed. Five private member variables should be declared: style of type String; and pillows, height, sheets, quilt of type int. A constructor should be coded which accepts these five member variables as parameters.

Also, six methods should be defined: make() has no return type and prints a message to the effect that the bed is being made; getStyle() which returns the value of style; getPilows() returns the number of pillows; getHeight() returns the height of the bed; getSheets() returns the number of sheets on the bed; and getQuilt() returns the value of quilt.

  1. Create a class with the name Ceiling. There are two member variables for this one, height, and paintedColor, both of type int. there should also be a constructor which accepts both member variables as parameters. there are also two additional methods which should be defined: getheight() shall return the value of height and getPaintedColor(0 should return the value of paintedColor.
  2. Create a class with the name Wall. It contains one member variable, direction, and is of type String. A constructor for Wall should accept one parameter for the member variable direction. A getter should also be defined for the direction field called getDirection().
  3. Create a class with the name Bedroom. This class contains eight member variables: name of type String; wall1, wall2, wall3, wall4 of type Wall; ceiling of type Ceiling; bed of type Bed, and a lamp of type Lamp. The class constructor should accept all eight of the member variables as parameters, and there should also be two additional methods: getLamp() which returns an object of type Lamp, and makeBed() which prints a message that the bed is being made and also calls the make() method in the Bed class.

Input/Output:

Once you have completed coding your classes you should then use the following code in your main class to test you code and for correct output. This way you can be sure that your code works before pasting your five classes into the code evaluator.

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("YOUR NAME HERE", wall1, wall2, wall3, wall4, ceiling, bed, lamp);bedRoom.makeBed();bedRoom.getLamp().turnOn();

Tips:

Remember that after testing you will not put your main method into the code evaluator. you will only paste in your five classes you have created in the exercise.

To be sure that the correct output is generated so your code passes the evaluation, use the following statements in your code.:

  1. System.out.println(“Bedroom → Making bed | “); should be used in the makeBed() method of the Bedroom class
  2. System.out.println(“Bed → Making | “); should be used in the make() method of the Bed class; and
  3. System.out.println(“Lamp → Turning on”); should be used in the turnOn() method of the Lamp class.

參考答案

ChallengeBedroom

public class ChallengeBedroom {
private String name;
private ChallengeWall wall1;
private ChallengeWall wall2;
private ChallengeWall wall3;
private ChallengeWall wall4;
private ChallengeCeiling ceiling;
private ChallengeBed bed;
private ChallengeLamp lamp;
public ChallengeBedroom(String name, ChallengeWall wall1, ChallengeWall wall2, ChallengeWall wall3, ChallengeWall wall4, ChallengeCeiling ceiling, ChallengeBed bed, ChallengeLamp 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 ChallengeLamp getLamp() {return this.lamp;} public void makeBed(){
System.out.println("Bedroom -> Making bed");
bed.make();
}
}

ChallengeBed

public class ChallengeBed {    private String style;
private int pillows;
private int height;
private int sheets;
private int quilt;
public ChallengeBed(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");
}
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;
}
}

ChallengeCeiling

public class ChallengeCeiling {
private int height;
private int paintedColor;
public ChallengeCeiling(int height, int paintedColor) {
this.height = height;
this.paintedColor = paintedColor;
}
public int getHeight() {
return height;
}
public int getPaintedColor() {
return paintedColor;
}
}

ChallengeLamp

public class ChallengeLamp {
private String style;
private boolean battery;
private int globRating;
public ChallengeLamp(String style, boolean battery, int globRating) {
this.style = style;
this.battery = battery;
this.globRating = globRating;
}
public void turnOn() {
System.out.println("Lamp -> Turning on");
}
public String getStyle() {
return style;
}
public boolean isBattery() {
return battery;
}
public int getGlobRating() {
return globRating;
}
}

ChallengeWall

public class ChallengeWall {
private String direction;
public ChallengeWall(String direction) {
this.direction = direction;
}
public String getDirection() {
return direction;
}
}

--

--

張小雄
張小雄

Written by 張小雄

記錄成為軟體工程師的過程

No responses yet