Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Programming: Loop Xiaozhong Liu

Similar presentations


Presentation on theme: "Web Programming: Loop Xiaozhong Liu"— Presentation transcript:

1 Web Programming: Loop Xiaozhong Liu http://scholarwiki.indiana.edu/S517/S517.html

2 Mario problem http://www.pouetpu-games.com/index.php?section=2&game_id=1&w=640&h=480 Mario player = new Mario(); //hit a special brick if (player.status.equals(“small”)) { show_mushroom(); } else { show_flower(); }

3 Mario problem Mario player = new Mario(); //Mario change if (get_mushroom()) { player.status = “big”; } if (get_flower()){ player.status = “shoot”; } if (get_hurt() && player.status.equals(“big”)) { player.status = “small”; } if (get_hurt() && player.status.equals(“small”)) { player.status = “die”; }

4 Example 1 If your age > 20, serve you wine; else, serve you soda… int age; String drink; age = ??; if (age > 20) { drink = “wine”; } else { drink = “soda”;}System.out.println(drink);

5 Practice 3 Shopping problem. Suppose that there is a String variable called PayStatus that is either “Hourly” or “Salaried”, and that we want to compute the Pay of an employee using a variable Hours that tells how many hours they worked that week, and the variable PayRate that tells how much per hour that they make. Salaried employees are always paid as if they worked 40 hours, no matter how many hours they actually worked. If hourly employees work more than 40 hours in a week, then they get 1.5 times their pay rate for any hours over 40.

6 Sum of: 1 + 2 + 3 + 4 + 5 + 6 +……….. + 100 int result = 0; result = result + 1; result = result + 2; result = result + 3; …… result = result + 100; System.out.println(result);

7 Sum of: 1 + 2 + 3 + 4 + 5 + 6 +……….. + 100 int result; result = 0; for (int i = 0; i <= 100; i = i + 1) { result = result + i; } System.out.println(result);

8 int result; result = 0; for (int i = 0; i <= 100; i = i + 1) { result = result + i; } System.out.println(result); for (initialization; condition; change) Life cycle of variable i

9 Initialization Loop Body Condition? Increment no yes int i = 1 result = result + i; i <= 100? i ++ no yes Example

10 Sum of: 1 + 2 + 3 + 4 + 5 + 6 +……….. + ??? Result more than 1000 int result = 0; int number = 0; while (result < 1000) { number = number + 1; result = result + number; } System.out.println(number); Initialize Testing Change

11 Compare for (int i = 0; i <= 100; i = i + 1) { result = result + i; } while (result < 1000) { number = number + 1; result = result + number; }

12 For or While? 1.Compute 25! (factorial) 2. Save $1000 in the bank, interest rate is 3.5%. How many years? total = $1800 3. Send messages to your facebook friends automatically

13 Web Programming<html><head> Compute test Compute test Please submit your information Please submit your information First number: First number: </tr> Second number: Second number: </td></tr><tr> </tr></table></form></body></html>

14 Web Programming protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println(" "); out.println(" Compute test "); out.println(" Please submit your information "); out.println("<form method=\"post\" action =\"" + request.getContextPath() + "/web_coompute\" >"); out.println(" "); out.println("First number: "); out.println(" "); out.println("Second number: "); out.println(" "); }

15 Web Programming protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Enumeration paramNames = request.getParameterNames(); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println(" "); out.println(" Result "); out.println(" Compute result: "); int firstnum = Integer.parseInt(request.getParameter("firstnum")); int secondnum = Integer.parseInt(request.getParameter("secondname")); int sum = firstnum + secondnum; out.println(Integer.toString(sum)); out.println(" "); }

16 Web Programming protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); out.println(" "); out.println(" Result "); out.println(" Compute result: "); int firstnum = Integer.parseInt(request.getParameter("firstnum")); int secondnum = Integer.parseInt(request.getParameter("secondname")); int sum = firstnum + secondnum; out.println(Integer.toString(sum)); out.println(" "); }


Download ppt "Web Programming: Loop Xiaozhong Liu"

Similar presentations


Ads by Google