COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State...
-
Upload
adela-joseph -
Category
Documents
-
view
216 -
download
0
Transcript of COM S 207 Classes and Objects Instructor: Ying Cai Department of Computer Science Iowa State...
COM S 207Classes and Objects
Instructor: Ying Cai
Department of Computer ScienceIowa State [email protected]
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
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
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(…);
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(…);}
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
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
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);
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
Example 1: Counter
Concept: private variables
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
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.
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.
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();
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