Unformatted text preview:

COMP 14 - 03 Introduction to ProgrammingToday in Comp 14Data Scope ExampleReview Overloading MethodsConstructorsCreating and Using ObjectsExerciseSlide 8Slide 9Slide 10The toString MethodRectangle.javaSlide 13Slide 14The Reference thisSlide 16ReverseString.javaReview Classes and Methods ReverseString.javaSlide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Communication between ClassesQuestionSolutionThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian IlieCOMP 14 - 03Introduction to ProgrammingAdrian IlieAdrian IlieJuly 13, 2005July 13, 2005The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie2Today in Comp 14•Review Classes and Methods♦Rectangle class♦ReverseString classThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie3Data ScopeExamplepublic class Rectangle{// variables declared here are class-level// available in all methods in Rectangle classpublic 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 parametersReviewOverloading MethodsThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie5Constructorspublic 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);r2500250023Creating and Using Objectsr23250025005 10The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie7ExerciseWrite 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 Ilie8public 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 Ilie9public 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 Ilie10public 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 classWhat if we just wanted to output r1 and r2 using System.out.println?TestingThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie11The 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 ownSystem.out.println(r1);System.out.println(r1.toString());The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie12Rectangle.javapublic 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: 3The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie13Rectangle.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 Ilie14Rectangle.javapublic 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 Ilie15The 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 nameThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie16Rectangle.javapublic 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 Ilie17ReverseString.java•Available on course web page♦Fully commented•ReverseTester.java♦Driver class : Tests the ReverseString class♦Also available onlineThe UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie18Review 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 Ilie19Review 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.javaNo dot operator to access computeReverse()The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie20Review Classes and Methods ReverseString.java•When do you use dot operator (.) to access methods?♦Must use


View Full Document

UNC-Chapel Hill COMP 14 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?