Spring Semester 2002


Lecture Notes Eight: Diagrams for object-oriented concepts.
1. Draw this:
class A {

} 
Don't forget the blueprint:


2. Draw this:
class B {

  int u;
  double q;

} 

Notice that the blueprint is no longer empty.


3. Draw this:
class Student {

} 

Easy. Student or A - same difference.


4. Draw this:
class Student {

  int age; 

} 

5. Draw this:
class Z {

  int m; 
  double n; 

} 

6. Draw this:
class M {
  
  int m; 
  static double n;

} 

7. Draw this:
class Q {

  int v; 
  double z;

  public static void main(String[] args) {

  } 

} 
For this the picture is bigger:

8. Draw this:
class E {
  
  int v; 
  double z;
  
  public static void main(String[] args) {

    int q; 
    
    q = 3; 

  } 

} 
Same as above only main no longer is empty.

9. Draw this:
class Student {

  int age; 
  double gpa; 

  public static void main(String[] args) {
 
    int q = 21; 
    String n = "John"; 

  } 

} 

10. Draw this:
class J {
  
  int x; 
  
  public  static void main(String[] args) {

    J z = new J(); 
    int x = 5; 
    z.x = 5; 

  } 

} 

11. Draw this:
class Student {

  int age;
  double gpa; 

  public static void main(String[] args) {

    int q;
    String n; 
    Student s;

    s = new Student(); 

  }

} 

12. Draw this:
class Student {
  
  int age = 20; 
  double gpa; 

  public static void main(String[] args) {

    Student s = new Student(); 
    Student q = new Student(); 
    q.age = 19; 
    s.age = 21; 
    q.age = 3 + q.age; 

  }

} 

13. Now write this:
Create two accounts for Doug and Adrian. Initial balance for any account is 10. Then Doug's account doubles, Adrian's decreases by 3, and Doug's account has one more dollar deposited to it.
Let me try it. How about this?
class Account {
  double balance = 10;

  public static void main(String[] args) {
    Account doug, adrian; 

    doug = new Account(); 
    adrian = new Account(); 

    doug.balance = doug.balance * 3; 
    adrian.balance -= 3; 
    doug.balance++; 
  } 
}
Looks good. Thank you.

14. Draw this:
class BankAccount {
  double balance = 25; 

  public static void main(String[] args) {

    BankAccount doug = new BankAccount(); 
    BankAccount adrian = new BankAccount();

    doug.balance = doug.balance * 2; 
    adrian.balance -= 3; 

    doug.balance++; 
    

  } 

} 
This is similar to what we drew at step 12.

15. Draw this:
class Account {
  
  double balance = 20; 
  
  public static void main(String[] args) {

    Account m = new Account(); 
    Account q = new Account(); 

    m.deposit(10); 
    n.deposit(3); 

  } 

  void deposit (int n) {
    this.balance += n; 
  } 

} 

16. Draw this:
class Account {
  
  double balance; 
  
  Account(double x) {

  } 

  public static void main(String[] args) {
    Account m = new Account(10); 
    Account q = new Account(3); 
    m.balance = 10 + m.balance; 
  }

} 

17. Draw this:
class X {

  int x; 

} 

18. Draw this:
class A {
  
  int x;

  A (int initialValue) {
    this.x = initialValue; 
  } /* do this when you first create the
    object but don't make this part of the
    object */ 

} 
Speaking of this can I show you an example?

Sure, go ahead. Here it is:
class Speakers {
    public static void main(String[] args) {
	Speaker a = new Speaker("Larry"); 
	Speaker b = new Speaker("Michael");
 	Speaker c = new Speaker("Tony"); 
	a.talk(); 
	b.talk(); 
	c.talk(); 
    } 
}

class Speaker {
    String speaker; 
    Speaker (String name) {
	speaker = name; 
    }
    void talk() {
	System.out.println(this.speaker + " is very happy."); 
    } 

Very nice. Thanks. this writer is very happy.

19. Write this:

20. Draw this:
class Student {
  String name; 
  Student (String w) {
    this.name = w; 
  } 
} 

class Friday {
  public static void main(String[] args) {
    Student q = new Student("Larry"); 
    Student u = new Student("John"); 
    Student s = new Student("Adrian"); 
  } 
} 
Funny.

21. Draw this:
class Checking {

  double balance; 
  
  Checking(double x) {
    this.balance = x; 
  } 

  void deposit(double y) { 
    this.balance = this.balance + y; 
  }

} 

22. Draw this:
class Tea {
  
  double bird; 

  void book(double watch) { // int z = 3;  
    this.bird = watch - this.bird; 
  }

  Tea (double snake) {
    this.bird = snake * 2; 
  }

} 
Oh, this is almost entirely exactly the same.

23. Draw this:
class Hat {
  public static void main(String[] args) {
    
    Tea m = new Tea(-2); 
    Tea n = new Tea(42); 

    m.book(92); 
    n.bird = m.bird + n.bird;

    n.book(25); 
    n.book(25); 

  } 
} 

24. Draw this:
class X {
  int q;
} 

class Play {
  public static void main(String[] args) {
    X u = new X(); 
    X p = new X(); 
    u.q = 3; 
    p.q = u.q + p.q; 
    u.q = u.q + p.q; 
  } 
} 

This last one was rather easy.


Last updated: Jan 28, 2002 by Adrian German for A201