190 likes | 338 Views
http://discern.uits.iu.edu:8790/S517/S517.html. Introduction to Application Programming. IST 256 Application Programming for Information Systems Xiaozhong Liu. Variable type. Variable name (no space, start With alphabet, case sensitive). String String name = “ Obama ” ;. Variable.
E N D
http://discern.uits.iu.edu:8790/S517/S517.html Introduction toApplication Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
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)
More Assignment Statements • The value on the right-hand-side of an assignment statement can be computed with an arithmetic expressionpricewithtax = price * 1.0825; • This can include other variables or even the same variable price = 5; price = price + 10.0; First compute the value Move that value to be the new value of the variable
Output 1 Input Process Information problem… Output 2 Yes great, you get it! I need some wine How old are you? Age > 20??? No Sorry, you are not old enough!
If-Then statement if (comparison) (boolean variable) { [some actions] } ELSE { [some different actions] } Selection
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); Example 1
Shopping problem. If you purchase $50 or more, you get 10% off for your total bill; otherwise, you get 5% discount. Practice 1
If you have data plan, your monthly payment is 59.99; if you don’t have data plan, you only pay 39.99 Example 2 booleandataplan; double payment; dataplan = true; //true or false if (dataplan) { payment = 59.99; } else { payment = 39.99; }
If you are VIP user, and you purchased $100 above, you get 15% discount Multiple conditions boolean VIP; double payment; //VIP and payment input if (VIP == true && payment > 100) { payment = payment * (1 – 0.15); }
If you are VIP user, or you purchased $100 above, you get 15% discount boolean VIP; double payment; //VIP and payment input if (VIP == true || payment > 100) { payment = payment * (1 – 0.15); } Multiple conditions
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. Practice 2
What is variable? What is expression? What is Standard arithmetic? What is IF-ELSE statement? What is multiple conditions? How to add the code to GUI?
Classes and Functions • Java organizes programs and its programming libraries into units called “classes” • • Some classes contain useful functions • • Functions are named by giving the class name, “.”, and the function name • • Example: the Math class contains a large number of mathematical functions • e.g. Math.pow computes exponents • • To call a function, give its name followed by any argument: • Math.pow(2.0 , 3.0 ) expression that computes 2 to the power 3 • • Value of this expression is 8.0 • • Or use the value in an assignment: result = Math.pow(2.0 , 3.011)
Form Objects and Functions The object-oriented part of Java is that many values are created as “objects” that are “instances” of classes Examples of classes used in creating forms: – JFrame, JButton, jLabel, jTextField Each time that we create a textfield on a form, Java creates an instance of the class – jTextField1, jTextField2, jTextField3 There are functions that we can use for any instance, and these functions also use the “.” – // getText() returns a string from the TextField String text = jTextField1.getText ( ); – // setText takes a string argument and puts it into the TextField jTextField1.setText( text );
<html><head> • <title>Compute test</title></head><body> • <h2>Please submit your information</h2> • <form method="post" action ="/week1/web_coompute" > • <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> Web Programming
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub 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>"); } 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.PrintWriterout = 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")); intsum = firstnum + secondnum; out.println(Integer.toString(sum)); out.println("</body></html>"); } Web Programming
protected void doPost(HttpServletRequestrequest, HttpServletResponseresponse) throws ServletException, IOException { // TODO Auto-generated method stub Enumeration paramNames = request.getParameterNames(); response.setContentType("text/html"); java.io.PrintWriterout = 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")); intsum = firstnum + secondnum; out.println(Integer.toString(sum)); out.println("</body></html>"); } Web Programming