1. How do I study for the exam? 

Go through the study guide, re-read the text, focus on the questions posted. Those and only those will be on 
the exam (a random selection of those for each person). So study the programs and review all questions. Nothing
on the exam can't be predicted except for which one program and random ten questions you will have to solve, the
entire list is there. You just don't know which specifically will come to you. 

2. Are exams individualized? 

Yes, each exam has the picture and name of the taker already on them. Additional scratch paper will be marked with
a special symbol like last time (the scratch pape last time had a Feynman diagram in it, now it will be something
else, to minimize the likelihood of bringing written paper from homw (don't do it). 

3. What's the structure of the exam?

(a) One problem from the 4-14 posted in the study guide (text is available as is a sample solution). 

(b) Ten short answer questions from the 108 entries listed. 

4. Why have you provided answers to all the questions on the study guide? 

So you know what a good answer looks like. Your answer may be different (code or short answer) it doesn't matter, 
as long as it is correct. Don't memorize the sample right answers, just try to understand them. 

5. Will this exam help with the first one if I didn't do well on the first one? 

If your score on the short answer part is better than the adjusted score on the first exam it will replace it. 

6. Can I still make up the first (and other) exams? 

Yes, with Adrian, in LH204 (make an appointment and make sure there's time for that when you do it). 

7. How do I make up old homework? 

Finish it, mail it to Adrian (via Canvas!) then come defend it in person (appointments in LH204). 

8. Can you post a sample midterm for us to see? 

Yes, here's the URL: 

...

9. Is there a paper version and a computer version of the exam like last time? 

Yes, you go to lab and self-evaluate yourself just like last time. The adjusted score will take into account your
raw score and your lab report in which you explain your mistakes and why you made them and what you learned.

10. How does the raw score and the lab report combine to give me the adjusted score? 

If your raw score is above 50% you might get half the points you lost back (if the report is excellent). If you
score below 50% the best you can do is to get half the points you earned. In other words if your raw score for the
written part is raw the formula is:

   
                           adjusted score = (raw + lab) / 2

The lab score is 

                           lab = 100 - 2 * raw, if raw >= 50

and 

                           lab = 2 * raw, if raw <= 50

Notice for raw == 50 the two scores are equal. 

This essentially gives you (at most, meaning the lab report must be perfect for that) back half the points you 
lost, or earns you half the points you made already, whichever is smaller.

11. What does the lab report need to contain? 

A clear (and critical) explanation of your written answers. If your answers were correct explain briefly why, 
if they were wrong fix them (by finishing them in the style they were providing in writing by you on the test). 
In other words don't just scratch a bad answer and replace it with a good answer provided by the book or the
study guide, tell me how close you got to those in the exam and what's the minimal fix to what you wrote!

12. Will JFrames be on the exam? 

Look at the Study Guide. What you see there is on the exam: both questions and answers have been provided. 

13. Will 2D arrays be on the exam? 

Look at the Study Guide. What you see there is on the exam: both questions and answers have been provided. 

14. Did you say that the raw score on the 10 short answer questions if bigger than the adjusted score on the
first exam will replace it?

Yes. Only if bigger, otherwise it will not affect it. Exams can still be made up in person regardless. 

15. How many questions are on the lab portion? 

The lab portion like last time is an open-book, talk to anyone, opportunity to understand what you did wrong,
determine a raw score, and submit a lab report on the entire written exam. 

16. Is there available a Java dictionary?

I assume you refer to the online API, which the book describes, please look at the answers to the study guide
questions that were posted. Maybe these will help along with what I wrote and showed on Mon in class: 

http://www.cs.indiana.edu/classes/c212-dgerman/sum2013/classNotes.html

http://www.cs.indiana.edu/classes/c212/sum2011/classNotes.html

But for the exam the study guide is what you need to follow. 

17. Is there actual coding on this exam?

Yes, you get one program from those listed 4, 5, 6,l 7, 8, 9, 10, 11, 12, 13 and/or 14 on the study guide. 

18. There really is an exam on Wednesday? 

Yes. It's the second exam (midterm exam). There are four exams in total in this class. That was discussed
in the first lecture and What's New? for the first week has the syllabus posted: 

http://www.cs.indiana.edu/classes/c212-dgerman/fall2016/c212-fall2016.pdf

19. Is there homework due on the day of the exam? 

No, there is currently nothing due at all. Just prepare for the exam. We will resume posting assignments
next week (new assignments, that is). 

20. What packages do we need to memorize? 

Just be able to answer correctly any of the study guide questions (and programs) posted.

21. How comprehensive is the study guide in terms of test material? Is most everything on there? 

The study guide has your 1 program + 10 questions in it. You just don't know which they are as I will
select them randomly (via a program). But your test is in there. Nothing on your test will come from anywhere
else. It will all come from the questions and programs listed in the study guide (exclusively). 

22. I tried the answer you posted for EEE#25 and it doesn't compile... 

Did you include class Point? Did you write a main method? 

Here's a good definition for Point: 

public class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x;
    this.y = y; 
  }
  double distanceTo(Point other) {
    return Math.sqrt( Math.pow(this.x - other.x, 2) +  
                      Math.pow(this.y - other.y, 2) ); 
  }
}

And here's Circle, with a main: 

class Circle {
  Point center;
  int radius;
  Circle(Point center, int radius) {
    this.center = center;
    this.radius = radius;
  }
  boolean overlaps(Circle other) {
     return this.center.distanceTo(other.center) <= this.radius + other.radius; 
  }
  public static void main(String[] args) {
    Circle a = new Circle(new Point(100, 100), 20); 
    Circle b = new Circle(new Point(200, 200), 30);
    System.out.println( a.overlaps(b) ); // should be false
    System.out.println( a.overlaps(new Circle(new Point(135, 135), 30) ) ); // should be true but just barely ...
    System.out.println( a.overlaps(new Circle(new Point(135, 135), 29) ) ); // should be false but just barely ...
  }
}

Here's how this runs: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\c212 fall 2016\tianqi cai
> run Circle
false
true
false


23. Yes, you provided a Study Guide, but I still feel confused and don't know how to prepare. 

Read the book from Chaper 1 to Chapter 8 and study the questions in the study guide. The questions 
are in the book and so are the answers. Look at the 11 programs posted (also from the book) and make
sure you can write any one of them in writing, closed-book, on the exam. Read the book and the study
guide at the same time. 

24. Will exam cover all material through chapter 9? 

See Study Guide. There are 14 questions (roughly from Chapter 1), 41 from Chapter 2, here's a table:

  Chapter    # of questions
    1             14
    2             41
    3             16
    4             13
    5              6  
    6              5 
    7              4 
    8              3
    9              2

So there are even two questions from Chapter 9. But you know what they are. And you know the right answers. 

25. How much of the grade is split between the program and the questions?

It's fifty-fifty. 

26. How long do you think my study will take? 

It depends from person to person, I expect 3-5 hours will be enough. You're welcome to study longer, until 
you know everything very well. 

27. How can I get the most of office hours? What do I need to bring? 

Bring questions and what you did on trying to answer them and where you got stuck. 

28. Can you write the code of the last problem in the study guide here, just the code, and how the program works? 

https://www.cs.indiana.edu/classes/a290-java/fall2012/templates/fifteen.htm

https://www.cs.indiana.edu/~dgerman/sum2012/c212/ch06/image047.jpg

import java.util.*;

class Fourteen{
  public static void main(String[] args){
    ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
    for(String arg : args){
      ArrayList<Integer> row = new ArrayList<Integer>();
      row.add(Integer.parseInt(arg));
      a.add(row);
    }
    System.out.println( a );
    while (a.size() > 1 ){
      for(int i = 0; i < a.size() - 1; i = i + 1){
        a.add(i, merge(a.remove(i), a.remove(i)));
        System.out.println( a );
      }
    }
  }

  static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b){
    ArrayList<Integer> result = new ArrayList<Integer>();
    while(a.size() > 0 || b.size() > 0){
      if (a.size() == 0) result.add(b.remove(0));
      else if (b.size() == 0) result.add(a.remove(0));
      else if (a.get(0) > b.get(0)) result.add(b.remove(0));
      else result.add(a.remove(0));
    }
    return result;
  }
}

Here's how it works:

-bash-4.2$ javac Fourteen.java
-bash-4.2$ java Fourteen 3 2 4 6 1 7 5 2 4 3 6
[[3], [2], [4], [6], [1], [7], [5], [2], [4], [3], [6]]
[[2, 3], [4], [6], [1], [7], [5], [2], [4], [3], [6]]
[[2, 3], [4, 6], [1], [7], [5], [2], [4], [3], [6]]
[[2, 3], [4, 6], [1, 7], [5], [2], [4], [3], [6]]
[[2, 3], [4, 6], [1, 7], [2, 5], [4], [3], [6]]
[[2, 3], [4, 6], [1, 7], [2, 5], [3, 4], [6]]
[[2, 3, 4, 6], [1, 7], [2, 5], [3, 4], [6]]
[[2, 3, 4, 6], [1, 2, 5, 7], [3, 4], [6]]
[[2, 3, 4, 6], [1, 2, 5, 7], [3, 4, 6]]
[[1, 2, 2, 3, 4, 5, 6, 7], [3, 4, 6]]
[[1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7]]
-bash-4.2$

29. Can you do the same for problem 4 (implement a static method that calculates the square root)? 

Here's the code: 

-bash-4.2$ cat Four.java
import java.util.*;

class Four {
  public static void main(String[] args) {
    String line;
    Scanner sc = new Scanner(System.in);
    System.out.print("type> ");
    line = sc.nextLine();
    while (! line.equals("bye")) {
      try {
        System.out.println(Math.sqrt(Double.parseDouble(line)));
      } catch (Exception e) {
        System.out.println("Type bye to go away or a double for God's sake!");
      }
      System.out.print("type> ");
      line = sc.nextLine();
    }
    System.out.println("Thanks.");
  }
}
-bash-4.2$ javac Four.java
-bash-4.2$ java Four
type> 2
1.4142135623730951
type> 3
1.7320508075688772
type> 9
3.0
type> bye
Thanks.
-bash-4.2$

30. Can you do the same about the problem 13 (playing Nim)? 

Here's the code and how it works: 

-bash-4.2$ cat Nim.java
// try java Nim Larry Laura Leslie 23
import java.util.*;

class Nim {
  int marbles, turn = -1;
  ArrayList<String> players;
  Scanner in = new Scanner(System.in);
  Nim(int marbles, ArrayList<String> players) {
    this.marbles = marbles;
    this.players = players;
    System.out.println(this.marbles + " marbles game for " + this.players);
  }
  void play() {
    while (this.marbles > 0) {
      this.turn = (this.turn + 1) % this.players.size();
      System.out.print(this.marbles + " marbles, " + this.players.get(this.turn) + "'s turn: ");
      int howMany = in.nextInt();
      this.move(howMany);
    }
  }
  void move(int marbles) {
    if (marbles < 0 || marbles > this.marbles ||
        marbles != 1 && marbles > this.marbles / 2 ||
        marbles == 1 && this.marbles == 1) {
      this.marbles = 0;
      System.out.println("Sorry, " + players.get(this.turn) + ", you lose.");
    } else
      this.marbles -= marbles;
  }
  public static void main(String[] args) {
    ArrayList<String> players = new ArrayList<String>();
    for (int i = 0; i < args.length - 1; i++) players.add(args[i]);
    (new Nim(Integer.parseInt(args[args.length - 1]), players)).play();
  }
}
-bash-4.2$ javac Nim.java
-bash-4.2$ java Nim Larry Laura Leslie 23
23 marbles game for [Larry, Laura, Leslie]
23 marbles, Larry's turn: 1
22 marbles, Laura's turn: 11
11 marbles, Leslie's turn: 5
6 marbles, Larry's turn: 2
4 marbles, Laura's turn: 2
2 marbles, Leslie's turn: 1
1 marbles, Larry's turn: 1
Sorry, Larry, you lose.
-bash-4.2$ java Nim Larry Laura Leslie 23
23 marbles game for [Larry, Laura, Leslie]
23 marbles, Larry's turn: 22
Sorry, Larry, you lose.
-bash-4.2$

31. Can you please do the same for program #11 (scramble the word)? 

Sure, here's the program code and how it runs: 

-bash-4.2$ cat Eleven.java
// java Eleven Doubting, dreaming dreams no mortal ever dared to dream before;
import java.util.*;

class Eleven {
  public static void main(String[] args) {
    ArrayList<String> sentence = new ArrayList<String>();
    for (String word : args) sentence.add( scramble( word ) );
    System.out.println( sentence );
  }
  public static int index(String word) {
    return (int) (Math.random() * (word.length()-2)) + 1;
  }
  public static String scramble(String word) {
    if (word.length() >= 4) {
      int i = index(word), j = index(word);
      while (j == i) { j = index(word); }
      if (i > j) { int a = i; i = j; j = a; }
      System.out.println( word + " " + i + ", " + j + " ");
      return word.substring(0  , i  ) +
             word.substring(j  , j+1) +
             word.substring(i+1, j  ) +
             word.substring(i  , i+1) +
             word.substring(j+1     );
    } else return word;
  }
}
-bash-4.2$ javac Eleven.java
-bash-4.2$ java Eleven Doubting, dreaming dreams no mortal ever dared to dream before;
Doubting, 1, 2
dreaming 4, 5
dreams 1, 2
mortal 1, 4
ever 1, 2
dared 2, 3
dream 2, 3
before 2, 4
[Duobting,, dreaimng, derams, no, martol, eevr, daerd, to, draem, berofe]
-bash-4.2$ java Eleven Doubting, dreaming dreams no mortal ever dared to dream before;
Doubting, 1, 5
dreaming 4, 5
dreams 2, 4
mortal 1, 2
ever 1, 2
dared 1, 3
dream 2, 3
before 2, 3
[Diubtong,, dreaimng, drmaes, no, mrotal, eevr, derad, to, draem, beofre]
-bash-4.2$ java Eleven Doubting, dreaming dreams no mortal ever dared to dream before;
Doubting, 4, 5
dreaming 1, 6
dreams 1, 2
mortal 2, 4
ever 1, 2
dared 1, 2
dream 2, 3
before 1, 2
[Doubitng,, dneamirg, derams, no, moatrl, eevr, draed, to, draem, bfeore]
-bash-4.2$

32. Can you do the same for program #9 (GPA converter)? 

Yes, here's the source code and how the program works: 

-bash-4.2$ cat Eight.java
class Eight {
  public static void main(String[] args) {
    String[] letters = {"F", "D-", "D", "D+",  "C-", "C", "C+",  "B-", "B", "B+",  "A-", "A"};
    double[] values =  { 0, 1-0.3,  1, 1+0.3, 2-0.3,  2, 2+0.3, 3-0.3,  3, 3+0.3, 4-0.3,  4};
    System.out.print("Enter value please: ");
    double value = (new java.util.Scanner(System.in)).nextDouble();
    if (value > 4 || value < 0)
      System.out.println("Invalid input.");
    else
      for (int i = letters.length - 1; i > 0; i = i - 1) {
        if (value >= (values[i] + values[i-1]) / 2) {
          System.out.println( letters[i] );
          System.exit(0);
        }
      }
      System.out.println("F");
  }
}
-bash-4.2$ javac Eight.java
-bash-4.2$ java Eight
Enter value please: 2.85
B
-bash-4.2$ java Eight
Enter value please: 2.8499999
B-
-bash-4.2$ java Eight
Enter value please: 2.1
C
-bash-4.2$ java Eight
Enter value please: 0.35
D-
-bash-4.2$ java Eight
Enter value please: 0.34999
F
-bash-4.2$

33. Can you do the same for program #10 (Alternating Sum)? 

Yes, here's the source code and how it runs: 

-bash-4.2$ cat Thirteen.java
class Thirteen {
  static boolean sameSet(int[] a, int[] b) {
    for (int e : a)
      if (! contains(b, e))
        return false;
    for (int e : b)
      if (! contains(a, e))
        return false;
    return true;
  }
  static boolean contains(int[] values, int value) {
    for (int v : values)
      if (v == value)
        return true;
    return false;
  }
  public static void main(String[] args) {
    int[] u = {1, 4, 9, 16, 9, 4, 7, 9, 11};
    int[] i = {11, 11, 7, 9, 16, 4, 1};
    System.out.println(sameSet(u, i));
  }
}
-bash-4.2$ javac Thirteen.java
-bash-4.2$ java Thirteen
true
-bash-4.2$

34. Can you do the same for program #7 (Standard Deviation)? 

Yes, here's the source code and how it runs for me: 

-bash-4.2$ cat Six.java
// sample use: java Six 1 2 3 4 5 6 7 8 9 returns 2.7386127875258306

class Six {
  public static void main(String[] args) {
    double sum = 0, sumOfSquares = 0, count = 0;
    for (String arg : args) {
      double n = Double.parseDouble( arg );
      sum += n;
      sumOfSquares += n * n;
      count += 1;
    }
    System.out.print("Standard deviation is: ");
    System.out.println( Math.sqrt( (sumOfSquares - sum * sum / count) / (count - 1) ));
  }
}
-bash-4.2$ javac Six.java
-bash-4.2$ java Six 1 2 3 4 5 6 7 8 9
Standard deviation is: 2.7386127875258306
-bash-4.2$

34. Can you do the same for program #8 (Simple Modeling)? 

Yes, here's the source code and how it runs for me: 

class Example {
  public static void main(String[] args) {
    Car a = new Car(35, 10);
    a.drive(  10);
    a.drive(   3);
    a.drive( 890);
  }
}

class Car {
  double fuel;
  double efficiency;
  double odometer;
  Car(int efficiency, int fuel) {
    this.efficiency = efficiency;
    this.fuel = fuel;
  }
  void drive(double miles) {
    this.odometer = this.odometer + miles;
    double consumedFuel = miles / this.efficiency;
    System.out.println("Driving " + miles + " miles consumes: " + consumedFuel + " fuel.");
    System.out.println("Mileage now at: " + this.odometer);
    this.fuel = this.fuel - consumedFuel;
    System.out.println("Fuel level now at: " + this.fuel);
  }
}

/**********

-bash-4.1$ javac Car.java
-bash-4.1$ java Example
Driving 10.0 miles consumes: 0.2857142857142857 fuel.
Mileage now at: 10.0
Fuel level now at: 9.714285714285714
Driving 3.0 miles consumes: 0.08571428571428572 fuel.
Mileage now at: 13.0
Fuel level now at: 9.628571428571428


 **********/

-bash-4.2$ javac Car.java
-bash-4.2$ java Example
Driving 10.0 miles consumes: 0.2857142857142857 fuel.
Mileage now at: 10.0
Fuel level now at: 9.714285714285714
Driving 3.0 miles consumes: 0.08571428571428572 fuel.
Mileage now at: 13.0
Fuel level now at: 9.628571428571428
Driving 890.0 miles consumes: 25.428571428571427 fuel.
Mileage now at: 903.0
Fuel level now at: -15.799999999999999
-bash-4.2$

35. Can you explain command line arguments again? 

-bash-4.2$ cal
    October 2016
Su Mo Tu We Th Fr Sa
                   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
-bash-4.2$ cal -3
   September 2016         October 2016          November 2016
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
             1  2  3                     1         1  2  3  4  5
 4  5  6  7  8  9 10   2  3  4  5  6  7  8   6  7  8  9 10 11 12
11 12 13 14 15 16 17   9 10 11 12 13 14 15  13 14 15 16 17 18 19
18 19 20 21 22 23 24  16 17 18 19 20 21 22  20 21 22 23 24 25 26
25 26 27 28 29 30     23 24 25 26 27 28 29  27 28 29 30
                      30 31
-bash-4.2$ cal 2016
                               2016

       January               February                 March
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6          1  2  3  4  5
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    6  7  8  9 10 11 12
10 11 12 13 14 15 16   14 15 16 17 18 19 20   13 14 15 16 17 18 19
17 18 19 20 21 22 23   21 22 23 24 25 26 27   20 21 22 23 24 25 26
24 25 26 27 28 29 30   28 29                  27 28 29 30 31
31
        April                   May                   June
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

        July                  August                September
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
       October               November               December
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31

-bash-4.2$ cal 6 2016
      June 2016
Su Mo Tu We Th Fr Sa
          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

This is cal, a program that can be called in many ways. 

In Java you can access these optional command line arguments too: 

public class Example {
  public static void main(String[] args) {
     if (args.length == 0)
       System.out.println( "you are not giving me any command line arguments." ); 
     else {
       System.out.println( "here are your command line arguments: " + java.util.Arrays.toString( args ) );
       for (String word : args)
         System.out.println( word );  
       System.out.println("---------------------\nagain, again:\n");
       for (int i = 0; i < args.length; i++)
         System.out.println("Command line argument #" + i + " is: " + args[i]); 
     }

  }
}


This runs as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman\c212 fall 2016\tianqi cai
> java Example
you are not giving me any command line arguments.
> java Example who are you
here are your command line arguments: [who, are, you]
who
are
you
---------------------
again, again:

Command line argument #0 is: who
Command line argument #1 is: are
Command line argument #2 is: you