Method exercises with input parameters, return values and test cases:

Create one project called NumberMoving. Create all these methods and then create a main method that uses all these methods:

 

1. 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"

 

2. loopExponent

Input parameters:

 

double

Any number

 

int

Power to raise

Description of processing:

 

If the power is one, return 1. Otherwise, Multiply the number as many times as the power to raise it to.

Also If the power is negative, take the reciprocal (which is 1 over the result).

Return:

 

double

Number raised to that power

 

Tie all these together with one main method that calls all the methods you created. It should do the following:

1) Ask the user to enter a number (and then read it in)
2) Use the
loopAddUp method to determine the sum of all numbers from one to the number the user entered.
3) Use the loopExponent method to determine that number raised to the power of 3.
4) Tell the user : The sum of all numbers from 1 to ___(what they entered) is ____
5) Tell the user: When you raise ___ to the power of 3, you get ____.
6) Tell the user goodbye.

 

Just practice calling methods:

31. Write a statement to call each of the following methods. If it returns a variable, put the result into a variable of the correct type. You have 3 variables in your program: double price, double cost, int quantity and String name.

public static void printMe(String nameIn) Your call is: _____________________________

public static void printLine(int qtyIn, double costIn) Your call is : ___________________________

public static double getTotal(double priceIn, int qtyIn) Your call is : ___________________________

public static double getTotal2(int qtyIn, double priceIn) Your call is : ___________________________

public static String getMessage(int qtyIn, double priceIn, String nameIn) Your call is : ___________________________

 

Some If statements to practice:

 

11. Ask the user for a quantity. If it is less than 12, add 5. If it is greater than 20, add 50. Print the number.

12. Ask the user for a quantity. If they are buying over 100, the price will be 30. If it they are buying over 50, the price is 50. Otherwise, the price is 70. Print the price (with discount if applicable) and total price.

13. Ask the user for a quantity. The base price is 70. Give a 10% discount if the quantity is between 5 and 60. Plus, if the quantity is exactly 100, give the 10% discount. Print the price (with discount if applicable) and total price.

Some loops to practice:

You figure out which type of loop (and one is a while loop)

21a. This program asks for the score from each game that two teams play. They play 15 games. It asks the score of each team and prints the score.

21b. This is 1a plus: At the end, it prints the total wins for each team. (There are only two teams.)

22. This program asks for almost the same thing, but you don't know how many games were played. Before each score, you ask if they have another score to enter.

A swap to practice:

41. I have a program which has a certain value in variable a and another in variable b. Swap the values in these 2 variables.