Download presentation
Presentation is loading. Please wait.
Published byDorthy Barton Modified over 9 years ago
1
S517 Web Programming Assistant Professor Xiaozhong Liu http://scholarwiki.indiana.edu/S517/S517.html
2
Command line Or GUI Compute result Command line Or GUI Compute result Java Numeric Text File Internet … Numeric Text File Internet …
3
What is programming? Real WorldProgramming 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 Int birth_year; birth_year = 1979 How old? Int age; age = 2010 – birth_year Use computer + programming to solve real-world problem…
4
A java example Real world problem: how to describe a student? 1. Define student public class student { String name; int age; } 2. Describe a student static void main public static void main(String[] args) { student stud_A = new student( ); stud_A.name = “Xiaozhong Liu” stud_A.age = 30; }
5
A java example Real world problem: add two numbers static void main 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); }
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 Information problem… Input Variable double price, pricewithtax; price = 65.20 price = price*1.0825 pricewithtax = price
8
VariableString String name = “Obama”; String name = “Obama”; int int age = 30; int age = 30; double double price = 15.25; double price = 15.25; boolean boolean dataplan = true; (or false) boolean dataplan = true; (or false) Variable type Variable name (no space, start With alphabet, case sensitive) Variable value
9
Variable String name = “Obama”; String name = “Obama”; Variable type Variable name (no space, start With alphabet, case sensitive) Name should be readable or understandable int a, b, c, k1… //not very good names… int temperature, femalePercentage, heartRate… //good names Variable name feels like nouns, later, method name feels like verb…
10
Variable String name = “Obama”; String name = “Obama”; Variable type Variable name (no space, start With alphabet, case sensitive) Each variable is an instance of a CLASS! int x, y; Student xiaozhong = new Student(); Mistake: re-define variable! Use variable without define! Type mismatch!
11
Variable int number = 58; int number = 58; Integer: minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (32-bit) short number = 12; short number = 12; long large_number = 41244523342; long large_number = 41244523342; Short: minimum value of -32,768 and a maximum value of 32,767 (16-bit) Long: minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (64-bit)
12
Variable double number = 0.32413; double number = 0.32413; Double: range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). (64-bit) float number = 2.634; float number = 2.634; Float: range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). (32-bit)
13
Variable String name = “Xiaozhong Liu”; String name = “Xiaozhong Liu”; String has some operation: String name = “Xiaozhong Liu”; name = name + “ is a professor @ SLIS”; System.out.println(name); int num = 20; System.out.println(“The result is: ” + num);
14
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
15
Java basics - comments Comments – plain English text help you/colleagues understand the code logic… VERY IMPORTANT for system maintenance. Document purpose, author, revision, history, copyright… Describe variable, class, methods…
16
Java basics - comments Block of comments between /* …… */ Single-line comments /** * Servlet implementation class SearchLab */ /** * Servlet implementation class SearchLab */ double pricewithtax; //save the price information with 8% tax
17
Java basics - syntax public class ABC { public static void main(String[] args) { int x, y; …… } Class start with { End with } Class start with { End with } Always end with semicolon
18
Java basics - syntax Format your code by using eclipse
19
Standard arithmetic Java supports basic arithmetic operators: +, -, *, /, and %. Add two numbers? int number1, number2, result; numbers1 = 18; number2 = 9; result =number1+ number2; System,out.println(result);
20
Standard arithmetic Java special assignments a = a + b; a+ = b; a = a - b; a- = b; a = a * b; a* = b; a = a / b; a/ = b; a = a % b; a% = b; a++; a = a + 1; a--; a = a - 1;
21
Standard arithmetic The type of the result is determined by the types of the operands, not the values! i.e., [int] + [double] [double] int x = 2; double y = 0.49; int z = x + y; System.out.println("result is: " + z); int z= (int)(x + y); int z= (int)Math.round(x + y);
22
double x, y; x = 7.486; y = x+15*x-7/x*x; (?????????) System,out.println(y);
23
If a and b are integers, a/b [int] i.e., 10/3 = 3; 3/4 = 0; 4/5.0 = 0.8 double result = 3/4; System.out.println(result);
24
double result = 2.0/7; double result = 15/2.0; int num1 = 10; int num2 = 3; double result = (double)num1 / (double)num2;
25
Exercise: Implement the following expression:
26
Exercise: To convert from meters to feet, multiply the number of meters by 3.28084 1.71 meter = ???? Feet 5.68 feet = ???? meter
27
Exercise: To convert from meters to feet, multiply the number of meters by 3.28084 final double CONVERT_METER_TO_FEET = 3.28084; double meter = ???; double feet = meter * CONVERT_METER_TO_FEET; System.out.println(“The result is: ” + feet + “ feet”); final int NUMBER_OF_HOURS_IN_A_DAY = 24; NUMBER_OF_HOURS_IN_A_DAY = 23; //WRONG!!
28
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
29
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
30
int number1 = 5; String number2 = “10”; int result; result = number1 + number2; What is the result??? Variable type conversion
31
double number; String str = “10.735”; number = Double.parseDouble(str) StringStringintint str = Integer.toString(num) num = Integer.parseInt(str); StringStringdoubledouble str = Double.toString(num) num = Double.parseDouble(str); Two questions: 1.What is “Double” and “Integer”??? 2. Why we need type conversion???
32
int firstnum = Integer.parseInt(request.getParameter("firstnum")); int secondnum = Integer.parseInt(request.getParameter("secondname")); int sum = firstnum + secondnum; out.println(Integer.toString(sum)); int firstnum = Integer.parseInt(request.getParameter("fi rstnum"));
33
Compute test Please submit your information First number: Second number:
34
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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(" "); }
35
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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(" "); }
36
1.What is variable? 2.What is expression? 3.What is Standard arithmetic? 4.What is type conversion? 5.What is Web? What is Serverlet? Homework: Install Eclipse + Tomcat in your PC or Mac
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.