1 / 37

COMP 14 - 03 Introduction to Programming

COMP 14 - 03 Introduction to Programming. Adrian Ilie July 13, 2005. Today in Comp 14. Review Classes and Methods Rectangle class ReverseString class. Data Scope Example. public class Rectangle { // variables declared here are class-level // available in all methods in Rectangle class

Télécharger la présentation

COMP 14 - 03 Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 14 - 03Introduction to Programming Adrian Ilie July 13, 2005

  2. Today in Comp 14 • Review Classes and Methods • Rectangle class • ReverseString class

  3. 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() } }

  4. ReviewOverloading 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

  5. Constructors public class Rectangle { private int length; private int width; Rectangle r2 = new Rectangle (5, 10); public Rectangle () { length = 0; width = 0; } public Rectangle (int l, int w) { length= l; width= w; }

  6. Creating and Using Objects r • 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); 2500 2500 3 2 r 2500 2500 3 5 2 10

  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. publicvoid 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. publicvoid 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. 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(); 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?

  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.toString()); System.out.println(r1);

  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. Rectangle.java • What if we wanted to print out the box when we say System.out.println(r1)?

  14. Rectangle.java public class Rectangle { private intlength; private intwidth; public Rectangle (intlength, intwidth) { length = length; width = width; } Which length and which width??

  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. Rectangle.java public class Rectangle { private intlength; private intwidth; public Rectangle (intlength, intwidth) { this.length = length; this.width = width; }

  17. ReverseString.java • Available on course web page • Fully commented • ReverseTester.java • Driver class : Tests the ReverseString class • Also available online

  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 stringSystem.out.println("The reverse of the string is " + r1.getReverse() ); Inside ReverseTester.java Dot operator to access method getReverse()

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

  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. Review Classes and Methods ReverseString.java • When do you use public vs. private? public boolean isPalindrome(){ // method body} private void computeReverse(){ // method body} Only called from ReverseString class Called from ReverseTester class Where are these methods called from? (What class are they accessed in?)

  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 mustbe public

  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. 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 declared static inside class classname classname.methodname() Example:Integer.parseInt(“48”); public static int parseInt(String s) Dot operator

  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. Review Classes and Methods ReverseString.java • Why did we not use static in ReverseString class? When and whydo 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. 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 main method is static, therefore myMethod() must also be static within same class public static void main (String[] args) { myMethod(); } public static void myMethod ( ) { // method body }

  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. 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. 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. 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 ReverseString r1 = new ReverseString( keyboard.readLine() ); Inside ReverseTester class Actual Parameter public ReverseString(String s){ original = s; computeReverse();} Formal Parameter Inside ReverseString class

  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. 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 Call to getReverse() will be replaced with a String return type public String getReverse(){ return reverse;} Inside ReverseString class

  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() ); Inside ReverseTester class Replaced with the value of reverse public String getReverse(){ return reverse;} Inside ReverseString class

  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. Question • Write a program that takes in 5 int values, computes the sum, and then prints the values in reverse order.

  37. Solution 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); What if we wanted to add and print 100 values?

More Related