COMP 14 03 Introduction to Programming Adrian Ilie July 13 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Today in Comp 14 Review Classes and Methods Rectangle class ReverseString class 2 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Review Overloading Methods 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 4 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Creating and Using Objects r Create an object 2500 2500 Rectangle r r new Rectangle 2 3 2 OR Rectangle r new Rectangle 2 3 3 Use the object and the dot operator to access methods r setLength 5 r setWidth 10 6 Adrian Ilie r 2500 2500 5 2 3 10 The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 System out println r1 output r1 and r2 using System out println r2 System out println end of RectangleTester class 10 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 11 System out println r1 Adrian Ilie System out println r1 toStri ng The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Rectangle java What if we wanted to print out the box when we say System out println r1 13 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Rectangle java public class Rectangle private int length private int width public Rectangle int length int width this length length this width width 16 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL ReverseString java Available on course web page Fully commented ReverseTester java Driver class Tests the ReverseString class Also available online 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Review Classes and Methods ReverseString java When do you use dot operator to access ReverseString r1 new ReverseString keyboard readLine methods Output the reverse of the entered string System out println The reverse of the string is r1 getReverse Inside ReverseTester java 18 Adrian Ilie Dot operator to access method getReverse The UNIVERSITY of NORTH CAROLINA at CHAPEL Review Classes and Methods ReverseString java When do you use dot operator to access ReverseString r1 new ReverseString keyboard readLine methods 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL 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
View Full Document