Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.

Similar presentations


Presentation on theme: "Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST."— Presentation transcript:

1 Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST

2 Today The anatomy of a class Attributes Methods  Method calls, return values Constructors Importing existing classes RectangleDemo API documentation

3 Goals for today To (better) understand the concepts of classes and objects To be able to create objects To be able to call methods To be able to browse the API documentation

4 Chapter 2 Classes and Objects

5 Objects and Classes Object: entity that you can manipulate in your programs (by calling methods) Each object belongs to a class. For example, System.out belongs to the class PrintStream Figure 3: Representation of the System_out object

6 The Anatomy of a Class Package and import statements Class signature Attributes Methods Note! Main is a method package... import... public class ClassName { } public String getArtist(String n){... } public String artist; private int numberOfHits; private void computeNumHits(){... }

7 The Anatomy of a Class Your code goes here! package... import... public class ClassName { } public String getArtist(String n){... } public String artist; private int numberOfHits; private void computeNumHits(){... }

8 Attributes Attributes are variables (data) that are valid for the whole object Typical examples: names, status information Attributes can be public (known outside) or private (known only within the object)‏ Attributes can be objects themselves Attributes define the state of any given object public String artist; private int numberOfHits; private int numberOfRecords; private Album bestAlbum;

9 Methods Method: Sequence of instructions that accesses the data of an object You manipulate objects by calling its methods Class: Set of objects with the same behavior The class determines legal methods Continued… String greeting = "Hello"; greeting.println() // Error greeting.length() // OK

10 Methods Method signature: specification of what goes into the method, and what comes out (i.e., what is returned from the method)‏ Method body: the actual code that does all the work Methods can have local variables public String getArtist(String name) { String defaultArtist = “Sting”; if(name.equals(“Prince”)) { return “The artist formerly known as “ + name; } return defaultArtist; }

11 Method signature public String getArtist(String name) { String defaultArtist = “Sting”; if(name.equals(“Prince”)) { return “The artist formerly known as “ + name; } return defaultArtist; } Method name Access specifier Return type Argument list

12 Invoking Methods public static void main(String[] args) { System.out.println(“Who released Purple Rain?”); System.out.println(getArtist(“Prince”)); System.out.println(“Who released Nothing Like the Sun?”); System.out.println(getArtist(“not Prince”)); } public String getArtist(String name) { String defaultArtist = “Sting”; if(name.equals(“Prince”)) { return “The artist formerly known as “ + name; } return defaultArtist; } returnValue = methodName(arguments);

13 Return Values Figure 6: Invoking the length Method on a String Object

14 Passing Return Values You can also use the return value as a parameter of another method: Not all methods return values. Example: Continued… System.out.println(greeting.length()); println()‏

15 Passing Return Values Figure 7: Passing the Result of a Method Call to Another Method

16 A More Complex Call replace method carries out a search-and- replace operation As Figure 8 shows, this method call has  one implicit parameter: the string "Mississippi"  two explicit parameters: the strings "issipp" and "our"  a return value: the string "Missouri" Continued… river.replace("issipp", "our") // constructs a new string ("Missouri")‏

17 A More Complex Call Figure 8: Calling the replace Method

18 Method Definitions If method returns no value, the return type is declared as void A method name is overloaded if a class has more than one method with the same name (but different parameter types) public void println(String output) // in class PrintStream public void println(String output) public void println(int output)‏

19 Rectangular Shapes and Rectangle Objects Objects of type Rectangle describe rectangular shapes Figure 9: Rectangular Shapes

20 Rectangular Shapes and Rectangle Objects A Rectangle object is not, in fact, a rectangular shape–it is an object that contains a set of numbers (attributes) that describe each individual rectangle, along with a number of operations (methods) that can be applied to it Figure 10: Rectangular Objects

21 Constructing Objects Detail:  The new operator makes a Rectangle object  It uses the parameters (in this case, 5, 10, 20, and 30 ) to initialize the data of the object  It returns the object Usually the output of the new operator is stored in a variable Rectangle box = new Rectangle(5, 10, 20, 30); new Rectangle(5, 10, 20, 30)‏

22 Constructing Objects The process of creating a new object is called construction The four values 5, 10, 20, and 30 are called the construction parameters Some classes let you construct objects in multiple ways new Rectangle() // constructs a rectangle with its top-left corner // at the origin (0, 0), width 0, and height 0

23 Syntax 2.3: Object Construction new ClassName(parameters)‏ Example: new Rectangle(5, 10, 20, 30) new Rectangle()‏ Purpose: To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object

24 Accessor and Mutator Methods Figure 11: Using the translate Method to Move a Rectangle

25 Implementing a Test Program Write a new class Supply a main method Inside the main method, construct one or more objects Call methods in the objects Display the results of the method calls

26 Importing Packages Don't forget to include appropriate packages:  Java classes are grouped into packages  Import library classes by specifying the package and class name:  You don't need to import classes in the java.lang package such as String and System import java.awt.Rectangle;

27 Syntax 2.4: Importing a Class from a Package import packageName.ClassName; Example: import java.awt.Rectangle; Purpose: To import a class from a package for use in a program.

28 A demo: RectangleDemo.java Start in main Construct a new RectangleDemo object, which inherits from javax.swing.JPanel Create 4 Rectangle objects Create a javax.swing.JFrame window frame and add the panel Draw the Rectangle objects in a paint method inherited from javax.swing.JPanel

29 Inheriting from Classes /** * RectangleDemo * Demo illustrating the creation... */ package gui; import javax.swing.JFrame;... import java.awt.Color; public class RectangleDemo extends JPanel {

30 From main to Constructor /** * main - the program starts here * @param args */ public static void main(String[] args) { RectangleDemo me = new RectangleDemo(); } Create an instance of this class to get out of the static main context

31 Data and Constructor public class RectangleDemo extends JPanel { Rectangle rect1, rect2, rect3, rect4; /** Constructor */ public RectangleDemo() { rect1 = new Rectangle(5,10,30,40); rect2 = new Rectangle(30,30,67,10); rect3 = new Rectangle(76,12,12,12); rect4 = new Rectangle(100,100,200,200);... Use attributes to represent rectangles

32 paint – an inherited method public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setBackground(new Color(100, 100, 200)); g2.setColor(new Color(10,20,30)); g2.draw(rect1); g2.setColor(new Color(110,120,130)); g2.draw(rect2); g2.setColor(Color.YELLOW); g2.fill(rect3); g2.setColor(Color.BLACK); g2.fill(rect4); } Paint rectangles: java.awt.Graphics2D

33 The API Documentation API: Application Programming Interface Lists classes and methods in the Java library http://java.sun.com/javase/6/docs/api/

34 The API Documentation of the Standard Java Library Figure 13: The API Documentation of the Standard Java Library

35 The API Documentation for the Rectangle Class Figure 14: The API Documentation of the Rectangle Class


Download ppt "Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST."

Similar presentations


Ads by Google