CS111 Test 1 Coding: Review Problems
-
Roger's parents do not allow him to eat ice cream on a weekday.
On a non-weekday he can eat ice cream much as he wants.
After complaining that it is not enough, his parents now also allow him to eat it on
any day that school is not in session. Write a Java method, canEatIceCream(),
that will return a true or false value indicating if Roger can eat ice cream. Assume the
method requires two boolean parameters, weekday and schoolNotInSession.
- When math and CS majors get together for a party, they like to eat pizza.
A party is successful when the number of pizza pies is between 10 and 20.
Unless it is a weekend, in which case there is no upper bound on
the number of pizzas. Write a Java method, pizzaParty(), that will return a true or false value indicating if the party is successful.
Assume the method requires two parameters, nPizzas is an integer and isWeekend is a boolean.
- You and your best friend are trying to get into a party.
The man at the door will decide if you get in based on how stylish your footwear is.
Footwear stylishness will be rated from 0 to 10, with 0 being absolutely horrible and
10 being spectacularly stylish.
The result of getting into the party is encoded as an integer value:
0: no
1: maybe
2: yes
Rules:
- if either of you has bad shoes (2 or less), then the result is 0 (no).
- If neither you or your friend bad shoes and if one of you has stylish shoes, 8 or more,
then the code result is 2 (yes).
- Otherwise the result is 1 (maybe)
Write a Java method, admission(), that will return a 0, 1, or 2 indicating admittance.
Assume the method requires two parameters, you is the stylish rating for your footwear and
friend is the stylish rating for your friend's footwear.
- Write a method, shuffle(), that receives a text string str.
The method should return a shuffled version of str. Do not use an imported classes,
such as Collections) to solve this problem.
- Write a method, buildArray(), that receives an integer parameter, n.
The method should return an integer array of length n, containing the numbers 0, 1, 2, ..., n-1.
- Write a method, zeroToTheBack(), that receives an array of integers, nums.
The method should return these numbers rearranged so that all the 0's are grouped at the
back of the array.
For example:
{2,3,4,0,0,6,7,0,0} may be rearranged as {2,3,4,6,7,0,0,0,0}
Note: The order is not important as long as the 0's appear in the back of the list.
- Write a method, pin_pan(), that receives a String, str.
Determine if the word "pin" appears the string the same number of times as "pan".
The method should return a true or false.
Example:
- pin_pan("This is a Pin. Pans are used for cooking.") - TRUE
- pin_pan("Pin pan pin pan pin pan") - TRUE
- pin_pan("pi n pan pin pan") - FALSE
- Write a method, noVowels(), that receives a String, str and remove all the vowels.
Example:
- noVowels("These fries are cold!") - "Ths frs r cld!"
- noVowels("What time is it?") - Wht tm s t?
- Write a method, sumDigits(), that receives a String, str.
Return the sum of the digits 0-9 that appear in the string, ignoring all other characters.
Return 0 if there are no digits in the string.
Example:
- sumDigits("These 9 fries r4u!") - 13
- sumDigits("What does 8 and 4 represent?") - 12
- Write a method, arrange(), that rearranges the values in any given array and returns the new arrangement.
The contents are rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order.
You may modify and return the given array, or make a new array.
evenOdd([1, 0, 1, 0, 0, 1, 1]) -> [0, 0, 0, 1, 1, 1, 1]
evenOdd([3, 3, 2]) -> [2, 3, 3]
evenOdd([2, 2, 2]) -> [2, 2, 2]
-
Write a method, merge (), that creates a new array list by merging two array lists,
alternating elements from both array lists. If one array list is shorter than the other,
then alternate as long as you can and then add the remaining elements from the longer array list.
ArrayList a: 4, 2, 6, 8
ArrayList b: 5, 1, 7
New ArrayList: 4, 5, 2, 1, 6, 7, 8
-
Write a method, rowAverage (), to compute and return the average of the sum of rows for a
two-dimensional array of any size.
-
Create a Java class that represents a date.
A date is defned by month, day, and year. Each of these attributes are private data members.
Include a default and explicit constructor.
Include setters and getters.
In addition, allow functionality that formats the date as mm/dd/yyyy. This method will be called toString().
Test your class by building an array containing 10 dates. Display all the dates before the year 2000.
-
Create a Java class that represents a card in a deck of cards.
Card.java.
-
Create a Fraction Java class that represents a fraction. Include an explicit constructor, getters, setters, and functionality for add() and toString().
-
A square is a rectangle whose length and width are the same.
Implement a square class from a rectangle base class by using inheritance.
- Implement the rectangle class as the parent class (base class).
- Implement the square class as a derived class (child class).
- Provide methods for computing perimeter and area.