1 / 12

Method exercises

Learn how to create and test methods without using the "IF" statement. This tutorial provides step-by-step instructions and examples.

Download Presentation

Method exercises

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Method exercises 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 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

More Related