COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State...

15
COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University [email protected]

Transcript of COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State...

Page 1: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

COM S 207Classes and Objects

Instructor: Ying Cai

Department of Computer ScienceIowa State [email protected]

Page 2: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

So far we have covered

Memory allocation and data manipulation Primitive data types Array of primitive data types

Execution control if, while-loop, for-loop, do-while-loop

Program structuring methods

These are essential, but not enough Experience shows that it is difficult to understand and update

a program that consists of a large collection of methods

Page 3: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Object-Oriented Programming

A programming style in which tasks are solved by collaborating objects Each object has its own set of data, together with a set of methods that act upon the data

We have experienced some, e.g., String, Scanner

Scanner in = new Scanner(System.in);

if (in.hasNextInt()) { … }

classobject construct a Scanner object

invoke a method of Scanner

Page 4: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Classes and Objectsclass className { // declare instance variables type var1; type var2; ::: type varn; //declare constructor className(parameters) {} ::: className(parameters) {} :::

//declare methods type method1(parameters) {} type method2(parameters) {} ::: type methodm(paramters) {}} //end className

className obj = new className(…);

Page 5: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Definition of Class Vehicle (version1)class Vehicle { int passengers; int fuelcap; int mpg; }

public static void main(String[] args){ Vehicle minivan = new Vehicle(); Vehicel spartscar = new Vehicle(); minivan.passengers = 7; //use dot (.) to link minivan.fuelcap = 16; //the name of an object minivan.mpg = 21; //with the name of a member

sportscar.passengers = 2; sportscar.fuelcap = 14; sportscar.mpg = 12;

int range1 = minivan.fuelcap * minivan.mpg; int range2 = sportscar.fuelcap * sportscar.mpg; System.out.println(…);}

Page 6: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Memory allocation

passenger 7

fuelcap 16

mpg 21

minivan

class Vehicle { int passengers; int fuelcap; int mpg; }

public static void main(String[] args){ Vehicle minivan = new Vehicle(); Vehicel spartscar = new Vehicle(); minivan.passengers = 7; minivan.fuelcap = 16; minivan.mpg = 21;

sportscar.passengers = 2; sportscar.fuelcap = 14; sportscar.mpg = 12; :::}

passenger 2

fuelcap 14

mpg 12

sportscar

Each object has its own copy of the instant variables of the class

Page 7: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Adding Constructor

class Vehicle { int passengers; int fuelcap; int mpg;

Vehicle(int p, int f, int m) { passengers = p; fuelcap = f; mpg = m; } }

you can have multiple constructors, each having its own parameters

Page 8: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Adding Methods

class Vehicle { int passengers; int fuelcap; int mpg; int range() {…}; // without parameter double fuelNeeded(int miles); // with a parameter}

int range1 = minivan.range();int range2 = sportscar.range();

double gas1 = minivan.fuelNeeded(400);double gas2 = sportscar.fuelNeeded(400);

Page 9: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Diagram of program structure

A program consists of one or more classes

Typically, each class is in a separate .java file

Program

File

File

File

FileClas

sVariablesConstructors

Methods

Variables

Variables

Statements

Statements

Page 10: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Example 1: Counter

Concept: private variables

Page 11: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Example 2: BankAccount

public class BankAccount { private int accountNumber; private String SSN; private String name; private double balance;

private static lastAccountNumber; // multiple constructors BankAccount(String SSN, Name) // methods: deposit, withdraw, etc.}

Concept: static variables

Page 12: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Example 3: Address

Implement a class Address. An address has a house number, a street, an optional apartment number, a city, a state, and a postal code. 1)Supply two constructors: one with an apartment number, one without.2)Supply a print method that prints the address with the street on one line and the city, state, and zip code on the next line.

Page 13: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Example 4: Student

Implement a class Student. A student has a name and a total quiz score. 1)Supply an appropriate constructor.2)Supply the following methods: addQuiz(int score), getTotalScore(), and getAverageScore(). To compute the latter, you need to store the number of quizzes that the student took.

Page 14: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Example 5: Bug

Implement a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bug moves to the right, but it can turn to change its direction. In each move, its position changes by one unit in the current direction.1)Supply a constructor that gives a bug’s initial position.2)Supply the following methods: move(), turn(), getPosition();

Page 15: COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu.

Summary

A class contains Code (methods) + Data (instance variables)

Members of a class are accessed through the DOT (.) operator

Each object has its own copy of the instance variables of the class

Constructors Methods Other notions: static, public, private