java, programming homework help

java, programming homework help

Here are 7 questions that need to be answered using the new method ( in java )  as much as possible.

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 1 Pennies 33

PeNnIeS

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

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

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

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

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

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

4. Write a static method named "evenBeforeOdd" that accepts an array of integers as a parameter and

rearranges its elements so that all even values appear before all odds. For example, if the following

array is passed to your method:

int[] numbers = {5, 2, 4, 9, 3, 6, 2, 1, 11, 1, 10, 4, 7, 3};

Then after the method has been called, one acceptable ordering of the elements would be:

{4, 2, 4, 10, 2, 6, 3, 1, 11, 1, 9, 5, 7, 3}

The exact order of the elements does not matter, so long as all even values appear before all odd values.

For example, the following would also be an acceptable ordering:

{2, 2, 4, 4, 6, 10, 1, 1, 3, 3, 5, 7, 9, 11}

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.

You MAY NOT use any temporary arrays to help you solve this problem. (But you may declare as many

simple variables as you like, such as ints.) 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.

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. In this problem, the swap happens when the left

element is odd and the right element is even.

You may use the following code to test your program:

If your method is modeled after a bubble sort, expected output is:

[2, 4, 6, 2, 10, 4, 5, 9, 3, 1, 11, 1, 7, 3]

import java.util.*;

public class EvenBeforeOdd {

public static void main(String[] args) {

int[] numbers = {5, 2, 4, 9, 3, 6, 2, 1, 11, 1, 10, 4, 7, 3};

evenBeforeOdd(numbers);

System.out.println(Arrays.toString(numbers));

}

// *** Your method code goes here ***

} // End of EvenBeforeOdd class Answer

— — — — — — — — — — — — — — — — — — — — — — — — —

5. Write a static method called "reverse3" that takes an ArrayList of Integer values as a parameter and

that reverses each successive sequence of three values in the list. 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:

reverse3(list);

Afterwards the list should store the following sequence of values:

[19, 8, 3, 26, 7, 42, 193, -8, 19, -4, 6, 204]

The first sequence of three values (3, 8, 19) has been reversed to be (19, 8, 3). The second sequence of

three values (42, 7, 26) has been reversed to be (26, 7, 42). And so on.

If the list has extra values that are not part of a sequence of three, those values are to remain

unchanged. For example, if the list had instead stored:

[3, 8, 19, 42, 7, 26, 19, -8, 193, 99, 2]

The result would have been:

[19, 8, 3, 26, 7, 42, 193, -8, 19, 99, 2]

Note that the values (99, 2) are unchanged in position because they were not part of a sequence of

three 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 Reverse3 {

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();

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.toString()); // [3, 8, 19, 42, 7, 26, 19, -8, 193, 99, 2]

reverse3(list);

System.out.println(list.toString()); // [19, 8, 3, 26, 7, 42, 193, -8, 19, 99, 2]

}

// *** Your method code goes here ***

} // End of Reverse3 class

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

6. Write a static method called "acronym" that takes as a parameter a String containing a phrase and

that returns an acronym for the phrase. For example, the following call:

acronym("self contained underwater breathing apparatus")

should return "SCUBA".

The acronym is formed by combining the capitalized first letters of each word in the phrase. Words in

the phrase will be separated by some combination of spaces or tabs. There might be extra spaces at the

beginning or end of the phrase. The String will not contain any characters other than spaces, and

letters, and is guaranteed to contain at least one word.

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 Acronym {

public static void main(String[] args) {

System.out.println(acronym(" automatic teller machine ")); // "ATM"

System.out.println(acronym("personal identification number")); // "PIN"

System.out.println(acronym("computer science")); // "CS"

System.out.println(acronym("merry go round")); // "MGR"

System.out.println(acronym("All my Children")); // "AMC"

System.out.println(acronym("Troubled Assets Relief Program")); // "TARP"

System.out.println(acronym("quite confusing punctuation ")); // "QCP"

System.out.println(acronym(" loner ")); // "L"

}

// *** Your method code goes here ***

} // End of Acronym class

7. Write a static method called "compress" that takes an ArrayList of Strings as a parameter. It should

replace each sequence of two or more equal Strings in the ArrayList with a single String consisting of the

number of Strings that were equal, an asterisk, and the original String.

For example, suppose that the parameter is an ArrayList named a containing:

["clam", "squid", "squid", "squid", "clam", "octopus", "octopus"]

Then after calling "compress" the ArrayList a should contain:

["clam", "3*squid", "clam", "2*octopus"]

So the 3 occurrences of "squid" were replaced with "3*squid", and the 2 occurrences of "octopus" were

replaced with "2*octopus". The two occurrences of "clam" arenít changed, since they arenít next to

each other.

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 Compress {

public static void main(String[] args) {

// ["clam", "squid", "squid", "squid", "clam", "octopus", "octopus"]

ArrayList<String> list = new ArrayList<String>();

list.add("clam");

list.add("squid");

list.add("squid");

list.add("squid");

list.add("clam");

list.add("octopus");

list.add("octopus");

compress(list);

System.out.println(list.toString()); // [clam, 3*squid, clam, 2*octopus]

}

"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.


Discount Code: CIPD30



Click ORDER NOW..

order custom paper