Read through the slides for your notes today.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Review of Math Class Methods abssqrt minmax powround ceilfloor.
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.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
1 Web Based Programming Section 6 James King 12 August 2003.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
Object Oriented Programming one of the main reasons we now teach Java instead of C++. C++ was designed to be backwardly compatible with the original (non-OOP)
Java Applet Presented by Fitsum Okubu. Introduction Introduction Graphics Graphics Methods and Variables Methods and Variables Events Events Decision.
// Java0802.java // CardDeck Case Study #02 // Variables, called attributes or data fields, are added to the class. public class Java0802 { public static.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
// Java0601.java // This program demonstrates that the methods of a class are not always // accessible, like they were with the class. In this case an.
Exposure Java-A 2006 Chapter 4 Slides Using Methods and Parameters
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Primitive Data Types vs. Classes A simple/primitive data type can store only one single value. This means an int can only store one integer. A double.
Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
TCU CoSc Introduction to Programming (with Java) Java Language Overview.
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.
Review Expressions and operators Iteration – while-loop – for-loop.
Primitive Data Types vs. Classes A simple/primitive data type can store only one single value. This means an int can only store one integer. A double.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Agenda For Feb PowerPoint Presentation on Java Methods. 3. Finish Happy Face Assignment (Due by the.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
Review of Math Class Methods abssqrt minmax powround ceilfloor.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
// Java0501.java // This program (and the next few programs) demonstrate user keyboard input // during program execution. This particular program demonstrates.
Primitive Data Types vs. Classes A simple/primitive data type can store only one single value. This means an int can only store one integer. A double.
Arrays Chapter 7.
A structured walkthrough
Computer Programming Your First Java Program: HelloWorld.java.
Introduction to Programming
JavaScript/ App Lab Programming:
MOM! Phineas and Ferb are … Aims:
Exam #1 You will have exactly 30 Mins to complete the exam.
User-Written Functions
Java Applet Programming Barry Sosinsky Valda Hilley
PowerPoint Presentation Authors of Exposure Java
Introduction to Python
Topic: Python’s building blocks -> Variables, Values, and Types
Functions CIS 40 – Introduction to Programming in Python
Section 6.2 Classes & Objects.
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Chapter 6 Methods: A Deeper Look
Variables ICS2O.
Group Status Project Status.
Subprograms Intro & Procedures.
Tutorial on using Java Sprite Animation
Exposure Java 2015 AP®CS Edition
Introduction to Primitive Data types
Lesson 06: Functions Class Chat: Attendance: Participation
Parameter Passing in Java
Section 9.1 Introduction.
Take out a piece of paper and PEN.
Documentation and Style
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Classes, Objects and Methods
Handout-14 Applets and Graphics
Chapter 1 Introducing Small Basic
PowerPoint Presentation Authors of Exposure Java
Variables in C Topics Naming Variables Declaring Variables
Compiler vs linker The compiler translates one .c file into a .o file
APPLET PROGRAMMING.
Introduction to Primitive Data types
Methods Scope How are names handled?
Presentation transcript:

Read through the slides for your notes today.

Use the following slide as a reference for your graphics program

// Java0708.java // This program places the six methods from the // previous program into their own <House> class. import java.awt.*; import java.applet.*; public class Java0708 extends Applet { public void paint(Graphics g) House.drawFloors(g); House.drawRoof(g); House.drawDoor(g); House.drawWindows(g); House.drawChimney(g); } class House public static void drawFloors(Graphics g) Expo.setColor(g,Expo.blue); Expo.drawRectangle(g,200,200,500,300); Expo.drawRectangle(g,200,300,500,400); public static void drawRoof(Graphics g) Expo.setColor(g,Expo.red); Expo.drawLine(g,200,200,350,100); Expo.drawLine(g,500,200,350,100); Expo.drawLine(g,200,200,500,200); public static void drawDoor(Graphics g) Expo.setColor(g,Expo.black); Expo.drawRectangle(g,330,340,370,400); Expo.drawOval(g,350,370,10,20); Expo.fillCircle(g,366,370,3); public static void drawWindows(Graphics g) Expo.drawRectangle(g,220,220,280,280); Expo.drawLine(g,220,250,280,250); Expo.drawLine(g,250,220,250,280); Expo.drawRectangle(g,420,220,480,280); Expo.drawLine(g,420,250,480,250); Expo.drawLine(g,450,220,450,280); Expo.drawRectangle(g,320,220,380,280); Expo.drawLine(g,320,250,380,250); Expo.drawLine(g,350,220,350,280); Expo.drawRectangle(g,220,320,280,380); Expo.drawLine(g,220,350,280,350); Expo.drawLine(g,250,320,250,380); Expo.drawRectangle(g,420,320,480,380); Expo.drawLine(g,420,350,480,350); Expo.drawLine(g,450,320,450,380); public static void drawChimney(Graphics g) Expo.drawLine(g,420,146,420,80); Expo.drawLine(g,420,80,450,80); Expo.drawLine(g,450,80,450,166);

The next two slide are important for your understanding of using parameters

Important Rules About Using Parameters with Methods #1 The number of parameters in the method call must match the number of parameters in the method heading. The corresponding parameters in the method call must be the same type as the parameters in the heading. The sequence of the parameters in the method call must match the sequence of the parameters in the heading. The parameter identifiers in the method call may be the same identifier or a different identifier as the parameters in the heading.

Important Rules About Using Parameters with Methods #2 The number of actual parameters must match the number of formal parameters.   The corresponding actual parameters must be the same type as the formal parameters. The sequence of the actual parameters must match the sequence of the formal parameters. The actual parameter identifiers may be the same identifier as or a different identifier from as the formal parameters. These rules say the same thing as the previous slide.

The next two slides are important for you to understand the terminology. This will help you talk to each other intelligently and ask meaningful questions.

Variable Terminology Variables that are declared inside a method or block are called local variables. Local variables are only accessible inside the method or block that they are defined in. Variables that are declared inside a class, but outside any method, are class variables. Class variables are accessible by any method of the class. Class variables are also called attributes. If a variable is only used by one method, it should be declared inside that method as a local variable. If a variable is used by 2 or more methods of a class, it should be declared as a class variable.

Program Design Notes This was the first introduction to program design. Additional design features will be introduced as you learn more object-oriented programming. At this stage you can already consider the following: • Programs should use self-commenting identifiers. • Control structures and block structure need to use a consistent indentation style. • Specific tasks should be placed in modules called methods. • Similar methods accessing the same data should be placed in a class. • The main method should be used for program sequence, not large numbers of program statements.

This next section should give you a few ideas to help with your graphics program.

Section 7.6 Creating Methods with Other Methods

public class Java0725 extends Applet { public void paint(Graphics g) // Java0725.java // This program demonstrates a <picket> method that will be used to display a fence.   import java.awt.*; import java.applet.*; public class Java0725 extends Applet { public void paint(Graphics g) Expo.setBackground(g,Expo.black); Expo.setColor(g,Expo.tan); Expo.fillRectangle(g,0,500,1000,525); Expo.fillRectangle(g,0,600,1000,625); Expo.setColor(g,Expo.brown); for (int x = 2; x < 1000; x+=40) picket(g,x); } public static void picket(Graphics g, int x) Expo.fillPolygon(g,x,650,x,500,x+18,450,x+36,500,x+36,650); Expo.delay(250); // delay for ¼ a second

These pickets are actually drawn one at a time with a ¼ second delay NOTE: These pickets are actually drawn one at a time with a ¼ second delay between each picket. To see this effect execute the program on your computer.

The Logic of the picket Method picket(g,x); All graphics methods need g. x is the horizontal value of the bottom left corner of the picket. The y (vertical) value of the bottom left corner is always 650 since all pickets will be at the bottom of the screen. The other 4 coordinates of the picket are relative to the point (x,650). x+18,450 x,500 x+36,500 x,650 x+36,650

public class Java0726 extends Applet { public void paint(Graphics g) // Java0726.java // This program uses the <picket> method to create the <fence> method.   import java.awt.*; import java.applet.*; public class Java0726 extends Applet { public void paint(Graphics g) Expo.setBackground(g,Expo.black); fence(g); } public static void picket(Graphics g, int x) Expo.fillPolygon(g,x,650,x,500,x+18,450,x+36,500,x+36,650); Expo.delay(250); // delay for ¼ a second public static void fence(Graphics g) // cross beams Expo.setColor(g,Expo.tan); Expo.fillRectangle(g,0,500,1000,525); Expo.fillRectangle(g,0,600,1000,625); // pickets Expo.setColor(g,Expo.brown); for (int x = 2; x < 1000; x+=40) picket(g,x);

// Java0727.java // This program combines many user-defined methods to create a graphics image. import java.awt.*; import java.applet.*; public class Java0727 extends Applet { public void paint(Graphics g) Expo.setBackground(g,Expo.black); nightSky(g); fence(g); } public static void randomStar(Graphics g, int x) int y = Expo.random(25,175); int radius = Expo.random(15,20); int points = Expo.random(5,10); Expo.setRandomColor(g); Expo.fillStar(g,x,y,radius,points); Expo.delay(250); // delay for ¼ a second public static void nightSky(Graphics g) moon(g); for (int x = 25; x <= 825; x+= 50) randomStar(g,x); public static void moon(Graphics g) { Expo.setColor(g,Expo.white); Expo.fillCircle(g,920,85,70); Expo.setColor(g,Expo.black); Expo.fillCircle(g,895,70,60); } public static void picket(Graphics g, int x) Expo.fillPolygon(g,x,650,x,500,x+18,450, x+36,500,x+36,650); Expo.delay(250); // delay for ¼ a second public static void fence(Graphics g) // cross beams Expo.setColor(g,Expo.tan); Expo.fillRectangle(g,0,500,1000,525); Expo.fillRectangle(g,0,600,1000,625); // pickets Expo.setColor(g,Expo.brown); for (int x = 2; x < 1000; x+=40) picket(g,x);

public class Java0728 extends Applet { public void paint(Graphics g) // Java0728.java // This program does the exact same thing as the previous program, but without the structure methods. // Several method calls are commented out and in their place is the code from that method. // This is also done for some of the methods from the Expo class. // The intent is to show that using methods makes your program much more readable and easier to // work with. import java.awt.*; import java.applet.*; public class Java0728 extends Applet { public void paint(Graphics g) /////////////////////////////////////////////////////////////// // Expo.setBackground(g,Expo.black); Expo.setColor(g,Expo.black); Expo.fillRectangle(g,0,0,4800,3600); ////////////////////////////////////////////////////////////// // nightSky(g); ////////////////////////////////////////////////////////// // moon(g); Expo.setColor(g,Expo.white); Expo.fillCircle(g,920,85,70); Expo.fillCircle(g,895,70,60); // continued on the next several slides…

for (int centerX = 25; centerX <= 825; centerX += 50) { int centerY = Expo.random(25,175); int radius = Expo.random(15,20); int points = Expo.random(5,10); ////////////////////////////////////////////////////// // Expo.setRandomColor(g); int red = Expo.random(0,255); int green = Expo.random(0,255); int blue = Expo.random(0,255); Expo.setColor(g,red,green,blue); ///////////////////////////////////////////////////// // Expo.fillStar(g,x,y,radius,points); int halfRadius = radius / 2; switch(points) case 3 : halfRadius = 140 * radius / 500; break; case 4 : halfRadius = 170 * radius / 400; break; case 5 : halfRadius = 192 * radius / 500; break; case 6 : halfRadius = 233 * radius / 400; break; case 7 : halfRadius = 179 * radius / 500; break; case 8 : halfRadius = 215 * radius / 400; break; case 9 : halfRadius = 173 * radius / 500; break; case 10 : halfRadius = 212 * radius / 400; break; default : if (points < 52) if (points % 2 == 1) halfRadius = (180-points) * radius / 500; else halfRadius = (222-points) * radius / 400; } halfRadius = radius / 2;

int p = points; points *= 2; int xCoord[] = new int[points]; int yCoord[] = new int[points]; int currentRadius; for (int k = 0; k < points; k++) { if (k % 2 == 0) currentRadius = radius; else currentRadius = halfRadius; xCoord[k] = (int) Math.round(Math.cos(2 * Math.PI * k/points - Math.PI/2) * currentRadius) + centerX; yCoord[k] = (int) Math.round(Math.sin(2 * Math.PI * k/points - Math.PI/2) * currentRadius) + centerY; } int x = (p-5)/2+1; if (p >= 5 && p <= 51) switch(p % 4) case 1 : yCoord[x] = yCoord[x+1] = yCoord[points-x-1] = yCoord[points-x]; break; case 2 : yCoord[x] = yCoord[x+1] = yCoord[points-x-1] = yCoord[points-x]; yCoord[x+3] = yCoord[x+4] = yCoord[points-x-4] = yCoord[points-x-3]; break; case 3 : yCoord[x+2] = yCoord[x+3] = yCoord[points-x-3] = yCoord[points-x-2];

g.fillPolygon(xCoord,yCoord,points); /////////////////////////////////////////////////////// // Expo.delay(250); // delay for ¼ a second long startDelay = System.currentTimeMillis(); long endDelay = 0; while (endDelay - startDelay < 250) endDelay = System.currentTimeMillis(); } /////////////////////////////////////////////////////////////// // fence(g); // cross beams Expo.setColor(g,Expo.tan); Expo.fillRectangle(g,0,500,1000,525); Expo.fillRectangle(g,0,600,1000,625); // pickets Expo.setColor(g,Expo.brown); for (int x = 2; x < 1000; x+=40) { ////////////////////////////////////////////////////// // picket(g,x); ////////////////////////////////////////////////// //Expo.fillPolygon(g,x,650,x,500,x+18,450,x+36,500,x+36,650); Polygon poly = new Polygon(); poly.addPoint(x,650); poly.addPoint(x,500); poly.addPoint(x+18,450); poly.addPoint(x+36,500); poly.addPoint(x+36,650); g.fillPolygon(poly);

////////////////////////////////////////////////// // Expo.delay(250); // delay for ¼ a second long startDelay = System.currentTimeMillis(); long endDelay = 0; while (endDelay - startDelay < 250) endDelay = System.currentTimeMillis(); }

Next open Labs and follow the directions for the graphics lab Next open Labs and follow the directions for the graphics lab. If your graphic is too easy you will not receive credit. Due date will be Friday, Feb. 17.