Write a function called sum that takes in three numbers (ints) and returns their sum.
In the main method, write code that asks the user for three numbers. Then call your function to get the sum, and, in the main method, print it out for the user.
To pass the I/O testcase, the input and output should look like:
Enter number 1:3
Enter number 2:4
Enter number 3:5
The sum is 12.
Name this class Homework1.
Write a function called peoplePerCar that takes in two ints, representing the number of people on a subway train and the number of cars in that train. The function should calculate the average number of people per subway care on that train, and return it as a double.
In the main method, write code that asks the user for the number of people and the number of cars. Then call your function to get the average, and, in the main method, print it out for the user.
To pass the I/O testcase, the input and output should look like:
Enter number of people on the train:150
Enter number of train cars:8
The average number of people per train car is 18.75.
Name this class Train.
Write a RECURSIVE function called powerOf3 that takes in a number (int) n, and return 3^n. You may assume n is small enough that 3^n can still be represented as an int.
In the main method, test your code by calling your function at least three times with different values of n.
Name this class PowerOf3.
Write a RECURSIVE method (function) that has two int parameters, m and n. This method should return the sum of all integers between m and n, inclusive, which is m + (m+1) + (m+2) + ... + (n-1) + n. Assume that n != m.
For example, if m = 3 and n = 6, then your method should return 3 + 4 + 5 +6 = 18.
If m = -2 and n = 1, then your method should return (-2) + (-1) + 0 + 1 = -2.
Call your method at least three times in the main method to test it.
Call the method sumBetween
and the class Homework3_1
.
Hint: First modify the factorial code from class to add up all the numbers from 1 to n, instead of multiplying them. Once that is working, add in a second parameter m to start the sum from.
Write a program that converts hours in days plus leftover hours. Your program should ask the user for a number of hours. It should output the corresponding number of days plus leftover hours. This output should be grammatically correct (i.e. day or days and hour or hours as appropriate).
To pass the test cases, make sure your I/0 matches the following example:
Enter number of hours:49 That is equivalent to 2 days and 1 hour.
Call your class HourConverter
. You can assume the user always enters an whole number of hours >= 0.
This is not needed to do this problem, but could be helpful: The type for a string of characters (i.e. "hello") is String.
Write a program that asks the user for their full name and returns a user ID. This program should contain a method called getUserID
that computes the user ID from the name.
In more detail, the method getUserID
should take in two Strings as parameters, representing the user's first and last name. getUserID
should return a user ID that is the first three letters of the first name, followed by the first three letters of the last name, followed by the number of letters total in the two names. If the first or last name is less than three letters, then the whole first or last name should be used. The user ID should be all lowercase.
For example, if the person's name is Jose Rodriguez, then the user ID would be josrod13. If the person's name is Bonnie Li, then the user ID would be bonli8.
To pass the I/O test case, the input and output should look like:
Enter your full name:Bonnie Li Your userID is bonli8.
If the user enters more than two names (i.e. more than one space), use whatever is before the first space as the first name and whatever is after the last space as the last name. Hint: You can find the last space using the String method lastIndexOf().
Call this class UserID.
Write a program that asks the user for a word, and then outputs whether the word starts with a vowel or not.
This program should contain and call a method called startsWithVowel
that accepts a String as its only parameter and returns true if that String begins with a vowel and false otherwise. (Note that 'y' is considered a consonant when it starts a word.)
To pass the I/O testcase, the input and output should look like:
Enter a word:cat The word cat starts with a consonant.
or
Enter a word:elephant The word elephant starts with a vowel.
Call this class Words
.
Write a program that asks the user for a word, translates it into Pig Latin, and outputs the translation. To do the actual translation, your program should call a method called convertToPigLatin
, which takes in one String (which you can assume is a single word), and returns that word converted into Pig Latin according to the following rules:
1) If the English word starts with one or more consonants, all consonants before the first vowel are removed from the front of the English word, and added to the end of it, keeping the order the same. The string “ay” is then added to the end of this string.
For example, "jello" becomes "ellojay", "breakfast" becomes "eakfastbray", and "school" becomes "oolschay".
2) If the English word starts with a vowel, append the string “hay” to the end of that word.
For example "egg" beomes "egghay" and "apple" becomes "applehay".
For simplicity, always treat ‘y’ as a consonant, assume the word is all lowercase, and assume there are at most three consonants at the beginning of the word.
To pass the I/O testcase, your input and output should look like:
Enter a word:cat Your word in Pig Latin is atcay.
Call your class PigLatin
.
Hint: 1) You can use your method startsWithVowel
from Homework 4 #2.
2) You can do this without a loop, but if you want to use one, the while loop in Java is almost the same as in Python:
while (Boolean condition) {
// loop body
}
Write a RECURSIVE method called printStr
that accepts two parameters: a String s and an int n. This method should return a String containing the String s written n times, separated by a space each time. Assume n >= 1.
For example, calling printStr("Lehman", 2)
should return "Lehman Lehman", and calling printStr("The Bronx", 4)
should return "The Bronx The Bronx The Bronx The Bronx".
Call your class Homework5_2
. In the main method, call your method printStr
several times to test it.
Hint: This is very similar to the recursive method to add up a number n times (from the second class). However, instead of adding, say 3, each time, we are concatenating/adding the String s and a space.
Write a program that removes all double letters from a String either by using recursion or by using a loop. More specifically if the String contains two or more consecutive characters that are the same, then the duplicate characters should be removed.
For example, for the String "Hello", removing the duplicates gives the String "Helo"; for the String "abbcciiiabb", removing the duplicates gives the String "abciab".
Your program should contains two methods, each of which takes in one String and return that String with duplicate characters removed, as described above. The first method should be called removeDupsRecursive
, and should use recursion. The second method should be called removeDupsLoop
, and should use a loop.
To pass the I/O test case, the input and output should look like:
Enter a word:Mississippi Enter a computation method (recursive/loop):loop Your word with consecutive duplicate characters removed is Misisipi.
You may assume the user enters either "recursive" or "loop".
Name your class Homework6
.