1 / 49

CMSC 150 classes

CMSC 150 classes. CS 150: Mon 6 Feb 2012 . Images: Library of Congress. First: quick review of using methods. Remember types of methods : Function : creates and “returns” a value int score = game.getScore (); System.out.println ( “You get “ + game.numPointsForWin () );

siran
Download Presentation

CMSC 150 classes

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. CMSC 150classes CS 150: Mon 6 Feb 2012 Images: Library of Congress

  2. First: quick review of using methods • Remember types of methods: • Function: creates and “returns” a value intscore = game.getScore(); System.out.println( “You get “ + game.numPointsForWin() ); intnewScore = game.getScore() – game.numPointsPerMove(); Room playerRoom = game.getRoom( playerRow, playerCol ); • Remember to either: • Save the returned value in a variable of the appropriate type, or • Use the returned value in an expression.

  3. First: quick review of using methods • Remember types of methods: • Procedure: does work & returns no value (i.e., void return type) game.updateScore(newScore); game.updateMessage( “It’s the StayPuft Marshmallow Man!”); roomForSlimer.addSlimer(); • Remember: • methods with void return type don’t create a value • illogical to use them in an assignment or expression

  4. Anatomy of a method call • game.updateScore( newScore ); • An object reference is always needed to call a method Object to call the method with.

  5. Anatomy of a method call • game.updateScore( newScore ); • An object reference is always needed to call a method Name of the method to call.

  6. Anatomy of a method call • game.updateScore( newScore ); • An object reference is always needed to call a method Parameters – input values for the method.

  7. One Exception public class GhostBusters { … public booleanhandleMove( ) { … boolean result = checkForLoss( ); } public booleancheckForLoss( ) { … } } • EXCEPT: if the method definition and the method call are in the same class, the object reference is “implicit” • Supplied automatically by Java

  8. One other exception • Static methods are a little different int jenny = Integer.parseInt(“8675309”); This is the name of a class, not a reference to an object.

  9. What is a Class? • A “blueprint” of your entity to be implemented • Defines and encapsulates your entity’s • Characteristics (data) • Behaviors (methods) • Example:

  10. Example: Dog • What characteristics do dogs have in general? •  Your class’s data (instance variables) • What behaviors do dogs exhibit? •  Your class’s methods • But do these identify a specific dog?

  11. Example: Dog • Do these identify a specific dog?

  12. Example: Dog

  13. Example: Dog • Class: Dog • Characteristics: breed, eye color, weight • Behaviors: fetch(), lick(), sit(), stay(), poop() • Science Lab Lilly: an instance (object) of class Dog • chocolate lab, golden eyes, 65 lbs. • can perform Dog behaviors • Darth Bailey: an instance (object) of class Dog • black lab, brown eyes, 50 lbs. • can perform Dog behaviors

  14. In Summary • Write the class “blueprint” once • Create a specific object of that class • Create another object of that class • And another… • Consider a familiar example

  15. Write Our Own String Class public class SimpleString { // instance variables (data) // methods } • Notice no main() method • Only when you want to directly execute that class

  16. Writing Your Own String Class public class SimpleString { // instance variables (data) private char myFirstCharacter; private char mySecondCharacter; private intmyLength; // methods }

  17. Methods • Constructor • must have same name as class • syntax: public ClassName( parameters… ) • creates the object of this class type SimpleStringstr = new SimpleString(‘Z’,’a’); • no return type in method definition (implicit) • All other methods

  18. Writing Your Own String Class public class SimpleString { // instance variables (data) private char myFirstCharacter; private char mySecondCharacter; private intmyLength; // constructor public SimpleString( char charOne, char charTwo ) { myFirstCharacter = charOne; mySecondCharacter = charTwo; myLength = 2; } }

  19. In Another Class… public class Tester { public static void main( String[] args ) { SimpleStringstr1 = new SimpleString( ‘O’, ‘y’ ); SimpleString str2 = new SimpleString( ‘Z’, ‘a’ ); } }

  20. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } SimpleString.java public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } Tester.java

  21. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } Variables are of the class type SimpleString SimpleString.java public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } Tester.java

  22. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } Objects will be constructed using the constructor from that class SimpleString.java public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } Tester.java

  23. The Two Programs Together • SimpleString.java: • defines blueprint for a generic "SimpleString" object • we do not directly run this • Tester.java: • we run this (because it contains main()) • uses SimpleString as a variable type • creates objects of the class SimpleString • Let's see what happens in memory as we run Tester…

  24. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 128 129 130 131 SimpleString.java 132 declare… 133 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 137 Tester.java 138

  25. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 128 129 130 131 SimpleString.java 132 character literals stored elsewhere automatically 133 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  26. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 128 129 130 131 SimpleString.java 132 reserve enough space for a SimpleString object… 133 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  27. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 128 129 130 myFirstChar DATA 131 mySecondChar SimpleString.java 132 myLength reserve enough space for a SimpleString object… 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  28. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 128 129 130 myFirstChar 131 mySecondChar SimpleString.java 132 myLength invoke the constructor… 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  29. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 char1 128 char2 129 130 myFirstChar 131 mySecondChar which reserves space for the parameters (like variables)… SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  30. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 ‘O’ char1 128 char2 129 parameters 130 myFirstChar 131 mySecondChar SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } arguments 135 136 ‘O’ 137 Tester.java ‘y’ 138

  31. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 ‘O’ char1 128 ‘y’ char2 129 parameters 130 myFirstChar 131 mySecondChar SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } arguments 135 136 ‘O’ 137 Tester.java ‘y’ 138

  32. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 ‘O’ char1 128 ‘y’ char2 129 ‘O’ 130 myFirstChar 131 mySecondChar execute the first statement in the constructor… SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  33. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 ‘O’ char1 128 ‘y’ char2 129 ‘O’ 130 myFirstChar ‘y’ 131 mySecondChar execute the next statement in the constructor… SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  34. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 ‘O’ char1 128 ‘y’ char2 129 ‘O’ 130 myFirstChar ‘y’ 131 mySecondChar execute the next statement in the constructor… 2 SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  35. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } str1 127 128 129 ‘O’ 130 myFirstChar ‘y’ 131 mySecondChar as the constructor finishes, space for the parameters is reclaimed… 2 SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  36. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } 130 str1 127 128 129 ‘O’ 130 myFirstChar constructor call returns the memory address of the new object, which is assigned to str1… ‘y’ 131 mySecondChar 2 SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  37. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } 130 str1 127 128 129 ‘O’ 130 myFirstChar str1 variable now “references” the SimpleString object ‘y’ 131 mySecondChar 2 SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  38. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } variable of type SimpleString (containing reference) 130 str1 127 128 129 ‘O’ 130 myFirstChar object (instance) of class SimpleString ‘y’ 131 mySecondChar 2 SimpleString.java 132 myLength 133 METHOD STUFF 134 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 135 136 ‘O’ 137 Tester.java ‘y’ 138

  39. public class SimpleString { private char myFirstChar; private char mySecondChar; private intmyLength; public SimpleString(char char1, char char2) { myFirstChar= char1; mySecondChar= char2; myLength = 2; } } 340 str2 337 338 next statement results in similar picture elsewhere in memory 339 ‘Z’ 340 myFirstChar ‘a’ 341 mySecondChar 2 SimpleString.java 342 myLength 343 METHOD STUFF 344 public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } } 345 346 347 Tester.java 348

  40. public class MyString { // instance variables (data) private char myFirstCharacter; private char mySecondCharacter; private intmyLength; // default constructor public SimpleString() { myFirstCharacter = ‘ ‘; mySecondCharacter = ‘ ‘; myLength = 0; } // constructor with two parameters public SimpleString( char charOne, char charTwo ) { myFirstCharacter = charOne; mySecondCharacter = charTwo; myLength = 2; } }

  41. Methods • Constructor • must have same name as class • syntax: public ClassName( arguments… ) • creates the object of this class type MyStringstr = new MyString(); • no return type (implicit) • All other methods • use any non-reserved name • two types: perform action, return a value

  42. Methods • All other methods • use any non-reserved name • two types: perform action, return a value 1. perform an action: don't return anything public void doSomething() { … public void doSomethingElse() { … 2. return a value: provide a specific return type public int length() { … public String substring( int …

  43. Methods • All other methods • use any non-reserved name • two types: perform action, return a value 1. perform an action: don't return anything public void doSomething( … ) { … public void doSomethingElse( … ) { … 2. return a value: provide a specific return type public int length() { … public String substring( int …

  44. public class SimpleString { … // these methods do something without // needing to return any sort of value public void printTheString() { System.out.println( “” + myFirstCharacter + mySecondCharacter); } public void clearTheString() { myFirstCharacter= ‘ ‘; mySecondCharacter= ‘ ‘; myLength = 0; } }

  45. return type of method public class SimpleString { … // these methods do something without // needing to return any sort of value public void printTheString() { System.out.println( “” + myFirstCharacter + mySecondCharacter); } public void clearTheString() { myFirstCharacter= ‘ ‘; mySecondCharacter= ‘ ‘; myLength = 0; } }

  46. public class SimpleString { … // these methods do something without // needing to return any sort of value public void printTheString() { System.out.println( “” + myFirstCharacter + mySecondCharacter); } public void clearTheString() { myFirstCharacter= ‘ ‘; mySecondCharacter= ‘ ‘; myLength = 0; } } void: no return statement

  47. public class SimpleString { … // these methods return some sort of info public int length() { return myLength; // return instance variable } public char charAt(intindex) { char returnChar = ‘ ‘; if (index == 0) { returnChar = myFirstCharacter; } else if (index == 1) { returnChar = mySecondCharacter; } return returnChar; } }

  48. return type of method public class SimpleString { … // these methods return some sort of info public int length() { return myLength; // return instance variable } public char charAt(intindex) { char returnChar = ‘ ‘; if (index == 0) { returnChar = myFirstCharacter; } else if (index == 1) { returnChar = mySecondCharacter; } return returnChar; } }

  49. public class SimpleString { … // these methods return some sort of info public int length() { return myLength; // return instance variable } public char charAt(intindex) { char returnChar = ‘ ‘; if (index == 0) { returnChar = myFirstCharacter; } else if (index == 1) { returnChar = mySecondCharacter; } return returnChar; } } return statement using a variable of type int return statement using a variable of type char

More Related