Today is Wednesday.

We meet MW and next Monday is Labor Day.

We're going to have a course reset then. 

Reading assignment for tomorrow and Fri: chapter 3.

Reading assignment for next Wed: chapter 4. 

I need to post more readings. 

So far we learned to write a program that  

  (a) compiles and runs
  (b) and prints something 
  (c) and reads from the user
  (d) and asks for more using it read so far 
  (e) and reads some more data from the user 
  (c) and writes a final report. 

Let me give you an example of this program: 

import java.util.Scanner;
import java.math.BigDecimal;

public class Wednesday {
  public static void main(String[] args) {
    System.out.print("How much fuel: "); 
    Scanner scanographe = new Scanner(System.in); 
    String g = scanographe.nextLine();
    BigDecimal gallons = new BigDecimal(g); 
    System.out.print("Efficiency: "); 
    String eff = scanographe.nextLine(); 
    BigDecimal mpg = new BigDecimal(eff); 
    System.out.println("Your autonomy: " +
                       gallons.multiply(mpg)); 
    
  }


Here's how it runs in DrJava:

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> run Wednesday
How much fuel: 4.35
Efficiency: 100
Your autonomy: 435.00
> 4.35 * 100
434.99999999999994


Now let's practice writing expressions involving BigDecimals. 

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> import java.math.BigDecimal; // auto-import
> BigDecimal a = new BigDecimal( 1 )
> BigDecimal b = new BigDecimal( 2 )
> BigDecimal c = new BigDecimal( 3 )
> BigDecimal d = new BigDecimal( 4 )
> BigDecimal e = new BigDecimal( 5 )
> 2 * (1 + 3)
8
> (1 - 2) * (4 - 3 + 5)
-6
> b.multiply(a.add(c))
8
> (a.subtract(b)).multiply((d.subtract(c)).add(e))
-6
> a.subtract(b).multiply(d.subtract(c).add(e))
-6


Now some important general thoughts. 

In Java you can write functions we call them methods. 

Methods can only be defined in classes. 

Also they can be static or non-static. 

A class is a container for static members. 

It also contains non-static members (instance members).

Instance members form a blueprint. 

Classes are therefore also factories, because they are used to
creating objects by instantiating the blueprint(s) they have inside. 

public class Toyota {
  public static String talk() {
    return "We are Toyota."; 
  }
  public static void paint() {
    System.out.println( talk() ); 
  }
  public String drive() {
    return "I am driving..."; 
  }   
  public void pollute() {
    System.out.println( drive() ); 
  } 
}

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Toyota.talk()
"We are Toyota."
> Toyota.talk() + " Ha, ha."
"We are Toyota. Ha, ha."
> Toyota.paint()
We are Toyota.
> Toyota.paint() + "Ha, ha."
Static Error: Bad type in addition
> Toyota.drive()
Static Error: No method in static Toyota has name 'drive'
> Toyota a = new Toyota()
> a
Toyota@47b310
> Toyota b = new Toyota()
> b
Toyota@f7484d
> b.drive()
"I am driving..."
> a.drive()
"I am driving..."
> a.drive() + " It feels so good. "
"I am driving... It feels so good. "
> b.pollute()
I am driving...
> b.pollute() + " skdjhvs dfsdjnfvda v."
Static Error: Bad type in addition
>

Static methods belong to the class. Whether you create 0, 1 or 
more objects of that class you still have one static method. Static 
methods are available once the class is compiled. 

Instance methods belong to the objects created from that class. If
you have no objects you have no methods to invoke. 

public class Fraction {
  int num;
  int den;
  Fraction(int n, int d) {
    num = n;
    den = d;
  }
  public String report() {
    return num + "/" + den; 
  }
  public Fraction add(Fraction other) {
    int numerator = num * other.den + den * other.num; 
    int denominator = den * other.den; 
    return new Fraction( numerator, denominator );
  }
}

This is how this class works:

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Fraction a = new Fraction(1, 2)
> Fraction b = new Fraction(1, 2)
> Fraction c = a.add(b); 
> a
Fraction@1aeef92
> b
Fraction@166745b
> c
Fraction@3d36c1
> a.report()
"1/2"
> b.report()
"1/2"
> c.report()
"4/4"


--

Welcome to DrJava.  Working directory is C:\Users\soicloan\Desktop
> Fraction a = new Fraction(3, 5)
> Fraction b = new Fraction(1, 2)
> (a.add(b)).report()
"11/10"
> (new Fraction(1, 2)).add(new Fraction(3, 4)).report()
"10/8"


See you in lab.