Drawing rectangles and ovals in the Applet window Displaying Text in the Java Console Window Demo of...

25
• Drawing rectangles and ovals in the Applet window • Displaying Text in the Java Console Window • Demo of the HelloAgain program • Arithmetic expressions • Examples 415.111 Wednesday 7 th May, 2003 Java Lecture 2: Overview

Transcript of Drawing rectangles and ovals in the Applet window Displaying Text in the Java Console Window Demo of...

• Drawing rectangles and ovals in the Applet window

• Displaying Text in the Java Console Window

• Demo of the HelloAgain program

• Arithmetic expressions

• Examples

415.111 Wednesday 7th May, 2003Java Lecture 2: Overview

415.111 Wednesday 7th May, 2003Java Lecture 2: Overview

• Labs 15% (Not 25)

• Test 20% (Not 10)

• Final Exam 65%

• Lectures Final exam

• Homework Final exam

• Labwork Final exam

Important Note 1Important Note 1

Important Note 2

• You must gain a pass in BOTH

• Practical work 15/15

AND

Theory (Test + exam) 37/75 fail

Important Note 3

• Friday’s lecture – Lab08 given out and discussed– Demos– Please bring your Explorer’s guide

• Statements: g.drawString("Hello Again", 70, 180);

• Parameters: the 3 items inside the brackets are parameters

• Comments:

– One line comments, e.g.// A trivial program to display Hello Again

– Multiple line comments, e.g./*A trivial program to display Hello Againon the computer screen*/

Programs consist of Statements and Commentsin a particular order.

Programs consist of Statements and Commentsin a particular order.

Drawing Lines p15Drawing Lines p15

// Draw a line from the origin to the point (100, 100)

g.drawLine( 0, 0, 100, 100 );

// Draw a line from the point (10,5) to (16, 5)

g.drawLine( 10,5, 16,5 );

NOTE: NOT 16

Exercise 2 p 17 Exercise 2 p 17

What is the output of this program segment? Use a sheet of squared paper to help you find out.

g.drawLine( 40, 100, 130, 50 );g.drawLine( 130, 50 , 130, 150 );g.drawLine( 130, 150, 40, 100 );g.drawLine( 160, 100, 70, 50 );g.drawLine( 70, 50, 70, 150 );

g.drawLine( 70, 150, 160, 100 );

Drawing Rectangles p18Drawing Rectangles p18

g.drawRect(left, top , width, height);

Example:g.drawRect( 50, 60, 100, 70 );

100

70

Note the order of the parametersNote the order of the parameters

g.drawRect(left, top , width, height);

g.setColor(Color.red); g.fillRect(left, top , width, height);

100

70height

top

width

left

Note the order of the parametersNote the order of the parameters

g.drawRect(left, top , width, height);

g.setColor(Color.red); g.fillRect(left, top , width, height);

100

70height

top

width

left

Drawing Ovals (and circles) p20Drawing Ovals (and circles) p20

g.drawOval(left, top , width, height);

Example:g.drawOval( 50, 60, 100, 70 );

100

70

Drawing Ovals (and circles) p20Drawing Ovals (and circles) p20

g.fillOval(left, top , width, height);

Example:g.fillOval( 50, 60, 100, 70 );

100

70

Desk Checking and Testing p25Desk Checking and Testing p25

(i) Start by drawing (accurate) diagrams in pencil on squared paper.

(ii) Check the keywords

(iii) Check carefully: the order of all parameters and the values of all parameters.

(iv) Enter and test your program a few lines at a time.

At the worst, comment out sections of the program by putting in /* and */

List of Terms for Chapter 1 p26List of Terms for Chapter 1 p26

Comment 10 (9)Coordinates 11(10)drawLine 16 (15)drawOval 21 (20)drawRect 19 (18)drawString 12 (11)Font 13 (12)Output 9 (8)parameter 14 (13)Pixel 11 (10)Program 7

setColor 23 (22)Statement 7

Chapter 2: Displaying Text in the Java Console Window FirstHello.java: Explorer’s Guide p30

Chapter 2: Displaying Text in the Java Console Window FirstHello.java: Explorer’s Guide p30

Now let us look at the program in the HelloAgain.java file:

import java.awt.*;import java.applet.Applet;public class FirstHello extends Applet{

public void init() {

// * * * * * * * * * * * * * * * * * * * * * // A Trivial program that displays a string in// the Java Console window

System.out.println("Hello Computer Again");// * * * * * * * * * * * * * * * * * * * * * }

}

• A string is any sequence of characters on the screen.

• To display the string: Hello Computer on the screen

– The command we used was:System.out.println(“Hello Computer”);

• What will be displayed if we use the following lines?• System.out.println(“Junk”);

• System.out.println“^&*(*”);

• System.out.println(“6 + 6 = 200”);

• Notice that the quotes do not appear on the screen.

Displaying StringsDisplaying Strings

Order of Program Execution

• Statements are executed one after another

import java.awt.*;import java.applet.Applet;

public class ScrambledEggs extends Applet{public void init() {

// * * * * * * * * * * * * * * * * * * * * * * * * * * * /*

Program to give instructions on how to makescrambled eggs System.out.println();System.out.println"Whisk");System.out.prntln("Break eggs into bowl)system.out.println('Cook gently');

// * * * * * * * * * * * * * * * * * * * * * * * * * * *

Exercise 2 Bad Eggs E.G. p35There are at least 8 mistakes in this program

Exercise 2 Bad Eggs E.G. p35There are at least 8 mistakes in this program

• We can instruct the computer to do arithmetic for us.

• We shall begin by using only integers: (numbers with no fractional part, e.g. 23, 1990, 0, -899).

• Numbers must NOT be enclosed in quote marks if they are to be used in calculations.

Arithmetic ExpressionsE. G. p40

Arithmetic ExpressionsE. G. p40

• Unlike strings numbers must not be enclosed in quotes!

Arithmetic operatorsArithmetic operators

+ plus

- minus

* multiplication / division

Expressions E. G. p41

Expressions E. G. p41

// Program to show how integers can be used// in arithmetic expressions

System.out.println();System.out.println(200);System.out.println(1999 + 1);System.out.println(2000 - 1999);

System.out.println(4 * 5 + 6); System.out.println(4 * (5 + 6));

System.out.println("4 * 5 = " + (4 * 5));

First, any expressions in brackets are evaluated

Then any multiplications or divisions are evaluated (from left to right, taking account of brackets)

Then any additions or subtractions are evaluated (from left to right again), thus arriving at the value of the expression.

Order of Evaluating ExpressionsE. G. p41

Order of Evaluating ExpressionsE. G. p41

• Needed for changing the order in which the computer will evaluate an expression.

• If an expression follows a string you should enclose it in brackets.

• For example:.

System.out.println("2 + 3 = " + 2 + 3) --->?

System.out.println("2 + 3 = " + (2 + 3)) --->?

• We also use brackets to make our expressions clearer to read.

BracketsBrackets

Homework

• Finish reading all Chapter 1 of the Explorers Guide

• Do as many exercises in Chapter 1 as you can.

• Read Chapter 2, pages 28 – 40

• Do as many Chapter 2 exercises as you can.

• Bring any questions to Friday’s lecture

Drawing Ovals (and circles) p20Drawing Ovals (and circles) p20

g.drawOval(left, top , width, height);

Example:g.drawOval( 50, 60, 100, 70 );

100

70