4: classes, objects · 6.092: intro to java 4: classes, objects cite as: evan jones, olivier koch,...

45
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].

Upload: others

Post on 24-Aug-2020

2 views

Category:

Documents


0 download

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

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

Overview

Week 1: Intro/Overview, Variables, Types, Operators, Methods.

Week 2: Loops, Arrays, Objects & Classes/OOP 1, Collections.

Week 3: OOP 2, Inheritance, Abstraction, 3rd­Party 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

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

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

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

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

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

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

Object­Oriented 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

Object­Oriented Programming

int, float, double, strings are low­level.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}}

non­static 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

Object methods vs class methods

static  = the same for all objects

non­static = 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

Object methods vs class methods

The same concept applies to methods.

static methods are defined for a class.

non­static 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

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

static or non­static?

● As you like! ● Most of the time, both will work.

● However, one may make more sense than the other: speedUp should be non­static (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

static (class)

static void speedUp (Bicycle b); //declarespeedUp (bike1); // call

non­static (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

A common mistake

“non­static 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

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

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

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.