Wed next week exam. 

Review Mon. 

Lab, Homework for this week posted. 

Let's assume we have Point.java with Point the class.

public class Circle {
  private Point center;
  private int radius;  
  public Circle(int x, int y, int radius) {
    this.center = new Point(x, y);
    this.radius = radius; 
  }
  public Point(Point center, int radius) {
    this.center = center; 
    this.radius = radius; 
  }
  public boolean overlaps(Circle theOther) {
    double distance = this.center.distanceTo(theOther.center);
    if (distance <= this.radius + theOther.radius) 
      return true; 
    else
      return false; 
  } 
  public static void main(String[] args) {
    Circle a, b; 
    a = new Circle(Point.origin, 10); 
    b = new Circle(4, 4, 4);   
    System.out.println( a.overlap(b) ); // what do you think? 
  }
}

Can you simplify overlaps? 

Second let's ask the same questions for

  Rectangle objects

  that have the sides parallel with the axes

Introduction 

Using Objects

Implementing Classes

Values, Types, Expressions, Statements

Decisions (nested decisions are common)

Loops (nested loops)

Arrays and ArrayLists (simple data structures, maybe multidimensional)

Solve the following problem:

  program prompts user for values

  reads them one at a time

  user ends data entry with "done" 

  program reports sum, count and average of all data

// Program.java
import java.util.Scanner; 

public class Program {
  public static void main(String[] args) {
    System.out.print("Type: "); 
    Scanner in = new Scanner(System.in); 
    String line = in.nextLine();
    int sum = 0, count = 0; 
    while ( ! line.equals( "done" ) ) {

      int num = Integer.parseInt( line ); 
      sum += num; 
      count += 1; 
      System.out.println( sum + " " + count + " " + (double) sum / count ); 

      System.out.print("Type: "); 
      line = in.nextLine();

    } 
    in.close(); 
    System.out.println("Thank you, come again!"); 
  } 
}

Here's how this runs in DrJava: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Program
Type:  [DrJava Input Box]
Thank you, come again!
> run Program
Type:  [DrJava Input Box]
3 1 3.0
Type:  [DrJava Input Box]
4 2 2.0
Type:  [DrJava Input Box]
12 3 4.0
Type:  [DrJava Input Box]
Thank you, come again!
> run Program
Type:  [DrJava Input Box]
1 1 1.0
Type:  [DrJava Input Box]
3 2 1.5
Type:  [DrJava Input Box]
6 3 2.0
Type:  [DrJava Input Box]
10 4 2.5
Type:  [DrJava Input Box]
15 5 3.0
Type:  [DrJava Input Box]
21 6 3.5
Type:  [DrJava Input Box]
33 7 4.714285714285714
Type:  [DrJava Input Box]
Thank you, come again!


Book suggests to create DataSet objects like Students ... 

I want to write a program that reads a positive number from the user

Then it should report the square root of the number

You can't use Math.sqrt(...) or Math.pow(..., 0.5) or any Math method.

import java.util.Scanner; 

public class Calculation {
  public static void main(String[] args) {
    System.out.println("Type: "); 
    Scanner in = new Scanner(System.in);
    String line = in.nextLine(); 
    double num = Double.parseDouble(line);
    // your code here 
  }
}
    low                                               high
     0------------------------+-------------------------6
    low                     high
     0-----------+------------3
                low         high 
                1.5-----+-----3
                      2.25

Here's the code:

import java.util.Scanner; 

public class Calculation {
  public static void main(String[] args) {
    System.out.print("Type: "); 
    Scanner in = new Scanner(System.in);
    String line = in.nextLine(); 
    double num = Double.parseDouble(line);
    double low = 0; 
    double high = num;
    double middle = (high + low) / 2; 
    while ( high - low >= 0.000000001 ) {
      if (middle * middle > num) {
        high = middle;
      } else if (middle * middle < num) {
        low = middle; 
      } else {
        break;  
      }    
      middle = (high + low) / 2;
    }
    System.out.println( middle * middle + " " + num );
    System.out.println( middle + " " + Math.sqrt( num ) ); 
  }
}

artames 


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Calculation
Type:  [DrJava Input Box]
6.000000000409257 6.0
2.4494897428667173 2.449489742783178