chapter 1

36
Chapter 1 Introducti on to Computers and Java 1

Upload: valentine-hyde

Post on 01-Jan-2016

28 views

Category:

Documents


1 download

DESCRIPTION

Chapter 1. Introduction to Computers and Java. Chapter 1. 1.1 Computer Basics 1.2 A Sip of Java 1.3 Programming Basics 1.4 Graphics Supplement. 1.1 Computer Basics. The CPU is the brain. Memory is the storage. RAM is volatile. Secondary memory is permanent. - PowerPoint PPT Presentation

TRANSCRIPT

1

Chapter 1

Introduction to

Computers and Java

2

Chapter 1

1.1 Computer Basics

1.2 A Sip of Java

1.3 Programming Basics

1.4 Graphics Supplement

3

1.1 Computer

Basics

4

The CPU is the brain

5

Memory is the storage

6

RAM is volatile

7

Secondary memory is permanent

8

Memory is byte addressable

1000

999

998

997

996

995

994

993

0000 0000

0000 0000

0000 0000

0000 0001

1000 0001

1

'A'

4 byte memory locationat address

8 bits = 1 byte

9

Programmers writeprograms

10

Computers executeprograms

11

Programs must betranslated

Java code Machine code

12

Using a Compiler<Compile once – run many>

Source codeHigh level

CompilerAssembly code

Low level Binary codeMachine level

13

Using an Interpreter<Compile-run cycle>

InterpreterSource code

High level

Assembly codeLow level

Binary codeMachine level

Programexecution

14

The Java way usesboth techniques

Java Virtual Machine<Interpreter>

15

A class loader combinesall the bytecode files

16

1.2 A Sip of Java

17

Text Application Deconstructed<FirstProgram.java>

import java.util.Scanner;

System.out.println("Please enter two numbers and"); System.out.println("I will compute their sum.");

int n1; int n2; Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt(); System.out.println("Their sum is"); System.out.println(n1 + n2); }// end main()

public static void main(String[] args) {

public class FirstProgram{

}// end FirstProgram

Program uses the scanner

class

Inform the user

Declare variables Read from

the keyboard

Give user an answer

18

Text Application Deconstructed<output>

Please enter two numbers andI will compute their sum.10 30Their sum is40

19

GUI Program Deconstructed<HappyFace.java>

package happyface;

import javax.swing.JFrame;import java.awt.Graphics;

public class HappyFaceApplet extends JFrame { @Override public void paint(Graphics canvas) {

}}

canvas.drawOval(100, 50, 200, 200); canvas.fillOval(155, 100, 10, 20); canvas.fillOval(230, 100, 10, 20); canvas.drawArc(150, 160, 100, 50, 180, 180);

Classes we need for GUI

programs

Using drawing commands

20

GUI Program Deconstructed<HappyFace.java>

public HappyFace() { setSize(600, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); }// end HappyFace()

public static void main(String[] args) { HappyFace guiWindow = new HappyFace(); guiWindow.setVisible(true); }// end main() }// end HappyFace

Set window's initial width and

height

Initialize the window and

make it visible

Exit program when window is

closed.

21

1.3 Programming

Basics

22

Java is OO

23

Objects can representmany things

24

A class providesthe blueprint

Car

Attributes<instance variables>

Behavior <methods>

25

OOP is based onthree pillars

Encapsulati

on

Inhe

ritan

ce

Polymorph

ism

26

Encapsulation = Information Hiding

You can drive a car, but you may not know how an engine works.

27

Inheritance = Reuse / Extend

Objects can be reused

Objects can be extended

28

Polymorphism = React in own way

Come back!

29

Before coding beginsdevelop your algorithms

Algorithms come inmany flavors

30

Once coding beginsbe aware of bugs

Syntax – Grammar not followed

Run-time – Computer can't honor request

Logic – Programmer slip

31

1.4Graphics

Supplement

32

The Coordinate System

(0,0)

(100, 50)

X

Y

33

Drawing an Oval

(0,0)

(100, 50)

X

Y

canvas.drawOval(100, 50, 100, 100);

34

Filling an Oval

(0,0)

(100, 50)

X

Y

canvas.fillOval(155, 100, 10, 20);

35

Filling an Oval

(0,0)

(100, 50)

X

Y

canvas.fillOval(230, 100, 10, 20);

36

Drawing an Arc

(0,0)

(150, 160)

X

Y

canvas.drawArc(150, 160, 100, 50, 180, 180);