S517 Web Programming Assistant Professor Xiaozhong Liu

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
CMT Programming Software Applications
Some basic I/O.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
S517 Web Programming Assistant Professor Xiaozhong Liu
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CSE 131 Computer Science 1 Module 1: (basics of Java)
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Mixing integer and floating point numbers in an arithmetic operation.
ECE 122 Feb. 1, Introduction to Eclipse Java Statements Declaration Assignment Method calls.
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Programming for Interactivity Professor Bill Tomlinson Tuesday & Wednesday 6:00-7:50pm Fall 2005.
Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Web Programming: Loop Xiaozhong Liu
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Web Programming: If Statement and Servlet
Computing Fundamentals
Lecture 2: Data Types, Variables, Operators, and Expressions
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2.
Statements, Comments & Simple Arithmetic
Chapter 2: Basic Elements of Java
Fundamentals 2.
elementary programming
Building Java Programs
Building Java Programs
Building Java Programs
Unit 3: Variables in Java
Names of variables, functions, classes
Presentation transcript:

S517 Web Programming Assistant Professor Xiaozhong Liu

Command line Or GUI Compute result Command line Or GUI Compute result Java Numeric Text File Internet … Numeric Text File Internet …

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…

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; }

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); }

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 Information problem… Input Variable double price, pricewithtax; price = price = price* pricewithtax = price

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

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…

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!

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 = ; long large_number = ; 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)

Variable double number = ; double number = ; Double: range from e-324d to e+308d (positive or negative). (64-bit) float number = 2.634; float number = 2.634; Float: range from e-45 to e+38 (positive or negative). (32-bit)

Variable String name = “Xiaozhong Liu”; String name = “Xiaozhong Liu”; String has some operation: String name = “Xiaozhong Liu”; name = name + “ is a SLIS”; System.out.println(name); int num = 20; System.out.println(“The result is: ” + num);

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 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…

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

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

Java basics - syntax Format your code by using eclipse

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);

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;

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);

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

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);

double result = 2.0/7; double result = 15/2.0; int num1 = 10; int num2 = 3; double result = (double)num1 / (double)num2;

Exercise: Implement the following expression:

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

Exercise: To convert from meters to feet, multiply the number of meters by final double CONVERT_METER_TO_FEET = ; 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!!

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

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

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???

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"));

Compute test Please submit your information First number: Second number:

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(" "); }

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(" "); }

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