4: classes, objects · 6.092: intro to java 4: classes, objects cite as: evan jones, olivier koch,...
TRANSCRIPT
![Page 1: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/1.jpg)
6.092: Intro to Java
4: Classes, Objects
Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering in Java, IAP 2008. MIT OpenCourseWare (http://ocw.mit.edu/), Massachusetts Institute of Technology. Downloaded on [DD Month YYYY].
![Page 2: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/2.jpg)
Overview
Week 1: Intro/Overview, Variables, Types, Operators, Methods.
Week 2: Loops, Arrays, Objects & Classes/OOP 1, Collections.
Week 3: OOP 2, Inheritance, Abstraction, 3rdParty libraries.
Week 4: Exceptions, Testing, Documentation, I/O, recap, & the "Real World."
![Page 3: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/3.jpg)
Assignment 3
Finding the lowest and highest value in an array.Ranking elements in an array.
![Page 4: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/4.jpg)
Reviews
Name of the class = Name of the file!! class Marathon goes into Marathon.java
Do not reuse variable names! String buffer;int buffer; // WRONG!!
![Page 5: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/5.jpg)
Reviews
Mind the scope of a variable
for (int i =0; i < times.length; i++) {int y = i * i;
}int x = y + 1; // WRONG!!
![Page 6: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/6.jpg)
Reviews
Do not mix up an array value and an array index.
int maxval = 0;for (int i =0; i < times.length; i++) {
if (times[i] > maxval) {maxval = times[i];
}}int y = times[maxval]; // WRONG!!int y = maxval; // CORRECT
![Page 7: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/7.jpg)
Reviews
int array[] = new int[5];array[2] = 33;for (int value : array) {System.out.println(value);
}
![Page 8: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/8.jpg)
Reviews
The for loop is strict.
for (int i=0; i > 4 && i < 7; i++) {System.out.println(i);
}
Nothing will be printed!
![Page 9: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/9.jpg)
ObjectOriented Programming
How do you represent the world on yourcomputer?
![Page 10: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/10.jpg)
ObjectOriented Programming
int, float, double, strings are lowlevel.
Can we do things at a higher level?
![Page 11: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/11.jpg)
Objects
Objects are a collection of related data and methods.
![Page 12: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/12.jpg)
Objects
Example: Strings
A string is a collection of characters (letters) and has a set of methods built in.
String nextTrip = “Mexico”;int size = nextTrip.length(); // 6
![Page 13: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/13.jpg)
Objects
Point p;p.x = 23;p.y = 12;
public static Point middlePoint (Point p1, Point p2) {Point q = new Point ((p1.x+p2.x)/2, (p1.y+p2.y)/2);return q;
}
![Page 14: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/14.jpg)
Objects
To create a new object, use the new operator.If you do not use new, you are making a reference
to an object (i.e a pointer to the same object).
![Page 15: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/15.jpg)
Objects and references
Point p1 = new Point (12,34);Point p2 = p1;
System.out.println(“x = “ + p2.x); // 12
p1.x = 24;System.out.println(“x = “ + p2.x); // 24!
![Page 16: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/16.jpg)
Reference vs primitive
class Example {public static void compute (int x) {
x = 2;}
public static void main(String[] arguments) {int y = 1;System.out.println(y); // 1compute (y);System.out.println(y); // 1
}}
![Page 17: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/17.jpg)
Reference vs primitive
class Example {public static void compute (Point p) {
p.x = 2;}
public static void main(String[] arguments) {Point q = new Point(1,1);System.out.println(q.x); // 1compute (q);System.out.println(q.x); // 2!
}}
![Page 18: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/18.jpg)
The null object
null simply represents no object. It is convenient as a return value to mean that a method failed for example. It may also be used to “remove” an element from an array.
String values[] = { “Adam”, “Bob”, “Mary” };values[1] = null;
![Page 19: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/19.jpg)
Classes
A class is a prototype to design objects.
It is a set of variables and methods that encapsulates the properties of the class of
objects.
![Page 20: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/20.jpg)
Classes
In Java, you can (will) create several classes in a project.
![Page 21: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/21.jpg)
Syntax Issues
● Class names begin with a capital letter (e.g WeeklyPay)
● One class = one file
● Only one class has the main method (startup class)
![Page 22: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/22.jpg)
class Bicycle {int speed;int gear;
void changeGear () {gear += 1;
}
void speedUp () {speed += 10;
}
void slowDown () {speed = 10;
}}
![Page 23: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/23.jpg)
Here we create two separate objects of the class Bicycle.
class CambridgeBicycleStore {
public static void main (String arguments[]){
Bicycle bike1 = new Bicycle();Bicycle bike2 = new Bicycle();bike1.speedUp();
}
![Page 24: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/24.jpg)
Packages
A package is a set of classes that relate to a same purpose.
Example: math, graphics, input/output...
In order to use a package, you have to import it.
![Page 25: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/25.jpg)
Class1
ect
Package
Class Class Class
Object
Obj Object
Object
![Page 26: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/26.jpg)
Class constructors
A class constructor is called each time an object of the class is instantiated (created).
![Page 27: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/27.jpg)
Class constructors
class Bicycle {public int speed;public int gear;
public Bicycle() {speed = 0;gear = 1;
}}
![Page 28: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/28.jpg)
Class constructors
public static void main (String[] arguments) {Bicycle bike = new Bicycle ();System.out.println (bike.speed); // 0System.out.println (bike.gear); // 1
}
![Page 29: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/29.jpg)
Constructors with arguments
class Bicycle {int speed;int gear;
public Bicycle(int speedval, int gearval) {speed = speedval;gear = gearval;
}}
![Page 30: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/30.jpg)
Constructors with arguments
public static void main (String[] arguments) {Bicycle bike = new Bike (10,3);System.out.println (bike.speed); // 10System.out.println (bike.gear); // 3
}
![Page 31: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/31.jpg)
Object methods vs class methods
So far, we have seen class methods.
Class methods are declared static.
![Page 32: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/32.jpg)
class Bicycle {static int gear, speed;
public static void speedUp (Bicycle b) {b.speed += 10;
}
public static void main (String[] arguments) {Bicycle bike1 = new Bicycle();speedUp (bike1);
}}
static means that the variable is going to be same for all objects of the class.
![Page 33: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/33.jpg)
class Bicycle {static int gear, speed;
public static void speedUp (Bicycle b) {b.speed += 10;
}
public static void main (String[] arguments) {Bicycle bike1 = new Bicycle();Bicycle bike2 = new Bicycle();System.out.println(bike2.speed); // 0speedUp (bike1);System.out.println(bike2.speed); // 10!
}}
This is not convenient!!We changed the speed on bike 1 and it automaticallychanged the speed on bike 2!!
![Page 34: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/34.jpg)
class Bicycle {static int gear, speed;
public static void speedUp (Bicycle b) {b.speed += 10;
}
public static void main (String[] arguments) {System.out.println(Bicycle.speed);
}}
![Page 35: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/35.jpg)
Object methods vs class methods
What we really want is a different speed value for each bike!
(i.e a different variable value for each object)
![Page 36: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/36.jpg)
class Bicycle {static int gear, speed;
public static void speedUp (Bicycle b) {b.speed += 10;
}
public static void main (String[] arguments) {Bicycle bike1 = new Bicycle();Bicycle bike2 = new Bicycle();
System.out.println(bike2.speed); // 0
speedUp (bike1);System.out.println(bike2.speed); // 0
}}
nonstatic means that the variable is going to be different for each object.
![Page 37: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/37.jpg)
Object methods vs class methods
static = the same for all objects
nonstatic = different for each object
![Page 38: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/38.jpg)
Object methods vs class methods
The same concept applies to methods.
static methods are defined for a class.
nonstatic methods are defined for an object.
![Page 39: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/39.jpg)
class Bicycle {static int gear, speed;
public static void speedUp () {speed += 10;
}
public static void main (String[] arguments) {Bicycle bike1 = new Bicycle();Bicycle bike2 = new Bicycle();
System.out.println(bike2.speed); // 0
bike1.speedUp ();System.out.println(bike2.speed); // 0
}}
![Page 40: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/40.jpg)
static or nonstatic?
● As you like! ● Most of the time, both will work.
● However, one may make more sense than the other: speedUp should be nonstatic (object) comparePrice(bike1, bike2) should be static (class)
![Page 41: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/41.jpg)
static (class)
static void speedUp (Bicycle b); //declarespeedUp (bike1); // call
nonstatic (object)
void speedUp(); // declarebike1.speedUp(); // call
![Page 42: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/42.jpg)
A common mistake
“nonstatic variable gear cannot be referenced from a static context “
class Bicycle {int gear, speed;
public static void speedUp () {speed += 10; // WRONG!!
}}
![Page 43: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/43.jpg)
Assignment 4: the game of Belote
In the Belote game, a pack of 52 cards is distributed among 4 players. Each turn, each player plays a card based on the following rules:
● The first player plays the first card in hand ● All other players play the first card of the same suit in hand ● If a player does not have the required suit, they play the first card in hand
![Page 44: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/44.jpg)
Assignment 4: the game of Belote
● At the end of the turn, the player who played the highest card in the required suit wins the four cards on the table. ● The player who won the turn starts the next turn. ● The game goes on until no cards remain in hand. ● At the end, each player sums the value of the cards they won.The player with the highest score wins the game.
![Page 45: 4: Classes, Objects · 6.092: Intro to Java 4: Classes, Objects Cite as: Evan Jones, Olivier Koch, and Usman Akeju, course materials for 6.092 Introduction to Software Engineering](https://reader035.vdocument.in/reader035/viewer/2022071020/5fd4573c3f7c256018486b94/html5/thumbnails/45.jpg)
Assignment 4: the game of Belote
Your goal is to simulate a Belote card game in Java.
Optional: Cards should be randomly shuffled at the beginning of the game.