Define a class Circle. 

Define a class Rectangle. 

Your Rectangle class models rectangles with sides parallel to the axes. 

Teach your Circle objects to detect if they overlap with other Circles. 

Same for Rectangles. 

mahazuga: Mary Ann
serepate: Serena
hgarciah: Henri
balbakr: Bakr
ruifan: Rui
mzelenin: Matthew
jnp2: Jay
zeyang: Zejun
zhang486: Jingyun
                                                   yiwecao: Yiwei
 
rthammon: Ryan
wang686: Jiaxing
dweissma: Andrew
kevcao: Kevin
ssalmero: Salmeron, Santiago (TA)
luo23: Yawen
runxzhao: Runxia
dgerman: German, Dan-Adrian (Primary Instructor)
                                                   creba: Chris

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 12 + 21
33
> 123 + 321
444
> 1234 + 4321
5555
> 12345 + 54321
66666
> 123456 + 65432l
188888
> 12 + 2l
14
> 2l
2
> int a = 21
> a = 2l
Static Error: Bad types in assignment: from long to int
> a = (int) 2l
2
> double b = 2.3
> b = 1.4f
1.399999976158142
> float c = 2.3
Static Error: Bad types in assignment: from double to float
> float c = 2.3f
> c = 2.134
Static Error: Bad types in assignment: from double to float
> c = (float) 2.134
2.134
> c
2.134
> c = 2
2.0
> x
Static Error: Undefined name 'x'
> c
2.0
> a
2
> a = 2.3
Static Error: Bad types in assignment: from double to int
> a = (int) 2.3
2
> a
2
> a = (int) 123.456
123
> a
123


Here are some more experiments:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 'a'
'a'
> (int) 'a'
97
> 'a' + 0 
97
> 'a' + 1
98
> (char)('a' + 1)
'b'
> 'A' + 0
65
> ' ' + 0
32
> '0' + 0
48
> '5' - '0'
5
>

Exercise: take a String made of digits add them up. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> String a = "3425";
> for (int i = 0; i < a.length(); i = i + 1) 
    System.out.println( a.chartAt(i) - '0' );
Static Error: No method in String has name 'chartAt'
> for (int i = 0; i < a.length(); i = i + 1) 
    System.out.println( a.charAt(i) - '0' );
3
4
2
5
> int sum = 0; 
> for (int i = 0; i < a.length(); i = i + 1) 
    sum = sum + (a.charAt(i) - '0');
> sum
14


Let's now examine the following:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int i = 3
> i = i + 1
4
> i
4
> i += 1
5
> i
5
> i++
5
> i
6


Now another experiment:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int i = 7
> i++
7
> i
8
> ++i
9
> i
9
> i++ + 2
11
> i
10
> ++i + i++
22
> i
12


And from Summer of 2016 this example:

// compile then run java Two 12 
// instead of 12 put any number of circles you desire on the command line 

import javax.swing.*; 
import java.awt.*;
import java.util.*;
import java.awt.event.*; 

class Two extends JFrame {
  Two() {
    this.setVisible(true); 
    this.setSize(400, 500); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
  }
  public static void main(String[] args) {
    JFrame t = new Two(); 
    Three three = new Three(Integer.parseInt(args[0]));
    t.getContentPane().add( three ); 
  }
}

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x; 
    this.y = y; 
  }
}

class Circle{
  Point center;
  int radius;
  Color color; 
  Circle(Point a, int r, Color c) {
    this.center = a; 
    this.radius = r; 
    this.color = c; 
  } 
  void make(Color c) { this.color = c; } // [6] 
  void draw(Graphics g) {

    g.setColor(this.color); 
    g.fillOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 2 * this.radius); 
    g.setColor(Color.BLACK); 
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 2 * this.radius); 
  }
  void drawCircleOverlap(Graphics g) {

    g.setColor(Color.YELLOW); 
    g.fillOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 2 * this.radius); 
    g.setColor(Color.BLUE); 
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 2 * this.radius); 
  }
  boolean contains(Point where) {
    int dx = center.x; 
    int dy = center.y; 
    return this.distanceTo(where) < radius;
  }
  int distanceTo(Point where)
  {
    int dy = Math.abs(where.y - center.y);
    int dx = Math.abs(where.x - center.x);
    int distanceTo = (int) (Math.sqrt((dx * dx) + (dy * dy)));
   return distanceTo;    
  }
  void moveTo(Point where) {
    this.center = where; 
  } 
  public String toString() {
    return "Circle at " + this.center + " with radius " + this.radius; 
  }
}

class Three extends JComponent implements MouseListener, MouseMotionListener {
  public void mouseMoved(MouseEvent e) { } 
  public void mouseDragged(MouseEvent e) { 
    System.out.println( e.getX() + ", " + e.getY() );

    if (current != null) { 
      this.current.moveTo( new Point(e.getX(), e.getY()) );
      System.out.println( current ); 
      
      for(Circle r : this.shapes)
      {
        if(current.distanceTo(r.center) < current.radius + r.radius)
        {
          r.make(Color.YELLOW);       // [1]
          current.make(Color.YELLOW); // [2]
        } else {                      // [3]
          r.make(Color.WHITE);        // [4]
        }                             // [5]
      }
      repaint();
    }
  }
  public void mouseClicked(MouseEvent e) { } 

  Circle current;

  public void mousePressed(MouseEvent e) { 
    System.out.println( "Mouse pressed..." ); 
    Point mouse = new Point(e.getX(), e.getY()); 
    for (Circle r : this.shapes) {
      if (r.contains(mouse)) {
        this.current = r;
        break; 
      } 
    }
  } 
  public void mouseReleased(MouseEvent e) { 
    this.current = null; 
  } 
  public void mouseEntered(MouseEvent e) { } 
  public void mouseExited(MouseEvent e) { } 
  ArrayList<Circle> shapes; 
  Three(int size) {

    this.addMouseMotionListener( this ); 
    this.addMouseListener( this ); 

    this.shapes = new ArrayList<Circle>(); 
    for (int i = 0; i < size; i++) {
      Point where = new Point((int)(Math.random() * 300 + 50), 
                              (int)(Math.random() * 300 + 50)); 
      int radius = (int)(Math.random() * 50 + 20);
      Color color = new Color(255, 255, 255);
      this.shapes.add(new Circle(where, radius, color));
    } 
  }  
  public void paintComponent(Graphics g) {
    for (Circle r : this.shapes)
      r.draw( g ); 
  }
}

Compile and run it:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Two 12
Mouse pressed...
191, 231
Circle at Point@5e6dfab3 with radius 52
189, 230
Circle at Point@1539f30e with radius 52
183, 224
Circle at Point@28a02e23 with radius 52
149, 191
Circle at Point@74e270d4 with radius 52
144, 186
Circle at Point@66af1c3e with radius 52
126, 168
Circle at Point@2f266f9b with radius 52
125, 168
Circle at Point@77d7fe8e with radius 52
124, 168
Circle at Point@3a8231eb with radius 52
115, 168
Circle at Point@59c8de6c with radius 52
114, 168
Circle at Point@593f677 with radius 52
113, 168
Circle at Point@62d8adaf with radius 52
109, 168
Circle at Point@6a11410 with radius 52
108, 168
Circle at Point@49be52a2 with radius 52
108, 167
Circle at Point@48912b49 with radius 52
108, 166
Circle at Point@604b0029 with radius 52
109, 166
Circle at Point@6a1e2bb5 with radius 52
109, 164
Circle at Point@6f3286d5 with radius 52
Mouse pressed...
268, 267
Circle at Point@6933f23c with radius 57
268, 273
Circle at Point@2b96bc46 with radius 57
275, 305
Circle at Point@63fc3a5f with radius 57
277, 312
Circle at Point@6deca969 with radius 57
292, 347
Circle at Point@51720a88 with radius 57
293, 351
Circle at Point@1424c963 with radius 57
293, 353
Circle at Point@3f7391c3 with radius 57
296, 364
Circle at Point@2a8aa2a3 with radius 57
296, 365
Circle at Point@50046ec7 with radius 57
296, 366
Circle at Point@54024422 with radius 57
296, 367
Circle at Point@35a04372 with radius 57


public class Circle {
   
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Circle a = new Circle()
> a
Circle@75d2e863
> new Circle()
Circle@10eb699f
> a = new Circle()
Circle@57ff6e24
> a
Circle@57ff6e24


public class Circle {
  private Point center;
  private int radius; 
  public Circle(int x, int y, int r) {
    this.center = new Point(x, y); 
    this.radius = r;
  }
}

public class Point {
  private int x, y;
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Circle a = new Circle(2, 3, 10)
> a
Circle@39187531


public class Point extends java.lang.Object {
  private int x, y;
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Point a = new Point(2, 3)
> a
Point@2ee191b3
> a.toString()
"Point@2ee191b3"
> a
Point@2ee191b3
> System.out.println( a )
Point@2ee191b3


public class Point extends java.lang.Object {
  private int x, y;
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public String toString() {
    return "I am a Point (" + this.x + ", " + this.y + ")";  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Point a = new Point(2, 3)
> a.toString()
"I am a Point (2, 3)"
> a
I am a Point (2, 3)
> System.out.println( a )
I am a Point (2, 3)
> System.out.println( a.toString() )
I am a Point (2, 3)


https://www.cs.indiana.edu/classes/c212-dgerman/sum2018/older-exams/1013.jpg

public class Circle {
  private Point center;
  private int radius; 
  public Circle(int x, int y, int r) {
    this.center = new Point(x, y); 
    this.radius = r;
  }
  public boolean overlaps(Circle other) {
    if ( this.center.distanceTo(other.center) <= this.radius + other.radius )
      return true; 
    else 
      return false; 
  }
}

public class Point extends java.lang.Object {
  private int x, y;
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public String toString() {
    return "I am a Point (" + this.x + ", " + this.y + ")";  
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y;
    return Math.sqrt( dx * dx + dy * dy ); 
  }
}

Compile and run. Can't run. We need two main methods with unit testing code. 

See you in lab. 

--

http://www.javapuzzlers.com/