Assignment #1 Solutions
CS211 Fall 2000
3 Exercises, Total Points 100
Exercise 1 (35 Points)
Solution:
class TwoBuffer {
private Object first;
private Object second;
public TwoBuffer()
{
first = null;
second = null;
}
public boolean put(Object o)
{
if (first == null) //
empty buffer
{
first = o;
return
true;
}
else if (second ==
null) // buffer with one item
{
second = o;
return
true;
}
else // buffer full
return
false;
}
public Object get()
{
if (first == null)
return
null;
Object o = first;
first =
second;
second =
null;
return o;
}
}
Exercise 2 (35 Points)
a) (20 points)
Solution:
class HW1_P2a {
public static void main(String[] args)
{
char first, second, third;
int count = 0;
for (first = 'a';
first <= 'z'; first++)
for (second
= 'a'; second <= 'z'; second++)
for (third = 'a'; third <= 'z';
third++)
if (count++ % 1000 == 0)
System.out.println(first
+ "" + second
+ "" + third);
}
}
There are 26N different combinations or 52N combinations depending on how the question was interpreted.
b) (15 points)
Solution:
class HW1_P2b {
public static void main(String[] args)
{
char first, second, third;
int count
= 0;
for (first = 'a';
first <= 'z'; first++)
{
for (second
= 'a'; second <= 'z'; second++)
{
if (first == second)
continue;
for (third = 'a'; third <= 'z';
third++)
{
if (first == third || second == third)
continue;
if (count++ % 1000 == 0)
System.out.println(first+
"" + second +
""
+ third);
}
}
}
}
}
There are N!/(N-R)! different permutations.
R = the number of positions in the permutation
N = the number of choices for the position values
Exercise 3 (30 Points)
Solution:
1. import java.io.*;
2.
3. class MetricToEnglish {
4.
5. double
CentimetersToInches(double cent)
6. {
7. return (cent / 2.5);
8. }
9. }
10.
11. class LengthConverter
extends MetricToEnglish {
12.
13. public static void main(String[] args)
14. {
15. LengthConverter
lc = new LengthConverter();
16. double
centimeters = 45.0;
17. double
inches, feet;
18.
19. /*
Convert */
20. inches = lc.CentimetersToInches(centimeters);
21. feet = lc.InchesToFeet(inches);
22.
23. System.out.println(centimeters
+ "cm = " + feet + "ft");
24. }
25.
26. private double InchesToFeet(double inch)
27. {
28.
29. return
(inch / 12);
30. }
31. }
Line # |
Error |
Explanation |
1, 23 |
Add ; to the end of the line |
These statements need semicolons |
5 |
Change private to public (or protected) |
We need to be able to access this method in a subclass |
11 |
Change CLASS to class |
CLASS is not a keyword |
11 |
Change implements to extends |
Subclasses extend other classes |
13 |
Change PUBLIC to public |
PUBLIC is not a keyword |
13 |
Add static after public |
main method must be declared static |
13 |
Change Main to main |
main must be spelled in all lower-case letters |
13 |
Change string to String[] |
string is not a predefined class name. main method takes an array of Strings as an argument |
15 |
Add after lc = new LengthConverter() |
lc should refer to an object to access the InchesToFeet method defined in class LengthConverter |
17 |
Change int to double |
These values should be doubles to appropriately hold data and to match the methods |
19 |
Change */ to /* and /* to */ |
Java comments look line /* this */ not */ this /* |
23 |
Change all occurrences of ‘ to “ |
Java strings are delimited by double quotes |
29 |
Change RETURN to return |
RETURN is not a keyword |