Coding Exercise 31: Wall Area

2021. 4. 6. 21:21Java programming

Write a class with the name Wall. The class needs two fields with name width and height of type double.

 

The class needs to have two constructors. The first constructor does not have any parameters. The second constructor has parameters width and height of type double and it needs to initialise the fields. In case the width is less than 0 it needs to set the width field value to 0, in case the height parameter is less than 0 it needs to set the height field value to 0.

 

Write the following methods :

 

* Method named getWidth without any parameters, it needs to return the value of width field.

 

* Method named getHeight without any parameters, it needs to return the value of height field.

 

* Method named setWidth with one parameter of type double, it needs to set the value of the width field. If the parameter is less than 0 it needs to set the width field value to 0.

 

* Method named setHeight with one parameter of type double, it needs to set the value of the height field. If the parameter is less than 0 it needs to set the height field value to 0.

 

* Method named getArea without any parameters it needs to return the area of the wall.

 

 

public class Wall {
    private double width;
    private double height;

    public Wall(){
    }

    public Wall(double width, double height){
        if (width < 0.0){
            this.width = 0;
        } else {
            this.width = width;
        }

        if (height < 0.0){
            this.height = 0;
        } else {
            this.height = height;
        }
    }

    public double getWidth (){
        return this.width;
    }

    public double getHeight (){
        return this.height;
    }

    public void setWidth (double width){
        if (width < 0.0){
            this.width = 0;
        } else {
            this.width = width;
        }
    }
    
    public void setHeight (double height){
        if (height < 0.0){
            this.height = 0;
        } else {
            this.height = height;
        }
    }
    
    public double getArea (){
        return this.width * this.height;
    }
}

 

 

A constructor in Java is a special method which is used to initialise objects as defualt values. For that reason, the constructor is called when an object of a class is newly created.

 

The several reasons why I called it a special method is as follow.

 

The constructor is slightly different from a method we've been using so far. It does not have any return value but we don't put 'void' but omit it when creating a constructor.

 

Another distinctive rule compared to a method is the name of a constructor must be same as its class.

 

Constructors fall into two different types, a default constructor that has no parameters and the other with parameters.

 

Every class has at least one constructor. If a constructor is not created by a user, a default constructor is automatically added by the complier without any further actions. Once a constructor is used, the default constructor will never be created.

 

The number of constructors of a class can be more than one, of course, since constructors can be overloaded meaning constructors with the same name but different parameters are used.

 

In this coding exercise, we've got two different constructors. One has no parameter on the line five named Wall and the other one with the same name has two parameters in double type on the line eight if I'm right.

 

Speaking of the constructors called Wall, if a user inputs values into the parameter width and height of Wall, the constructor, then those values will be defined as this.width and this.height respectively unless they are less than zero. Since a variable with 'this.' indicates an instance variable, the values are applied to private double width and private double height on the line two and three for each.

 

That's it for today.

'Java programming' 카테고리의 다른 글

Coding Exercise 33: Carpet Cost Calculator  (0) 2021.04.08
Coding Exercise 32: Point  (0) 2021.04.07
Coding Exercise 30: Person  (0) 2021.04.05
Coding Exercise 29: Sum Calculator  (0) 2021.04.04
Coding Exercise 28: Paint Job  (0) 2021.04.04