1 / 20

Method exercises

Learn how to write and test methods in Java through a series of exercises. Set up a new project, create classes for methods and tests, and practice writing code. Includes step-by-step instructions and examples.

saugust
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

  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. • Create another TestIt class to hold the tests of each of the methods. (Every problem will end up having at least one method in the WorkIt class and another method in the TestIt class.)

  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: String). 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 1 – write the test case • Open the TestIt class. • Locate the curly brace that closed the last method. If it is the first, you should start right after the open curly brace for the class. • Type: public static boolean test + the method name + an open and close parentheses and an open and close curly brace: Ex: public static boolean testProfitOnOneSale() { }

  9. Step 4 result public class TestIt { public static boolean testProfitOnOneSale() { } }

  10. Step 5 – write one single test case After the method’s open curly brace {, write the word return and then open and close parentheses () and a semicolon;. ex: return (); Inside the parentheses, write the class.method you are testing (in this case, WorkIt.profitOnOneSale) followed by another () filled with the values you want to test in the order the method expects them. ex: return ( WorkIt.profitOnOneSale(5,3)); If the result is a primitive type (not String or another class), type == and the expected value, otherwise type .equals() with the expected value inside. ex: return(WorkIt.profitOnOneSale(5,3)== 2); ex: return (WorkIt.mergeChars(‘s’,’a’,’d’).equals(“sad”)); Compile.

  11. Step 5 result public class TestIt { public static boolean testProfitOnOneSale() { return(WorkIt.profitOnOneSale(5,3)== 2); } }

  12. Step 6 – write more cases Put another open parenthesis after the return and close it after the last close parenthesis: return(WorkIt.profitOnOneSale(5,3)== 2); - becomes: return( (WorkIt.profitOnOneSale(5,3)== 2) );

  13. Step 6 continued Copy the existing test and put && before it. return( (WorkIt.profitOnOneSale(5,3)== 2) ); - becomes: return( (WorkIt.profitOnOneSale(5,3)== 2) && (WorkIt.profitOnOneSale(5,3)== 2) );

  14. Step 6 continued • Change the second test case to another good case. • Add more as needed. Be sure to test all the borders, and a negative and positive result and input. Don’t forget 0.

  15. Step 6 result public class TestIt { public static boolean testProfitOnOneSale() { return( (WorkIt.profitOnOneSale(5,3) == 2) && (WorkIt.profitOnOneSale(5.45,10.40) == -4.95) && (WorkIt.profitOnOneSale(5.45,0) == 5.45) ); } }

  16. Step 7 – adjust for double • Doubles don’t hold the exact value, so change your equals statement to a range: return(WorkIt.profitOnOneSale(5,3)== 2); - Becomes: return ( (WorkIt.profitOnOneSale(5,3) < 2.01) && (WorkIt.profitOnOneSale(5,3) > 1.99) );

  17. Step 7 result public class TestIt { public static boolean testProfitOnOneSale() { return( (WorkIt.profitOnOneSale(5,3) < 2.01) && (WorkIt.profitOnOneSale(5,3) > 1.99) && (WorkIt.profitOnOneSale(5.45,10.40) > -4.96) && (WorkIt.profitOnOneSale(5.45,10.40) < -4.94) && (WorkIt.profitOnOneSale(5.45,0) < 5.46) && (WorkIt.profitOnOneSale(5.45,0) > 5.44) ); } }

  18. Step 8 – code inside the method • Go back to WorkIt • 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

  19. Step 8 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; } }

  20. Step 9 – test it Compile and run your unit test. If it fails, use “//” to comment out all but one test. If the result isn’t right, look to see whether the test or method is at fault. If you want to see what the method is producing, set a break point on the method. Keep adding back test lines until they all work.

More Related