adplus-dvertising

Why is one object returning null for assignment and the other is not?

Asked 2 days ago
Viewed 11 times

I have gone over this program again and again not sure why but even after debugging it doesn't make sense to me as to why the assignment of colour is not working in my Circle Class. I am learning about simple interfaces and this project is to simply demonstrate my understanding of it. However, in the process, my objects of class Circle are not returning the assigned colour and only storing as null. I have done the same process and code for the class Square and it has no issues storing the assigned value of colour for those objects. If someone can see and or explain where I have gone wrong it would be much appreciated. I showed it to my teacher in class and they also were unsure as to why the program was having issues with it.

Tester/main Class

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
 */

/**
 *
 * @author stephaniebrandon
 */
public class Lab2Tester {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

    Circle circle1 = new Circle(0.5, "blue");
    Circle circle2 = new Circle(0.2, "red");
    Square square1 = new Square(1, "blue");
    Square square2 = new Square(1.5, "green");
    Circle circle3 = new Circle(1, "green");
    Square square3 = new Square(1, "green");
    Square square4 = new Square(2, "blue");
    Circle circle4 = new Circle(1, "blue");
    Square square5 = new Square(0.5, "red");
    
    System.out.println(circle1.toString());
    System.out.println(circle2.toString());
    System.out.println(circle3.toString());
    System.out.println(circle4.toString());
    System.out.println(square1.toString());
    System.out.println(square2.toString());
    System.out.println(square3.toString());
    System.out.println(square4.toString());
    System.out.println(square5.toString());
    
    
    }
    
}

Shape Interface

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */

/**
 *
 * @author stephaniebrandon
 */
interface Shape {
    
    public String getColour();
    public double getArea();
    
}

Square Class that implements Shape interface

 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */

/**
 *
 * @author stephaniebrandon
 */
class Square implements Shape{
    
    public double side;
    public String colour;
    public double area;
    
    //Constructor
    Square(double side, String colour){
        this.side = side;
        this.colour = colour;
    }
    
    public double getSide(){
        return side;
    }
    public void setSide(double side){
        this.side = side;
    }
    @Override 
        public String getColour(){
        return colour;
    }
    public void setColour(String colour){
        this.colour = colour;
    }
    
    @Override
    public double getArea(){
        return area += (side * side);
    }
    @Override 
        public String toString(){
        return "The square's toatl area is: " + getArea() + ", with the colour: " + getColour();
    }
}

Circle class that implements Shape interface

 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */

/**
 *
 * @author stephaniebrandon
 */
class Circle implements Shape {
    
    public double radius;
    public String colour;
    public double area;
    
    //Constructor
    Circle(double radius, String newColour){
        this.radius = radius;
        this.colour = colour;
    }
    
    public double getRadius() {
        return radius;
    }
    
    public void setRadius(){
        this.radius = radius;
    }
    
    @Override
    public String getColour(){
        return colour;
    }
    public void setColour(String colour){
        this.colour = colour;
    }
    @Override 
    public double getArea(){
        return area += (3.14 * radius * radius);
    }
    @Override
    public String toString(){
        return "The circle's toatl area is: " + getArea() + ", with the colour: " + getColour();
    }
}

asked 2 days ago

Correct Answer

Instead of this

Circle(double radius, String newColour){
        this.radius = radius;
        this.colour = colour; // <-- you're assigning colour (null) to itself.
}

You should be doing this

Circle(double radius, String newColour){
        this.radius = radius;
        this.colour = newColour;
}

Or rename the parameter passed to constructor

Circle(double radius, String color){
        this.radius = radius;
        this.colour = colour; // Now it refers to parameter
}
answered 2 days ago