Java programs are made of classes. 

public class Program {
  
}

Welcome to DrJava.  Working directory is C:\Users\ccotten\Desktop
> Program a
> a = new Program()
Program@1dae7b55
> a
Program@1dae7b55


If a member is not static I need to instantiate the class to access it. 

public class Program {
  public void fun() {
    System.out.println("Howdy.");  
  }
}

Welcome to DrJava.  Working directory is C:\Users\ccotten\Desktop
> Program a = new Program()
> a.fun()
Howdy.


What if the member is static? 

public class Program {
  public static void fun() {
    System.out.println("Howdy.");  
  }
}

So the conclusion is:

  (a) access an instance member through a reference to that instance

  (b) access a static member through the class

Let's talk about the lab and homework for this week. 

Let's model a Point. 

A Point is a pair of two numbers (coordinates, x and y). 

Here's the unit test:

public class Point {
  // you design this

  public static void main(String[] args) {
    Point a = new Point(1, 1); 
    Point b = Point.origin; 
    System.out.println( a.distanceTo(b) ); // 1.4142... 
    Point c = new Point(); // that's a copy of the origin
    System.out.println( c.distanceTo(b) ); // 0 since they're both in origin
  }
}

Here's the answer:

public class Point {
  // you design this
  private int x, y;
  public Point() {
    this(0, 0); 
  }
  public Point(int x, int y) {
    this.x = x; 
    this.y = y; 
  }
  public static Point origin = new Point(0, 0); 
  public double distanceTo(Point other) {
    double distance; 
    double dx = this.x - other.x; 
    double dy = this.y - other.y; 
    distance = Math.sqrt( dx * dx + dy * dy ); 
    return distance; 
  }
  public static void main(String[] args) {
    Point a = new Point(1, 1);
    Point b = Point.origin;
    System.out.println( a.distanceTo(b) ); // 1.4142...
    Point c = new Point(); // that's a copy of the origin
    System.out.println( c.distanceTo(b) ); // 0 since they're both in origin
  }
}

Further exercises: define Line, Triangle, Circle... 

What's the easiest (simplest) way to calculate the area of a triangle 
starting from its vertices? Answer: use Hero's formula. 

https://en.wikipedia.org/wiki/Heron%27s_formula

Now I model a Circle:

import java.awt.Color; 
import java.awt.Graphics;

public class Circle {
  private int x, y; // coordinates of the center 
  private int radius; 
  Color color; 
  public Circle(int x, int y, int radius, Color c) {
    this.x = x; 
    this.y = y;
    this.radius = radius; 
    this.color = c;
  }
  public void draw(Graphics g) {
    g.setColor(this.color); 
    g.fillOval(this.x - this.radius, 
               this.y - this.radius, 
               this.radius * 2, 
               this.radius * 2);
    g.setColor(Color.BLACK); 
    g.drawOval(this.x - this.radius, 
               this.y - this.radius, 
               this.radius * 2, 
               this.radius * 2);
  }
}

How do I draw the circles? 

import javax.swing.JComponent; 
import javax.swing.JFrame; 
import java.awt.Color; 
import java.awt.Graphics; 

public class Circles extends JComponent {
  Circle a, b, c, d;   
  public Circles() {
    a = new Circle(100, 200, 50, new Color(255, 200, 120));
    b = new Circle(410, 300, 60, new Color(255,  60, 120));
    c = new Circle(200, 100, 40, new Color(155, 100, 220));
    d = new Circle(370, 400, 80, new Color( 55, 200, 120));
  }  
  public void paintComponent(Graphics g) {
    a.draw(g);
    b.draw(g);
    c.draw(g);
    d.draw(g);    
  }
  public static void main(String[] args) {
    JFrame f = new JFrame();  
    f.setVisible(true); 
    f.setSize(500, 500); 
    f.add(new Circles()); 
  }
}

So this is both as your lab and your homework. 

--

Lab 02 wang608 present

Sirui Song attendance lab 02 Rui Feng Zheng and Jun Wang

Lab 01 cmsozio just one

Yichen Rong perfect attendance 

jbushupa lab02 attendance (sick) 

https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html#fillRect-int-int-int-int-

https://en.wikipedia.org/wiki/Heron%27s_formula

https://www.cs.indiana.edu/classes/c212-dgerman/fall2017/lab01.html

https://www.cs.indiana.edu/classes/c212-dgerman/fall2017/lab03.html

http://silo.cs.indiana.edu:8346/cgi-bin/fall2017/schedule

--