Search code examples
javaoopmethodsgettersetter

Basic Question - Setting object's traits in a method and calling that method in the test/helper class - Java


I am learning very basic Object-oriented programming with Java, I have a triangle object called triangle1 and I set its values in my helper class, however, I want to use a setter and getter in my main class to change triangle1's values. I am just confused about how I call that method to show the updated values.

This is my main class where I have created my object of Triangle. Right under that I am trying to get the values and change them to 15.5,15.5,15.5, and green. I realize I am probably doing this wrongly.

public class Triangle extends codeAssignHelper{

    public double base;
    public double side1;
    public double side2;
    public String colour;


    Triangle(double base, double side1, double side2, String colour){
        this.base = base;
        this.side1 = side1;
        this.side2 = side2;
        this.colour = colour;
    }

    public void setBase(double base){
        this.base = 15.5;
    }

    public void setSide1(double side1){
        this.side1 = 15.5;
    }

    public void setSide2(double side2){
        this.side2 = 15.5;
    }

    public void setColour(String colour){
        this.colour = "green";
    }


    public double getBase(){
        return this.base;
    }
    public double getSide1(){
        return this.side1;
    }
    public double getSide2(){
        return this.side2;
    }
    public String getColour(){
        return this.colour;
    }


    public static void main(String[] args){


    }
}

This is my helper class and I have printed triangle1 and triangle2 and all I want is to reprint triangle1's values but updated. I realize it is probably not understanding I want to update triangle1 specifically.

class codeAssignHelper{

    public static void main(String[] args){

        Triangle triangle1 = new Triangle(1.0,1.0,1.0,"black");
        Triangle triangle2 = new Triangle(3.0, 4.0, 5.0, "red");


        System.out.println("Welcome to the Triangle tester!");
        System.out.println("-------------------------------------------------------");
        System.out.println("This program will instantiate objects of a triangle and test all of the methods of the Triangle class.");
        System.out.println("-------------------------------------------------------");
        System.out.println("For triangle1 length of base is:        " + triangle1.base);
        System.out.println("For triangle1 length of side 1 is:      " + triangle1.side1);
        System.out.println("For triangle1 length of side 2 is:      " + triangle1.side2);
        System.out.println("For triangle1 colour is:                " + triangle1.colour + "\n");

        System.out.println("Triangle2:");
        System.out.println("Base:       " + triangle2.base);
        System.out.println("Side1:      " + triangle2.side1);
        System.out.println("Side2:      " + triangle2.side2);
        System.out.println("Colour:     " + triangle2.colour + "\n");

    //MY PROBLEM - Need to update triangle1's values here
        System.out.println("Testing setter methods on triangle1.");
        System.out.println("Triangle1:");
        System.out.println("Base:       " + triangle1.getBase());
        System.out.println("Side1:      " + triangle2.getSide1());
        System.out.println("Side2:      " + triangle2.getSide2());
        System.out.println("Colour:     " + triangle2.getColour() + "\n");
    }
}

Expected output: Welcome to the Triangle tester!


This program will instantiate objects of a triangle and test all of the methods of the Triangle class.


For triangle1 length of base is: 1.0 For triangle1 length of side 1 is: 1.0 For triangle1 length of side 2 is: 1.0 For triangle1 colour is: black

Triangle2: Base: 3.0 Side1: 4.0 Side2: 5.0 Colour: red

Testing setter methods on triangle1. Triangle1: Base: 15.5 Side1: 15.5 Side2: 15.5 Colour: green


Solution

  • You simply need to call the setters like this, before printing the values out with the getters again:

    triangle1.setBase(15.5);
    triangle1.setBase(15.5);
    triangle1.setSide2(15.5);
    triangle1.setColour("green");
    

    I see that you have hardcoded the setters, to set specific values and ignoring the parameter. Maybe that was just for testing purposes, but it should be like this:

    public void setBase(double base){
            this.base = base;
        }
    

    same goes for the other setters of course.

    Lastly the data elements of the Class should be private, which is the reason you implement setters and getters in the first place, so only methods from the objects own class can mess with the values of its elements. So like this:

    private double base;
    private double side1;
    private double side2;
    private String colour;