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

Post on 22-Dec-2015

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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 – Ry256stefanko@cs.uchicago.edu

hai@cs.uchicago.edu

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/

CS102

Mailing List

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

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

cs102@cs.uchicago.edu

subscribe at:

CS102 Grading

20% - Homeworks40% - Midterm40% - Project

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

CS102 Project

due 08/29, 10:30am

topic?

What is Java?

Object oriented language.

Object oriented language.

The world around us consists of objects.

e.g. the ATM in Reynolds club

Object oriented language.

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

Object oriented language.

The program consist of objects.

Objects of the same kind form a class.

E.g. class ATM or class Money.

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

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

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

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

void depositMoney(ATMCard card,Money money)

1c)

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

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.

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.

What is Java?

Object oriented language.

What is Java?

Object oriented language.

Java virtual machine.Java platform.

a piece of software(interpreter)

collection of useful classes

+

What is Java?

Compiler

source code

byte code

JVM

Computer

programmer user

What is Java?

Compiler

source code

byte code

JVM

Computer

programmer user

portabilitysecurity

speed

Why Java? • simple• portable• secure• free

• slow

Two kinds of Java programs

applicationsapplets

a small application that can be displayed on a web page

Java Applet Example

Learning Java

• language• libraries

book, lectures

documentation

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

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

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

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

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

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.

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

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?

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.

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.

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

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

width

height

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)

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

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

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

/ 2 , / 2answer width height

answer = (width/2,height/2)

Using the documentation

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

Now we understand

Documentation for Graphics class

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

The result

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

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

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

More complicated programs

Input Output

3,4 7

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

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

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

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

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

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

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

Expressionsmass*velocity*velocity

(a+b)*(a-b)

1/(1-q)

Operators+,-,*,/,%

q

a%5

expression = variable | constant | expression op expression

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

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

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

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

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

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;

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

Assignmentsvariable = expression

The expression type must “fit in” thevariable type.

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

Assignmentsvariable = expression

The expression type must “fit in” thevariable type.

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

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

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

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

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

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

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;

top related