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 hw3
3. open gedit by typing
gedit &
4. Name your class Homework3:
public class Homework3 { }
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 Homework3.java
java Homework3
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 5 functions at once.
Method Specifications:
1. Write a method named primeFactors. Have this method take an integer input and return the number
of prime factors of the input. It should print a list of the prime factors (including repeats).
Examples:
primeFactors(15) returns 2 and prints "3, 5"
primeFactor(16) returns 4 and prints "2, 2, 2, 2"
2. You are helping an English professor write a program to perform some basic literary analysis. Write
a function named avgLength which takes a String as input. This function should count the words in
the input string and compute the average length of the words in the sentence. For the purposes of
this method, you should ignore punctuation. You may assume that words are always separated by a
single space. Your method should return a double indicating the average word length in a given String.
You may not use any String class methods except length and charAt. Don’t worry if you answer has
more/less significant digits.
Examples:
avgLength(In a hole in the ground, there lived a...) returns 3.625 (average of 2, 1, 4, 2, 3,
6, 5, 5, and 1)
avgLength(@#$% $%^ *&!!) returns 0.0 (all words are punctuation only)
3. Write a method named countMyCs which takes a String as an input parameter. The method should
count the number of C’s (both upper case or lower case) in the input string, and if they exist, replace
them with dashes. This method should return an integer representing the number of C’s and print the
new string with the dashes to the terminal. You may not use any String class methods except length
and charAt.
Examples:
countMyZs(ABCxYzCcBC) returns 4 and prints AB-xYz--BcountMyZs(HELLO) returns 0 and prints HELLO
countMyZs(CCCCC) returns 5 and prints -----
4. Write a method named salesTax which takes no input. The method should prompt the user to enter
"prices" one at a time until the user inputs "X" signaling that they are done entering prices. The
program should sum the inputs which they are given and compute the sales tax based on an 8% tax
rate. It should print out the total, the sales tax and the sum of the sales tax and the total.
Example:
User inputs $1.25, $2.50, $5.56, $23.11
The program should output:
Subtotal : $32 .42
Tax : $ 2.59
Total : $35 .01
The output should be formatted so that the amounts have two decimal places, and the decimal point
is alligned on all three dollar amounts.
5. Write a function named printFibonacci. A Fibonacci sequence is produced by adding the two preceding numbers together to produce the next integer. Your method should take in 2 integer parameters
and produce the next 10 numbers in a Fibonacci sequence which starts with the addition of the 2
parameters. Your function should work for any integer inputs.
Examples:
printFibonacci(1, 2) prints
3, 5, 8, 13, 21, 34, 55, 89, 144, 233
printFibonacci(1,9) prints
10, 19, 29, 48, 77, 125, 202, 327, 529, 856
printFibonacci(0,0) prints
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
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/hw3 directory and type the turn-in command
/home/cs17000a/turnin Homework3.java hw2
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!