Introduction To Www-2 Daniel Stefankovic – Ry165a Xuehai Zhang – Ry256

  • Uploaded by: arlenebareno
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Introduction To Www-2 Daniel Stefankovic – Ry165a Xuehai Zhang – Ry256 as PDF for free.

More details

  • Words: 1,663
  • Pages: 63
CS 102 – Introduction to WWW-2 instructor: TA:

v

Daniel Stefankovic – Ry165A v

[email protected]

Xuehai Zhang – Ry256 [email protected]

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 [email protected] 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. A method of the ATM class:

parameters

Money withdrawMoney(ATMCard card,int amount)

type of return value type of the parameter

name of the 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

portability security

What is Java? source code

JVM Compiler Computer

byte code programmer

user

speed

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 • language • libraries

book, lectures 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">

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

y (0,height-1)

(width-1,height-1)

Each pixel has a coordinate (x,y) (0,0) (width-1,0) x

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 we can have variables public class Sum { which contain a number public static void main(String args[]) { 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; JOptionPane.showMessageDialog(null,"The sum is " + sum); System.exit(0); } }

type,identifier

The Sum program import javax.swing.*; public class Sum { public static void main(String args[]) {

we can take user input and 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 2nd Sum

abstract boolean break byte case catch char class const¡ù continue do double

else extends final finally float for goto if implements import instanceof int

interface long native new null package private protected public return short static

super switch synchronized this throw throws transient try void volatile while

Expressions mass*velocity*velocity (a+b)*(a-b) 1/(1-q) expression = q variable | a%5 Operators +,-,*,/,%

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 float a,b; a+b,a*b,a-b,a/b 56 21.2

int double

21.2f

int float float

Expressions

expression = variable | constant | expression op expression

Expressions have type! int b; float a;

a+b,a*b,a-b,a/b

float

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; float a;

float a+b,a*b,a-b,a/b Casting (float) b

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

Expressions

expression = variable | constant | expression op expression

Expressions have type! byte->short->int->long->float->double a op b anything of type int “fits” in float

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 #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 (a/b)/c

1. int 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; float c;

a=b+c;

Assignments variable = expression The expression type must “fit in” the variable type. int a,b; float c;

a=b+c;

Assignments variable = expression The expression type must “fit in” the variable type. int a,b; float c;

a=b+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; a=(int)(b+c) float 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.0; d=(float)(a+b+c)/3;

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

Related Documents

Jerry Zhang
July 2020 18
Lab1-zhang
May 2020 14
Zhang 2018
October 2019 23
Review Zhang
July 2020 9
Introduction To
November 2019 56

More Documents from ""

May 2020 0
L01_intro
May 2020 0