1 / 23

S517 Web Programming

http://discern.uits.iu.edu:8790/S517/S517.html. S517 Web Programming. Assistant Professor Xiaozhong Liu. Java Statements Looping Arrays Functions Algorithm … …. Numeric Text File Internet … …. Command line Or GUI Compute result. J ava. Real World. Programming.

sophie
Download Presentation

S517 Web Programming

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. http://discern.uits.iu.edu:8790/S517/S517.html S517 Web Programming Assistant Professor Xiaozhong Liu

  2. Java Statements Looping Arrays Functions Algorithm … … Numeric Text File Internet … … Command line Or GUI Compute result Java

  3. Real World Programming What is programming? A student Public Class Student I am a student Student a_student= new Student( ) My name is Xiaozhong String name; name = “Xiaozhong” I was born in 1979 Intbirth_year; birth_year = 1979 How old? Int age; age = 2010 – birth_year Use computer + programming to solve real-world problem…

  4. Real world problem: how to describe a student? A java example 1. Define student 2. Describe a student public class student { String name; int age; } public static void main(String[] args) { student stud_A = new student( ); stud_A.name = “Xiaozhong Liu” stud_A.age = 30; }

  5. Real world problem: add two numbers public static void main(String[] args) { //input int number_1 = 20; int number_2 = 30; int sum; //process sum = number_1 +number_2; //output System.out.println(sum); } A java example

  6. Some Java Basics • Class name and file name should be the same • ABC.java -> public class ABC { } • Source extension .java Compiled file extension .class • The compile process creates a .class file from the .java file • Example: Project folder\src\MyProgram.java -> Project folder\build\MyProgram.class • Java is case sensitive

  7. Output Input Process Information problem… Variable double price, pricewithtax; price = 65.20 price = price*1.0825 pricewithtax = price

  8. Variable type Variable name (no space, start With alphabet, case sensitive) String String name = “Obama”; Variable int int age = 30; Variable value double double price = 15.25; boolean booleandataplan = true; (or false)

  9. Memory Model • Values in the process are stored in memory. • View memory as a sequence of slots that can hold values of different types • Variables name the slots, sometimes called locations of memory • Assignment can put values in variables

  10. Java supports basic arithmetic operators: +, -, *, /, and %. Add two numbers? Standard arithmetic int number1, number2, result; numbers1 = 18; number2 = 9; result =number1+number2; System,out.println(result);

  11. double x, y; x = 7.486; y = x+15*x-7/x*x; (?????????) System.out.println(y);

  12. Exercise: To convert from meters to feet, multiply the number of meters by 3.28084 1.71 meter = ???? Feet 5.68 feet = ???? meter

  13. Exercise: Implement the following expression:

  14. public class Arithmetic { public static void main(String[ ] args) throws Exception { //Input int num1 = 1; int num2 = 5; //Process int sum; sum = num1 + num2; //Output System.out.println("Result: " + sum); } } Arithmetic.java

  15. public class Arithmetic { public static void main(String[ ] args) throws Exception { //Input int num1 = 1; int num2 = 5; //Process int sum; sum = num1 + num2; //Output System.out.println("Result: " + sum); } } Arithmetic.java

  16. String title = “123”; title = title + “456” System.out.println(title); boolean flag = true; System.out.println(flag); double num1 = 3.6; intnum2 = 2; intresult = num1/num2; System.out.println(result); Test these code

  17. Variable type conversion int number1 = 5; String number2 = “10”; int result; result = number1 + number2; What is the result???

  18. num = Integer.parseInt(str); String int str = Integer.toString(num) num = Double.parseDouble(str); String double str = Double.toString(num) double number; String str = “10.735”; number = Double.parseDouble(str) Two questions: What is “Double” and “Integer”??? 2. Why we need type conversion???

  19. intfirstnum = Integer.parseInt(request.getParameter("firstnum")); intfirstnum = Integer.parseInt(request.getParameter("firstnum")); intsecondnum = Integer.parseInt(request.getParameter("secondname")); intsum = firstnum + secondnum; out.println(Integer.toString(sum));

  20. <html><head> <title>Compute test</title></head><body> <h2>Please submit your information</h2> <form method="post" action ="/week1/web_compute" > <table border="0"><tr><td valign="top"> First number: </td> <td valign="top"> <input type="text" name="firstnum" size="20"> </td></tr><tr><td valign="top"> Second number: </td> <td valign="top"> <input type="text" name="secondname" size="20"> </td></tr><tr><td valign="top"> <input type="submit" value="Submit Info"></td></tr> </table></form> </body></html>

  21. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); java.io.PrintWriterout = response.getWriter(); out.println("<html><head>"); out.println("<title>Compute test</title></head><body>"); out.println("<h2>Please submit your information</h2>"); out.println("<form method=\"post\" action =\"" + request.getContextPath() + "/web_coompute\" >"); out.println("<table border=\"0\"><tr><td valign=\"top\">"); out.println("First number: </td> <td valign=\"top\">"); out.println("<input type=\"text\" name=\"firstnum\" size=\"20\">"); out.println("</td></tr><tr><td valign=\"top\">"); out.println("Second number: </td> <td valign=\"top\">"); out.println("<input type=\"text\" name=\"secondname\" size=\"20\">"); out.println("</td></tr><tr><td valign=\"top\">"); out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>"); out.println("</table></form>"); out.println("</body></html>"); }

  22. 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("<html><head>"); out.println("<title>Result</title></head><body>"); out.println("<h2>Compute result:</h2>"); intfirstnum = Integer.parseInt(request.getParameter("firstnum")); intsecondnum = Integer.parseInt(request.getParameter("secondname")); int sum = firstnum + secondnum; out.println(Integer.toString(sum)); out.println("</body></html>"); }

  23. What is variable? What is expression? What is Standard arithmetic? What is type conversion? What is Web? What is Servlet? Homework: Install Eclipse + Tomcat in your PC or Mac

More Related