#### Transcript Writing classes - The University of North Carolina at Chapel Hill

```COMP 14 - 03
Introduction to Programming
July 13, 2005
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Today in Comp 14
• Review Classes and Methods
♦ Rectangle class
♦ ReverseString class
2
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Data Scope
Example
public class Rectangle
{
// variables declared here are class-level
// available in all methods in Rectangle class
public int computeArea()
{
// variables declared here are method-level
// only available in computeArea()
}
public void print()
{
// variables declared here are method-level
// only available in print()
}
}
3
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review
method name for multiple methods
• The signature of each overloaded method
must be unique
♦ Number of parameters
♦ Type of the parameters
♦ not the return type of the method, though
• The compiler determines which version of
the method is being invoked by analyzing
the parameters
4
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Constructors
public class Rectangle
{
private int length;
private int width;
Rectangle r2 = new
Rectangle (5, 10);
public Rectangle ()
{
length = 0;
width = 0;
}
5
public Rectangle (int l, int w)
{
length = l;
width = w;
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Creating and Using Objects
• Create an object:
r
2500
Rectangle r;
r = new Rectangle(2, 3);
2500
2
OR
3
Rectangle r = new Rectangle(2, 3);
• Use the object and the dot operator to
access methods:
r.setLength(5);
r.setWidth(10);
r
2500
2500
5
2
6
3
10
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Exercise
Write a method for the Rectangle class
called printBox that will print the
rectangle as a box made of %
example: length = 3, width = 5
%%%%%
%
%
%%%%%
7
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
public void printBox ()
{
for (int i = 1; i <= width; i++)
System.out.print("%");
System.out.println( );
for (int i = 1; i <= length - 2; i++) {
System.out.print("%");
for (int j = 1; j <= width - 2; j++)
System.out.print(" ");
System.out.println("%");
}
length = 3, width = 8
for (int i = 1; i <= width; i++)
System.out.print("%");
System.out.println( );
}
8
%%%%%%%%
%
%
%%%%%%%%
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
public void printBox ()
{
for (int i = 1; i <= width; i++)
System.out.print("%");
System.out.println( );
for (int i = 1; i <= length - 2; i++) {
System.out.print("%");
for (int j = 1; j <= width - 2; j++)
System.out.print(" ");
System.out.println("%");
}
length = 8, width = 3
for (int i = 1; i <= width; i++)
System.out.print("%");
System.out.println( );
}
9
%%%
% %
% %
% %
% %
% %
% %
%%%
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Testing
public class RectangleTester
{
public static void main (String[] args)
{
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle (20, 30);
r1.setWidth(5);
r1.setLength(10);
r1.print();
What if we just wanted to
r2.print();
output r1 and r2 using
System.out.println(r1);
System.out.println?
System.out.println(r2);
}
} // end of RectangleTester class
10
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
The toString Method
• Special method in Java classes
• Produces a String object based on the
current object, suitable for printing
• Mapped to the '+' operator
• Also called when the object is a
parameter in a print() or println()
method
• There is a default toString method,
but it's better if we write our own
System.out.println(r1);
11
System.out.println(r1.toString());
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Rectangle.java
public String toString()
{
String result = "";
result += "length: " + length + "\n";
result += "width: " + width;
return (result);
}
Rectangle r = new Rectangle (2,3);
System.out.println (r);
length: 2
width: 3
12
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Rectangle.java
• What if we wanted to print out
the box when we say
System.out.println(r1)?
13
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Rectangle.java
public class Rectangle
{
private int length;
private int width;
public Rectangle (int length, int width)
{
length = length;
width = width;
}
Which length and which width??
14
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
The Reference this
• Reserved word
• Refers to instance variables and
methods of a class
• Allows you to distinguish between
member variables and local variables
with the same name
15
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Rectangle.java
public class Rectangle
{
private int length;
private int width;
public Rectangle (int length, int width)
{
this.length = length;
this.width = width;
}
16
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
ReverseString.java
• Available on course web page
♦ Fully commented
• ReverseTester.java
♦ Driver class : Tests the ReverseString
class
♦ Also available online
17
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• When do you use dot operator (.)
to access methods?
ReverseString r1 = new ReverseString( keyboard.readLine() );
// Output the reverse of the entered string
System.out.println("The reverse of the string is " + r1.getReverse() );
Inside ReverseTester.java
18
Dot operator to access
method getReverse()
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• When do you use dot operator (.)
to access methods?
ReverseString r1 = new ReverseString( keyboard.readLine() );
// Output the reverse of the entered string
System.out.println("The reverse of the string is " + r1.getReverse() );
public ReverseString(String s){
original = s;
computeReverse();
}
Inside
ReverseString.java
No dot operator to access computeReverse()
19
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• When do you use dot operator (.)
to access methods?
♦ Must use dot operator to access method
that is in a different class
♦ To access a method in ReverseString from
ReverseTester, use dot operator
♦ To access a method in ReverseString from
ReverseString, NO dot operator
20
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• When do you use public vs. private?
private void computeReverse(){
public boolean isPalindrome(){
// method body
// method body
}
}
Only called from ReverseString
class
Called from ReverseTester class
Where are these methods called from? (What
class are they accessed in?)
21
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• When do you use public vs.
private?
♦ Use private:
• When the method or data member is only going
to be accessed inside the same class
♦ Use public:
• When the method or data member is going to
be accessed outside the class
• Anything that is called, or used, inside
ReverseTester class must be public
22
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why did we not use static in
ReverseString class?
When and why do we use static?
How do we access static methods or
variables from another class?
23
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why did we not use static in
ReverseString class?
When and why do we use static?
How do we access static methods or
variables from another class? This method must be
Dot classname.methodname()
operator
declared static inside
class classname
Example: Integer.parseInt(“48”);
public static int parseInt(String s)
24
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why did we not use static in
ReverseString class?
When and why do we use static?
• Do not need object to use method/variable
• Consistent among all objects
• Shared among all objects of the class
25
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why did we not use static in
ReverseString class?
When and why do we use static?
• Want all objects of class to share one copy of data
• Do not need method/variable to be object specific
NOTE:
• Can't call a non-static method from a static method
• Can't access non-static variables from a static
method
26
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why did we not use static in
ReverseString class?
NOTE:
• Can't call a non-static method from a static method
• Can't access non-static variables from a static
method
public static void main (String[] args) {
myMethod();
}
main method is static,
therefore myMethod() must
also be static within same
class
public static void myMethod ( ) {
// method body
}
27
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why was there no main method in
ReverseString class?
♦ Only a driver class (a class that tests your
program) has a main method
♦ General rule:
• For each program you write, there will only be one
main method
• Here, it was given in ReverseTester class
28
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why was there no keyboard input
in ReverseString class
♦ Keyboard input already performed in
ReverseTester
♦ How did we give input to ReverseString
class??
29
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why was there no keyboard input
in ReverseString class
♦ How did we give input to ReverseString
class?? Parameters
Specifically, in this example, parameter to the constructor
Inside
ReverseString
class
30
public ReverseString(String s){
original = s;
computeReverse();
}
Formal Parameter
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why was there no keyboard input
in ReverseString class
♦ How did we give input to ReverseString
class?? Parameters
ReverseString r1 = new ReverseString( keyboard.readLine() );
Inside ReverseTester class
Inside
ReverseString
class
31
Actual Parameter
public ReverseString(String s){
original = s;
computeReverse();
}
Formal Parameter
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why didn’t we use
System.out.println in the
ReverseString class?
♦ Output to the screen already performed in
ReverseTester
♦ How did we give information to
ReverseTester class for output?
32
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why didn’t we use System.out.println in
the ReverseString class?
♦ How did we give information to ReverseTester class
for output?
Return from Methods
Specifically, in this example:
System.out.println("The reverse of the string is " + r1.getReverse() );
Inside ReverseTester class
Inside
ReverseString
class
33
return type
Call to getReverse() will
be replaced with a String
public String getReverse(){
return reverse;
}
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Review Classes and Methods
ReverseString.java
• Why didn’t we use System.out.println in
the ReverseString class?
♦ How did we give information to ReverseTester class
for output?
Return from Methods
Specifically, in this example:
System.out.println("The reverse of the string is " + r1.getReverse() );
Inside ReverseTester class
Inside
ReverseString
class
34
public String getReverse(){
return reverse;
}
Replaced with
the value of
reverse
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Communication between
Classes
• Methods provide communication
between methods
♦ Parameters provide input into class
♦ Return value provides output
public returnType methodName ( formal parameters )
Output from method
35
Input to method
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Question
• Write a program that takes in 5
int values, computes the sum,
and then prints the values in
reverse order.
36
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL
Solution
int num0, num1, num2, num3, num4;
int sum;
System.out.println("Enter five integers, one per line ");
num0
num1
num2
num3
num4
=
=
=
=
=