Howdy. 

The reading assignment for today was Chapter 5: Methods. 

Methods are static and non-static. 

Static methods belong to a class. 

Static methods should be invoked through the class. 

Non-static methods are also called instance methods. 

Instance methods belong to the blueprint. 

Instance methods only exist in objects (once they are created). 

Instance methods should be invoked through object references. 

Basically we have the following situation: 

public class One {
  public static String greeting(String user) {
    return "Howdy, " + user + "!"; 
  }  
  public String goodbye(String user) {
    return "So long, " + user + "!"; 
  } 
}

public class Program {
  public static void main(String[] args) {
    System.out.println(  One.greeting("Leslie") ); 
    One a = new One(); // see the rule about constructors below 
    System.out.println( a ); 
    System.out.println( a.goodbye("Chris") ); 
  } 
}

Let's go back in time a bit:

public class One {

}

Not only there is something in this class (a blueprint) 

but there's a costructor in it too. 

The rule is as follows:

(a) If you have a class it has a blue print

(b) If you write no constructor the default no-arg constructor is there for you

What does this mean?

public class One {
  public One() {

  } 
}

That's what it means. If you write nothing inside you get this. 

(c) If you define at least one constructor there is no default. 

We have created (designed) data types Point, Line, Triangle. 

Let's look at some definitions existing already:

(a) Scanner (where is it defined: java.util)

(b) JFrame (defined in: javax.swing)

(c) ArrayList<...> (defined in: java.util)

The fundamental unit of compilation in Java is the package. 

A package is very much a folder, containing classes. 

We import classes not packages (e.g., import java.util.Scanner;)

The default package is the current folder and is nameless. 

The classes in java.lang and the default package need not be imported. 

public class Program {
  public static void main(String[] args) {
    System.out.println(  One.greeting("Leslie") ); 
    One a = new One(); // see the rule about constructors below 
    System.out.println( a ); // 
    One b = new One(); 
    System.out.println( b );     
    System.out.println( a.goodbye("Chris") ); 
  } 
}


This prints: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Program
Howdy, Leslie!
One@3fa1e2cf
One@20404c98
So long, Chris!


Regarding the predicates that come with defining a structure: 

public class Program {
  public static void main(String[] args) {
    One a = new One(); 
    Two b = new Two(); 
    if (a instanceof One) {
      System.out.println ("a is an object of type One"); // this is printed 
    } else {
      System.out.println ("a is not an object of type One");       
    }
    if (b instanceof Two) {
      System.out.println ("b is an object of type Two"); // and this one...
    } else {
      System.out.println ("b is not an object of type Two"); 
    }
  } 
}

This means not all hope is lost but there's more work to do. 

public class Two {
  
}

public class One {

}

Now let's create and print a Scanner. 

http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html

http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html

http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#hasNext()

http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#setVisible(boolean)

import java.util.*; 
import javax.swing.JFrame;

public class Program {
  public static void main(String[] args) {
    Scanner speckles = new Scanner(System.in); // Scanner class automatically imported 
    System.out.println( speckles );  
    // http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html
    JFrame frame = new JFrame(); 
    System.out.println( frame ); 
    // http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

    frame.setSize(200, 400); 
    frame.setVisible(true); 

  } 
}

This prints

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Program
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]
javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]


But what's more important it lets you interact with an actual JFrame. 

A Point is a pair of two int's. 

A Line is a pair of two Point's. 

A Triangle is a triplet of Line's (originally created from three Point's). 

A Circle is a center (location) with a radius. 

Define a class Circle with this model. 

Teach Circle objects to report their area. 

Email me what you have at dgerman@indiana.edu when you're done or at 12:45pm. 

Mark, Logan, Daniel, Jared, Austin, Trevor, 
Qin, Walter, Aleksa, Judy, Gabriela, Adam, Brennan, Jacquelyn
Nick, Yiming, Jingzhe, Zac, Paul, Morgan, Alex Ong, William
Jon, Grant, M. Alex R., Jack, James, Lauren 

See you in lab!

--

RMI 

Simulations

Greenfoot

Cracking the coding interview

Growing a language (Steele)