Question 1
Write a static method named "countCoins" that accepts one parameter (a Scanner attached to
an input file) whose data represents a person's money grouped into stacks of coins. Your
method should add up the cash values of all the coins and print the total money at the end.
The input consists of a series of pairs of tokens, where each pair begins with an integer and is
followed by the type of coin, which will be either "pennies" (1 cent each), "nickels" (5 cents
each), "dimes" (10 cents each), or "quarters" (25 cents each), case-insensitively. A given coin
might appear more than once on the same line.
For example, if the input file contains the following single line of text:
3 pennies 2 quarters 1 pennies 3 nickels 4 dimes
In this example: 3 pennies are worth 3 cents;
2 quarters are worth 50 cents;
1 penny is worth 1 cent;
3 nickels are worth 15 cents;
4 dimes are worth 40 cents.
The total of these is 1 dollar and 9 cents. Therefore your method whould print:
Total money: $1.09
Here is a second example. Suppose the input file contains the following 4 lines of
text. Notice the capitalization and spacing:
Copy the 4 lines below and paste into a text file named money.txt in the DrJava working
directory to test with the program code at the end of this question.
12 QUARTERS
PeNnIeS
1 Pennies
33
10 niCKELs
Then your method should produce the following output:
Total money: $3.84
You may assume that the file contains at least 1 pair of tokens. You may also assume that the
input is valid; that the input has an even number of tokens, that every other token is an integer,
and that the others are valid coin types.
You may use the following code to test your program:
Expected output is listed to the right of the method call.
import java.util.*;
import java.io.*;
public class CountMoney {
public static void main(String[] args) throws
FileNotFoundException {
Scanner fileIn = new Scanner(new File("money.txt"));
countCoins(fileIn); // money = $3.84
}
// *** Your method code goes here ***
} // End of CountMoney class
Answer
import java.util.*;
import java.io.*;
public class CountMoney {
public static void main(String[] args) throws FileNotFoundException {
Scanner fileIn = new Scanner(new File("money.txt"));
countCoins(fileIn); // money = $3.84
}
// *** Your method code goes here ***
public static void countCoins(Scanner keyin) {
int sum = 0;
int n = 0;
String money="";
while(keyin.hasNext())
{
if (keyin.hasNextInt()) {
n = keyin.nextInt();
}
else {
money = keyin.next();
money = money.toLowerCase();
if (money.equals("pennies")) {
sum += n;
}
else if (money.equals("nickels")) {
sum += n * 5;
}
else if (money.equals("dimes")) {
sum += n * 10;
}
else if (money.equals("quarters")){
sum += n * 25;
}
}
}
System.out.printf("Total money: $%.2f\n", 0.01 * sum);
}
} // End of CountMoney class
•
Question 2
Write a static method named "matchIndex" 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 places where the character at a given
0-based index from the two lines is the same.
For example, in the strings "hello" and "belt", the characters at indexes 1 ('e') and 2 ('l')
match. Your code should be case-sensitive; for example, "J" does not match "j".
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.)
index - 0123456789012345678901234567890123456789
line
1
The quick brown fox
2
Those achy down socks
3
Wheels on the school bus go round
4
The wipers go swish swish swish
5
His name is Robert Paulson
6
So long 'n thanks for all the fish
7
Humpty Dumpty sat on a wall
8
And then he also had a great fall
9
booyakasha
10
Bruno Ali G Borat
When passed the above file, your method would produce the following output:
lines 1 and 2: 0 1 7 12 13 14 15 17
lines 3 and 4: 1 2 13 14 23
lines 5 and 6: none
lines 7 and 8: 4 14 20 21 22
lines 9 and 10: none
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 10 lines below into a file named test.txt
in your DrJava working directory to use with the test program.
The quick brown fox
Those achy down socks
Wheels on the school bus go round
The wipers go swish swish swish
His name is Robert Paulson
So long 'n thanks for all the fish
Humpty Dumpty sat on a wall
And then he also had a great fall
booyakasha
Bruno Ali G Borat
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
Answer
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 ***
public static void matchIndex(Scanner input) {
int lineCount = 0;
while (input.hasNext()) {
System.out.print("lines " + (++lineCount) + " and " + (++lineCount)
+ ":");
String line1 = input.nextLine();
String line2 = input.nextLine();
int len;
if (line1.length() > line2.length())
len = line2.length();
else
len = line1.length();
boolean flag = true;
for (int i = 0; i < len; i++) {
if (line1.charAt(i) == line2.charAt(i)) {
System.out.print(" " + i);
flag = false;
}
}
if (flag)
System.out.print("none");
System.out.println();
}
}
} // End of MatchIndex class
•
Question 3
Write a static method named "longer" that accepts two arrays of strings a1 and a2 as
parameters and returns a new array a3 such that each element of a3 at each index i stores
whichever string has greater length (more characters) between the elements at that same index
i in arrays a1 and a2. If there is a tie, take the element from a1.
For example, if a1 and a2 store the following elements:
String[] a1 = {"star",
"pie", "jelly bean", "car"};
String[] a2 = {"cookie", "fig", "banana", "soda"};
Then your method should return the new array
{"cookie", "pie", "jelly bean", "soda"}
If the arrays a1 and a2 are not the same length, the result returned by your method should have
as many elements as the larger of the two arrays. If a given index i is in bounds of a1 but not
a2 (or vice versa), there are not two elements to compare, so your result array's element at
index i should store the value "oops". For example, if a1 and a2 store the following elements:
String[] a1 = {"Splinter", "Leo",
"April", "Don", "Raph"};
String[] a2 = {"Krang", "Shredder", "Bebop"};
Then your method should return the new array
{"Splinter", "Shredder", "April", "oops", "oops"}
For full credit, do not modify the elements of a1 or a2. Do not make any assumptions about
the length of a1 or a2 or the length of the strings. You may assume that neither array is null
and that no element of either array is null.
HINT: initially fill the new array with "oops" and then overwrite where the input arrays both
have values
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 Longer {
public static void main(String[] args) {
String[] a1 = {"star", "pie", "jelly bean", "car"};
String[] a2 = {"cookie", "fig", "banana", "soda"};
String[] a3 = longer(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 = longer(a4, a5); // [Splinter, Shredder,
April, oops, oops]
System.out.println(Arrays.toString(a6));
}
// *** Your method code goes here ***
} // End of Longer class
Answer
import java.util.*;
public class Longer {
public static void main(String[] args) {
String[] a1 = {"star", "pie", "jelly bean", "car"};
String[] a2 = {"cookie", "fig", "banana", "soda"};
String[] a3 = longer(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 = longer(a4, a5); // [Splinter, Shredder, April, oops, oops]
System.out.println(Arrays.toString(a6));
}
// *** Your method code goes here ***
public static String[] longer(String a1[],String a2[])
{
int l1,l2,l,i,s;
l1=a1.length;
l2=a2.length;
if(l1>=l2){
l=l1;s=l2;
}
else {
l=l2;s=l1;
}
String a3[]=new String[l];
for(i=0;i
Purchase answer to see full
attachment