Building Java Programs
Chapter 13 Lab Handout
1-Write a static method named "tonnage" that accepts one parameter (a Scanner
attached to an input file) whose data represents a series of weights representing onboard freight for a truck. Your method should add up the weights and display the
total-number of items and the total-weight of the truck cargo.
The input consists of a series of records where the first entry is a Sting representing
an item, followed by one or two pairs of tokens, where each pair is an integer
followed by a String representing the measure of weight used. Measures are
pounds, tons, and metric-tons. The measures are to be readcase-insensitively.
For example, if the input file contains the following lines of text:
Item-1 745 pounds
Item-2 2 Tons 550 pounds
Item-3 3 metric-Tons
Item-4 1 Metric-tons 75 pounds
In this example a ton is equal to 2000 pounds, a metric-ton equals 2205 pounds so
the total pounds equals: 745 + 4000 + 550 + 6615 + 2205 + 75 = 14,190 pounds =
7 tons 190 pounds;
The output of your method should be: 4 items weighing 7 tons 190 pounds
You may assume that the file entries are well-formed as in the example
above. You may use this file entries provided to test your code, BUT your code
should work correctly with any well-formed file. I will use a different file to
grade.
You may use the following code to test your program:
import java.util.*;
import java.io.*;
public class CountWeight {
public static void main(String[] args) throws FileNotFoundException {
Scanner fileIn = new Scanner(new File("weights.txt"));
coinCounter(fileIn);
fileIn.close();
}
// *** your method code goes here ***
} // End of CountWeight class
2- Write a static method named "compareLines" that accepts one parameter (a
Scanner attached to an input file). Your method should compare each neighboring
pair of lines (the 1st and 2nd lines, then the 3rd and 4th lines, and so on) looking
for indexes in both strings that contain vowels [a, e, i, o, u, y, A, E, I, O, U, Y] and
should output the index where both strings contained a vowel.
For example, in the strings "hello" and "bully, the characters at indexes 1 and 4
match since both contain vowels at that index.
For each pair of lines, your method should print output showing the character
indexes that match, separated by spaces in the format shown below. If no
characters match, print "none" instead as shown below.
For example, suppose the input file contains the following text.
(Line numbers and character indexes are shown around the input and matching
characters are shown in bold, but these markings do not appear in the actual file.)
0123456789012345678901234567890123456789 (String index)
The quick brown fox
Those achy down socks
Wheels on the school bus go round
The wipers go swish swish swish
This name is Robert Peltson
So long ‘n thanks for all the fish
Humpty Dumpty sat on a wall
and then he also had a great fall
When passed the above file, your method would produce the following output:
lines 1 and 2: 2 6 12 17
lines 3 and 4: 2 7 12 22
lines 5 and 6: none
lines 7 and 8: 12 15 18 21
Notice that lines are not generally the same length.
You may assume that the file contains an even number of lines.
Copy and paste the file lines above into a file named test.txt in your DrJava
working directory to use with the test program provided below.
import java.util.*;
import java.io.*;
public class MatchIndex {
public static void main(String[] args) throws FileNotFoundException {
Scanner fileIn = new Scanner(new File("test.txt"));
matchIndex(fileIn);
}
// *** Your method code goes here ***
} // End of MatchIndex class
3- Write a static method named "mostVowels" that accepts two arrays of
strings str1 and str2 as parameters andreturns a new array str3 such that each
element of str3 at each index i stores whichever string has greater number of
vowels when comparing the elements at that same index i in arrays str1 and str2. If
there is a tie, take the element from str1. You may create additional helper
methods if desired.
For example, if a1 and a2 store the following elements:
String[] str1 = {"star", "pie", "jelly bean", "car"};
String[] str2 = {"cookie", "fig", "banana", "soda"};
Then your method should return the new array
{"cookie", "pie", "jelly bean", "soda"}
If the arrays str1 and str2 are not the same length, the result returned by your
method should have as many elements as the smaller of the two arrays. For
example, if str1 and str2 store the following elements:
String[] str1 = {"Splinter", "Leo", "April", "Don", "Raph"};
String[] str2 = {"Krang", "Shredder", "Bebop"};
Then your method should return the new array
{"Splinter", "Leo", "Bebop"}
Do not make any assumptions about the length of str1 or str2 or the length of the
strings. You may assume that neither array is null and that no element of either
array is null.
You may use the following code to test your program:
Expected output is listed to the right of the method calls.
import java.util.*;
public class MoreVowels {
public static void main(String[] args) {
String[] a1 = {"star", "pie", "jelly bean", "car"};
String[] a2 = {"cookie", "fig", "banana", "soda"};
String[] a3 = mostVowels(a1, a2); // [cookie, pie, jelly bean, soda]
System.out.println(Arrays.toString(a3));
String[] a4 = {"Splinter", "Leo", "April", "Don", "Raph"};
String[] a5 = {"Krang", "Shredder", "Bebop"};
String[] a6 = mostVowels (a4, a5); // [Splinter, Leo, Bebop]
System.out.println(Arrays.toString(a6));
}
// *** Your method code goes here ***
} // End of MoreVowels class
4- Write a static method named "bigBeforeSmall" that accepts an. For example, if
the following array is passed to your method:
String[] numbers = { "one", "twelve", "three", "four", "fourteen", "six" };
Then after the method has been called, one acceptable ordering of the elements
would be:
["fourteen", "twelve", "three", "four", "one", "six" ]
["fourteen", "twelve", "three", "four", "six", "one" ]
Do not make any assumptions about the length of the array or the range of values it
might contain. For example, the array might contain no even elements or no odd
elements. You may assume that the array is not null.
Hint: This is actually a sorting problem. In BubbleSort you tested pairs of
elements and swapped them if the left element is larger than the right element.
You also may not use any other data structures such as the ArrayList class from
Chapter 10.
DO NOT use Arrays.sort in your solution.
You may use the following code to test your program:
If your method is modeled after a bubble sort, expected output is:
["fourteen", "twelve", "three", "four", "one", "six" ]
import java.util.*;
public class BigOnesFirst {
public static void main(String[] args) {
String[] numbers = {"one", "twelve", "three", "four", "fourteen", "six"};
bigBeforeSmall(numbers);
System.out.println(Arrays.toString(numbers));
}
// *** Your method code goes here ***
} // End of BigOnesFirst class
5- Write a static method called "flip4" that takes an ArrayList of integer values as a
parameter and that rewrites successive sequences of four values in the list. For
each sequence of four values, the method will swap the first element and the fourth
element and then swap the second element with the third element. For example,
suppose that a variable called list stores the following sequence of values:
[3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4]
and we make the following call: flip4(list);
Afterwards the list should store the following sequence of values:
[42, 19, 8, 3, -8, 19, 26, 7, -4, 6, 204, 193]
Each sequence of three elements will be rotated so that the 3rd value becomes the
1st value and the other two are moved to the next higher index; ie. the 1st becomes
the 2nd and the 2nd becomes the 3rd. If the list has extra values that are not part of
a sequence of three, those values are unchanged. For example, if the list had
instead stored:
[3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4, 99, 2, 1]
The result would have been:
[42, 19, 8, 3, -8, 19, 26, 7, -4, 6, 204, 193, 99, 2, 1]
Notice that the values (99, 2, 1) are unchanged in position because they were not
part of a sequence of four values.
You may use the following code to test your program:
Expected output is listed to the right of the print statements.
import java.util.*;
public class Flipper {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(3);
list.add(8);
list.add(19);
list.add(42);
list.add(7);
list.add(26);
list.add(19);
list.add(-8);
list.add(193);
list.add(99);
list.add(2);
System.out.println(list); // [3, 8, 19, 42, 7, 26, 19, -8, 193, 99, 2]
flip4(list);
System.out.println(list); // [42, 19, 8, 3, -8, 19, 26, 7, 193, 99, 2]
}
// *** Your method code goes here ***
} // End of Flipper class
6- Write a static method called "initials" that takes as a parameter a
String containing a persons name. For example, the
following call:
initials("Marie Elain Johnson") should return "MEJ".
Initials are formed by combining the capitalized first letters of each name
component. Components in each name will be separated by spaces, tabs, or the
hyphen character. There might be extra spaces at the beginning or end of the
phrase. You may assume that the name String will have at least one name
component.
You must extract the first letter of each name component and capitalize if needed
and use that to build the name’s initials. In the event of a hyphenated name, like
Wilson-Smith, you must first replace the hyphen with a space character
You may use the following code to test your program:
Expected output is listed to the right of the method calls.
import java.util.*;
public class Initials {
public static void main(String[] args) {
System.out.println(initials("John Allen Jay"));
// "JAM"
System.out.println(initials(" Lisa Wilson"));
// "LW"
System.out.println(initials(" Quinton "));
// "Q"
System.out.println(initials("lisa ann marie Kiley-Marsh")); // "LAMKM"
}
// *** Your method code goes here ***
} // End of Initials class
7- Write a static method called "removeDuplicates" that takes
an ArrayList of Strings as a parameter. It should first sort the ArrayList using the
sort function of the Containers helper class and then remove any duplicates so that
only one of any series of duplicates remains in the ArrayList.
For example, suppose that the parameter is an ArrayList named a containing:
["clam", "squid", "squid", "squid", "clam", "octopus", "octopus", "shrimp"]
Then after calling “removeDuplicates(list)” the ArrayList a should contain:
["clam", "octopus", "shrimp", "squid"]
Please note: quotation marks would not appear in the actual output of the
ArrayList, they are only included here for clarity. The actual ArrayList output
would be:
[clam, octopus, shrimp, squid]
You may use the following code to test your program:
Expected output is listed to the right of the print statement.
import java.util.*;
public class RemoveDuplicates {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("clam");
list.add("squid");
list.add("squid");
list.add("squid");
list.add("clam");
list.add("octopus");
list.add("octopus");
list.add("shrimp");
squeeze(list);
System.out.println(list); // [clam, octopus, shrimp, squid]
}
// *** Your method code goes here ***
} // End of RemoveDuplicates class
Purchase answer to see full
attachment