15-100 Sections S-V
Fall 2008 / Midterm Exam #1 (Programming Portion)
Exam Date: Fri 10-Oct-2008
2 Questions / 40 Minutes (Actual Time on Task)
BEFORE starting: Read these instructions first!
- For security reasons:
- Do not use any software except what is needed to follow these
instructions.
- Do not save or print this file.
- Do not print your solutions.
- Do not save your solutions (say, to a USB key or an Andrew
account) so that they can be accessed after the test is concluded,
except as described below.
- BEFORE you start, follow these steps:
- DOWNLOAD this file to your desktop:
Midterm.java
- RENAME the file, appending your AndrewID to the file name.
For me (koz), this would be: MidtermKoz.java
- OPEN the renamed Java file in DrJava, and add your name, your
Andrew ID, and your section clearly on the first line.
- COMPILE this renamed file and alert your proctor if there are any
problems.
- MINIMIZE DrJava.
- Finish reading the instructions then minimize your browser and
wait for the test to begin.
- For programming problems:
- While good style may help us understand your code and thus award
more partial credit, you are not required to use good style. During
exams and quizzes, you are only graded on correctness.
- The file includes test methods for all problems, even though you
will only attempt some of the problems -- comment out the other test
method calls in the main method!
- When grading, we may include a few more test cases than those
provided to you here.
- If you have questions about the problem statement, first look to the
test cases for answers.
- Note: for compatibility with Java 1.4, we have replaced the
"assert" statement with an "Assert" method. It works identically.
- What to submit
- Be certain your file is RENAMED (see above) and is NOT named
Midterm.java but rather Midterm[AndrewID].java (like MidtermKoz.java).
- After 40 minutes, you will be given a USB key. It will contain a
directory "15-100-midterms". Save your Java file there. Ask your
proctor for help if you are unsure of how to do this.
- Do EITHER ONE (your choice)
but NOT BOTH of these:
- nthPowerfulNumber
A "powerful" number is a positive integer m that for every prime
number p dividing m, p2 also divides m. So 12 is not powerful
because 3 divides 12 but 32=9 does not divide 12. We assume
1 is powerful (because no prime numbers divide it). Here are the first
several powerful numbers:
1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 72, 81, 100, 108, ...
Write the following method:
public static int
nthPowerfulNumber(int n)
This method takes a number n and returns the nth powerful number, or -1
if n is non-positive. Here is a test method: public static void testNthPowerfulNumber() {
System.out.print("Testing nthPowerfulNumber... ");
Assert(nthPowerfulNumber(-1) == -1);
Assert(nthPowerfulNumber(0) == -1);
Assert(nthPowerfulNumber(1) == 1);
Assert(nthPowerfulNumber(2) == 4);
Assert(nthPowerfulNumber(3) == 8);
Assert(nthPowerfulNumber(12) == 72);
Assert(nthPowerfulNumber(50) == 900);
System.out.println("Passed all tests!");
}
- nthOddishNumber
We will say that a number is "oddish" (our term) if it is positive
and it has more odd digits than even digits. So, 91234 is oddish because
it has 3 odd digits and 2 even digits, and 76 is not oddish because it
does not have more odd digits than even digits. Here are the first
several oddish numbers:
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 31, 33, 35, 37, 39, 51, ...
Write the following method:
public static int
nthOddishNumber(int n)
This method takes a number n and returns the nth oddish number, or -1 if
n is non-positive. Here is a test method: public static void testNthOddishNumber() {
System.out.print("Testing nthOddishNumber... ");
Assert(nthOddishNumber(-1) == -1);
Assert(nthOddishNumber(0) == -1);
Assert(nthOddishNumber(1) == 1);
Assert(nthOddishNumber(2) == 3);
Assert(nthOddishNumber(3) == 5);
Assert(nthOddishNumber(11) == 31);
Assert(nthOddishNumber(1234) == 3217);
System.out.println("Passed all tests!");
}
- Do EITHER ONE (your choice)
but NOT BOTH of these:
- medianLength
Write the following method:
public static int
medianLength(String[] a)
This method returns the median length of the Strings in the given array,
or -1 if the array is either empty or null. Note that the median of an
odd list is the middle element, and the median of an even list is the
average of the middle two elements.. Here is a test method: public static void testMedianLength() {
System.out.print("Testing medianLength... ");
Assert(medianLength(null) == -1);
String[] a0 = { };
Assert(medianLength(a0) == -1);
String[] a1 = { "this", "is", "a", "test" };
Assert(medianLength(a1) == 3);
String[] a2 = { "this", "is", "another", "test" };
Assert(medianLength(a2) == 4);
String[] a3 = { "12345", "1234", "1234567"};
Assert(medianLength(a3) == 5);
System.out.println("Passed all tests!");
}
- hasDuplicate
Write the following method:
public static
boolean hasDuplicate(String[] a)
This method returns true if at least one string occurs at least twice in
the given array, and false otherwise (including if the array is null)..
Here is a test method: public static void testHasDuplicate() {
System.out.print("Testing hasDuplicate... ");
Assert(hasDuplicate(null) == false);
String[] a1 = { "no", "No", "NO!!!" };
Assert(hasDuplicate(a1) == false);
String[] a2 = { "aaaa" };
Assert(hasDuplicate(a2) == false);
String[] a3 = { "this", "is", "a", "test", "it", "is" };
Assert(hasDuplicate(a3) == true);
System.out.println("Passed all tests!");
}
- Bonus/Optional: isHappy
A happy number is defined by the following process: Starting with any
positive integer, replace the number by the sum of the squares of its
digits, and repeat the process until the number equals 1 (where it will
stay), or it loops endlessly in a cycle which does not include 1. Those
numbers for which this process ends in 1 are happy numbers, while those that
do not end in 1 are unhappy numbers. Write the following method:
public static boolean
isHappy(int n)
This method takes an integer and returns true if it is a happy number and
false otherwise. Note that non-positive numbers are not happy. HINT:
Interestingly, every number that ends in a cycle ends in the SAME cycle. You
will probably need this hint to solve this problem. Here is a test method: public static void testIsHappy() {
System.out.print("Testing isHappy... ");
Assert(isHappy(-1) == false);
Assert(isHappy(0) == false);
Assert(isHappy(1) == true);
Assert(isHappy(10) == true);
Assert(isHappy(12) == false);
Assert(isHappy(13) == true);
Assert(isHappy(495) == false);
Assert(isHappy(496) == true);
System.out.println("Passed all tests!");
}
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem