Part One

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:

  • A Scanner object can be used to split a line of text into tokens. Note that the parameter for creating the Scanner object is a String object rather than System.in. The hasNext method will return false when there are no more tokens in the String.
  • max will be set to the maximum length of a token. max is initialized to 0 because no string can have a negative length.

Here are some suggestions for decomposing the task into subtasks, where each subtask is implemented by a method.

  • A method that produces a specific number of blanks would be useful, as well as a method that produces a specific number of dashes.
  • Each line in the box could be produced by a method call: one method for the first and last lines, and another method to print a token with the correct number of blanks.
  • The main method could consist only of method calls and assignments, so that any loops will be in other methods.

Part Two

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

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.