void with a ConstructorthisHomework 9 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
Avoid harm
public class PrintPoint {
public static void main (String[] args){
Point8 p = new Point8();
p.set_location(3,4);
System.out.println("p1 is (" + p.get_x() + ", " + p.get_y() + ")");
}
}
$ java PrintPoint
p1 is (3, 4)
Point8@6d06d69c
int, double
and boolean
public String toString() {
STATEMENTS_THAT_RETURN_A_STRING;
}
(3,4)where the first value is the x coordinate and the second is the y coordinate
public String toString(){
return "(" + x + "," + y + ")";
}
public class Point9Driver {
public static void main (String[] args){
Point9 p = new Point9();
p.set_location(3,4);
System.out.println("p1 is (" + p.get_x() + ", " + p.get_y() + ")");
System.out.println(p);
}
}
$ java Point9Driver
p1 is (3, 4)
(3,4)
Point9 p = new Point9(); p.set_location(3,4);
Point9 p = new Point9(3,4)
new keyword to create an object
public CLASS_NAME(DATA_TYPE PARAMETER_NAME, ...) {
STATEMENT;
STATEMENT;
...
}
public followed by the name of the classreturn statement
public Point10(int x_init, int y_init) {
x = x_init;
y = y_init;
}
public class Point10Driver {
public static void main (String[] args){
Point10 p = new Point10(3,4);
System.out.println(p);
}
}
$ java Point10Driver
(3,4)
Point10 p = new Point10(3,4);three things happened
| Type | Value |
|---|---|
| int | 0 |
| double | 0.0 |
| char | '\0' |
| boolean | false |
| objects | null |
null is a special value used for variables that point to objectsnullnull has the same function that 0 has for numbersvoid with a Constructorvoid
you've created a problem that is very hard to spot
public class BadPoint {
int x;
int y;
public void BadPoint(int x_init, int y_init) {
x = x_init;
y = y_init;
}
}
$ javac BadPoint.java $ ls BadPoint.* BadPoint.class BadPoint.java
public class BadPointDriver {
public static void main (String[] args){
BadPoint p = new BadPoint(3,4);
System.out.println(p);
}
public String toString(){
return "(" + x + "," + y + ")";
}
}
$ javac BadPointDriver.java
BadPointDriver.java:3: error: constructor BadPoint in class BadPoint cannot be applied to given types;
BadPoint p = new BadPoint(3,4);
^
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
1 error
void
required: no arguments
found: int,int
reason: actual and formal argument lists differ in length
x = x_init; y = y_init;we write
int x = x_init; int y = y_init;
$ javac BadPoint2.java $ ls BadPoint2.* BadPoint2.class BadPoint2.java
public class BadPoint2Driver {
public static void main (String[] args){
BadPoint2 p = new BadPoint2(3,4);
System.out.println(p);
}
}
$ javac BadPoint2Driver.java $ ls BadPoint2Driver.* BadPoint2Driver.class BadPoint2Driver.java
$ java BadPoint2Driver (0,0)
this
public Point10(int x_init, int y_init) {
x = x_init;
y = y_init;
}
public Point10(int x, int y_init) {
x = x;
y = y;
}
thisthis is the implicit parameter
public Point11(int x, int y) {
this.x = x;
this.y = y;
}
public class Point11Driver {
public static void main (String[] args){
Point11 p = new Point11(3,4);
System.out.println(p);
}
}
$ java Point11Driver
(3,4)