The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 - 03 Introduction to Programming...

37
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 - 03 Introduction to Programming Adrian Ilie Adrian Ilie July 13, 2005 July 13, 2005
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    0

Transcript of The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 - 03 Introduction to Programming...

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie

COMP 14 - 03Introduction to Programming

Adrian IlieAdrian IlieJuly 13, 2005July 13, 2005

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie2

Today in Comp 14

• Review Classes and Methods♦ Rectangle class♦ ReverseString class

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie3

Data ScopeExample

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()

}

}

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie4

• Overloading - the process of using the same 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

ReviewOverloading Methods

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie5

Constructors

public Rectangle (int l, int w){ length = l; width = w;}

public class Rectangle{

private int length;private int width; Rectangle r2 = new

Rectangle (5, 10);public Rectangle (){ length = 0; width = 0;}

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie6

• Create an object:Rectangle r;r = new Rectangle(2, 3);ORRectangle r = new Rectangle(2, 3);

• Use the object and the dot operator to access methods:r.setLength(5);r.setWidth(10);

r2500

25002 3

Creating and Using Objects

r

2 3

25002500

5 10

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie7

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

%%%%%

% %

%%%%%

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie8

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("%");}

for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( );}

%%%%%%%%

length = 3, width = 8

% %%%%%%%%%

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie9

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("%");}

for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( );}

%%%

length = 8, width = 3

% %% %% %% %% %% %%%%

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie10

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(); r2.print();

System.out.println(r1);System.out.println(r2);

}

} // end of RectangleTester class

What if we just wanted to output r1 and r2 using System.out.println?

Testing

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie11

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);

System.out.println(r1.toString());

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie12

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: 2width: 3

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie13

Rectangle.java

• What if we wanted to print out the box when we say System.out.println(r1)?

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie14

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??

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie15

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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie16

Rectangle.java

public class Rectangle{

private int length;private int width;

public Rectangle (int length, int width){

this.length = length; this.width = width;

}

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie17

ReverseString.java

• Available on course web page♦ Fully commented

• ReverseTester.java♦ Driver class : Tests the

ReverseString class♦ Also available online

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie18

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 stringSystem.out.println("The reverse of the string is " + r1.getReverse() );

Inside ReverseTester.javaDot operator to access method getReverse()

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie19

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 stringSystem.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()

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie20

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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie21

Review Classes and Methods ReverseString.java

• When do you use public vs. private?

private void computeReverse(){

// method body}

public boolean isPalindrome(){

// method body}

Where are these methods called from? (What class are they accessed in?)

Only called from ReverseString class

Called from ReverseTester class

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie22

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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie23

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?

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie24

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?

classname.methodname()

Example: Integer.parseInt(“48”);

public static int parseInt(String s)

This method must be declared static inside class classname

Dot operator

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie25

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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie26

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 specificNOTE:

• Can't call a non-static method from a static method• Can't access non-static variables from a static method

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie27

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 methodpublic 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

}

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie28

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

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie29

Review Classes and Methods ReverseString.java

• Why was there no keyboard input in ReverseString class (keyboard.readLine())? ♦ Keyboard input already performed in

ReverseTester

♦ How did we give input to ReverseString class??

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie30

Review Classes and Methods ReverseString.java

• Why was there no keyboard input in ReverseString class (keyboard.readLine())? ♦ How did we give input to ReverseString

class?? ParametersSpecifically, in this example, parameter to the

constructor

public ReverseString(String s){

original = s;computeReverse();

}

Formal Parameter

Inside ReverseStrin

g class

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie31

Review Classes and Methods ReverseString.java

• Why was there no keyboard input in ReverseString class (keyboard.readLine())? ♦ How did we give input to ReverseString

class?? Parameters

public ReverseString(String s){

original = s;computeReverse();

}

Formal Parameter

Inside ReverseStrin

g class

ReverseString r1 = new ReverseString( keyboard.readLine() );

Inside ReverseTester class Actual Parameter

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie32

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?

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie33

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 MethodsSpecifically, in this example:

System.out.println("The reverse of the string is " + r1.getReverse() );

public String getReverse(){

return reverse;}

Inside ReverseStrin

g class

Inside ReverseTester class return type

Call to getReverse() will be replaced with a String

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie34

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 MethodsSpecifically, in this example:

System.out.println("The reverse of the string is " + r1.getReverse() );

public String getReverse(){

return reverse;}

Inside ReverseStrin

g class

Inside ReverseTester class

Replaced with the value of reverse

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie35

Communication between Classes

• Methods provide communication between methods♦ Parameters provide input into class♦ Return value provides outputpublic returnType methodName ( formal

parameters ) Output from method

Input to method

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie36

Question

• Write a program that takes in 5 int values, computes the sum, and then prints the values in reverse order.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Adrian Ilie37

int num0, num1, num2, num3, num4;int sum;

System.out.println("Enter five integers, one per line ");

num0 = Integer.parseInt(keyboard.readLine());num1 = Integer.parseInt(keyboard.readLine());num2 = Integer.parseInt(keyboard.readLine());num3 = Integer.parseInt(keyboard.readLine());num4 = Integer.parseInt(keyboard.readLine());

sum = num0 + num1 + num2 + num3 + num4;

System.out.println("The sum of the numbers = " + sum);System.out.println(num4 + " " + num3 + " " + num2 + " “

+ num1 + " " + num0);

Solution

What if we wanted to add and print 100 values?