Howdy. We received a question about Homework Three. 

Here's how we get started:

import java.util.Scanner;

class Arrays {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] numbers; // declaration
    numbers = new int[0];
    System.out.println( /*java.util.*/Arrays.toString( numbers ) );
    System.out.print("type: ");
    String line = in.nextLine();
    while (! line.equals("bye")) {
      int number = Integer.parseInt( line );
      int[] temporary = /*java.util.*/Arrays.copyOf(numbers, numbers.length + 1);
      temporary[temporary.length - 1] = number;
      numbers = temporary;
      System.out.println( /*java.util.*/Arrays.toString( numbers ) );
      System.out.print("type: ");
      line = in.nextLine();
    }
    /*java.util.*/Arrays.sort( numbers );
    System.out.println( /*java.util.*/Arrays.toString( numbers ) );
  }
  // your code here ... 
}

You can't compile this file until you remove all the comments. 

import java.util.Scanner;

class Arrays {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] numbers; 
    numbers = new int[0];
    System.out.println( java.util.Arrays.toString( numbers ) );
    System.out.print("type: ");
    String line = in.nextLine();
    while (! line.equals("bye")) {
      int number = Integer.parseInt( line );
      int[] temporary = java.util.Arrays.copyOf(numbers, numbers.length + 1);
      temporary[temporary.length - 1] = number;
      numbers = temporary;
      System.out.println( java.util.Arrays.toString( numbers ) );
      System.out.print("type: ");
      line = in.nextLine();
    }
    java.util.Arrays.sort( numbers );
    System.out.println( java.util.Arrays.toString( numbers ) );
  }
  // your code here ... 
}

Now it compiles and runs. I run it like this:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Arrays
[]
type: 4
[4]
type: 3
[4, 3]
type: 5
[4, 3, 5]
type: 2
[4, 3, 5, 2]
type: 6
[4, 3, 5, 2, 6]
type: 1
[4, 3, 5, 2, 6, 1]
type: bye 
[1, 2, 3, 4, 5, 6]
>

So what do we have to do? 

Let me get started with toString(...) per your request. 

So I define one such method and use it:

import java.util.Scanner;

class Arrays {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] numbers; 
    numbers = new int[0];
    System.out.println( /*java.util.*/Arrays.toString( numbers ) );
    System.out.print("type: ");
    String line = in.nextLine();
    while (! line.equals("bye")) {
      int number = Integer.parseInt( line );
      int[] temporary = java.util.Arrays.copyOf(numbers, numbers.length + 1);
      temporary[temporary.length - 1] = number;
      numbers = temporary;
      System.out.println( /*java.util.*/Arrays.toString( numbers ) );
      System.out.print("type: ");
      line = in.nextLine();
    }
    java.util.Arrays.sort( numbers );
    System.out.println( /*java.util.*/Arrays.toString( numbers ) );
  }
  // your code here ... 
  public static String toString(int[] a) {
    String answer = ""; 
    for (int elem : a) {
      answer = answer + " " + elem;  
    }
    return "[" + answer + "]";  
  }  
}

I run this and it goes like this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Arrays
[]
type: 3
[ 3]
type: 4
[ 3 4]
type: 2
[ 3 4 2]
type: 7
[ 3 4 2 7]
type: 6
[ 3 4 2 7 6]
type: 1
[ 3 4 2 7 6 1]
type: 4
[ 3 4 2 7 6 1 4]
type: bye
[ 1 2 3 4 4 6 7]


What happened overall? 

public class Something {
  public static void main(String[] args) {
    System.out.println( Math.max( 4, 7 ) );  
  }
}

The answer: what if we change the name of the class to Math? 

public class Math {
  public static void main(String[] args) {
    System.out.println( Math.max( 4, 7 ) );  
  }
}

Now it doesn't compile unless we use this inside the print statement

                        java.lang.Math.max( 4, 7 ) 

Next: does this compile or not now? 

Now even this needs fix

public class Something {
  public static void main(String[] args) {
    System.out.println( java.lang.Math.max( 4, 7 ) );  
  }
}

Next we run Andrew:

import java.util.*;

public class Andrew {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);  
    String name = "Andy"; 
    if (args.length > 0 && args[0].equals("polite")) {
      name = "Mr. Wang";  
    }
    while (true) {
      System.out.print( name + " please type: "); 
      String line = s.nextLine();  
      
      if (line.equals("please stop")) {
        System.out.println("OK, you can go. Bye now."); 
        break; 
      } else {
        System.out.println( name + " typed \"" + line + "\"" );  
      }
      
    }
  }
}

Here's how it runs:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Andrew
Andy please type: hi there
Andy typed "hi there"
Andy please type: bye there
Andy typed "bye there"
Andy please type: wimbledon started today 
Andy typed "wimbledon started today"
Andy please type: argentina lost copa america centenario yesterday
Andy typed "argentina lost copa america centenario yesterday"
Andy please type: bye 
Andy typed "bye"
Andy please type: please stop
OK, you can go. Bye now.


We run it again with a special command line argument: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Andrew polite
Mr. Wang please type: please stop
OK, you can go. Bye now.
> run Andrew polite
Mr. Wang please type: today is Monday
Mr. Wang typed "today is Monday"
Mr. Wang please type: please stop
OK, you can go. Bye now.
> java Andrew 
Andy please type: please stop
OK, you can go. Bye now.


Now about Lab 05:

public class Point {
  
}

This already can be used:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> new Point()
Point@2698f792
> new Point()
Point@1b4fe6de
> new Point()
Point@51504453


Let's make this more complex:

public class Point {
  int x, y; 
  void talk() {
    System.out.println("There's a snake in my boot.");  
  }
}

This runs as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Point a = new Point()
> a
Point@31cfe044
> a.talk()
There's a snake in my boot.


public class Point {
  int x, y; 
  void talk() {
    System.out.println( "Hi.");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Point a = new Point()
> Point b = new Point()
> a
Point@5bac72f0
> b
Point@4cdc446e
> a.talk()
Hi.
> b.talk()
Hi.


public class Point {
  int x, y; 
  void talk() {
    System.out.println( this );  
  }
}


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Point a = new Point()
> Point b = new Point()
> a
Point@5e475dc2
> b
Point@63dca978
> a.talk()
Point@5e475dc2
> b.talk()
Point@63dca978


public class Point {
  int x, y; 
  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  void set(int x, int y) {
    this.x = x;
    this.y = y; 
  }
  void talk() {
    System.out.println( this );  
  }
  String report() {
    return "(" + this.x + ", " + this.y + ")";  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Point a = new Point()
Static Error: No constructor in Point matches this invocation
    Arguments: ()
    Expected return type: Point
    Candidate signatures: Point(int, int)
> Point a = new Point(3, 4)
> a.report()
"(3, 4)"
> a.set(2, -1)
> a.report()
"(2, -1)"


class One {
  int a = 0; // instance variable a
  int b = 0;
  void fun() { 
    /*this.*/b += 1;
    a += 1;
    System.out.print(a + " " + b + " ");
  }
  public static void main(String[] args) {
    One alpha = new One(); // create an object a and b inside are 0 
    alpha.fun(); // a and b inside alpha become 1 1 and that gets printed 
    alpha.fun();
    alpha.fun();
  }
}

The result is: 1 1 2 2 3 3 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
1 1 2 2 3 3 > 

See you tomorrow. 

--