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. Create another TestIt class to test each of the methods. Every problem should have at least one method in the WorkIt class and another method in the TestIt class. When you are done, hook all your test methods together.

 

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

 

3. 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

 

4. smallNumber

Input parameters:

 

double

Number to test

Description of processing:

If the number is greater than 20, return true; otherwise return false.

Return:

 

boolean

True if greater than   20

 


 

5. charsToWord

Input parameters:

 

char

Any character

 

char

Any character

 

char

Any character

Description of processing:

Put the 3 characters together into one string. Do not put any spaces between them.

Return:

 

String

String of the 3 given characters

 

5. 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.

 

6.  discountPlusTax

Input parameters:

 

double

Original price

 

double

Discount % (without the % sign), so 15% would be .15

 

double

Tax % (without the % sign), so 15% would be .15

Description of processing:

Multiply the original price by the discount% to find the discount amount. Subtract the discount amount from the original price to find the discounted price. Multiply the discounted price by the tax rate to find the tax amount, and add that to the discounted price.

Return:

 

double

Discounted price plus tax

 

 

7. quantityPricing

Input parameters:

 

double

Base Price

 

int

Quantity sold

Description of processing:

When the quantity sold is greater than 100, give a 10% discount. When the quantity sold is greater than 300, give a 30% discount. When the quantity sold is greater than 500, give a 50% discount. There is no discount for 100 and below. 

Return:

 

double

Discounted price for one item.

 


 

8. quantityPricingByGroup

Input parameters:

 

double

Base Price

 

char

Group

 

int

Quantity sold

Description of processing:

When the group is 'A', give a 5% discount only.

When the group is 'B', give only the quantity discount described in QuantityPricing. You should actually call that method.

When the group is 'C', give no discount.

When the group is 'D', give a 5% discount, and then give the quantity discount described in QuantityPricing on top of that 5%. You should actually call that method.

Return:

 

double

Discounted price for one item.

 

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

 

10. 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.

If the power is one, return 1.

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

Return:

 

double

Number raised to that power

 


EXTRA: Recursion

 

11. loopAddUpRecursion

Input parameters:

 

int

Number to count

Description of processing:

Starting at 1, keep adding the numbers until you reach the number to count.

But do not use a for loop. Use recursion instead. 

Return:

 

double

Sum of all the numbers up to the "Number to Count"

 

12. loopExponentRecursion

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.

If the power is one, return 1.

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

But do not use a for loop. Use recursion instead. 

Return:

 

Double

Number raised to that power