Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 - 03 Introduction to Programming Adrian Ilie July 13, 2005."— Presentation transcript:

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

2 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Today in Comp 14 Review Classes and Methods ♦ Rectangle class ♦ ReverseString class

3 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 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() }

4 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 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 Review Overloading Methods

5 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 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; }

6 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Create an object: Rectangle r; r = new Rectangle(2, 3); OR Rectangle r = new Rectangle(2, 3); Use the object and the dot operator to access methods: r.setLength(5); r.setWidth(10); r 2500 2 3 Creating and Using Objects r 2 3 2500 510

7 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 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 %%% % %%%

8 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 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 % % %%%%

9 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 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 % % % % % % % % % % % % %%

10 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 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

11 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 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());

12 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 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

13 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 Rectangle.java What if we wanted to print out the box when we say System.out.println(r1)?

14 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 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 ??

15 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 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

16 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 Rectangle.java public class Rectangle { private int length; private int width; public Rectangle (int length, int width) { this.length = length; this.width = width; }

17 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 ReverseString.java Available on course web page ♦ Fully commented ReverseTester.java ♦ Driver class : Tests the ReverseString class ♦ Also available online

18 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 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 Dot operator to access method getReverse()

19 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 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()

20 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 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

21 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 21 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

22 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 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

23 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 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?

24 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 24 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

25 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 25 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

26 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 26 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

27 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 27 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 }

28 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 28 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

29 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 29 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??

30 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 30 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 Specifically, in this example, parameter to the constructor public ReverseString(String s){ original = s; computeReverse(); } Formal Parameter Inside ReverseString class

31 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 31 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 ReverseString class ReverseString r1 = new ReverseString( keyboard.readLine() ); Inside ReverseTester class Actual Parameter

32 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 32 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?

33 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 33 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() ); public String getReverse(){ return reverse; } Inside ReverseString class Inside ReverseTester class return type Call to getReverse() will be replaced with a String

34 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 34 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() ); public String getReverse(){ return reverse; } Inside ReverseString class Inside ReverseTester class Replaced with the value of reverse

35 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 35 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 Input to method

36 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 36 Question Write a program that takes in 5 int values, computes the sum, and then prints the values in reverse order.

37 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 37 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?


Download ppt "The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 - 03 Introduction to Programming Adrian Ilie July 13, 2005."

Similar presentations


Ads by Google