Variables and Methods Chapter 3 – Lecture Slides.

Slides:



Advertisements
Similar presentations
Topic 9 more graphics Based on slides bu Marty Stepp and Stuart Reges from
Advertisements

Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Helper Methods ITP © Ron Poet Lecture 11.
Applets An applet is similar to a Java program, but it runs within a Web-browser on a client machine. For security, you cannot change anything on a client.
Java Applets A First Program. Applet Example /* The world’s simplest program, repeated once more in another format in an attempt to turn all of you into.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
(c)2003 E.S.Boese1 Loops Chapter 13 - Student. (c)2003 E.S.Boese 2 Repetition Statements Repetition statements allow us to execute a statement ________.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
A Simple Applet.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2 Software.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
TCU CoSc Introduction to Programming (with Java) Variables and Methods.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
(c)2009 by E.S. Boese. All Rights Reserved. Classes Chapter 13 – Lecture Slides 1 Got class?
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Variables and Methods Chapter 3 – Lecture Slides 1(c) 2008 by E.S.Boese. All Rights Reserved. Variables vary... After all, change is the status quo.
Building Java Programs Graphics Reading: Supplement 3G.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
Introducing, the JFrame Gives us a work area beside System.out.println.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
A structured walkthrough
Review.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Method Mark and Lyubo.
Building Java Programs
Constructor Laboratory /12/4.
Building Java Programs
Building Java Programs
Building Java Programs
Applet 2019/4/23.
Building Java Programs
Building Java Programs
Topic 9 More Graphics Based on slides bu Marty Stepp and Stuart Reges from
Debugging Exercise 00 Try compiling the code samples.
Building Java Programs
Building Java Programs
Building Java Programs
Making a Smile Face Pepper.
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

Variables and Methods Chapter 3 – Lecture Slides

Variables Declaration Graphics g; int numCourses; // integer values double salesTax; // floating pt number Initialization – assignment for the first time Assignment name = “Cookie Monster”; numCourses = 4; salesTax = 4.75; Variable Reference String myName; myName = “Java Guru”; g.drawString( myName, 0, 12 ); Declaration includes the data type and a variable name. Assignment changes the value of a variable..

Scope: Instance Variable vs. Local Variable Instance Variable vs. Local Variable import javax.swing.*; import java.awt.*; public class ColorEx extends JApplet { String favColor; String ski; public void paint( Graphics g ) { favColor = “Favorite color”; ski = “Love 2 ski”; g.setColor( Color.RED ); g.drawString( favColor, 30, 45 ); g.setColor( new Color( 12,34,52) ); g.drawString( ski, 30, 53 ); } import javax.swing.*; import java.awt.*; public class ColorEx extends JApplet { public void paint( Graphics g ) { String favColor = “Favorite color”; String ski = “Love 2 ski”; g.setColor( Color.RED ); g.drawString( favColor, 30, 45 ); g.setColor( new Color( 12,34,52) ); g.drawString( ski, 30, 53 ); } local: declared inside a method instance: declared inside the class

Methods Example public void paint( Graphics g ) Within the class squigglys { } Has it’s own scope squigglys { } Statements in method only run when the method is called/invoked Parameters (none or multiple) listed in parenthesis  each must specify the data type and a variable name  separate multiple ones with a comma  e.g. ( Graphics g ) return type of void designates the method doesn’t return anything g is a type of Graphics object, which has methods such as drawString that we can call paint is the name of the method

Method call that returns a value import java.awt.*; import javax.swing.*; public class Calculate extends JApplet { public void paint (Graphics g ) { int addition = getAdd( 2, 7 ); String added = "2 + 7 = " + addition; g.drawString ( added, 0, 12 ); String subtracted = "2 - 7 = " + getSubtract( 2, 7 ); g.drawString ( subtracted, 0, 24 ); } public int getAdd( int num1, int num2 ) { return num1 + num2; } public int getSubtract( int num1, int num2 ) { return num1 - num2; }

3 Types of Method calling 1. Within a class add( panel ); 2. In another class (type 1) Example: in the Graphics class g.drawString(“Hi”, 20, 10 ); (where g is from the Graphics class, e.g., public void paint( Graphics g ) 3. In another class (type 2) x = 22 + Math.abs( y );

Why Have Methods? Code Easier to read: Repeated code: For access by other objects: Events: (we’ll discuss later)

Why Have Methods? Code Easier to read: import java.awt.*; import javax.swing.*; public class House extends JApplet { public void paint (Graphics g ) { g.setColor( Color.pink ); g.fillRect ( 100,100,200,200 ); g.setColor( Color.black ); Polygon poly = new Polygon( ); poly.addPoint(100,100); poly.addPoint(200,50); poly.addPoint(300,100); g.fillPolygon(poly); g.setColor( Color.blue ); g.fillRect ( 200,230,40,70); g.setColor( Color.black ); g.fillRect ( 400,130,30,170 ); g.setColor( Color.green ); g.fillOval( 370,80,100,100 ); g.fillRect ( 0,295,500,5 ); } Code Easier to read: with methods import java.awt.*; import javax.swing.*; public class HouseMethods extends JApplet { int WINDOW_WIDTH = 20; int WINDOW_HEIGHT = 30; public void paint (Graphics g ) { paintHouse( g ); paintLandscape( g ); } public void paintHouse( Graphics grph ) { grph.setColor( Color.pink ); grph.fillRect ( 100,100,200,200 ); grph.setColor( Color.black ); Polygon poly = new Polygon(); poly.addPoint(100,100); poly.addPoint(200,50); poly.addPoint(300,100); grph.fillPolygon(poly); grph.setColor( Color.blue ); grph.fillRect ( 200,230,40,70); paintWindow( grph, 120, 150 ); paintWindow( grph, 150, 150 ); paintWindow( grph, 200, 150 ); paintWindow( grph, 230, 150 ); } public void paintWindow( Graphics gp, int x, int y ) { gp.setColor( Color.blue ); gp.fillRect ( x, y, WINDOW_WIDTH, WINDOW_HEIGHT ); } public void paintLandscape( Graphics g ) { g.setColor( Color.black );// tree g.fillRect ( 400,130,30,170 ); g.setColor( Color.green ); g.fillOval( 370,80,100,100 ); g.fillRect ( 0,295,500,5 );// grass }

Why Have Methods? Repeated code:  Example: import javax.swing.*; import java.awt.*; public class NeedMethods extends JApplet { public void paint( Graphics g ) { // row 1 g.fillRect( 20,20, 10,10 ); g.fillRect( 40,20, 10,10 ); g.fillRect( 60,20, 10,10 ); g.fillRect( 80,20, 10,10 ); g.fillRect( 100,20, 10,10 ); // row 2 g.fillRect( 30,30, 10,10 ); g.fillRect( 50,30, 10,10 ); g.fillRect( 70,30, 10,10 ); g.fillRect( 90,30, 10,10 ); g.fillRect( 110,30, 10,10 ); } Repeated code using a method: import javax.swing.*; import java.awt.*; public class NeedMethods2 extends JApplet { public void paint( Graphics g ) { drawRows( g, 20, 20 ); drawRows( g, 30, 30 ); } public void drawRows( Graphics graphics, int x, int y ) { graphics.fillRect( x,y, 10,10 ); graphics.fillRect( x+20, y, 10, 10 ); graphics.fillRect( x+40, y, 10, 10 ); graphics.fillRect( x+60, y, 10, 10 ); graphics.fillRect( x+80, y, 10, 10 ); } Which program would be easier to use to create a full-size checkerboard?

Why Have Methods? For access by other objects:  Graphics gr gr.drawString( gr.fillRect( gr.setColor(  If these weren’t methods, we wouldn’t be able to draw anything!

Summary Variables Scope: instance variables vs. local variables Method Structure Purpose of methods