Presentation is loading. Please wait.

Presentation is loading. Please wait.

A+ Computer Science METHODS.

Similar presentations


Presentation on theme: "A+ Computer Science METHODS."— Presentation transcript:

1 A+ Computer Science METHODS

2 © A+ Computer Science - www.apluscompsci.com
Instantiation AplusBug dude = new AplusBug(); dude is an AplusBug reference. new AplusBug() creates a new AplusBug Object out in memory. dude stores the location of that new AplusBug Object. new AplusBug() creates a new AplusBug object. © A+ Computer Science -

3 © A+ Computer Science - www.apluscompsci.com
Instantiation AplusBug dude = new AplusBug(); dude 0x234 0x234 AplusBug dude is an AplusBug reference. new AplusBug() creates a new AplusBug Object out in memory. dude stores the location of that new AplusBug Object. dude is a reference variable that refers to an AplusBug object. © A+ Computer Science -

4 Methods

5 © A+ Computer Science - www.apluscompsci.com
What is a method? A method is a storage location for related program statements. When called, a method usually performs a specific task. System.out.println( ) Methods store commands / program statements. When called, the code inside the method is activated. © A+ Computer Science -

6 © A+ Computer Science - www.apluscompsci.com
Common Methods Math.random() keyboard.nextInt( ) System.out.println( ) © A+ Computer Science -

7 © A+ Computer Science - www.apluscompsci.com
Methods public class AplusBug { public void speak() { out.println("chirp-chirp"); } The speak method shown above contains a single println command. The speak method would print out chirp-chirp on the console window. OUTPUT chirp-chirp © A+ Computer Science -

8 © A+ Computer Science - www.apluscompsci.com
Method Calls AplusBug dude = new AplusBug(); dude.speak(); OUTPUT chirp-chirp Once you have instantiated an object, you can call the methods contained in the class. dude is an AplusBug reference. dude stores the location of that new AplusBug Object. new AplusBug() creates a new AplusBug Object out in memory. dude can use all methods from the AplusBug class. dude.speak(); //calls the speak method from the AplusBug class © A+ Computer Science -

9 © A+ Computer Science - www.apluscompsci.com
Method Calls AplusBug dude = new AplusBug(); dude.speak(); OUTPUT chirp-chirp chirp-chirp chirp-chirp dude is an AplusBug reference. dude stores the location of that new AplusBug Object. new AplusBug() creates a new AplusBug Object out in memory. dude can use all methods from the AplusBug class. dude.speak(); //calls the speak method from the AplusBug class dude can use any of the methods from the AplusBug class as many times as needed. © A+ Computer Science -

10 © A+ Computer Science - www.apluscompsci.com
Methods access return type name params code public void speak( ) { System.out.println("chirp-chirp"); } A method has a signature. The signature provides information about the method. The name is most used and recognizable part of the signature. The method shown above is named speak. The return type states what the method will return. Method speak has a return type of void which means the method does not return a value. The access of method speak is public. This states that the method speak can be called from any location. © A+ Computer Science -

11 © A+ Computer Science - www.apluscompsci.com
Public All members with public access can be accessed or modified inside and outside of the class where they are defined. Public access simply means the member can be used anywhere inside or outside of the class. © A+ Computer Science -

12 © A+ Computer Science - www.apluscompsci.com
Private All members with private access can be accessed or modified only inside the class where they are defined. All members of a class with private access can be accessed or modified within the class where they are defined only. Private members cannot be accessed outside of the class. © A+ Computer Science -

13 © A+ Computer Science - www.apluscompsci.com
Basic Turkey Class public class Turkey { public void speak() out.println("gobble-gobble"); } public void sayName() out.println("big bird"); Turkey is a class that contains methods. In order to use the Turkey methods, you must create a new Turkey(). © A+ Computer Science -

14 © A+ Computer Science - www.apluscompsci.com
Instantiation Turkey bird = new Turkey(); bird 0x234 0x234 Turkey Turkey is a class that contains methods. In order to use the Turkey methods, you must create a new Turkey(). bird is a reference variable that refers to a Turkey object. © A+ Computer Science -

15 © A+ Computer Science - www.apluscompsci.com
Instantiation Turkey bird = new Turkey(); In order to use the Turkey class methods, you must instantiate a new Turkey object. Turkey is a class that contains methods. In order to use the Turkey methods, you must create a new Turkey(). Turkey one = new Turkey(); Turkey two = new Turkey(); Turkey three = new Turkey(); © A+ Computer Science -

16 © A+ Computer Science - www.apluscompsci.com
Instantiation Turkey bird = new Turkey(); Turkey one = new Turkey(); Turkey two = new Turkey(); Turkey three = new Turkey(); You can create as many new Turkey()s as you need. Turkey is a class that contains methods. In order to use the Turkey methods, you must create a new Turkey(). Turkey one = new Turkey(); Turkey two = new Turkey(); Turkey three = new Turkey(); © A+ Computer Science -

17 © A+ Computer Science - www.apluscompsci.com
Instantiation Turkey bird = new Turkey(); bird.sayName(); OUTPUT big bird Turkey is a class that contains methods. In order to use the Turkey methods, you must create a new Turkey(). Turkey one = new Turkey(); one.sayName(); Once you have a reference to a Turkey, you can call the methods that belong to the Turkey class. © A+ Computer Science -

18 © A+ Computer Science - www.apluscompsci.com
Turkey Runner //Code in the Turkey Runner Turkey bird = new Turkey(); bird.speak(); bird.sayName(); OUTPUT gobble-gobble big bird gobble-gobble big bird gobble-gobble Typically, you should place the class for the object in a file with the same name. Turkey.java contains the Turkey class. The runner with test code is placed in a runner file named with the main class name. TurkeyRunner.java is the name of the runner. In the Runner, you should instantiate several objects and call all methods to ensure all works. © A+ Computer Science -

19 © A+ Computer Science - www.apluscompsci.com
turkey.java turkeyrunner.java © A+ Computer Science -

20 Graphics and Constructors

21 © A+ Computer Science - www.apluscompsci.com
Constructors Constructors always have the same name as the class. GraphOne test = new GraphOne(); AplusBug rob = new AplusBug(); Turkey bird = new Turkey(); Constructors are used to initialize all of the data/properties inside the class. Constructors ensure that the Object is ready for use. © A+ Computer Science -

22 © A+ Computer Science - www.apluscompsci.com
Constructors reference variable Scanner keyboard = new Scanner(System.in); Scanner is a class which must be instantiated before it can be used. In other words, you must make a new Scanner if you want to use Scanner. A reference must be used to store the location in memory of the Scanner object created. System.in is the parameter passed to the Scanner constructor so that Java will know to connect the new Scanner to the keyboard. keyboard is a reference that will store the location of newly created Scanner object. object instantiation / constructor call © A+ Computer Science -

23 © A+ Computer Science - www.apluscompsci.com
Graphics Runner public class GraphicsRunner extends JFrame { //variables not shown public GraphicsRunner() setSize(WIDTH,HEIGHT); getContentPane().add( new Circles() ); setVisible(true); } //main not shown When a GraphicsRunner class is instantiated, the size of the JFrame is set and the visibility is also set. The setSize() method sets the width and height of the JFrame. The setSize() method tells the simply to either show the JFrame or hide the Frame. The add() method adds a Component to the JFrame. A new Circles() Object is being instantiated and added to the JFrame. GraphicsRunner is used to run all of the drawing and graphics code. © A+ Computer Science -

24 © A+ Computer Science - www.apluscompsci.com
Constructors public class GraphicsRunner extends JFrame { private static final int WIDTH = 640; private static final int HEIGHT = 480; public GraphicsRunner() setSize(WIDTH,HEIGHT); getContentPane().add( new Circles() ); setVisible(true); } public static void main( String args[] ) GraphicsRunner run = new GraphicsRunner(); the constructor When a GraphicsRunner class is instantiated, the size of the JFrame is set and the visibility is also set. The setSize() method sets the width and height of the JFrame. The setSize() method tells the simply to either show the JFrame or hide the Frame. The add() method adds a Component to the JFrame. A new Circles() Object is being instantiated and added to the JFrame. constructor call © A+ Computer Science -

25 © A+ Computer Science - www.apluscompsci.com
Frame Frame / JFrame Canvas / JPanel The Frame is used to hold up / display a Canvas or Panel. Frame / JFrame Objects are used to hold up / display Canvas and JPanel Objects. All drawing occurs on the Canvas / JPanel. The JFrame simply provides a place to show Canvas / JPanel after the drawing has occurred. © A+ Computer Science -

26 © A+ Computer Science - www.apluscompsci.com
Paint public class Circles extends Canvas { //constructors public void paint( Graphics window ) window.setColor(Color.BLACK); window.drawString("Circles", 50, 50); window.setColor(Color.BLUE); window.drawOval(500,300,40,40); } //other methods paint paint() is called automatically when you instantiate the class containing the paint method. When an event is triggered that requires a redraw, paint is called again. To call paint() without a Graphics parameter, you can use the repaint() method. paint() is the method typically used to draw Graphics on the window. There are other methods that could be used, but paint() is used most frequently. paint() is called when the window needs to be redrawn. If an event occurs that requires the window be updated, the system will call paint(). paint() can be called without a Graphics parameter by simply using the repaint() method. paintComponent() is another method used for drawing / redrawing the window. © A+ Computer Science -

27 © A+ Computer Science - www.apluscompsci.com
graphicsrunner.java circles.java © A+ Computer Science -

28 Graphics and Methods

29 frequently used methods © A+ Computer Science - www.apluscompsci.com
Graphics frequently used methods Name Use setColor(x) sets the current drawing color to x drawString(s,x,y) draws String s at spot x,y drawOval(x,y,w,h) draws an unfilled oval at spot x,y that is w wide and h tall fillOval(x,y,w,h) draws a filled oval at spot x,y that is w wide and h tall The Java Graphics class has many useful methods. The chart above lists the most common methods we will be using. import java.awt.Graphics; import java.awt.Color; import javax.swing.JFrame; © A+ Computer Science -

30 © A+ Computer Science - www.apluscompsci.com
Passing Parameters A parameter/argument is a channel used to pass information to a method. setColor() is a method of the Graphics class that receives a Color. void setColor(Color theColor) window.setColor( Color.RED ); Most, if not all, of the Graphics class methods require parameters. The parameters communicate to the Graphics methods information about what needs to be done. The setColor() method changes the current drawing color to the color passed in. setColor() cannot be called without a Color parameter. method call with parameter © A+ Computer Science -

31 © A+ Computer Science - www.apluscompsci.com
Passing Parameters void fillRect (int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); method call with parameters The fillRect() method requires four pieces of information. fillRect() needs an x value, a y value, a width, and a height. fillRect() will draw a filled rectangle on the window at x,y with height and width as stated by the parameters. © A+ Computer Science -

32 © A+ Computer Science - www.apluscompsci.com
Passing Parameters void fillRect(int x, int y, int width, int height) window.fillRect( 10, 50, 30, 70 ); The call to fillRect would draw a rectangle at position 10,50 with a width of 30 and a height of 70. The fillRect() method requires four pieces of information. fillRect() needs an x value, a y value, a width, and a height. fillRect() will draw a filled rectangle on the window at x,y with height and width as stated by the parameters. © A+ Computer Science -

33 frequently used methods © A+ Computer Science - www.apluscompsci.com
Graphics frequently used methods Name Use drawLine(a,b,c,d) draws a line starting at point a,b and going to point c,d drawRect(x,y,w,h) draws an unfilled rectangle at spot x,y that is w wide and h tall fillRect(x,y,w,h) draws a filled rectangle at spot x,y that is w wide and h tall import java.awt.Graphics; import java.awt.Color; import javax.swing.JFrame; © A+ Computer Science -

34 © A+ Computer Science - www.apluscompsci.com
Graphics X goes across 0,0 Y goes down Notice the Graphics screen being used with Graphics class does not use Cartesian coordinates. X goes across and Y goes down. X starts at 0 and goes to MAXX which in this case is Y starts at 0 and goes down to MAXY which in this case is 479. 639,479 window.fillRect( 10, 50, 30, 70 ); © A+ Computer Science -

35 © A+ Computer Science - www.apluscompsci.com
Passing Parameters X goes across 0,0 X=100 y=100 Y goes down width=50 height=50 window.fillOval( 100, 100, 50, 50 ); © A+ Computer Science -

36 © A+ Computer Science - www.apluscompsci.com
Rectangles public void paint( Graphics window ) { window.setColor(Color.BLUE); window.fillRect(150, 300, 100, 20); window.setColor(Color.GRAY); window.drawRect(200,80,50,50); } The paint() method is typically doing the most drawing. Other methods may be called from paint() as well. © A+ Computer Science -

37 © A+ Computer Science - www.apluscompsci.com
Running Examples public class GraphicsRunner extends JFrame { //variables not shown public GraphicsRunner() setSize(WIDTH,HEIGHT); getContentPane().add( new PickAnExample() ); setVisible(true); } //main not shown In the GraphicsRunner constructor, specify which example you wish to run. © A+ Computer Science -

38 © A+ Computer Science - www.apluscompsci.com
Running Examples public class GraphicsRunner extends JFrame { //variables not shown public GraphicsRunner() setSize(WIDTH,HEIGHT); getContentPane().add( new Rectangles() ); setVisible(true); } //main not shown In the GraphicsRunner constructor, specify which example you wish to run. © A+ Computer Science -

39 © A+ Computer Science - www.apluscompsci.com
rectangles.java © A+ Computer Science -

40 © A+ Computer Science - www.apluscompsci.com
Running Examples public class GraphicsRunner extends JFrame { //variables not shown public GraphicsRunner() setSize(WIDTH,HEIGHT); getContentPane().add( new Lines() ); setVisible(true); } //main not shown In the GraphicsRunner constructor, specify which example you wish to run. © A+ Computer Science -

41 © A+ Computer Science - www.apluscompsci.com
lines.java © A+ Computer Science -

42 Graphics frequently used methods Name Use
drawArc(x,y,w,h,startAngle,arcAngle) draws an arc at spot x,y that is w wide and h tall fillArc(x,y,w,h,startAngle,arcAngle) draws a filled arc at spot x,y that is w wide and h tall startAngle specifies the start of the arc arcAngle specifies the length of the arc import java.awt.Graphics; import java.awt.Color; import javax.swing.JFrame; © A+ Computer Science -

43 © A+ Computer Science - www.apluscompsci.com
arcs.java © A+ Computer Science -

44 © A+ Computer Science - www.apluscompsci.com
fonts.java © A+ Computer Science -

45 © A+ Computer Science - www.apluscompsci.com
colors.java © A+ Computer Science -

46 © A+ Computer Science - www.apluscompsci.com
Work on Programs! Crank Some Code! © A+ Computer Science -


Download ppt "A+ Computer Science METHODS."

Similar presentations


Ads by Google