// I kind of pass the task, but I have completely no idea what I have done
// Did I miss any element from the task?
/* if I type:
public Rectangle(int left, int width){ // case A
this.left = left;
this.width = width;
}
does it presents to the system the same as:
public Rectangle(int left, int top){ // case B
this.left = left;
this.top = top;
}
since when I key in said Rectangle(2, 2), basically system could only entertain either case A or case B */
// so ultimately, I just need to make 4 constructors with Rectangle(int i), Rectangle (int i, int ii), Rectangle (int i, int ii, int iii), & finally Rectangle (int i, int ii, int iii, int iv)?
// or the task wants to teach us something else?
// the passed code is also pasted for reference as below:
package com.codegym.task.task05.task0520;
/*
Create a Rectangle class
*/
public class Rectangle {
//write your code here
int top; int left; int width; int height;
public static void main(String[] args) {
}
public Rectangle(int left, int width){
this.left = left;
this.width = width;
}
public Rectangle(int left){
this.left = left;
}
public Rectangle(int left, int width, int height){
this.left = left;
this.width = width;
this.height = height;
}
public Rectangle(int left, int width, int height, int top){
this.left = left;
this.width = width;
this.height = height;
this.top = top;
}
}
Cannot get the point of this task
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
24 November 2020, 16:01useful
It looks like you figured it out. Constructors (and methods) have what is known as a signature. The signature would be the parameters (and also the name for methods). It is important to note that the parameters are specifically the variable type and not what you name the variable. Take, as an example, where you have a Person class. If you tried to make constructors such as:
(String address, String Name)
(String Name, String address)
The code would fail. Even though name and address are two different things to (most) humans, the computer only sees (String, String), and both of those are exactly the same to the computer.
Remember
- You can not have two identical signatures in the same class with constructors and methods
- The signature of a constructor is the parameters, and the signature of methods are the method name and parameters
- The only thing the computer cares about is the variable type, what you name the variable is at your discretion only.
*tip* always use meaningful names when naming variables. Even though the computer doesn't care what names you use, your future self or anyone else tasked with maintaining your code does. If you write obfuscated code, at least add something like this to the notes:
programming humor
+2
Misiu
24 November 2020, 15:24useful
The task is about overloading constructors (methods).
Constructors (methods) may have the same name. But they may not to have the same parameters in the same order.
Case A and case B are the same for a compiler. It sees them as:
Rectangle (int, int)
And compiler says: error.
There is another constructor suggested in the task:
Rectangle(Rectangle rectangle)
+2