CSCI A201/A597 and I210

Lab Notes Three

Second semester 2000-2001


Starting chapter 3: An Introduction to Classes
Goals for this lab:

Today the minute paper and the in-lab assignment will be one and the same exercise. It is posted in

QuizSite (prefix L3_ followed by the section number)
and it will stay open Thursday through Sunday. You need to turn in your solution during this time.

Examples below are from the book, lecture notes, some (simple ones) are made up.


1. Lecture notes 3 contain an example like this:

public class One
{ public static void main(String[] args) 
  { Rectangle a = new Rectangle(5, 10, 20, 30); 
    System.out.println(a); 
    a.translate(15, 25); 
    System.out.println(a); 
  }
}
Put this in a file called One.java, compile and run.

As the notes say, your program won't compile. What's missing?

Fix the program, compile and run it.

Your output should look like this:

frilled.cs.indiana.edu%java One
java.awt.Rectangle[x=5,y=10,width=20,height=30]
java.awt.Rectangle[x=20,y=35,width=20,height=30]
frilled.cs.indiana.edu%
Did you obtain the same output?


2. (Also from lecture notes 3) Create a program:

import java.awt.Rectangle; 

public class Two 
{ public static void main(String[] args) 
  { Rectangle a = new Rectangle(5, 10, 20, 30); 
    Rectangle b = a; 
    a.translate(10, 10);
    b.translate(10, 10); 
    System.out.println(a);
  }
}
Place it into a file called Two.java, compile and run it.

I obtain the following output:

frilled.cs.indiana.edu%java Two
java.awt.Rectangle[x=25,y=30,width=20,height=30]
frilled.cs.indiana.edu%
What output do you obtain?

Now suppose that instead of printing a at the end we print b.

What would the output of the program be then, and why?


3. Check the documentation for class Rectangle and look up the intersection method.

The intersection method computes the intersection of two rectangles -- that is, the rectangle that is formed by two overlapping rectangles:

You call this method as follows:
Rectangle r3 = r1.intersection(r2);
Write a program that constructs two rectangle objects, prints them, and then prints their intersection. What happens when the two rectangles do not overlap?

(Note: this is problem P1.6 from the textbook).

When you are done check your solution against the program below:

/* Proposed solution for problem P1.6. Note that we test two
   cases, one in which the two rectangles overlap and one in
   which they don't. Read the documentation about rectangles
   with negative values for either width or height (or both)
   standing for empty sets. Note that a point is different
   from an empty set, even though it has no width or height
   it has a location. See the third test for that. 

   Bottom line is: is this correct or not? 

   Why, or why not? */ 

import java.awt.Rectangle;

public class Three {
    public static void main(String[] args) {
	Rectangle a = new Rectangle(0, 0, 10, 10); 
	Rectangle b = new Rectangle(5, 5, 10, 10); 
	Rectangle c = a.intersection(b); 
	System.out.println(a); 
	System.out.println("intersected with"); 
	System.out.println(b); 
	System.out.println("produces"); 
	System.out.println(c);   
        System.out.println("----------------------");  
 	Rectangle d = new Rectangle(10, 10, 10, 10); 
	Rectangle e = new Rectangle(50, 50, 50, 50); 
	Rectangle f = d.intersection(e); 
	System.out.println(d); 
	System.out.println("intersected with"); 
	System.out.println(e); 
	System.out.println("produces"); 
	System.out.println(f); 
        System.out.println("----------------------");  
	Rectangle g = new Rectangle(0, 0, 10, 10); 
	Rectangle h = new Rectangle(-10, -10, 10, 10); 
	Rectangle i = g.intersection(h); 
	System.out.println(g); 
	System.out.println("intersected with"); 
	System.out.println(h); 
	System.out.println("produces"); 
	System.out.println(i); 
        System.out.println("----------------------");  
    } 
}
What is the output that the program produces?

Use the program above or your solution to P1.6 to calculate the intersection of

  1. a square located at (0, 0) with a side of 10, and
  2. a square located at (-5, -5) with a side of 10.

Any square is also rectangle, so the two squares above can be created as follows

  1. new Rectangle(0, 0, 10, 10), and
  2. new Rectangle(-5, -5, 10, 10)

4. BigIntegers are like Rectangles, but they represent numbers.

Lecture notes 5 contain the following example:

import java.math.*;

public class Four
{ public static void main(String[] args)
  { BigInteger a = new BigInteger("100000000000000000000000000000000000000");
    BigInteger b = new BigInteger("200000000000000000000000000000000000000");
    BigInteger c = new BigInteger("300000000000000000000000000000000000000");
    BigInteger d = a.add(b.multiply(c)); 
    System.out.println(d); 
  }
}
Compile and run this program.

BigIntegers provide immutable arbitrary-precision arithmetic. That means numbers without limits. As big as you want. You will of course remember (from the very same lecture notes 5) that regular arithmetic has its limitation in Java. So one could use BigIntegers to avoid those limitations.

We will however use them to become familiar with object notation.

And to keep things simple let's use small numbers.

Here's how we calculate

1 + 2 * 3
The calculation is identical to the example presented before, only fewer 0's are present.

import java.math.*;

public class Four
{ public static void main(String[] args)
  { BigInteger a = new BigInteger("1");
    BigInteger b = new BigInteger("2");
    BigInteger c = new BigInteger("3");
    BigInteger d = a.add(b.multiply(c)); 
    System.out.println(d); 
  }
} 
Can you see how that's done?

Practice some more by using BigIntegers to calculate:


5. What is the output of this program?

public class Five {
    public static void main(String[] args) {
	String greeting = "Hello, Bill!"; 
        greeting.toLowerCase(); 
        System.out.println(greeting); 
    } 
} 
Please try to deduce the answer first.

Then run the program to double check your answer.


6. Modify just one line in the proram above to obtain the following output:

HELLO, BILL!
You can only modify one line only.


7. The following program does not compile. Why?

public class Seven {
    public static void main(String[] args) {
	int one = 2; 
	int two = 3; 
        int two = two + one; 
	System.out.println(two); 
    } 
} 

8. The following program does not compile. Why?

public class Eight {
    public static void main(String[] args) {
	int n; 
        int m; 
        n = 2; 
        m = m + 2; 
	System.out.println(m); 
    } 
} 

9. Lecture notes 6 define a class BankAccount

public class BankAccount
{ public void deposit(double amount)
  { balance = balance + amount; 
  }
  public void withdraw(double amount)
  { balance = balance - amount;    
  }
  public double getBalance() 
  { return balance; 
  }
  private double balance;
} 
A second class, called Experiment, shows how one could use it.

public class Experiment 
{ public static void main(String[] args) 
  { BankAccount a = new BankAccount(); 
    BankAccount b = new BankAccount();   
    a.deposit(200); 
    b.deposit(300); 
    System.out.println(a.getBalance()); 
    System.out.println(b.getBalance()); 
    a.withdraw(100); 
    b.withdraw(200); 
    System.out.println(a.getBalance()); 
    System.out.println(b.getBalance()); 
  }
}
Create two files Place the code above in the corresponding files, then compile and run Experiment.

Explain the output.

Then write your own experiment, which simulates the following hypothetical sequence of events:


10. Now change the previous Experiment to allow the user to specify the amounts:


Last updated: Jan 25, 2001 by Adrian German for A201