Lecture 30 (Tychonievich) - Exam 2 Review

Lecture Date: Monday, March 30

We walked through the Test 2 Review Guide

We wrote the following code:

Example code from class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.ArrayList;
import java.util.Arrays;

public class Exam2Review1 {
  public static void main(String[] args) {
      String s = "123";
      int[] a = {1,2,3};
      ArrayList<Boolean> l = new ArrayList<Boolean>();
      l.add(false);
      l.add(true);
      l.add(true);
      int slen = s.length();
      int alen = a.length;
      int llen = l.size();
      
      String ss = s.toString();
      String as = a.toString(); // a was an int[], 
      // above toString was non-static and defined in int[] class (almost)
      
      String ls = l.toString();
      
      String as2 = Arrays.toString(a);
      // above toString was static and defined in Arrays class
  
      
      System.out.println(ss);
      System.out.println(as);
      System.out.println(as2);
      System.out.println(ls);
      System.out.println(System.out.toString());
      
      System.out.println(Exam2Review1.logBase3(1110.5));
      System.out.println(Exam2Review1.theAnswer());
  }
  
  public static void anything() {
      System.out.println("Anything could happen here");
  }
  public static String anything2() {
      return "Anything could happen here";
  }
  public static int theAnswer() {
      return 42;
  }
  
  /* logBase3(81) = 1 + logBase3(27) = 1 + 3 = 4
  * logBase3(27) = 1 + logBase3( 9) = 1 + 2 = 3
  * logBase3( 9) = 1 + logBase3( 3) = 1 + 1 = 2
  * logBase3( 3) = 1 + logBase3( 1) = 1 + 0 = 1
  * logBase3( 1) = 0
  */
  public static int logBase3(double x) {
      if (x <= 1) {
          // 1. base case
          return 0;
      } else {
          // 2. recursive case that 3. makes progress
          return 1 + logBase3(x / 3);
      }
  }
}

We also took the following problem description:

You are asked to create a simulation of a free market economy. The basic idea is that different people will trade money for products and that different people value different products differently but that they all want money. Your simulation should be able to run for different time steps and involve the idea that people only know a subset of the other people.

and identified potential classes Person, Market, Money, Product, and Time; mentioned that no all would need to be classes in all designs (double might do fine for Money, for example); and sketched out the following Person UML Class Diagram:

Person
- known : ArrayList<Person>
- desiredProducts : ArrayList<Product>
- money : Money
+ Person()
+ Person(wanted : ArrayList<Product>)
+ Person(wallet : Money)
+ trade(fromProduct : Product, toProdct : Product, Person) : boolean
+ trade(Product, Money, Person) : boolean
+ tax(Money) : boolean
+ tax(Money) : Money
+ tax() : Money
+ salesTax(Product) : Money