Presentation is loading. Please wait.

Presentation is loading. Please wait.

Drawing rectangles and ovals in the Applet window Displaying Text in the Java Console Window Demo of the HelloAgain program Arithmetic expressions Examples.

Similar presentations


Presentation on theme: "Drawing rectangles and ovals in the Applet window Displaying Text in the Java Console Window Demo of the HelloAgain program Arithmetic expressions Examples."— Presentation transcript:

1 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

2 Labs 15% (Not 25) Test 20% (Not 10) Final Exam 65% Lectures Final exam HomeworkFinal exam Labwork Final exam Important Note 1

3 Important Note 2 You must gain a pass in BOTH Practical work 15/15 AND Theory (Test + exam) 37/75  fail

4 Important Note 3 Friday’s lecture –Lab08 given out and discussed –Demos –Please bring your Explorer’s guide

5 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 Again on the computer screen */ Programs consist of Statements and Comments in a particular order.

6 Drawing 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

7 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 );

8 Drawing Rectangles p18 g.drawRect(left, top, width, height); Example: g.drawRect( 50, 60, 100, 70 ); 100 70

9 Note the order of the parameters g.drawRect(left, top, width, height); g.setColor(Color.red); g.fillRect(left, top, width, height); 100 70 height top width left

10 Note the order of the parameters g.drawRect(left, top, width, height); g.setColor(Color.red); g.fillRect(left, top, width, height); 100 70 height top width left

11 Drawing Ovals (and circles) p20 g.drawOval(left, top, width, height); Example: g.drawOval( 50, 60, 100, 70 ); 100 70

12 Drawing Ovals (and circles) p20 g.fillOval(left, top, width, height); Example: g.fillOval( 50, 60, 100, 70 ); 100 70

13 Desk 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 */

14 List of Terms for Chapter 1 p26 Comment10 (9) Coordinates11(10) drawLine16 (15) drawOval21 (20) drawRect19 (18) drawString12 (11) Font13 (12) Output9 (8) parameter14 (13) Pixel11 (10) Program7 setColor23 (22) Statement 7

15 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"); // * * * * * * * * * * * * * * * * * * * * * }

16 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 Strings

17 Order of Program Execution Statements are executed one after another

18 import java.awt.*; import java.applet.Applet; public class ScrambledEggs extends Applet{ public void init() { // * * * * * * * * * * * * * * * * * * * * * * * * * * * /* Program to give instructions on how to make scrambled 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. p35 There are at least 8 mistakes in this program

19 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 Expressions E. G. p40

20 Unlike strings numbers must not be enclosed in quotes! Arithmetic operators +plus -minus * multiplication / division

21 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));

22 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 Expressions E. G. p41

23 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. Brackets

24 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

25 Drawing Ovals (and circles) p20 g.drawOval(left, top, width, height); Example: g.drawOval( 50, 60, 100, 70 ); 100 70


Download ppt "Drawing rectangles and ovals in the Applet window Displaying Text in the Java Console Window Demo of the HelloAgain program Arithmetic expressions Examples."

Similar presentations


Ads by Google