1-5. Here's the program:

import java.math.BigDecimal; 

public class One {
  public static void main(String[] args) {
    BigDecimal one = new BigDecimal(1); 
    BigDecimal two = new BigDecimal("2"); // both ways of initialization work
    System.out.println( one.add(two) );                                          // [ 1] 
    System.out.println( two.multiply(new BigDecimal(3)) );                       // [ 2] 
    BigDecimal // might as well name these, for easier reuse 
      three = new BigDecimal(3), 
      four = new BigDecimal(4),
      five = new BigDecimal(5); 
    System.out.println( (one.subtract(two.subtract(three.subtract(four)))) );    // [ 3] 
                       // feels like (- 1 (- 2 (- 3 4))) doesn't it? 
    System.out.println( ((one.subtract(two)).subtract(three)).subtract(four) );  // [ 4] 
    System.out.println( (two.multiply(three)).add(four.multiply(five)) );        // [ 5] 
  }
}

Here's what the program prints when run:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\6w2c212a592exam01
> run One
3
6
-2
-8
26


6-16. Here are the answers:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\6w2c212a592exam01
> "\n\t\\\"".length()                                                            // [ 6] 
4
> "tomato".charAt(2) - 'n'                                                       // [ 7] 
-1
> (char)('a' + 3)                                                                // [ 8] 
'd'
> 13 % 7                                                                         // [ 9] 
6
> 5 % 7                                                                          // [10] 
5
> 13 / 7                                                                         // [11] 
1
> 5 / 7                                                                          // [12] 
0
> false && false || true                                                         // [13] 
true
> false && (false || true)                                                       // [14] 
false
> 2 / 3 * 3                                                                      // [15]
0
> 3 * 2 / 3                                                                      // [16]
2


17. b 

18. true

19. b = (n != 0); 

20. b = (n < 3) || (n > 5); 

Since n is an int 

    b = (n <= 3) || (n => 5); 

would simplifies further to: b = (n != 4); 

But 
    because we have < and > 
    instead of <= and => 

we can't simplify b = (n < 3) || (n > 5); further.

Here's another attempt: 

    b = (""+n).matches("[345]");

Or this one:

    b = ((n-3)*(n-4)*(n-5) == 0); 

Or even 

    b = (n*n - 8*n + 15 > 0); 

21. b = (n != 0); 

22. !b

23. n == 4 

Remember n is an int. 

24. n > 5

25. false 

No number satisfies that condition. 

26. First it becomes: (n > 3) || (n > 5) 

This simplifies further to: n > 3

27. b = 0; // this is the answer I was looking for

However this was a mistake, I meant to write int a = ++n - n++;

If you said the statement won't compile you will get a point too. 

28. 2 

Proof:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\6w2c212a592exam01
> int x = 18, y = 10; if (x < 10) { if (x > 5) y = 1; } else y = 2;

> y
2


Explanation: this exercise was discussed in lab on Wed 06/29. 

29. 10

Proof:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\6w2c212a592exam01
> int x = 18, y = 10; if (x < 10) if (x > 5) y = 1; else y = 2;

> y
10


Explanation: as discussed in lab this is a typical example of a dangling else.

30. 10

Proof: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\6w2c212a592exam01
>  int y = 6; y = --y + y--;

> y
10


Explanation: 

(a) y is decremented to 5, 
(b) then 5 is added to 5, giving 10 
(c) and y is decremented to 4
(d) however that is shortlived since 10 replaces it in y 

Again, we discussed this in class.

31.   6
32.  12
33.  38
34. 115  
35.   0

Proof: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\6w2c212a592exam01
> run One
6
12
38
115
0


36. Infinite loop, i starts as 0 then becomes 3, 6, 9, 12, ... 

37. i = i++ does not modify i so: infinite loop. Be careful!

Use i++ or i += 1 or i = i + 1 if you want to increase i by 1. 

i = ++i is not recommended but would work, while i = i++ is a bad idea.

38. i starts as 10, then becomes 7, 4, 1, -2 and the loop stops. 

Clearly not an infinite loop.

39. The ; between the condition and { creates an empty body for the while. 

If the while loop has an empty body i will never change. 

Since it starts as 10 and it never changes: infinite loop (i always > 0). 

40. Here's a little bit more than you bargained for: 

// This is Rectangles.java with the main method (compile and run it).

import javax.swing.JFrame; 
import java.awt.Graphics;
import java.awt.Container; 

public class Rectangles extends JFrame {
  public Rectangles() {
    Container c = this.getContentPane(); 
    c.add(new Screen()); 
  }
  public static void main(String[] args) {
    Rectangles a = new Rectangles(); 
    a.setSize(400, 400); 
    a.setVisible(true); 
    // a.setDefaultCloseOperation( 3 ); 
    // ask me about the line above 
  }
}

// This is Screen.java keep it in the same folder with Rectangles.java 

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

public class Screen extends JComponent {
  Rectangle a, b;
  public Screen() {
    this.a = new Rectangle(10, 10, 10, 10); 
    this.b = new Rectangle(5, 5, 3, 3); 
    if (a.overlaps(b)) {
      System.out.println("The two rectangles overlap!");  
    } else {
      System.out.println("The two rectangles do not overlap!");  
    }
  } 
  public void paintComponent(Graphics g) {
    this.a.draw(g); 
    this.b.draw(g); 
  }
}

// This is Rectangle.java our model for rectangles

import java.awt.Graphics; 

public class Rectangle {
  public static boolean overlaps(Rectangle a, Rectangle b) {
    return a.overlaps(b); // decided to not implement it twice
  }
  int x, y, // bottom left corner 
      width, height; // how far the rectangle extends east and north 
  public Rectangle(int x, int y, int w, int h) {
    this.x = x;
    this.y = y; 
    this.width = w;
    this.height = h; 
  }
  // that's the answer that I wanted
  // it's exactly the one developed in lab
  // as Levi stated in class this method is correct
  public boolean overlaps(Rectangle another) {
    return ! ( another.y - another.height > this.y ||
              // this condition says the another is entirely to our North 
               another.y < this.y - this.height || 
              // completely to the South 
              another.x > this.x + this.width || 
              // entirely to the East of us
              another.x + another.width < this.x
              // entirely to our West
             );     
  }
  public void draw(Graphics g) {
    g.drawRect(this.x, this.y, this.width, this.height);  
  }
}

Compile and run this to see how drawing is done and if the method works. 

41. Here's an older final exam program. 

This solution belongs to Ryan Rozanski: 

// 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 ); 
  }
}

Run this to see how overlap kicks in. 

The answer I was expecting: 

boolean overlaps(Circle other) {
  return this.radius + other.radius >= 
         Math.sqrt( Math.pow(this.x - other.x, 2) + 
                    Math.pow(this.y - other.y, 2)
                  ); 
}

--