Exposure Java 2013 APCS Edition Power Point Presentation

  • Slides: 47
Download presentation
Exposure Java 2013 APCS Edition Power. Point Presentation created by: Mr. John L. M.

Exposure Java 2013 APCS Edition Power. Point Presentation created by: Mr. John L. M. Schram and Mr. Leon Schram Authors of Exposure Java

Section 4. 1

Section 4. 1

Java vs. C++ Object Oriented Programming one of the main reasons we now teach

Java vs. C++ Object Oriented Programming one of the main reasons we now teach Java instead of C++ was designed to be backwardly compatible with the original (non-OOP) C programming language. Therefore in C++, OOP is optional. In Java OOP is required. The College Board wants students to learn OOP so Java it is.

Section 4. 2

Section 4. 2

The 4 Stages of Program Design Cryptic Programming Stage Unstructured, Spaghetti-Programming Stage Structured Programming

The 4 Stages of Program Design Cryptic Programming Stage Unstructured, Spaghetti-Programming Stage Structured Programming Stage Object Oriented Programming Stage

Avoid Spaghetti Programming Program Statement Program Statement

Avoid Spaghetti Programming Program Statement Program Statement

Section 4. 3

Section 4. 3

OOP Object Oriented Programming (OOP) is a style of programming that incorporates these 3

OOP Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction Object Oriented Programming simulates real life by using a program style that treats a program as a group of objects.

A car could be an object in Java. Objects have attributes and methods. Car

A car could be an object in Java. Objects have attributes and methods. Car Attributes Car Methods Make & Model Drive Color Park # of Doors # of Seats Reverse Tow Verbs Nouns OOP Example

Encapsulation Car Attributes Car Methods Make & Model Drive Color Park # of Doors

Encapsulation Car Attributes Car Methods Make & Model Drive Color Park # of Doors Reverse # of Seats Tow Encapsulation means packaging or encapsulating all of the attributes and methods of an object in the same container.

Polymorphism If we review our Algebra we should remember: A monomial is a single

Polymorphism If we review our Algebra we should remember: A monomial is a single term like: 3 x A binomial is 2 terms like: 3 x + 7 A polynomial is many terms like: x 2 + 3 x + 7 The prefix Poly means many. Polymorphism means many forms. Polymorphism is an advanced concept in computer science which will be discussed in chapter 14. To attempt to explain it now will only cause confusion.

Class Interaction - Inheritance There are different types of Class Interaction. One type is

Class Interaction - Inheritance There are different types of Class Interaction. One type is Inheritance. Suppose you wish to create Truck objects, Limo objects and Racecar objects. Instead of starting each from scratch we can use the existing Car in the following A Truck is a Car with 4 WD, big tires, and a bed. manner: A Limo is a very long luxury Car with many seats. A Racecar is a Car with 1 seat, a very powerful engine, and a number painted on the side.

Section 4. 4

Section 4. 4

// Java 0401. java // This program shows how to use the <sqrt> method

// Java 0401. java // This program shows how to use the <sqrt> method of the Math // class. The Math class is part of the java. lang package, which is // automatically loaded (imported) by the compiler. // Math. sqrt returns the square root of the argument. public class Java 0401 { public static void main (String args[]) { System. out. println("n. JAVA 0401. JAVAn"); int n 1 = 625; double n 2 = 6. 25; System. out. println("Square root of " + n 1 + ": " + Math. sqrt(n 1)); System. out. println("Square root of " + n 2 + ": " + Math. sqrt(n 2)); System. out. println(); } }

// Java 0401. java // This program shows how to use the <sqrt> method

// Java 0401. java // This program shows how to use the <sqrt> method of the Math // class. The Math class is part of the java. lang package, which is // automatically loaded (imported) by the compiler. // Math. sqrt returns the square root of the argument. public class Java 0401 { public static void main (String args[]) { System. out. println("n. JAVA 0401. JAVAn"); int n 1 = -625; double n 2 = 6. 25; System. out. println("Square root of " + n 1 + ": " + Math. sqrt(n 1)); System. out. println("Square root of " + n 2 + ": " + Math. sqrt(n 2)); Try This! System. out. println(); }Change the value of n 1 from 625 to -625. } Recompile and execute and see what happens.

// Java 0401. java // This program shows how to use the <sqrt> method

// Java 0401. java // This program shows how to use the <sqrt> method of the Math // class. The Math class is part of the java. lang package, which is // automatically loaded (imported) by the compiler. // Math. sqrt returns the square root of the argument. public class Java 0401 { public static void main (String args[]) { System. out. println("n. JAVA 0401. JAVAn"); int n 1 = -625; double n 2 = 6. 25; System. out. println("Square root of " + n 1 + ": " + Math. sqrt(n 1)); System. out. println("Square root of " + n 2 + ": " + Math. sqrt(n 2)); NOTE: Na. N means “Not A Number”. System. out. println(); Remember the square root of a } } negative number is not a real number.

Class Method Syntax Math. sqrt(n 1) 1. Math is the class identifier, which contains

Class Method Syntax Math. sqrt(n 1) 1. Math is the class identifier, which contains the methods separates the class identifier you call. 2. from the method identifier 3. sqrt is the method identifier 4. (n 1) n 1 is the argument or parameter

// Java 0402. java // This program shows different arguments that can be used

// Java 0402. java // This program shows different arguments that can be used with the <sqrt> // method. Note how a method call can be the argument of another method call. public class Java 0402 { public static void main (String args[]) { System. out. println("n. JAVA 0402. JAVAn"); double n 1, n 2, n 3, n 4; n 1 = Math. sqrt(1024); // constant argument n 2 = Math. sqrt(n 1); // variable argument n 3 = Math. sqrt(n 1 + n 2); // expression argument n 4 = Math. sqrt(256)); // method argument System. out. println("n 1: " + n 1); System. out. println("n 2: " + n 2); System. out. println("n 3: " + n 3); System. out. println("n 4: " + n 4);

Method Arguments or Parameters The information, which is passed to a method is called

Method Arguments or Parameters The information, which is passed to a method is called an argument or a parameter. Parameters are placed between parentheses immediately following the method identifier. Parameters can be constants, variables, expressions or they can be methods. The only requirement is that the correct data type value is passed to the method. In other words, Math. sqrt(x) can compute the square root of x, if x stores any non-negative number (int or double), but not if x stores a String value like "aardvark".

// Java 0403. java // This program demonstrates the <floor> <ceil> and <round> methods.

// Java 0403. java // This program demonstrates the <floor> <ceil> and <round> methods. // The <floor> method returns the truncation down to the next lower integer. // The <ceil> method returns the next higher integer. // The <round> method rounds the argument and returns the closest integer. public class Java 0403 { public static void main (String args[]) { System. out. println("n. JAVA 0403. JAVAn"); System. out. println("Math. floor(5. 001): " + Math. floor(5. 001)); System. out. println("Math. floor(5. 999): " + Math. floor(5. 999)); System. out. println("Math. floor(5. 5) : " + Math. floor(5. 5)); System. out. println("Math. floor(5. 499): " + Math. floor(5. 499)); System. out. println("Math. ceil(5. 001) : " + Math. ceil(5. 001)); System. out. println("Math. ceil(5. 999) : " + Math. ceil(5. 999)); System. out. println("Math. ceil(5. 5) : " + Math. ceil(5. 5)); System. out. println("Math. ceil(5. 499) : " + Math. ceil(5. 499)); System. out. println("Math. round(5. 001): " + Math. round(5. 001)); System. out. println("Math. round(5. 999): " + Math. round(5. 999)); System. out. println("Math. round(5. 5) : " + Math. round(5. 5)); System. out. println("Math. round(5. 499): " + Math. round(5. 499)); System. out. println(); } }

// Java 0404. java // This program demonstrates the <max> and <min> methods. //

// Java 0404. java // This program demonstrates the <max> and <min> methods. // Math. max returns the largest value of the two arguments. // Math. min returns the smallest value of the two arguments. public class Java 0404 { public static void main (String args[]) { System. out. println("n. JAVA 0404. JAVAn"); System. out. println("Math. max(100, 200): " + Math. max(100, 200)); System. out. println("Math. max(-10, -20): " + Math. max(-10, -20)); System. out. println("Math. max(500, 500): " + Math. max(500, 500)); System. out. println("Math. min(100, 200): " + Math. min(100, 200)); System. out. println("Math. min(-10, -20): " + Math. min(-10, -

// Java 0405. java // This program demonstrates the <abs> and <pow> methods. //

// Java 0405. java // This program demonstrates the <abs> and <pow> methods. // Math. abs returns the absolute value of the argument. // Math. pow returns the first argument raised to the power // of the second argument. public class Java 0405 { public static void main (String args[]) { System. out. println("n. JAVA 0405. JAVAn"); System. out. println("Math. abs(-25): " + Math. abs(-25)); System. out. println("Math. abs(100): " + Math. abs(100)); System. out. println("Math. abs(0) : " + Math. abs(0)); System. out. println("Math. pow(3, 4) : " + Math. pow(3, 4)); System. out. println("Math. pow(-2, 2): " + Math. pow(-2, 2)); System. out. println("Math. pow(2, -2): " + Math. pow(2, -2)); System. out. println(); } }

// Java 0406. java // This program demonstrates the <PI> and <E> fields of

// Java 0406. java // This program demonstrates the <PI> and <E> fields of the // Math class. // Both <PI> and <E> are "final" attributes of the <Math> class. // <PI> and <E> are not methods. Note there are no parentheses. public class Java 0406 { public static void main (String args[]) { System. out. println("n. JAVA 0406. JAVAn"); System. out. println("Math. PI: " + Math. PI); System. out. println("Math. E : " + Math. E); System. out. println(); } }

AP Exam Alert The Math class has many methods. Only the abs, pow, sqrt

AP Exam Alert The Math class has many methods. Only the abs, pow, sqrt and random methods will be tested on the AP Computer Science Examination. You will learn about Math. random() in chapter 6.

Section 4. 5

Section 4. 5

Learning Graphics Programming Learning graphics programming is not simply a fun issue. You will

Learning Graphics Programming Learning graphics programming is not simply a fun issue. You will learn many sophisticated computer science concepts by studying graphics programs. Some of the most sophisticated programs are video games. Only very dedicated and knowledgeable programmers can write effective video games.

Graphics & Coordinate Geometry A graphics window uses a system of (X, Y) coordinates

Graphics & Coordinate Geometry A graphics window uses a system of (X, Y) coordinates in a manner similar to the use of coordinates that you first learned in your math classes. The next slide shows an example of the Cartesian Coordinate System. In particular, note that the Cartesian system has four quadrants with the (0, 0) coordinate located in the center of the grid where the X-Axis and the Y-Axis intersect.

Cartesian Coordinate Graph

Cartesian Coordinate Graph

Executing Java Applet Programs All the graphics programs examples that follow in this section

Executing Java Applet Programs All the graphics programs examples that follow in this section are created as Java applets. Make sure that you remember to compile the Java source code file, and then switch to some small web page file for execution. It is possible to create the same exact displays with Java applications.

The draw. Line Method draw. Line(int x 1, int y 1, int x 2,

The draw. Line Method draw. Line(int x 1, int y 1, int x 2, int y 2) Draws a line from coordinate (x 1, y 1) to coordinate (x 2, y 2) x 1, y 1 x 2, y 2

// Java 0407. java // This program demonstrates how to draw lines. // Lines

// Java 0407. java // This program demonstrates how to draw lines. // Lines are drawn from (X 1, Y 1) to (X 2, Y 2) with draw. Line(X 1, Y 1, X 2, Y 2). import java. awt. *; import java. applet. *; public class Java 0407 extends Applet { public void paint(Graphics g) { g. draw. Line(0, 0, 800, 600); g. draw. Line(0, 600, 800, 0); g. draw. Line(100, 300, 700, 300); g. draw. Line(400, 100, 400, 500); } } <!-- Java 0407. html --> <APPLET CODE = "Java 0407. class" WIDTH=800 HEIGHT=600> </APPLET>

The draw. Rect Method draw. Rect(int x, int y, int width, int height) Draws

The draw. Rect Method draw. Rect(int x, int y, int width, int height) Draws a rectangle with top-left corner at coordinate (x, y) using width and height dimensions. fill. Rect uses identical parameters, but fills in the rectangle. x, y height width

// Java 0408. java // This program introduces the rectangle command. A rectangle is

// Java 0408. java // This program introduces the rectangle command. A rectangle is drawn from // the top-left (X, Y) coordinate of a rectangle followed by Width and Height using // <draw. Rect(X, Y, Width, Height)>. // The <fill. Rect> command draws a rectangle filled with solid pixels. import java. awt. *; import java. applet. *; public class Java 0408 extends Applet { public void paint(Graphics g) { g. draw. Rect(50, 100, 100); g. draw. Rect(300, 50, 300, 150); g. fill. Rect(50, 400, 100); g. fill. Rect(300, 400, 300, 150); } } <!-- Java 0408. html --> <APPLET CODE = "Java 0408. class" WIDTH=800 HEIGHT=600> </APPLET>

The draw. Oval Method draw. Oval(int x, int y, int width, int height) Draws

The draw. Oval Method draw. Oval(int x, int y, int width, int height) Draws an oval that is circumscribed by the rectangle with top-left corner at coordinate (x, y) using width and height dimensions. fill. Oval uses identical parameters, but fills in the oval. x, y height width

// Java 0409. java // This program uses the <draw. Oval> method to draw

// Java 0409. java // This program uses the <draw. Oval> method to draw ovals and circles. // The four parameters of the <draw. Oval> method are identical to the parameters // of the <draw. Rect> method. With <draw. Oval(X, Y, Width, Height)> (X, Y) is the // coordinate of the top-left corner of the rectangle that circumscribes the oval. // It also shows that the Graphics variable does not have to be "g". import java. awt. *; import java. applet. *; public class Java 0409 extends Applet { public void paint(Graphics screen) { screen. draw. Oval(50, 100, 100); screen. draw. Oval(300, 50, 300, 50); screen. fill. Oval(50, 400, 100); screen. fill. Oval(300, 400, 300, 150); } } <!-- Java 0409. html --> <APPLET CODE = "Java 0409. class" WIDTH=800 HEIGHT=600> </APPLET>

The draw. Arc Method draw. Arc(int x, int y, int width, int height, int

The draw. Arc Method draw. Arc(int x, int y, int width, int height, int start, int degrees) Draws part of an oval. The 1 st 4 parameters are the same as draw. Oval. Start indicates the degree location of the beginning of the arc. Degrees indicates the number of degrees traveled by the arc. 0 degrees is at the 3: 00 o’clock position and increases counter clockwise to 360 degrees. fill. Arc uses identical parameters, but “fills” in the arc. x, y 90 180 0, 360 270 width height

// Java 0410. java // This program uses the <draw. Arc> and <fill. Arcs>

// Java 0410. java // This program uses the <draw. Arc> and <fill. Arcs> methods. // Method <draw. Arc(X, Y, Width, Height, Start, Degrees)> uses the first four // parameters in the same manner as the <draw. Oval> method. Start is the // degree value of the arc-start and Degrees is the number of degrees the arc travels. // Start (0 degrees) is at 3: 00 o'clock and positive degrees travel counter-clockwise. import java. awt. *; import java. applet. *; public class Java 0410 extends Applet { public void paint(Graphics g) { g. draw. Arc(50, 100, 0, 180); g. fill. Arc(200, 50, 100, 0, 270); g. draw. Arc(350, 100, 0, 360); g. fill. Arc(500, 50, 100, 0, -180); g. draw. Arc(50, 250, 100, 200, 0, 180); g. fill. Arc(200, 250, 100, 200, 0, 270); g. draw. Arc(350, 200, 100, 0, 360); g. fill. Arc(350, 400, 200, 100, 0, -180); } } <!-- Java 0410. html --> <APPLET CODE = "Java 0410. class" WIDTH=800 HEIGHT=600> </APPLET>

// Java 0411. java // This program demonstrates the significance of using parameters in

// Java 0411. java // This program demonstrates the significance of using parameters in the // correct sequence Java 0411. java is very similar to Java 0409. java with // rearranged parameters. import java. awt. *; import java. applet. *; public class Java 0411 extends Applet { public void paint(Graphics screen) { screen. draw. Oval(100, 50, 50); screen. draw. Oval(50, 300, 50, 300); screen. fill. Oval(400, 50, 100); screen. fill. Oval(150, 300, 400, 300); } } <!-- Java 0411. html --> <APPLET CODE = "Java 0411. class" WIDTH=800 HEIGHT=600> </APPLET>

Parameter Sequence Matters Java 0409. java vs. Java 0411. java screen. draw. Oval(50, 100,

Parameter Sequence Matters Java 0409. java vs. Java 0411. java screen. draw. Oval(50, 100, 100); screen. draw. Oval(300, 50, 300, 50); screen. fill. Oval(50, 400, 100); screen. fill. Oval(300, 400, 300, 150); screen. draw. Oval(100, 50, 50); screen. draw. Oval(50, 300, 50, 300); screen. fill. Oval(400, 50, 100); screen. fill. Oval(150, 300, 400, 300);

// Java 0412. java // This program demonstrates how to control the output display

// Java 0412. java // This program demonstrates how to control the output display color with // the <Color> class and the <set. Color> method. import java. awt. *; import java. applet. *; public class Java 0412 extends Applet { public void paint(Graphics g) { g. set. Color(Color. red); g. fill. Oval(50, 100, 100); g. set. Color(Color. green); g. fill. Oval(200, 50, 100); g. set. Color(Color. blue); g. fill. Oval(350, 100, 100); g. set. Color(Color. orange); g. fill. Oval(500, 50, 100); g. set. Color(Color. cyan); g. fill. Oval(50, 200, 100); g. set. Color(Color. magenta); g. fill. Oval(200, 100, 100); g. set. Color(Color. yellow); g. fill. Oval(350, 200, 100); g. set. Color(Color. gray); g. fill. Oval(500, 200, 100); g. set. Color(Color. light. Gray); g. fill. Oval(50, 350, 100); g. set. Color(Color. dark. Gray); g. fill. Oval(200, 350, 100); g. set. Color(Color. pink); g. fill. Oval(350, 100, 100); g. set. Color(Color. black); g. fill. Oval(500, 350, 100); } } <!-- Java 0412. html --> <APPLET CODE = "Java 0412. class" WIDTH=800 HEIGHT=600> </APPLET>

The set. Color Method set. Color(Color. constant) Sets the graphics display color of the

The set. Color Method set. Color(Color. constant) Sets the graphics display color of the following graphics output to the specified constant of the Color class. There are 13 color constants listed below. red green blue magenta yellow gray drak. Gray pink black orange light. Gray white cyan NOTE: You are not limited to only these 13 colors. By combining different amounts of red, green, and blue values you can create any of over 16 million different colors. You will be shown how to do this in a later chapter.

The draw. String Method draw. String(String s, int x, int y) Draws a String

The draw. String Method draw. String(String s, int x, int y) Draws a String s starting at the at coordinate (x, y). Hello there! x, y

// Java 0413. java // This program demonstrates the <draw. String> method. // With

// Java 0413. java // This program demonstrates the <draw. String> method. // With <draw. String("Hello World", x, y)>, the string Hello World // will be displayed starting at the [x, y] pixel coordinate. import java. awt. *; import java. applet. *; public class Java 0413 extends Applet { public void paint(Graphics g) { g. draw. String("This string will display in default black at coordinate [200, 250]", 200, 250); g. set. Color(Color. red); g. draw. String("This string will display in red at coordinate [5, 50]", 5, 50); g. set. Color(Color. blue); g. draw. String("This string will display in blue at coordinate [400, 500]", 400, 500); } }<!-- Java 0413. html --> <APPLET CODE = "Java 0413. class" WIDTH=800 HEIGHT=600> </APPLET>

Pre. AP and AP Graphics Alert If you used the Expo class in a

Pre. AP and AP Graphics Alert If you used the Expo class in a Pre. AP Computer Science course, be aware that you will strictly use Java commands that are part of Java standard libraries. Using the Expo class was like riding a bike with training wheels. Now that you are in AP Computer Science, the time has come to remove those training wheels.