Professional Documents
Culture Documents
Introduction To WWW-2 Daniel Stefankovic - Ry165A Xuehai Zhang - Ry256
Introduction To WWW-2 Daniel Stefankovic - Ry165A Xuehai Zhang - Ry256
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?
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:
Compiler JVM
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
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);
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.*;
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;
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;
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;
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;
System.exit(0);
}
}
expression,assignment
Type = what kind of things
can be stored in a
variable
int firstNumber,secondNumber,sum;
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!
byte->short->int->long->float->double
a op b