Presentation is loading. Please wait.

Presentation is loading. Please wait.

Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.

Similar presentations


Presentation on theme: "Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your."— Presentation transcript:

1 Method exercises Without IF

2 Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your methods.

3 First problem 1. ProfitOnOneSale

4 ProfitOnOneSale Step 1: Write the comment for your method Open the class Locate the curly brace that closed the last method. If it is the first, it should go right after the open curly brace for the class. Write the comments: public class WorkIt { /** * profitOnOneSale : Calculate profit on one sale * given the cost and price * @param double price - the price that comes in; * double cost – the cost that comes in * @return double profit – price minus cost */ }

5 ProfitOnOneSale – Step 2 Step 2 – write the method header Under the bracket, or after the end of another method, write “public static” Write the type of your result (ex: int). If it returns nothing, write “void”. Write the name of your method Between parentheses, write the type of your input followed by the name of the variable your method will use. If you have more than one, separate by commas. Every variable needs a type before it. Type an open and close curly brace.

6 ProfitOnOneSale – Step 2 Result public class WorkIt { /** * profitOnOneSale : Calculate profit on one sale * given the cost and price * @param double price - the price that comes in; * double cost – the cost that comes in * @return double profit – price minus cost */ public static double profitOnOneSale (double price, double cost) { }

7 ProfitOnOneSale – Step 3 Step 3 – Send back a dummy result so it will compile Type return Type a value that would be valid for the return type. Type a semicolon Compile to see no errors. Ex: return 3;

8 Step 4 – write the test case Right click on the workit class and select “create test case” Right click on the test case and select “create test method” Right click on the workit class and select “profitOnOneSale” method. Name the test profitOnOneSaleTest1 and Enter some inputs and then enter the expected result. Repeat for about 3 test cases.

9 Step 4 result public class WorkIt { /** * profitOnOneSale : Calculate profit on one sale * given the cost and price * profitOnOneSale(6,2.5)== 3.5 * profitOnOneSale(2,3) == -1.0 * profitOnOneSale(6.5,0) == 6.5

10 Step 5 – code inside the method Knowing you have a box in memory for every input variable already, code inside the method’s curly braces so that the proper return value will be sent back. This is very specific to every type of problem. Compile

11 Step 5 result public class WorkIt { /** * ProfitOnOneSale : Calculate profit on one sale * given the cost and price * @param double price - the price that comes in; * double cost – the cost that comes in * @return double profit – price minus cost */ public static double profitOnOneSale (double price, double cost) { return price - cost; }

12 Step 6 – test it Compile and run your method. Run it for each of your test cases (right click on the test case and choose Test All Click on view / test results to see which tests passed


Download ppt "Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your."

Similar presentations


Ads by Google