230 likes | 244 Views
Join us for Lecture 3 as we review classes, instances, fields, and class methods in Java programming. Learn essential concepts and examples with a focus on practical applications. Get ready to dive into the world of object-oriented programming!
E N D
Announcements • Newsgroup: cornell.class.cs100 • Assignment W1 due tomorrow at the beginning of class • Assignment P1 can be picked up at the end of class Lecture 3
Today’s Topics • Review • Classes • Instance of a Class (an object) • Fields • Declaration of class variables • The new operator • Class methods Lecture 3
Review • What’s the difference between a variable and a constant? • When might you want to use an ‘if’ statement? • What is the purpose of ‘block statements’? Lecture 3
Review Example 1 final int THRESHOLD = 65; System.out.println(“Enter temperature: ”); int temperature = Integer.parseInt(stdin.readLine()); if (temperature < THRESHOLD) { System.out.println(“It’s cold.\n”); } Lecture 3
Review Example 2 final boolean FOURLEGS = true; final boolean BARKS = true; final boolean MEOWS = false; if (FOURLEGS == false) System.out.println(“not four-legged”); else if (BARKS) System.out.println(“4 legs, probably a dog”); else if (MEOWS) System.out.println(“4 legs, probably a cat); else System.out.println(“4 legs, no idea what.”); Lecture 3
Rewrite of Previous example: final boolean FOURLEGS = true; final boolean BARKS = true; final boolean MEOWS = false; if (FOURLEGS == false) System.out.println(“not four-legged”); if ( FOURLEGS && BARKS && (MEOWS == false)) System.out.println(“4 legs, probably a dog”); if (FOURLEGS && MEOWS && (BARKS == false)) System.out.println(“4 legs, probably a cat); if (FOURLEGS && (MEOWS == false) && (BARKS == false)) System.out.println(“4 legs, no idea what.”); Lecture 3
Review: Formatting Output • What does the following produce? • “\tCourse\tStudents\n\tCS100\tMany” Course Students CS100 Many Lecture 3
Why Classes? • Recall: a variable is a named box into which one can place values • Suppose we want boxes (variables) that contain more than one value? • For example:Coordinate c x -532 x -532 y 25 Lecture 3
x 25 y 25 More examples • Coordinate c1 • Coordinate c3 • Coordinate is a class. It’s like a type, except that you (the programmer) define it. • c1 and c3 are instances of the class; we call them objects • c1.x, c1.y, c3.x, c3.y are fields of objects c1 and c3 respectively. x 3 y 4 Lecture 3
Declaring a class in Java publicclass Coordinate { publicint x; publicint y; } • keywords public and class • name of the class -- naming convention: capitalize all words in class names, e.g. StringBuffer • body of the class (within { and }) • field declarations (normal type and class declarations) • methods and constructors (later in the course) Lecture 3
Type declarations -- Class declarations • int x;// x • Coordinate w;// w • null is a Java constant -- it denotes the absence of an object • Just as x hasn’t been assigned (so defaults to 0), w hasn’t been assigned, it has only been declared 0 null Lecture 3
Take a breath • So, objects (like c, as declared in Coordinate c) have a type -- the type is the object’s class (here: Coordinate) • Classes have methods and fields • fields are data variables associated with a class and its objects • methods contain the executable code of a class Lecture 3
Creating a class instance • To create a new instance of a class, use the operator new • Coordinate w;// w • w = new Coordinate();// w null x ? y ? Lecture 3
Referencing fields x 3 y 5 // w 6 // x x = w.x; x 3 y 5 // w // x 3 Lecture 3
x 7 y 5 x 3 y 5 Assigning to fields // w w.x = 7; // w Lecture 3
x 7 y 1 x 7 y 1 x 3 y 5 x 3 y 5 Instance1 = Instance2 ?? // w • Assigning one instance to another creates aliases -- 2 names refer to the same object. // h h = w; // w // h Lecture 3
x 7 y 1 x 3 y 5 x 3 y 5 What if you want to copy? // w h.x = w.x; h.y = w.y; // h // w x 3 y 5 // h Lecture 3
Methods of Classes publicclass Coordinate { publicint x; publicint y; // Set field x to p*p public void setX(int p) { x= p*p; } // Set field y to q public void setY(int q) { y= q;} // Return the sum of the squares of the fields public int sumSquares() { return x*x + y*y; } } Classes can have methods that operate on fields of the class Lecture 3
Return types of methods // Return the sum of the squares of the fields public int sumSquares() { return x*x + y*y; } • sumSquares should return an integer to wherever it is called from Lecture 3
Returns • return <expression> • terminates execution of the method in which it appears • returns value of <expression> to the place of the call to the method • NB: <expression> must match the return type in the method header • If the type is not void, Java insists that a return statement exist • Can also have plain ‘return’ with no expression if void Lecture 3
Method calls • Suppose we want to call some of the methods of class Coordinate • Use object name followed by a dot ‘.’ followed by method name: • c.setX(3); // x field of c becomes 9 • a = c.sumSquares(); // a becomes . . . 81? Lecture 3
Example • c.setX(3) • c.setY(2) • // Store 85 in ss = c.sumSquares(); // c x 9 y 2 Lecture 3
Another example d = new Coordinate(); d.setX(c.sumSquares()); d.setY(c.x) // d x 7225 y 9 Lecture 3