Search code examples
javaoopencapsulation

Encapsulation with Java


I'm trying to implement encapsulation in a program as part of some coursework however I've run into an error which I just can't seem to be able to fix with my limited knowledge which isn't helped by my Teacher/Lecturer who is very good at what he does however doesn't do very well when it actually comes to communicating the information, because of this could someone help me fix the error which is presented from the following program and explain to me why it's not working as intended.

class TwoDShapeEncap{
    double width, height;

    //Width
    void setWidth(double w){
        width = w;
    }

    double getWidth(){
        return width;
    }
    //Height
    void setHeight(double h){
        height = h;
    }

    double getHeight(){
        return height;
    }
}

class Triangle extends TwoDShapeEncap{
    String type;
    private double sideA, sideB, sideC, adjacent, opposite;

    //Side A
    void setsideA(double a){
        sideA = a;
    }
    double getsideA(){
        return sideA;
    }
    //Side B
    void setsideB(double b){
        sideB = b;
    }
    double getsideB(){
        return sideB;
    }
    //Side C
    void setsideC(double c){
        sideC = c;
    }
    double getsideC(){
        return sideC;
    }
    //Adjacent
    void setadjacent(double a){
        adjacent = a;
    }
    double getadjacent(){
        return adjacent;
    }
    //Opposite
    void setopposite(double o){
        width = o;
    }
    double getopposite(){
        return opposite;
    }

    double getPerimeter(){
        if(getsideB() == 0.0 && getsideC() == 0.0){
            type = "equilateral";
            return getsideA() * 3;
        }
        else if (getsideC() == 0.0){
            type  = "isosceles";
            return getsideA() + getsideB() * 2;
        }
        else{
            type = "scalene";
            return getsideA() + getsideB() + getsideC();
        }
    }


    //*******************************************************************************************
    //* Paste the perimeter() and hypotenuse() methods from your previous class into this class *
    //*******************************************************************************************

    //***************************************
    //* add an area method()into this class *
    //***************************************

    double area(double a, double b){
        getWidth();
        getHeight();
        return (getWidth() * getHeight()/2);
    }
}

class Rectangle extends TwoDShapeEncap{
    boolean issquare;

    private double height, width;

    //Height
    void setHeight(double h){
        height = h;
    }
    double getHeight(){
        return height;
    }
    //Width
    void setWidth(double w){
        width = w;
    }
    double getWidth(){
        return width;
    }

    double perimeter(double h, double w){
        getHeight();
        getWidth();
        return getHeight() * 2 + getWidth() * 2;
    }

    double area(double a, double b){
        //getWidth();
        //getHeight();
        return (getWidth() * getHeight()/2);
    }

    boolean testSquare(double h, double w){
        //getHeight();
        //getWidth();
        if (getHeight() == getWidth())
            issquare = true;
        else issquare = false;
        return issquare;
    }

    //*********************************************
    //* add area and perimeter methods this class *
    //*********************************************

    //*************************************************************************
    //* add a testSquare method to test if a particular rectangle is a square *
    //*************************************************************************

}

//Add a circle class which includes area and circumference methods

class Circle extends TwoDShapeEncap{
    double radius, diameter;

    double area (double r){
        radius = r;
        return Math.PI * (radius * radius);
    }

    double perimeter (double r){
        radius = r;
        return 2 * (Math.PI * radius);
    }
}

class TwoDShapeEncapDemoNew {
    public static void main(String args[]) {
    //Triangle
    Triangle t = new Triangle();
    t.setsideA(5.7);
        System.out.println("The perimeter is " + t.getPerimeter());
        System.out.println("If sideA is " + t.getsideA() );
        System.out.println("The type is " + t.type);
        System.out.println();
    t.setsideB(7.3);
        System.out.println("The perimeter is " + t.getPerimeter());
        System.out.println("If sideA is " + t.getsideA() );
        System.out.println("If sideB is " + t.getsideB() );
        System.out.println("The type is " + t.type);
        System.out.println();
    t.setsideC(2.7);
        System.out.println("The perimeter is " + t.getPerimeter());
        System.out.println("If sideA is " + t.getsideA());
        System.out.println("If sideB is " + t.getsideB());
        System.out.println("If sideC is " + t.getsideC());
        System.out.println("The type is " + t.type);
        System.out.println();
        //Rectangle
        Rectangle r = new Rectangle();
    r.setHeight(7.8);
    r.setWidth(4.2);
        System.out.println("The perimeter is " + r.perimeter());
        System.out.println("The");

    }
}

Error message:

Main.java:186: error: method perimeter in class Rectangle cannot be applied to given types; System.out.println("The perimeter is " + r.perimeter()); ^ required: double,double found: no arguments reason: actual and formal argument lists differ in length 1 error –


Solution

  • When you call:

    System.out.println("The perimeter is " + r.perimeter());
    

    in r.perimeter you must pass two parameters (as your signature wants)

    Your method in Rectangle class:

    double perimeter(double h, double w){
        getHeight();
        getWidth();
        return getHeight() * 2 + getWidth() * 2;
    }
    

    So fix:

    System.out.println("The perimeter is " + r.perimeter(r.getHeight(), r.getWidth()));
    

    You also can fix your method perimeter without parameters because in the body you use getHeigth() and getWidth() properties

    So you can write:

    double perimeter(){
        return getHeight() * 2 + getWidth() * 2;
    }