CSCI A202 - Introduction to Programming (II)

Lecture 10: Classes and Objects (II)

We are going to continue from lecture notes 9.

Things to remember:

Things that we will do today: Here's the code for Fraction that we developed in class today.

I am including a complete Unix session in which I:

Here's the complete session:
tucotuco.cs.indiana.edu% vi Test.java
tucotuco.cs.indiana.edu% cat Test.java
class Fraction {
  public int num, den; 
  Fraction (int num, int den) { 
    this.num = num; 
    this.den = den; 
  }   
  public Fraction add (Fraction anotherFraction) {
    return new Fraction(num * anotherFraction.den + 
                        den * anotherFraction.num, 
                        den * anotherFraction.den); 
  } 
  public String toString () {
    return "(" + num + "/" + den + ") ";  
  } 
  public static void main(String[] args) {
    Fraction f = new Fraction(1, 3); 
    Fraction g = new Fraction(1, 2); 
    Fraction h = f.add(g); 
    System.out.println( f + " + " + g + " = " + h); 
  } 
} 
tucotuco.cs.indiana.edu% ls -l
total 2
-rw-r--r--   1 dgerman  students     598 Feb 11 14:39 Test.java
tucotuco.cs.indiana.edu% javac Test.java
tucotuco.cs.indiana.edu% ls -l
total 6
-rw-r--r--   1 dgerman  students    1180 Feb 11 14:39 Fraction.class
-rw-r--r--   1 dgerman  students     598 Feb 11 14:39 Test.java
tucotuco.cs.indiana.edu% java Fraction 
(1/3)  + (1/2)  = (5/6) 
tucotuco.cs.indiana.edu%
Note that Your solution to problem 10 on the assignment should check for proper denominators and use gcd to reduce their fractions to their lowest terms. gcd is explained on page 149 in your textbook. Try to implement it if you have everything else working in your Fraction class.

Here's now the other code that we wrote when we discussed main, again in a complete Unix session. The steps are similar to the ones above, in that we first create three source files (this time we use the same names as the classes themselves) and compile them all. Then we can start with any main method: A's, B's or C's.

tucotuco.cs.indiana.edu% ls -l
total 12
-rw-r--r--   1 dgerman  students     118 Feb 11 14:59 A.java
-rw-r--r--   1 dgerman  students     118 Feb 11 14:59 B.java
-rw-r--r--   1 dgerman  students      99 Feb 11 14:58 C.java
-rw-r--r--   1 dgerman  students    1180 Feb 11 14:39 Fraction.class
-rw-r--r--   1 dgerman  students     598 Feb 11 14:39 Test.java
tucotuco.cs.indiana.edu% cat A.java
class A {
  public static void main(String[] args) {
    System.out.println("This is A"); 
    B.main(args); 
  } 
} 
tucotuco.cs.indiana.edu% cat B.java
class B {
  public static void main(String[] args) {
    System.out.println("This is B"); 
    C.main(args); 
  } 
} 
tucotuco.cs.indiana.edu% cat C.java
class C {
  public static void main(String[] args) {
    System.out.println("This is C"); 
  } 
} 
tucotuco.cs.indiana.edu% javac ?.java # one char followed by dot java 
tucotuco.cs.indiana.edu% ls -l ?.*    # one char dot extension 
-rw-r--r--   1 dgerman  students     476 Feb 11 15:00 A.class
-rw-r--r--   1 dgerman  students     118 Feb 11 14:59 A.java
-rw-r--r--   1 dgerman  students     476 Feb 11 15:00 B.class
-rw-r--r--   1 dgerman  students     118 Feb 11 14:59 B.java
-rw-r--r--   1 dgerman  students     451 Feb 11 15:00 C.class
-rw-r--r--   1 dgerman  students      99 Feb 11 14:58 C.java
tucotuco.cs.indiana.edu% java B
This is B
This is C
tucotuco.cs.indiana.edu% java C
This is C
tucotuco.cs.indiana.edu% java A
This is A
This is B
This is C
tucotuco.cs.indiana.edu% 
This last part can be done in Cafe by setting the Main Class to either A, B or C in the Project menu (choose Project, then Settings and then type in the name of the Main Class and click OK) after which you can rebuild the project and run it. When you do that the Main Class's main method will be executed.

I'll add the code for Point a bit later.