230 likes | 319 Views
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.
E N D
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 Java
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…
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; }
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
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
Output Input Process Information problem… Variable double price, pricewithtax; price = 65.20 price = price*1.0825 pricewithtax = price
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)
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
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);
double x, y; x = 7.486; y = x+15*x-7/x*x; (?????????) System.out.println(y);
Exercise: To convert from meters to feet, multiply the number of meters by 3.28084 1.71 meter = ???? Feet 5.68 feet = ???? meter
Exercise: Implement the following expression:
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
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
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
Variable type conversion int number1 = 5; String number2 = “10”; int result; result = number1 + number2; What is the result???
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???
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));
<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>
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>"); }
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>"); }
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