Class #05: More about Objects and Methods Software Design I (CS 120): M. Allen, 11 Sept. 2018 Important Java Syntax I: Identifiers Each object in a Java program has an identifier (name) This includes: any class, like DrawingGizmo any variable, like pen or pencil any method, like moveforward() anything else Each identifier in a Java program: will contain one or more alphanumeric characters: a z, A Z, 0 9 can also contain the underscore or dollar symbol: _, $ must not begin with a number (e.g., a3 is legal, but 3a is not) Other rules: Names must be unique within same scope This means that, inside any block defined by brackets, we cannot have two different elements with the same identifier Names are case sensitive Names should be meaningful Tuesday, 11 Sep. 2018 Software Design I (CS 120) 2 Important Java Syntax, II: Classes Important Java Syntax, III: main() Every working Java program must define at least one class of object. Here, our program begins by defining the Driver class. We choose this name, so it could be called something else, like Program, or PictureMaker, or whatever we like, as long as it is not some name that already exists. For instance, we cannot call it DrawingGizmo, since that is the name that already belongs to another piece of code we are using in this program. The format of the class definition we will use in most cases is: public class ClassName Every working Java program must begin with a special method called main(), that always looks exactly the same on its first line. This method uses constructors like DrawingGizmo() to actually create all the other objects we need and run their methods for us. Tuesday, 11 Sep. 2018 Software Design I (CS 120) 3 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 4 1
How Does main() Work? Important Java Syntax, IV The main() method that runs everything has a complex form: public static void main( String[] args ) Over the semester, we will learn what this all means! Every complete block of code must be enclosed by braces Blocks include things like a class or a complete method like main() here. Every complete line of code must end with a semi-colon. Tuesday, 11 Sep. 2018 Software Design I (CS 120) 5 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 6 Important Java Syntax, V These are Javadoc comments. Start at opening marker (/**) and end at close (*/). Usually appear right before methods, to explain their operation. Each method you write should have at least one line of these kind of comments. Java Coding Style The name of a class, like Driver or DrawingGizmo, should start with a Capital Letter. The name of a variable, like pencil, or a method, like main() or moveforward(), should start with a lower-case letter. Java allows us to place comments in code. Completely ignored by the computer, and are meant only for ourselves and other programmers to read. These are inline comments. Each line must start with the comment marker (//). Generally used inside body of a class or method (and should be used relatively little except in highly complex code). For multi-word names, we use capitals or underscore symbols, or both, to separate words: multiplewordmethodname() multiple_word_method_name() ComplexClassName Complex_Class_Name Tuesday, 11 Sep. 2018 Software Design I (CS 120) 7 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 8 2
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 The DrawingGizmo Specification The invariants give us the basic details about the objects, in terms of properties that must always hold: Invariants A DrawingGizmo object appears appears as an arrow within a window entitled Drawing Canvas, and is either in drawing mode (in which case it is green) or moving mode (in which case it is red). Two main parts: 1. Invariants: things that are always true of class objects 2. Methods: all the operations involving the class, with: Pre-conditions: Anything that needs to be true before the method will work properly Post-conditions: Anything that will be true after the method is done Tuesday, 11 Sep. 2018 Software Design I (CS 120) 9 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 10 Basic Methods Update Methods public void draw() post: The DrawingGizmo is set to drawing mode (and is colored green). public void dontdraw() post: The DrawingGizmo is set to moving mode (and is colored red). public void turnclockwise() post: The DrawingGizmo is rotated 30 degrees clockwise from its current heading. public void turncounterclockwise() post: The DrawingGizmo is rotated 30 degrees counterclockwise from its current heading. Some methods have post-conditions only Nothing special is required for them to work Only need DrawingGizmo object instance to call them Tuesday, 11 Sep. 2018 Software Design I (CS 120) 11 Methods with Both Pre- and Post-conditions public void moveforward() pre: The DrawingGizmo should be at least 20 pixels from the edge of the screen towards which it is pointing; if it is not, then the object and part of the line it draws will appear o -screen. post: TheDrawingGizmo 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. The preconditions on the moveforward() method tell us what must be true before the method can properly run. (If these are not satisfied, then the method may not necessarily work properly, or even work at all.) The postconditions tell us what will be true after the method has finished running properly. Tuesday, 11 Sep. 2018 Software Design I (CS 120) 12 3
More Complex Methods DrawingGizmo object has simple methods, each of which does the same thing, the same way, every time But it also has some more complex methods, which: Take input parameters Change what they do based on those parameters DrawingGizmo <<constructor>> DrawingGizmo() <<update>> void moveforward() void turnclockwise() void turncounterclockwise() void dontdraw() void draw() void turnby( int ) void moveby( int ) void delayby( int ) A Parameterized Method public void turnby( int n ) post: This object is rotated n degrees clockwise from its previous direction. The turnby( int ) method wants us to insert an integer value ( int in Java) as an input parameter We must give it an integer when we call it, or it won t work! pen.turnby( 9000 ); pen.turnby( -180 ); pen.turnby( 3.6 ); pen.turnby(); All of these are OK. These are both wrong. Tuesday, 11 Sep. 2018 Software Design I (CS 120) 13 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 14 Parameterized Methods vs. Non-parameterized Methods The parentheses () in a method specification serve two purposes: 1. They indicate that it is a method, so compiler and coder know what it is 2. They show whether or not the method has any input parameters If the method is not parameterized, then nothing is shown in the parentheses, and when we use it, we cannot put anything there, or our code won t compile Similarly, if the method is parameterized, then we have to put actual parameters into the code when we use it In this description, the diagram shows the type of the input parameter It is up to us to actually choose what particular input value we will put in there when we call the method DrawingGizmo <<constructor>> DrawingGizmo() <<update>> void moveforward() void turnclockwise() void turncounterclockwise() void dontdraw() void draw() void turnby( int ) void moveby( int ) void delayby( int ) Two More Parameterized Methods public void setbackground( java.awt.color ) public void setforeground( java.awt.color ) These new DrawingGizmo methods change the color of the background window and drawing line E.g., for a yellow line on a blue background: pen.setbackground( java.awt.color.blue ); pen.setforeground( java.awt.color.yellow ); Tuesday, 11 Sep. 2018 Software Design I (CS 120) 15 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 16 4
The Java Color Class Address java.awt.color.green library group library package class class member Supported Colors java.awt.color.black java.awt.color.blue java.awt.color.cyan java.awt.color.darkgray java.awt.color.gray java.awt.color.green java.awt.color.lightgray java.awt.color.magenta java.awt.color.orange java.awt.color.pink java.awt.color.red java.awt.color.white java.awt.color.yellow Tuesday, 11 Sep. 2018 Software Design I (CS 120) 17 Using Import Statements Instead Rather than write the entire Color address each time, we can import the Color class, using information found in the list of standard Java classes Inside the Color class specification, we find the library path for importing: Class package and name One or more lines of a class hierarchy http://docs.oracle.com/javase/8/docs/api/ The last line gives the path we need to import and use the class Tuesday, 11 Sep. 2018 Software Design I (CS 120) 18 public class Driver public static void main( String[] args ) DrawingGizmo pen; pen = new DrawingGizmo(); Using the Imported Code pen.setbackground( java.awt.color.blue ); pen.setforeground( java.awt.color.yellow ); import java.awt.color; public class Driver public static void main( String[] args ) DrawingGizmo pen; pen = new DrawingGizmo(); This adds the import command to the class, and now we don t have to use the full address for our colors These programs produce exactly the same result on-screen when they run Tuesday, 11 Sep. 2018 Software Design I (CS 120) 19 pen.setbackground( Color.blue ); pen.setforeground( Color.yellow ); What About the DrawingGizmo? If we want to use built-in Java classes like Color, we need to either refer to their full class address or import For classes that aren t built in to the language, there is another simple option: put all the class files into the same source directory (folder) When we run our program, the JVM will allow us to use any class of object that we address, import, or that it can find along-side the source code for the main() method Tuesday, 11 Sep. 2018 Software Design I (CS 120) 20 5
For This Week Meetings this week: Tuesday Thursday: regular classroom Friday: in the CS Lab (16 Wing) Homework 01: Colonel Sanders image Friday, 14 Sept., 5:00 PM, D2L Reading assignment: Chapters 1 2, due today Chapter 3, sections 1 9: Next Tuesday, 18 Sept. (start of day) Quiz 01: Covers lectures 01 04 (all will be on Notes page) Tuesday, 18 Sept. (end of class) Office Hours: Wing 210 Monday/Wednesday/Friday, 12:00 PM 1:00 PM Tuesday/Thursday, 1:30 PM 3:00 PM Tuesday, 11 Sep. 2018 Software Design I (CS 120) 21 6