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.
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;
}
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;
}
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;
}
thisthis is the implicit parameter
public Point11(int x, int y) {
this.x = x;
this.y = y;
}
public class Point2{
int x;
int y;
public void translate(int dx, int dy) {
x += dx;
y += dy;
}
}
public class Point2Driver{
public static void main(String[] args) {
Point2 p = new Point2();
p.x = 7;
p.y = 2;
System.out.println("p is (" + p.x + ", " + p.y + ")");
p.translate(-2, 3);
System.out.println("p is (" + p.x + ", " + p.y + ")");
}
}
$ java Point2Driver
p is (7, 2)
p is (5, 5)
public class VarScope {
public static void main (String[] args){
int sum = 0;
for (int i = 0; i < 5; i++) {
}
System.out.println("sum: " + sum);
}
}
$ java VarScope
sum: 5
for loop outside the loop
public class VarScope2 {
public static void main (String[] args){
for (int i = 0; i < 5; i++) {
System.out.println("i: " + i);
}
System.out.println("i: " + i);
}
}
$ javac VarScope2.java
VarScope2.java:6: error: cannot find symbol
System.out.println("i: " + i);
^
symbol: variable i
location: class VarScope2
1 error
public class VarScope3 {
public static void main (String[] args){
int var = 0;
{
var += 1;
System.out.println("var: " + var);
{
var += 2;
System.out.println("var: " + var);
}
}
System.out.println("var: " + var);
}
}
$ java VarScope3
var: 1
var: 3
var: 3
public class MethodScope {
public static void main (String[] args){
method_1();
}
public static void method_1(){
method_2();
}
public static void method_2(){
System.out.println("Hello from method_2");
}
}
$ java MethodScope
Hello from method_2
public class VarScope4 {
public static void main (String[] args){
for (int i = 0; i < 5; i++) {
int sum = 5;
System.out.println("sum: " + sum);
}
System.out.println("sum: " + sum);
}
}
$ javac VarScope4.java
^
1 error
public class Greeting {
String greeting = "Hello";
public void say_something(){
String greeting = "Goodbye";
System.out.println(greeting);
}
}
$ javac Greeting.java $ ls Greeting.* Greeting.class Greeting.java
public class GreetingDriver {
public static void main (String[] args){
Greeting g = new Greeting();
g.say_something();
}
}
$ java GreetingDriver
Goodbye
this to access the object's fields
public class Greeting2 {
String greeting = "Hello";
public void say_something(){
String greeting = "Goodbye";
System.out.println(this.greeting);
System.out.println(greeting);
}
}
public class Greeting2Driver {
public static void main (String[] args){
Greeting2 g = new Greeting2();
g.say_something();
}
}
$ java Greeting2Driver.java
Hello
Goodbye
static DATA_TYPE VARIABLE_NAME;
public class Sale {
s
public Sale(){
number_sold += 1;
}
public int sales(){
return number_sold;
}
}
public class SaleDriver {
public static void main (String[] args){
Sale s1 = new Sale();
System.out.println(s1.sales());
Sale s2 = new Sale();
System.out.println(s2.sales());
Sale s3 = new Sale();
System.out.println(s3.sales());
}
}
$ java SaleDriver
1
2
3
public static final DATA_TYPE VARIABLE_NAME = VALUE;
public class DayOfWeek {
public static final int days_in_week = 7;
String day_name;
public DayOfWeek(String name){
day_name = name;
}
public String toString(){
return day_name + " is one of the " + days_in_week + " days in the week";
}
}
public class DayOfWeekDriver {
public static void main (String[] args){
DayOfWeek mon = new DayOfWeek("Monday");
System.out.println(mon);
DayOfWeek tsd = new DayOfWeek("Tuesday");
System.out.println(tsd);
DayOfWeek wed = new DayOfWeek("Wednesday");
System.out.println(wed);
}
}
$ java DayOfWeekDriver
Monday is one of the 7 days in the week
Tuesday is one of the 7 days in the week
Wednesday is one of the 7 days in the week
public Point12(){
x = 0;
y = 0;
}
public Point12(int x, int y) {
this.x = x;
this.y = y;
}
public class Point12Driver {
public static void main (String[] args){
Point12 p1 = new Point12();
System.out.println(p1);
Point12 p2 = new Point12(3,4);
System.out.println(p2);
}
}
$ java Point12Driver
(0,0)
(3,4)
Point12() Point12(int x, int y)
public void set_location(){
x = 0;
y = 0;
}
public void set_location(int x, int y){
this.x = x;
this.y = y;
}
public class Point13Driver {
public static void main (String[] args){
Point13 p = new Point13();
System.out.println(p);
p.set_location(3,4);
System.out.println(p);
p.set_location();
System.out.println(p);
}
}
$ java Point13Driver
(0,0)
(3,4)
(0,0)