Practice and Evaluation Practice Develop a java class called: SumCalculator.java which computes a sum of all integer from 1 to 100 and displays the result to the screen Problems with practice exercise • Problem solving skill: – Recognize and understand problems – Develop an algorithm – Coding • Java syntax – Translate from algorithm to code – Practice. Practice. Practice Flowchart and Algorithm - a computer is only a problem-solving tool! - Java is one tool used to solve problems Method of Problem Solving • • • • • • Recognize and understand the problem. Accumulate facts. Select appropriate theory. Make necessary assumptions. Solve the problem. Verify results. Method of Problem Solving • • • • • Develop an Algorithm. Write the program in a computer language. (i.e. in C, Java etc) Enter the program into the computer. Test and debug the program. Run the program, input data, and get the results from the computer. Algorithm and Flowchart • • An Algorithm is just a detailed sequence of simple steps that are needed to solve a problem. A Flowchart is a graphical representation of an algorithm. Algorithm (Week 3 practice) Develop a java class called: SumCalculator.java which computes a sum of all integers from 1 to 100 and displays the result to the screen Step 1: Understand the problem: Computes a sum of all integers from 1 to 100 Step 2: What is the input? Start from 1, stop if reach 100 or (1 and 100) Step 3: What is the output? Sum Algorithm Step 4: How do we compute the output? (first solution) Step 4.1: Start with the current integer: 1 Step 4.2: Sum starts with 0 Step 4.3: Add the current integer to Sum Step 4.4: If the current integer is less than 100, keep on adding the current integer to sum and increase It by 1(i.e, go back to 4.3). Step 4.5:Otherwise, print out the sum Algorithm Start Sequential steps Stop Condition/ Decision Flowchart Step 4.1: Start with the first integer: 1 Step 4.2: Sum starts with 0 Step 4.3: Add the current integer to Sum Increase Step 4.4: If the current integer is less Current than 100, go to next integer Integer by 1 and keep on adding the current integer to sum (i.e, go back to 4.3). Start Current Integer=1 Sum=0 Sum = Sum + Current Integer Current Integer < 100 Step 4.5:Otherwise, print out the sum Print Sum Stop Practice Step 4 (second solution): sum = 100*(100+1)/2 Testing a program • Ideal: Test cases should be designed BEFORE writing the code • Practice: Test cases are only designed AFTER the code is written Time to write codes <= Time to test What should we test • Philosophy: – Test everything that could reasonably break. For example: Test if the inputs received from a user is valid Test if the computation is done correctly How to develop a test case • A test case is a document that contains - test case id - description of what this test case is - an input, - an action, or event and - an expected response to determine if a feature of an application is working correctly. Example • Test Case Id: Testcase01 Description: Testing if the program exits when an invalid number of weeks is entered Input: an integer represents a number of weeks and is outside of [1..12] Expected response: the program will display a message and exit Testing Amortization.java • Please test the Amortization.java in (c), (d) and (e) following the test cases given Practice - Modify the existing SumCalculator.java program to print out the average: 1 100 average i 100 i 1 Practice • Develop a class called PrintEven to print only even integers from 1 to 100 • Modify the existing PrintEven to print only even integers from 1 to n. Allow a user to enter the value for n. Check list • Make sure comments are included in LoanCalculator.java • Make sure that the directories are created, classes and java files are copied according to the requirements of the project • Make sure to copy the design document • Email (zip files), or submit your project on floppy disk or CD on time Practice • Write the code ComputeMin to print out the minimum of three integers. Three integers are inputs from a user • Reuse the code ComputeMin to develop ComputeMax which prints out the maximum of three integers. Three integers are inputs from a user Practice • Develop a class call PrimeNumber that: – Get an integer as input from users – If the integer is a prime number, print out a message that says: Prime number – If not, print out a message that says: It is not a prime number Algorithm • Run a for loop that tests to see if any number from 2 up to sqrt(n) divides evenly into the given number. If you find one, then the number isn't prime. If you don't find one, the number is prime.