Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 63

CS 102 – Introduction to WWW-2

v
instructor: Daniel Stefankovic – Ry165A
v

stefanko@cs.uchicago.edu
TA: Xuehai Zhang – Ry256
hai@cs.uchicago.edu
You will need a CS unix account
http://www.cs.uchicago.edu/info/
services/account_request
Textbook
W.Savitch: JAVA - an Introduction to
Computer Science and Programming
CS 102
Homeworks, powerpoint presentations
available from the class webpage
people.cs.uchicago.edu/~stefanko
Homeworks – due Friday 9:00pm
• download all files to a directory e.g. hw1
• solve the problems
• submit the solutions using
hwsubmit cs102 ~/hw1/
CS102
Office hours
Friday 6:30-8:00pm, Ry256

Mailing List
cs102@cs.uchicago.edu
subscribe at:
http://mailman.cs.uchicago.edu/mailman/listinfo/cs102
CS102
Grading
20% - Homeworks
40% - Midterm
40% - Project
Midterm
08/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.
The program consist of objects.
Objects of the same kind form a class.
Each object has some methods.
Money withdrawMoney(ATMCard card,int amount)

(Objects in the same class


have the same methods.)
Object oriented language.
parameters
A method of the ATM class:
Money withdrawMoney(ATMCard card,int amount)

type of return value


name of the
type of the parameter
parameter

myPurse.addMoney(theATMInReynolds.
withdrawMoney(myATMCard,1000));
EXERCISE #1:
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?
Money withdrawMoney(ATMCard card,int amount)
myPurse.addMoney(theATMInReynolds.withdrawMoney(
myATMCard,1000));
SOLUTION #1:
1a) Card and Money
1b) 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 create


class ATMWithClocks extending class
ATM. 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?
source code

Compiler JVM

byte code Computer

programmer user
What is Java? portability
security
source code
JVM
Compiler

Computer
byte code

speed
programmer user
Why Java?
• simple
• portable
• secure
• free

• slow
Two kinds of Java programs
applications
applets
a small application that can be
displayed on a web page

Java Applet Example


Learning Java
book, lectures
• language
• libraries
documentation
http://java.sun.com/docs/

examples on the web


(problem – often old version of Java)
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);
}
} java.awt.Graphics
java.applet.Applet

Importing a package allows you to


use shorter name of the class.
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);
}
}
Documentation for Graphics class

drawString is a method of the


Graphics class. It has some parameters.
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)
(0,0) (width-1,0)

(width-1,height-1)
x
y
(0,height-1)
EXERCISE #2:
Let width and height be odd. What
are the coordinates of the middle
pixel?
SOLUTION #2:
width = 3, height = 3
answer = (1,1)
answer = ((width-1)/2,(height-1)/2)
answer = (  width / 2  ,  height / 2  )

answer = (width/2,height/2)
Using the documentation
Now we understand
g.drawString("Hello!",20,50);

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
The Sum program
Create 3 containers that can hold numbers,
the containers are labeled firstNumber,
secondNumber and sum.
Ask the user for the first number and put
it in the container firstNumber.
Ask the user for the second number and put
it in the container secondNumber.
Compute the sum of the numbers in
containers firstNumber and secondNumber
and 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.*;
we can have variables
public class Sum { which
public static void main(String args[]) {
contain a number
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);

}
}
type,identifier
The Sum program
import javax.swing.*;
we can take user input and
public class Sum {
public static void main(String args[]) { store it in a variable
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 output content of a variable
The Sum program
import javax.swing.*;
we can compute something
public class Sum { using values in variables and
public static void main(String args[]) {
store the result in a variable
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);

}
}
expression,assignment
Type = what kind of things
can be stored in a
variable
int firstNumber,secondNumber,sum;

int = integer in range –2147483648,+2147483647


similarly byte,short,long

float = floating point number (“real number”)


similarly double

20 20
10 +1=10
Identifier = the name of a
variable
Any sequence of letters and digits,
starting with a letter, except keywords.
Convention: the first letter lowercase
abstract else interface super

2nd boolean
break
byte
extends
final
finally
long
native
new
switch
synchronized
this

Sum
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
Expressions
mass*velocity*velocity
(a+b)*(a-b)
1/(1-q)
expression =
q variable |
constant |
a%5 expression op expression
Operators
+,-,*,/,%
expression =
Expressions 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 int 21.2f float


21.2 double
expression =
Expressions variable |
constant |
expression op expression
Expressions have type!

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


float a;
behaves as if b were float
anything of type int “fits” in float
expression =
Expressions variable |
constant |
expression op expression
Expressions have type!

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


float a; Casting (float) b
behaves as if b were float
anything of type int “fits” in float
expression =
Expressions variable |
constant |
expression op expression
Expressions have type!

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

a op b

anything of type int “fits” in float


expression =
Expressions 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 #4
Let a=7,b=2,c=2
What is the type and value of
(a/b)/c
in the following cases
1.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
1. int a,b,c; (a/b)/c
(7/2)/2 -> (3)/2 -> 1
2. int a,b; float c;
(7/2)/2 -> (3)/2 -> 1.5
3. int a; float b,c;
float a,b,c;
(7/2)/2 -> (3.5)/2 -> 1.75
Assignments
variable = expression

The expression type must “fit in” the


variable type.
int a,b;
a=b+c;
float c;
Assignments
variable = expression

The expression type must “fit in” the


variable type.
int a,b;
a=b+c;
float c;
Assignments
variable = expression

The expression type must “fit in” the


variable type.
int a,b;
a=b+c;
float c;
a=(int)(b+c)
Casting will cause truncation
Assignments
variable = expression

The expression type must “fit in” the


variable type.
a=b+c;
int a,b;
float c; a=(int)(b+c)

b=2, c=2.7 -> (int)(b+c)=4


Casting will cause truncation
EXERCISE #5
int a,b,c;
float d;
You want to put the average of a,b,c to d.
SOLUTION #5
int 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.0f;
d=(a+b+c)/3.0;
d=(float)(a+b+c)/3; ?
SOLUTION #5
int 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.0f;
d=(a+b+c)/3.0;
d=(float)(a+b+c)/3;

You might also like