public class Rectangle {
	private double length;
	private double breadth;

    public void Rectangle(){}
    
    public Rectangle(double length, double breadth){    	this.length = length;
    	this.breadth = breadth;
    }
    
    public void setLength(double length){
    	this.length = length;
    }
    public double getLength(){
    	return length;
    }
    
    public void setBreadth(double breadth){
    	this.breadth = breadth;
    } 
    public double getBreadth(){
    	return breadth;
    }
    
    public double calcArea(){
    	return length * breadth;
    }
    
    public String toString(){
    	return "Area of rectangle is " + calcArea();
    }
}
public class RectangleTest {

    public static void main(String[] args) {
    	Rectangle rtg = new Rectangle(10, 5);
    	System.out.println(rtg.toString());
    }
}

here is my problem, if i put "void" on the green colour statement,
the RectangleTest cannot be execute due to some problems that i don't know,
if i remove the void is can be execute smoothly already, i want to know why?
what is the differences between with void and without void?

The line highlighted in green is your "constructor" definition. Constructors are special methods in the sense that they don't have a return type and you are not allowed to provide one. The name of the constructor should be exactly the same as the name of your class or put in a different way, any method which has the same name as that of the class becomes a constructor and the above mentioned rules apply.

For regular methods, "void" signifies that the method need not or doesn't return anything.

class Box {

    private int mangoCount;

    // Constructor for the "Box" class
    public Box(int count) {
        this.mangoCount = count;
        // notice the lack of return; this special method is automatically called when you invoke
        // "new Box(12)" and is responsible for creating a new "Box" object.
    }

    public int getMangoesInTheBox() {
        // failure to return an "int" from this method raises a compile time error
        return mangoCount;
    }

    public void setMangoesInBox(int mangoes) {
        // returning a "value" from this method raises a compile time error though
        // you can always have a blank/free standing return
        mangoCount = mangoes;
        return; // OPTIONAL
    }

}

so what should i do in testing file if i still want to put void into the green color statement (question required)

public void Rectangle(double length, double breadth)

You can't. Either change the name of the method to something other than Rectangle or change the name of the class. Though it completely eludes me why you would want to have "void" as return for a constructor...

Edited 5 Years Ago by ~s.o.s~: n/a

Your test method includes the perfectly reasonable line

Rectangle rtg = new Rectangle(10, 5);

this will look for a constructor of the Rectangle class that takes two numbers as parameters, and that's exactly what this is:

public Rectangle(double length, double breadth){    	
        this.length = length;
    	this.breadth = breadth;
    }

Like ~s.o.s~ said, you cannot use the void keyword in defining a constructor. So this

public void Rectangle(){}

is almost certainly wrong.
Why do you want to put void into those lines anyway?

because the tutorial question is require to put a "void" type in this Rectangle method. i just follow the order XD

...which implies two things: either the tutorial is wrong or your interpretation is wrong. :-)

If you need more help, you might want to copy-paste the tutorial question here (assuming you are allowed to) so that we have more context.

then i post tutorial question here, i scare i go for wrong thinking, but not negative thinking. XD

Class name
Rectangle
Private members (instance variables)
length : double
breadth : double
Default constructor, initializes length and breadth to zeroes
Rectangle() : void
Overloaded constructor, initializes length and breadth using incoming arguments
Rectangle (double, double) : void
Accessors, returning the private data members
getLength(): double
getbreadth(): double
Mutators, changing values of the private data members
setLength(double): void
setBreadth(double): void
Other methods, computes and returns areas of rectangle etc
calcArea(): double
toString() : String

Overloaded constructor, initializes length and breadth using incoming arguments
Rectangle (double, double) : void

This is a simple error in the tutorial question itself. The ": void" should not be there. You need to discuss this with your instructor, because it's their mistake.

The tutorial question seems to be using "UMLish" notation for laying down the requirements/specification but doesn't take into consideration the fact that Constructors don't actually return anything. A constructor would be denoted by something like:

==========================================================
| Rectangle                                                |
==========================================================
- length: double
- breadth: double
-----------------------------------------------------------
+ <<constructor>> Rectangle()
+ <<constructor>> Rectangle(length:double, breadth: double)
+ getLength(): double
+ getBreadth(): double
-----------------------------------------------------------

Anyways, to conclude, just ignore it as a typo and carry/move forward with your solution.

This article has been dead for over six months. Start a new discussion instead.