class specifications
play

Class Specifications } A class diagram only gives a sketch of that - PowerPoint PPT Presentation

Class Specifications } A class diagram only gives a sketch of that class } More detail is given by the full specification of a class } Every useful Java class should have such a specification } When you write code, its up to you to create


  1. Class Specifications } A class diagram only gives a “sketch” of that class } More detail is given by the full specification of a class } Every useful Java class should have such a specification } When you write code, it’s up to you to create this document, so others can make proper use of your code } T wo main parts: Class #24: Using Methods Invariants: things that are always true of class objects 1. Methods: all the operations involving the class, with: 2. } Pre-conditions: Anything that needs to be true before the method Software Design I (CS 120): D. Mathias will work properly } Post-conditions: Anything that will be true after the method is done 2 Software Design I (CS 120) Basic Methods (DrawingGizmo class) Methods with Both Pre- and Post-conditions Update Methods public void moveForward() public void draw() pre : The DrawingGizmo should be at least 20 pixels from the edge of the screen towards post : The DrawingGizmo is set to drawing mode (and is colored green). which it is pointing; if it is not, then the object and part of the line it draws will appear o ff -screen. public void dontDraw() post : The DrawingGizmo is set to moving mode (and is colored red). post : The DrawingGizmo moves in the direction of its arrow by 20 pixels. If it is is drawing mode, a line segment will be drawn between its current and prior position. public void turnClockwise() post : The DrawingGizmo is rotated 30 degrees clockwise from its current heading. public void turnCounterclockwise() The preconditions on the moveForward() method tell us what post : The DrawingGizmo is rotated 30 degrees counterclockwise from its current heading. must be true before the method can properly run. (If these are not satisfied, then the method may not work properly, or } Some methods have post-conditions only even work at all.) } Nothing special is required for them to work } Only need DrawingGizmo object instance to call them The postconditions tell us what will be true after the method has finished running properly. 3 4 Software Design I (CS 120) Software Design I (CS 120) 1

  2. More Complex Methods A Parameterized Method } DrawingGizmo object has public void turnBy( int n ) DrawingGizmo simple methods, each of post: This object is rotated n degrees clockwise from its which does the same thing, previous direction. the same way, every time <<constructor>> } The turnBy( int ) method wants us to insert an DrawingGizmo() integer value (“ int ” in Java) as an input parameter <<update>> } But it also has some more } We must give it an integer when we call it, or it won’t work! void moveForward() complex methods, which: void turnClockwise() pen.turnBy( 90 ); void turnCounterclockwise() } Take input parameters All of these are OK. void dontDraw() pen.turnBy( 9000 ); void draw() } Change what they do based pen.turnBy( -180 ); on those parameters void turnBy( int ) pen.turnBy( 3.6 ); void moveBy( int ) These are both wrong. void delayBy( int ) pen.turnBy(); 5 6 Software Design I (CS 120) Software Design I (CS 120) Parameterized Methods vs. Two More Parameterized Methods Non-parameterized Methods The parentheses () in a method } specification serve two purposes: DrawingGizmo public void setBackground( java.awt.Color ) They indicate that it is a method , so 1. compiler and coder know what it is public void setForeground( java.awt.Color ) 2. They show whether or not the method has any input parameters <<constructor>> If the method is not parameterized, then } These new DrawingGizmo methods change the color } DrawingGizmo() nothing is shown in the parentheses, and of the background window and drawing line when we use it, we cannot put anything there, or our code won’t compile <<update>> void moveForward() Similarly, if the method is parameterized, } void turnClockwise() then we have to put actual parameters } E.g., for a yellow line on a blue background: void turnCounterclockwise() into the code when we use it void dontDraw() In this description, the diagram shows the } void draw() pen.setBackground( java.awt.Color.blue ); type of the input parameter pen.setForeground( java.awt.Color.yellow ); It is up to us to actually choose what } void turnBy( int ) particular input value we will put in there when we call the method void moveBy( int ) void delayBy( int ) 7 8 Software Design I (CS 120) Software Design I (CS 120) 2

  3. Exercise Void vs Non-void Methods } List 3 methods you have used this semester that do not } Void Method: outputs something or changes one or more take parameters. objects but does not return a value } System.out.println( ) } window.setBackground(Color.blue) } List 3 methods you have used this semester that do take } Non-void Method: may make some change to object but at least one parameter. primary effect is that it returns a value } window.getWidth( ) } oval.getBackground( ) } In general: } Setters are void methods } Getters are non-void methods 9 10 Software Design I (CS 120) Software Design I (CS 120) Calling Void and Non-void Methods Non-Void Methods and Operators } Void method: standalone call String } These methods have non- void return types, meaning win.setWidth(500); <<constructor>> that after they have String( String ) win.setHeight(600); completed running, they System.out.println(“Window size set.”); <<query>> output a value char charAt( int ) int length() String substring( int ) } Non-void method: make use of returned value String substring( int, int ) } The basic rule of such <<translate>> methods is that they can be int w = win.getWidth( ); String toLowerCase() called and used anywhere if(win.getHeight( ) > w) String toUpperCase() System.out.println(“Portrait mode.”); in our code that a value of the return type can be used 11 12 Software Design I (CS 120) Software Design I (CS 120) 3

  4. Methods in the String Class } Strings are objects with many methods String Returns character at input position <<constructor>> first char at : 0 last char at : length() – 1 String( String ) <<query>> Returns number of chars in String char charAt( int ) int length() Returns sub-part of String, String substring( int ) starting from input position, String substring( int, int ) going all the way to end <<translate>> Returns subpart of String, starting String toLowerCase() from first input position, String toUpperCase() going to second - 1 Returns lower/upper case version Full list online at: (Note: does not change original) http://docs.oracle.com/javase/8/docs/api/java/lang/String.html 13 Software Design I (CS 120) 4

Recommend


More recommend