In this homework you will write several methods. Each method below should operate as specified. In particular, pay attention to the number and type of input parameters, the return value type, and the name

of the function. Not following these specifications will cause you to lose points. For each function, I have

provided several examples of possible input(s) and expected output. However, these few examples are not

necessarily sufficient to ascertain that your function is entirely correct. You should test your methods as

much as needed by placing calls to in in your main method.

Getting started:

1. Log in to a lab machine, and open a terminal.

2. At the prompt in the terminal, move into your cs170 directory, and create a new directory for this

homework.

cd cs170

mkdir hw4

3. open gedit by typing

gedit &

4. Name your class Homework4:

public class Homework4 { }

5. Add your main method:

public static void main(String[] args) { }

6. Save your file and then compile and run it to make sure you have everything working:

javac Homework4.java

java Homework4

7. One by one, add the functions below. It is highly recommended that you work on one function at a

time. Write, compile, and test frequently. Do NOT try to write all 4 functions at once.

Method Specifications:

1. Write a method named parseString which takes a String as an input parameter and returns an array

of Strings. The method should take the input and separate the words in the string as an entry in the

array.

Example:

parseString("The quick brown fox") returns {"The", "quick", "brown", fox"}

2. Write a method named listOfPrimes which takes an integer as an input and returns an array of

integers. The array that is returned should contain all of the primes up to and including the input, if

the input is prime.

Examples:

listOfPrimes(3) returns {2, 3}

listOfPrimes{15} returns {2, 3, 5, 7, 11, 13}

listOfPrimes{30} returns {2, 3, 5, 7, 11, 13, 17, 19, 23, 27, 29}

3. The Goldbach Conjecture is a mathematical statement which says that every postive even integer can

be expressed as the sum of two primes. For example, 4 = 2 + 2, 6 = 3 + 3, and 100 = 3 + 97. Goldbach

suggested the conjecture in 1742, making it one of the oldest unsolved problems in mathematics.

Write a method names goldbach which takes an array of positive integers as an input and returns a

2D integer array. The returned array should have three columns. The first column should be a copy

of the input. The second an third columns will contain two prime numbers which sum to the number

in the first column, in the case that the number is greater than 2 and even. If the entry in the first

column is 2 or smaller, or if it is odd then the second and third column should contain zeros.

Example:

Integer | Prime 1 | Prime 2 |

8 | 3 | 5 |

12 | 5 | 7 |

15 | 0 | 0 |

-4 | 0 | 0 |

goldbach ( {8, 12, 15, -4} ) returns { {8, 3, 5}, {12, 5, 7}, {15, 0, 0}, {-4, 0, 0} }

You may use the method listOfPrimes that you wrote for problem 2.

4. This problem will implement a portion of the Luhn checksum algorithm. Most e-commerce websites

these days take credit cards. Users must enter their credit card number and the merchant verifies

that the number is valid. Then Visa, Mastercard, or AmEx process the payment to the merchant and

pass the bill along to the user. However, users often mistype their credit card number by one or two

digits. These common errors are why credit cards are designed with a secret. Using just the credit card

number, we can detect (most) mistakes and errors caused by user mistypes. The credit card contains

an error control code called a "checksum". Specifically, the credit card number is formatted to comply

with a Luhn-10 checking algorithm. In problem 4, you will write a method to calculate the checksum

for a given credit card number.

The Luhn-10 algorithm is a weighted algorithm. Each digit in the credit card number is multiplied by

a weight. These weights are then summed, forming the checksum. If the checksum is divisible by 10,

then the credit card number is valid. If it is not divisible by 10, then the user made an error and can

be prompted to reenter the credit card number. The weighting for the Luhn-10 algorithm is as follows:

◦ Beginning with the first digit in the credit card, every other number is multiplied by 2. If the

product results in a 2 digit number (e.g. 6 × 2 = 12), then the individual digits (e.g. 1 and 2) are

added to the checksum.

◦ The remaining digits of the credit card number are simply added to the checksum. That is, their

weight is 1.

◦ Several (small) examples are given below, but this algorithm will work with your Visa or Mastercard number. Try it!.

Write a method named luhnChecksum which takes an array of integers as an input parameter and

returns the integer checksum computed by the above algorithm.

Examples:

luhnChecksum({4,5,6,3,9,2}) returns 30 (see below for full calculation)

luhnChecksum({4,9,9,1,6,5,7}) returns 40

Example number 456392

digit multiplied by | 4 2 | 5 1 | 6 2 | 3 1 | 9 2 | 2 1 |

product 8 5 12 3 18 2 | ||||||

checksum: 8 + 5 + 1+2 + 3 + 1+8 + 2 = 30 |

Submission:

Don’t forget the required honor code statement (see the syllabus if you are unsure about this). Also don’t

forget that every function that you write should have \high level" comments describing (at minimum) the

required inputs, expected output, and purpose of each function.

Make sure you’re in your cs170/hw4 directory and type the turn-in command

/home/cs17000a/turnin Homework4.java hw4

Make sure you see a message that says \+++Allowed". This means that your HW was turned in correctly.

If you submit more than once, you will see a prompt asking you if you are sure you want to overwrite a

previous submission. You will need to type \y" to complete the submission!