Transcript is a method
Basic Programming
INS-basis
GF, PDP and HST
Jens Dalsgaard Nielsen
Jan Dimon Bendtsen
Dept. of Electronic Systems
Today
• The anatomy of a class
• Attributes
• Methods
Method calls, return values
• Constructors
• Importing existing classes
• RectangleDemo
• API documentation
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
Chapter 2
Classes and Objects
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
The Anatomy of a Class
package ...
import ...
public class ClassName {
public String artist;
private int numberOfHits;
• Package and
import
statements
• Class signature
• Attributes
public String getArtist(String n){
...
}
private void computeNumHits(){
...
}
}
• Methods
• Note! Main is a
method
The Anatomy of a Class
package ...
import ...
public class ClassName {
public String artist;
private int numberOfHits;
public String getArtist(String n){
...
}
private void computeNumHits(){
...
}
}
• Your code
goes here!
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
public String artist;
object
private int numberOfHits;
private int numberOfRecords;
private Album bestAlbum;
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
String greeting = "Hello";
greeting.println() // Error
greeting.length() // OK
Continued…
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;
}
Method signature
Access specifier
Return type
Method name
Argument list
public String getArtist(String name) {
String defaultArtist = “Sting”;
if(name.equals(“Prince”)) {
return “The artist formerly known as “ + name;
}
return defaultArtist;
}
Invoking Methods
returnValue = methodName(arguments);
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;
}
Return Values
Figure 6:
Invoking the length Method on a String Object
Passing Return Values
• You can also use the return value as a
parameter of another method:
System.out.println(greeting.length());
• Not all methods return values. Example:
println()
Continued…
Passing Return Values
Figure 7:
Passing the Result of a Method Call to Another Method
A More Complex Call
• replace method carries out a search-andreplace operation
river.replace("issipp", "our")
// constructs a new string ("Missouri")
• 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…
A More Complex Call
Figure 8:
Calling the replace Method
Method Definitions
• If method returns no value, the return type is
declared as void
public void println(String output) // in class PrintStream
• 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)
public void println(int output)
Rectangular Shapes and
Rectangle Objects
• Objects of type Rectangle describe
rectangular shapes
Figure 9:
Rectangular Shapes
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
Constructing Objects
•
•
new Rectangle(5, 10, 20, 30)
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);
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
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
Accessor and Mutator Methods
Figure 11:
Using the translate Method to Move a
Rectangle
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
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:
import java.awt.Rectangle;
You don't need to import classes in the
java.lang package such as String and
System
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.
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
Inheriting from Classes
/**
* RectangleDemo
* Demo illustrating the creation...
*/
package gui;
import javax.swing.JFrame;
...
import java.awt.Color;
public class RectangleDemo extends JPanel {
From main to Constructor
• Create an instance of this class to get out of
the static main context
/**
* main - the program starts here
* @param args
*/
public static void main(String[] args) {
RectangleDemo me = new RectangleDemo();
}
Data and Constructor
• Use attributes to represent rectangles
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);
...
paint – an inherited method
• Paint rectangles: java.awt.Graphics2D
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);
}
The API Documentation
• API: Application Programming Interface
• Lists classes and methods in the Java library
• http://java.sun.com/javase/6/docs/api/
The API Documentation of the
Standard Java Library
Figure 13:
The API Documentation of the Standard Java Library
The API Documentation for the
Rectangle Class
Figure 14:
The API Documentation of the Rectangle Class