Method exercises with
input parameters, return values and test cases:
Create one new project to hold all of these. Create all of the methods in one WorkIt class.
1. profitOnOneSale
Input parameters: |
||
|
Double |
Price |
|
Double |
Cost |
Description of processing: The profit is the price minus the cost. |
||
Return: |
||
|
Double |
Profit |
2. profitOnManySales
Input parameters: |
||
|
Double |
Price |
|
Double |
Cost |
|
Int |
Quantity sold |
Description of processing: The profit is the price minus the cost. Multiply that times the quantity sold for the total profit. |
||
Return: |
||
|
Double |
Profit |
4. productMinusTenPercent
Input parameters: |
||
|
int |
First number - int |
|
int |
Second number - an int |
Description of processing: Multiply the two numbers and then reduce that number by 10% |
||
Return: |
||
|
double |
Result of first * second minus 10% of total. |
5. loopAddUp
Input parameters: |
||
|
int |
Number to count |
Description of processing: Starting at 1, keep adding the numbers until you reach the number to count. |
||
Return: |
||
|
double |
Sum of all the numbers up to the "Number to Count" |
6. loopExponent
Input parameters: |
||
|
double |
Any number |
|
int |
Power to raise |
Description of processing: Multiply the number as many times as the power to raise it to. Assume power is positive |
||
Return: |
||
|
double |
Number raised to that power |
7. oneName
Input parameters: |
||
|
String |
First |
|
String |
Last |
Description of processing: The full name is the first name plus a space plus the last name |
||
Return: |
||
|
String |
Full name |
8. charsToWord
Input parameters: |
||
|
char |
Any character |
|
char |
Any character |
Description of processing: Put the 2 characters together into one
string starting with "And the word is ". Do not put any spaces between
them. (For example, send 't' and 'o' and get back "And the word is to" |
||
Return: |
||
|
String |
String of the 2 given characters after "And
the word is " |
9. Tie all these together with one main method that calls all the
methods you created. It should do the following:
1) Ask the user for their first name (and then read it in)
2) Ask the user for their last name (and then read it in)
3) Use the oneName method to get their full name.
4) Tell the user, "Hello" and then their name.
5) Ask the user for the cost and price (and then read them in)
6) Use the profitOnOneSale method to determine
the profit.
7) Tell the user that your profit is: and then the profit.
8) Ask the user how many they sold (and then read it in)
9) Tell the user their total profit on ___ items is ____.
10) Ask the user to enter a number (and then read it in)
11) Use the loopAddUp method to
determine the sum of all numbers from one to the number the user
entered.
12) Use the loopExponent method to determine that number raised to the
power of 3.
13) Tell the user : The sum of all numbers from 1 to ___(what they
entered) is ____
14) Tell the user: When you raise ___ to the power of 3, you get ____.
15) Tell the user goodbye.
10. Tie these all together one more way in the method sales. This
new method sales should take in no parameters and give out no
parameters. It should do the following:
1) Ask the user how much they are willing to pay for a really good
book.
2) Calculate the profit you will make on the book if it costs
$3.50. Call profitOnOneSale to get that
price.
3) Tell the user the total profit you will make on a book.
4) Ask the user how many books they are willing to buy.
5) Tell the user how much profit you will make on all the books (and
use profitOnManySales to help you).
6) Tell the user how much profit you will make if they buy just 3 more
books.
7) (for extra credit) Create a loop to tell the user how much profit
you will make on 1
book, then 2, then 3, all the way up to 10 books. Use a call to
method profitOnManySales each time. It will look something like:
for 1 books, I will have a profit of 30
for 2 books, I will have a profit of 60
for 3 books, I will have a profit of 90
....
for 10 books, I will have a profit of xxxx