Summary: Implement a program that will input a line of text from the user, and print the words (aka tokens) on separate lines contained in an ASCII "box". For example,
Enter a line of text: Java is a fun language to learn.
+--------+
|Java____|
|is______|
|a_______|
|fun_____|
|language|
|to______|
|learn.__|
+--------+
The program should be implemented so that different subtasks are implemented by different methods. For example, one of the subtasks is determining the length of the longest token. For free, here is a method to perform this operation.
// Returns the length of the longest token in the line.
public static int longestToken(String line) {
Scanner scanLine = new Scanner(line);
int max = 0;
while (scanLine.hasNext()) {
String token = scanLine.next();
max = Math.max(max, token.length());
}
return max;
}
Here is some explanation of the method:
Here are some suggestions for decomposing the task into subtasks, where each subtask is implemented by a method.
Answer the following questions and save your answers in a text file (.txt extension) in your Eclipse project. You can start editing a text file in Eclipse using:
File > New > Untitled Text File
Then do File > Save to start a dialog to save the file in your project.
1. In the Java documentation (https://docs.oracle.com/javase/8/docs/api/), find System.in (in the "Field Summary" of the System class). Write exactly the Modifier and Type and Field and Description for System.in. Now in the Scanner class, find two Constructor and Descriptions, one that corresponds to new Scanner(System.in) and one that corresponds to the new Scanner(line) shown in the longestLine method above. Write exactly only those two Constructor and Descriptions. Hint: In the first part of this exercise, you should have found out what type of object System.in is.
2. What does the following code print?
int[] a = new int[3];
a[0] = 2;
a[1] = 3;
a[2] = a[0] + a[1];
a[0] = a[1] + a[2];
a[1] = a[0] + a[2];
System.out.printf("%s %s %s", a[0], a[1], a[2]);
3. Consider the following static method:
public static double mystery3(double x) {
if (x > 1) {
return x – 1;
} else if (x < -1) {
return x / 2;
}
return x;
}
a) Provide three different values for x such that mystery3(mystery3(x)) will return 0.5
b) Provide three different values for x such that mystery3(mystery3(x)) will return -0.75
4. Consider the following static method:
public static int[] mystery4(int a, int b) {
int[] x = new int[b];
for (int i = 0; i < b; i++) {
x[i] = i + a + b;
}
return x;
}
a) What value(s) will be returned by the call mystery4(-3, 3)?
b) What call to mystery4 will return the array {-2, -1, 0, 1, 2}?
5. Consider the following static method:
public static void mystery5(int[] a) {
for (int i = 1; i < a.length; i++) {
a[i] += a[i-1];
}
return x;
}
a) If a is the array {1, 1, 1, 1}, what values will be in a after the call mystery5(a)?
b) If a is the array {1, 4, 9, 16} after calling mystery5(a), what values were in a before the call?
6. Rewrite Programming Exercise 6.9 so that it is correct. I am not asking for a program here. You won't get any extra credit for writing a program. I am asking for a (much) better problem statement. Hint: The book is very sloppy concerning correct numbers.
6.9 (Conversions between feet and meters) Write a class that contains the following two methods:
/** Convert from feet to meters */
public static double footToMeter(double foot);
/** Convert from meters to feet */
public static double meterToFoot(double meter);
The formula for the conversion is:
meter = 0.305 * foot
foot = 3.279 * meter
Write a test program that invokes these methods to display the following tables:
Feet | Meters | Meters | Feet |
1.0 | 0.305 | 20.0 | 65.574 |
2.0 | 0.610 | 25.0 | 81.967 |
... | ... | ... | ... |
9.0 | 2.745 | 60.0 | 196.721 |
10.0 | 3.050 | 65.0 | 213.115 |