Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Computer Science

Similar presentations


Presentation on theme: "Department of Computer Science"— Presentation transcript:

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

2 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

3 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()) { … } object construct a Scanner object class invoke a method of Scanner

4 Classes and Objects className obj = new className(…);
class className { // declare instance variables type var1; type var2; ::: type varn; //declare constructor className(parameters) {} //declare methods type method1(parameters) {} type method2(parameters) {} type methodm(paramters) {} } //end className className obj = new className(…);

5 Definition of Class Vehicle (version1)
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(…);

6 Memory allocation minivan passenger 7 fuelcap 16 mpg 21 sportscar
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; ::: minivan passenger 7 fuelcap 16 mpg 21 sportscar passenger 2 fuelcap 14 mpg 12 Each object has its own copy of the instant variables of the class

7 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

8 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);

9 Diagram of program structure
File Class Variables Constructors Variables Statements Methods A program consists of one or more classes Typically, each class is in a separate .java file

10 Example 1: Counter Concept: private variables

11 Example 2: BankAccount Concept: static variables
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. }

12 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. Supply two constructors: one with an apartment number, one without. 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.

13 Example 4: Student Implement a class Student. A student has a name and a total quiz score. Supply an appropriate constructor. 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.

14 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. Supply a constructor that gives a bug’s initial position. Supply the following methods: move(), turn(), getPosition();

15 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


Download ppt "Department of Computer Science"

Similar presentations


Ads by Google