COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Sept 19, 2011) Topics Announcements TurningRobot example PersonAge example Announcements The submit command now works. Quiz: Monday October 3, 2011 Generating javadocs from netbeans is broken (on all platforms). I have reported this problem to netbeans. (Generating javadocs does work if you have a package which we have not yet examined in this course.) To submit from home install the secure shell software on your machine as described in the Department's Network Users Guide. Then: Create a zip file (important note to Windows users: ensure that it is compressed to a.zip file not an.rar file!) Copy the zip file to the Departmental network. Open a remote terminal to the Departmental machine and run the submit command. (You must run this command in the same folder that you copied your zip file to.) Review What is a class/object? The class file source code provides the template or blueprint for creating and using objects of the class type and consists of: instance variables: These correspond to the characteristics of the object. They are declared giving each a name and data type. They are usually private. Instance variables can be used in
COE318 Lecture Notes: Week 3 2 of 8 any method or constructor code. Constructors: They are used to perform any initialization needed when creating an object. Although they look like methods, they are different in that they have no return type and must have the same name as the class. Methods: These provide behavior to objects. They must hava a return type (void if nothing is returned) If they are part of the class design, they must be public. Helper methods or others that are not part of the public design should be private. Other details The? Operator Format: <boolean-expression>? <A> : <B> where A and B are expressions of the same type. The value of the expression is A if the boolean-expression is true and B if it is false. Example:.class and.java files int i, j, max; max = i > j? i : j; The jave source code language for a class must have the same name as the class and the extension.java. For example, the source code for the Robot class is in a file called Robot.java. (Note that case is important! Class names should begin with an uppercase letter.) When the source code is compiled, the compilation result is placed in a file with the same name as the class but with the extension.class instead of.java. Example: the TurningRobot class In this example, the robot responds tot the commands left, right, up and down Ii.e. It has methods left() etc. As before. However, a TurningRobot can only move forwards; it cannot move sideways or backwards. But it can turn 90 counter clockwise. For example, if a TurningRobot is moving right and is then asked to move left it has to rotate
COE318 Lecture Notes: Week 3 3 of 8 180 to make its forward direction left. Like a Robot object, a TurningRobot has to know its position (the x and y instance variables). In addition, it has to know its current direction. New Java features used: the switch statement local variables (dir in printdirection) helper private methods public class TurningRobot { //Instance variables private int direction = 0; private int x = 0; private int y = 0; public void right() { setdirection(0); x++; step(); public void up() { setdirection(1); y++; step(); public void left() { setdirection(2); x--; step(); public void down() { setdirection(3); y--; step(); private void setdirection(int desired) { while (desired!= direction) { turn();
COE318 Lecture Notes: Week 3 4 of 8 private void turn() { direction++; if (direction == 4) { direction = 0; printdirection(); dir); ", " + private void printdirection() { String dir; switch (direction) { case 0: dir = "right"; break; case 1: dir = "up"; break; case 2: dir = "left"; break; case 3: dir = "down"; break; default: dir = "???"; System.out.println("Turned to move " + private void step() { System.out.println("Stepping to (" + x + y + ")"); Example: the AgePerson class
COE318 Lecture Notes: Week 3 5 of 8 This example shows how a AgePerson object can be immutable. (Age changes with time, of course, but age does not have to be an instance variable; if it were, the object would be mutable.) The trick is to use Date of Birth as an instance variable. The getage() method then calculates the age at the present time. Note: You are not responsible for knowing about the Calendar or GregorianCalendar classes. import java.util.calendar; import java.util.gregoriancalendar; public class PersonAge { private Calendar dob; private String name; public PersonAge(String n, Calendar d) { name = n; dob = d; public String getname() { return name; public int getage() { Calendar today = new GregorianCalendar(); int yearnow = today.get(calendar.year); int birthyear = dob.get(calendar.year); int age = yearnow - birthyear; if (today.get(calendar.day_of_year) < dob.get(calendar.day_of_year)) { age--; return age; Data types There are 2 general data types:
COE318 Lecture Notes: Week 3 6 of 8 1. primitive, and 2. reference Primitive data types are int, float, double, short, long, byte, boolean and char. When a primitive data type is declared, the memory required to hold the data is allocated and that memory contains the value of the data. For example, the declaration byte i; allocates one byte of memory for the variable i. The assignment statement i = 5; would set that memory location's value to 5. Every class (and there are thousands built into the java library) is also a data type. But it is a reference data type. The reference points to an area in memory where the actual object of the type resides. The most common built-in reference data type is String. When a reference data type is declared, memory is set aside to hold the reference. This is always 4 bytes no matter how big the actual data object is. The reference is instantiated by assigning it to an object (which may need to be created) of the correct data type. For example: Robot robbie; robbie = new Robot(); An actual Robot object is created with new Robot() (which invokes the Robot class's constructor) and it resides somewhere in memory (and uses many more than 4 bytes!). The reference value robbie points to the starting address of the actual Robot object. The place in memory where objects exist is called the heap. The Java Virtual Machine (JVM) keeps track of how many references there are to objects in the heap. When the reference count becomes zero, it is no longer possible to access the object. It has become garbage. The JVM periodically runs a Garbage Collector which frees up the space occupied by garbage objects. Where variable names are used Variable names can be used for instance variables, parameter names and local variables. Instance variables are declared in a class body. They can be accessed by any method of the class. They are usually public. Parameter names are the arbitrary names given to the passed parameters of methods or constructors. Parameters only exist during the execution of the method or constructor and cannot be seen by other methods. (If 2 methods have the same name for a parameter, no interference results.)
COE318 Lecture Notes: Week 3 7 of 8 If a parameter name and an instance variable name are the same, the name all by itself refers to the paramter. To access the instance variable of the same name, use this.name. Local variables are declared and used inside methods. They only exist during the execution of the method. To be even more specific, on entry to a method, space for all the local variables is allocated on the stack. Questions 1. Given the two classes, A and B, shown below identify identify each as mutable or immutable. Explain. public class A { private final int p; public int q; public A(int p, int q) { this.p = p; this.q = q; public int getp() { return p; public int getq() { return q; public class B { private final int x; private int y; public B(int x, int y) { this.x = x; this.y = y;
COE318 Lecture Notes: Week 3 8 of 8 /** * @return the x */ public int getx() { return x; /** * @return the y */ public int gety() { return y;