cs 102 – introduction to services/account_request you will need a cs unix account textbook...

63
102 – Introduction to WWW- http://www.cs.uchicago.edu/info/ services/account_request will need a CS unix account Textbook vitch: JAVA - an Introductio uter Science and Programming Daniel Stefankovic – Ry165 v v nstructor: TA: Xuehai Zhang – Ry256 [email protected] [email protected]

Post on 22-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

CS 102 – Introduction to WWW-2

http://www.cs.uchicago.edu/info/services/account_request

You will need a CS unix account

TextbookW.Savitch: JAVA - an Introduction to Computer Science and Programming

Daniel Stefankovic – Ry165Av v

instructor:

TA: Xuehai Zhang – [email protected]

[email protected]

Page 2: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

CS 102 Homeworks, powerpoint presentations

available from the class webpagepeople.cs.uchicago.edu/~stefanko

Homeworks – due Friday 9:00pm

1. download all files to a directory e.g. hw12. solve the problems3. submit the solutions using

hwsubmit cs102 ~/hw1/

Page 3: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

CS102

Mailing List

Office hoursFriday 6:30-8:00pm, Ry256

http://mailman.cs.uchicago.edu/mailman/listinfo/cs102

[email protected]

subscribe at:

Page 4: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

CS102 Grading

20% - Homeworks40% - Midterm40% - Project

Midterm08/22, 10:30-12:30, open book

Page 5: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

CS102 Project

due 08/29, 10:30am

topic?

Page 6: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

What is Java?

Object oriented language.

Page 7: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Object oriented language.

The world around us consists of objects.

e.g. the ATM in Reynolds club

Page 8: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Object oriented language.

The world around us consists of objects.Let the program consist of objects.

Page 9: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Object oriented language.

The program consist of objects.

Objects of the same kind form a class.

E.g. class ATM or class Money.

Page 10: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Object oriented language.

Each object has some methods.Money withdrawMoney(ATMCard card,int amount)

The program consist of objects.Objects of the same kind form a class.

(Objects in the same classhave the same methods.)

Page 11: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Object oriented language.

Money withdrawMoney(ATMCard card,int amount)

A method of the ATM class: parameters

type of return value

myPurse.addMoney(theATMInReynolds.withdrawMoney(myATMCard,1000));

type of the parametername of the parameter

Page 12: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

EXERCISE #1:

Money withdrawMoney(ATMCard card,int amount)

ATM should have depositMoney method.1a) What are its parameters? 1b) What is the type of its return value?Assume that Purse has getMoney method, Money getMoney(int amount)

1c) What statement would deposit $300 from your purse to your account?

myPurse.addMoney(theATMInReynolds.withdrawMoney(myATMCard,1000));

Page 13: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

SOLUTION #1:1a) Card and Money1b) nothing void

void depositMoney(ATMCard card,Money money)

1c)

theATMInReynolds.depositMoney(myCard,myPurse.getMoney(300));

Page 14: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Object oriented language.

The world around us consists of objects.Let the program consist of objects.

more ideas borrowed from the real world:

encapsulation – you do not need to know how the ATM works inside.

Page 15: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Object oriented language.

The world around us consists of objects.Let the program consist of objects.

more ideas borrowed from the real world:

inheritance – you can easily createclass ATMWithClocks extending classATM. The new class inherits the methods of the ATM class.

Page 16: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

What is Java?

Object oriented language.

Page 17: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

What is Java?

Object oriented language.

Java virtual machine.Java platform.

a piece of software(interpreter)

collection of useful classes

+

Page 18: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

What is Java?

Compiler

source code

byte code

JVM

Computer

programmer user

Page 19: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

What is Java?

Compiler

source code

byte code

JVM

Computer

programmer user

portabilitysecurity

speed

Page 20: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Why Java? • simple• portable• secure• free

• slow

Page 21: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Two kinds of Java programs

applicationsapplets

a small application that can be displayed on a web page

Java Applet Example

Page 22: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Learning Java

• language• libraries

book, lectures

documentation

examples on the web(problem – often old version of Java)

http://java.sun.com/docs/

Page 23: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

The first applet

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}

Page 24: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

The first applet

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}

what to draw where to draw it

Page 25: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Viewing the applet 1. compile

javac FirstApplet.java

2. insert following tag in a web page<APPLET CODE="FirstApplet.class" WIDTH="100" HEIGHT="100"></APPLET>

3. view the webpage

Page 26: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Importing packages

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}

Classes are grouped in packages.

E.g. Applet is in java.applet.

Page 27: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Importing packages

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}

Importing a package allows you to use shorter name of the class.

java.applet.Appletjava.awt.Graphics

Page 28: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Extending existing classes

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}

FirstApplet is an Applet (inheritance).

What did we inherit?

Page 29: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Implementing methods

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}

We modify the paint method.

Page 30: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Implementing methods

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}

The body of a method consist of a sequence of statements.

Page 31: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

The important part

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); }}

drawString is a method of theGraphics class. It has some parameters.

Documentation for Graphics class

Page 32: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

The coordinate system The applet is drawn in a rectangle, which consists of pixels.

width

height

Page 33: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

The coordinate system Each pixel has a coordinate (x,y)

x

y

(0,0) (width-1,0)

(0,h

eigh

t-1)(w

idth-1,height-1)

Page 34: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

EXERCISE #2:Let width and height be odd. Whatare the coordinates of the middlepixel?

Page 35: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

SOLUTION #2:width = 3, height = 3answer = (1,1)

answer = ((width-1)/2,(height-1)/2)

/ 2 , / 2answer width height

answer = (width/2,height/2)

Page 36: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Using the documentation

g.drawString("Hello!",20,50);

Now we understand

Documentation for Graphics class

Page 37: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

A modification

import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); g.drawLine(0,0,99,99); g.drawLine(0,99,99,0); }}

Page 38: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

The result

Page 39: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

EXERCISE #3: Modify the paint method of the FirstApplet class to look as follows:

public void paint(Graphics g) { g.drawString("Hello!",20,50); ???? }

Page 40: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

SOLUTION #3: import java.applet.Applet;import java.awt.Graphics;

public class FirstApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello!",20,50); g.drawLine(0,0,99,0); g.drawLine(99,0,99,99); g.drawLine(99,99,0,99); g.drawLine(0,99,0,0); }}

Page 41: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

More complicated programs

Input Output

3,4 7

Page 42: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Create 3 containers that can hold numbers,the containers are labeled firstNumber,secondNumber and sum.Ask the user for the first number and putit in the container firstNumber.Ask the user for the second number and putit in the container secondNumber. Compute the sum of the numbers in containers firstNumber and secondNumberand store it in sum.Output the content of sum.

The Sum program

Page 43: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

import javax.swing.*;

public class Sum { public static void main(String args[]) {

int firstNumber,secondNumber,sum;

firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:"));

sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum);

System.exit(0); }}

The Sum program

Page 44: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

import javax.swing.*;

public class Sum { public static void main(String args[]) {

int firstNumber,secondNumber,sum;

firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:"));

sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum);

System.exit(0); }}

we can have variableswhich contain a number

type,identifier

The Sum program

Page 45: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

import javax.swing.*;

public class Sum { public static void main(String args[]) {

int firstNumber,secondNumber,sum;

firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:"));

sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum);

System.exit(0); }}

we can take user input and store it in a variable

we can output content of a variable

The Sum program

Page 46: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

import javax.swing.*;

public class Sum { public static void main(String args[]) {

int firstNumber,secondNumber,sum;

firstNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the first number:")); secondNumber=Integer.parseInt( JOptionPane.showInputDialog("Enter the second number:"));

sum=FirstNumber+SecondNumber; JOptionPane.showMessageDialog(null,"The sum is " + sum);

System.exit(0); }}

The Sum program we can compute something using values in variables and store the result in a variable

expression,assignment

Page 47: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Type =

int firstNumber,secondNumber,sum;

what kind of thingscan be stored in a variable

int = integer in range –2147483648,+2147483647

similarly byte,short,long

float = floating point number (“real number”)

10 +1=10 20 20

similarly double

Page 48: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Identifier = the name of a variable

Any sequence of letters and digits,starting with a letter, except keywords.

abstract else interface super boolean extends long switch break final native synchronized byte finally new this case float null throw catch for package throws char goto private transient class if protected try const¡ù implements public void continue import return volatile do instanceof short while double int static

Convention: the first letter lowercase

2ndSum

Page 49: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Expressionsmass*velocity*velocity

(a+b)*(a-b)

1/(1-q)

Operators+,-,*,/,%

q

a%5

expression = variable | constant | expression op expression

Page 50: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Expressions expression = variable | constant | expression op expression

Expressions have type!

int a,b; a+b,a*b,a-b,a/b int

float a,b; a+b,a*b,a-b,a/b float

56 int21.2 double

21.2f float

Page 51: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Expressions expression = variable | constant | expression op expression

Expressions have type!

int b; a+b,a*b,a-b,a/b floatfloat a;

behaves as if b were float anything of type int “fits” in float

Page 52: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Expressions expression = variable | constant | expression op expression

Expressions have type!

int b; a+b,a*b,a-b,a/b floatfloat a;

behaves as if b were float anything of type int “fits” in float

Casting (float) b

Page 53: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Expressions expression = variable | constant | expression op expression

Expressions have type!

anything of type int “fits” in float

byte->short->int->long->float->double

a op b

Page 54: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Expressions expression = variable | constant | expression op expression

Expressions have type!

The behavior of some operators depends on the type of operands!

a=7; b=2; a/b is 3 if both a,b are int

a/b is 3.5 if a or b or both are float

Page 55: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

EXERCISE #4Let a=7,b=2,c=2

What is the type and value of (a/b)/c

in the following cases1.int a,b,c;2.int a,b; float c;3.int a; float b,c;4.float a,b,c;

Page 56: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

SOLUTION #4 a=7,b=2,c=2(a/b)/c1. int a,b,c;

(7/2)/2 -> (3)/2 -> 12. int a,b; float c;

(7/2)/2 -> (3)/2 -> 1.53. int a; float b,c; float a,b,c;

(7/2)/2 -> (3.5)/2 -> 1.75

Page 57: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Assignmentsvariable = expression

The expression type must “fit in” thevariable type.

int a,b;float c; a=b+c;

Page 58: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Assignmentsvariable = expression

The expression type must “fit in” thevariable type.

int a,b;float c; a=b+c;

Page 59: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Assignmentsvariable = expression

The expression type must “fit in” thevariable type.

int a,b;float c; a=b+c;

a=(int)(b+c)

Casting will cause truncation

Page 60: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

Assignmentsvariable = expression

The expression type must “fit in” thevariable type.

int a,b;float c;

a=b+c;

a=(int)(b+c)

Casting will cause truncationb=2, c=2.7 -> (int)(b+c)=4

Page 61: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

EXERCISE #5int a,b,c;float d;

You want to put the average of a,b,c to d.

Page 62: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

SOLUTION #5int a,b,c;float d;

You want to put the average of a,b,c to d.

d=(a+b+c)/3;

d=(a+b+c)/3.0;

d=(a+b+c)/3.0f;

d=(float)(a+b+c)/3; ?

Page 63: CS 102 – Introduction to   services/account_request You will need a CS unix account Textbook W.Savitch: JAVA - an

SOLUTION #5int a,b,c;float d;

You want to put the average of a,b,c to d.

d=(a+b+c)/3;

d=(a+b+c)/3.0;

d=(a+b+c)/3.0f;

d=(float)(a+b+c)/3;