15-100 Sections U-V-AA / Spring 2009
Midterm Exam #1 (Programming Portion)
Exam Date: Tue 3-Mar-2009
2 Questions + 1 Bonus Question / 20
Minutes
All questions are equally weighted (10 points each / 20 points total)
Notes:
- These are the same directions as for Quiz 5.
- On this portion of the midterm, you will use a
compiler to write code.
- You may access the course web site, Sun’s online API,
and the compiler of your choice.
- You may not access anything else (no notes,
books, etc, no Google or other searches, and certainly no code!).
- Place your solutions in a single Java file
named Midterm<andrewId>.java
(So for me it would be Midtermkoz.java)
- When you are done, you should email your
solution both as an attachment and pasted into the body of the
message. Send it to me (andrew id:
koz), and be sure to cc yourself!
- In your code, you may use loops,
conditionals, helper methods, methods from any Java classes in
Sun’s library, etc, even arrays if you wish (though they are not
required).
- Each problem asks you to write a void method
that takes no parameters.
- Your code will only be graded for correctness,
not style.
- For efficiency during a quiz, do not use
prompts, etc. Just provide the
required UI.
- Your code must compute the answers! Hardcoded answers will not receive any
credit.
- Do not use web-based scanners for this
quiz. Use file-based scanners.
- Thus, before starting this quiz, download this
file to your desktop:
http://kosbie.net/cmu/spring-09/15-100/resources/millionDigitsOfPi.txt
- To save some time, you should
start with this code, which also includes the import statements and the
getFileScanner helper method for you.
//
<name>, <andrewId>, <section>
import java.util.*;
import java.io.*;
class Midterm {
public static void main(String[]
args) {
printMostFrequentDigit();
printOddsOf20OrMore();
}
public static void
printMostFrequentDigit() {
System.out.println("printMostFrequentDigit not yet
written!");
}
public static void
printOddsOf20OrMore() {
System.out.println("printOddsOf20OrMore not yet written!");
}
// Convenient helper method for
reading from a file.
public static Scanner
getFileScanner(String filename) {
Scanner scanner = null;
try { scanner = new Scanner(new
java.io.File(filename)); }
catch (Exception e) {
System.out.println("File
not found");
return null;
}
return scanner;
}
}
1. printMostFrequentDigit
Write a method, printMostFrequentDigit, that prints the digit that occurs most
frequently in the file. In case of a
tie, print all such digits in order from least to greatest.
2. printOddsOf20OrMore
If you roll 5 six-sided dice and add their values, the sum must be between
5 and 30 (right?). Write a method,
printOddsOf20OrMore, that uses Monte Carlo
methods to compute the odds that this sum is at least 20 or greater. As usual, use enough trials to get a
reasonably accurate result. Hint: remember to create only one instance of
Random!
3. Bonus/Optional: sort
Write a method that takes an array of int’s and sorts them without calling Arrays.sort. Write a short explanation in a comment as to
how your method works.