chapter 4 objects and classes. assignment 2 solution in class exercise

36
Chapter 4 Objects and Classes

Upload: janel-burns

Post on 11-Jan-2016

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Chapter 4

Objects and Classes

Page 2: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Assignment 2 Solution

• In class exercise

Page 3: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Object Oriented Programming

• OOP• Programs made up of objects– Functionality exposed to users– Implementation hidden from users

• Objects can be used from library or custom created

• Using object and their methods can reduce program complexity

Page 4: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Classes

• The template or “blueprint” from which objects are made.

• Construct an object from class means to create an “instance” of that class

• Encapsulation – aka info hiding.• The data inside an object is known as its

instance fields• Function that operate on the data are called

methods

Page 5: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Class cont’

• Notion that classes are cookie cutters and objects are the cookies created from classes

• The user interacts with the methods of class – you should never deal with how the methods are implemented

Page 6: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Classes cont’

• Java has the ability to “extend” a class. • A new class has all of the properties and

methods of the class you extend• You can write new methods and add

properties that apply to your new class. • This is also referred to as “inheritance”

Page 7: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Where to start??

• The top down approach doesn’t apply– You don’t start with the void main(String args[])

• When thinking about creating your own classes (use nouns for analyzing the problem space)

• Use verbs to describe methods on the nouns

Page 8: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Using Predefined Classes

• Take, for example, the Math class. You have seen that you can use methods of the Math class, such as Math.random(), without needing to know how they are implemented—

• all you need to know is the name and parameters (if any). – That is the point of encapsulation and will certainly be true of

all classes.• But the Math class only encapsulates functionality; it neither

needs nor hides data. Because there is no data, you do not need to worry about making objects and initializing their instance fields—there aren’t any

• Arrays class is another example of this

Page 9: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Object Variables• To work with objects, you first construct them and specify their

initial state. Then you apply methods to the objects.• You use constructors to construct new instances. A constructor is a

special method whose purpose is to construct and initialize objects.• Constructors always have the same name as the class name. To

construct an object, you combine the constructor with• the new operator.

– new Date();• There is an important difference between objects and object

variables. • Date deadline; // deadline doesn't refer to

any object• The above defines an object variable, deadline, that can refer to

objects of type Date

Page 10: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Object Variables Cont’

• The return value of the new operator is also a reference. A statement such as

Date deadline = new Date();• Has two parts. The expression new Date()

makes an object of type Date, and its value is a reference to that newly created object. That reference is then stored in the deadline variable.

Page 11: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Lets create an Object!!!

• Bicycle Class–Nouns? •Bike components

–Verbs?•Bike (behaviors)

Page 12: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Bicycle Class

• Bike properties– Gear, speed, and

cadence

• State– Current gear, speed,

and cadence

• Methods– SpeedUp, ChangeGear,

Brake, ChangeCadence

Page 13: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Bike Class Java Code

public class Bicycle { //members (properties of Bike class)

int cadence = 0;int speed = 0;int gear = 1;

Page 14: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Bike Class Java Code cont’void changeCadence(int newValue) {

cadence = newValue; } void changeGear(int newValue) {

gear = newValue; } void speedUp(int increment) {

speed = speed + increment; } void applyBrakes(int decrement) {

speed = speed - decrement; } void printStates() { System.out.println("cadence:"+cadence+" speed:” +speed+" gear:"+gear); } } //end of class

Page 15: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Bike Class Java Code cont’class BicycleDemo {

public static void main(String[] args) { // Create Bicycle objects Bicycle bike1 = new Bicycle(); // Invoke methods on those objects

bike1.changeCadence(50); bike1.speedUp(10); bike1.changeGear(2); bike1.printStates();

}//end main()}//end BicycleDemo

Page 16: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Extending Bicycle

class MountainBike extends Bicycle { // new fields and methods defining a mountain bike would go here

}

Page 17: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Relationships Between Classes

Page 18: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Constructors

Page 19: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Constructors Cont’

Page 20: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

this

Page 21: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Accessor and Mutator and Private

• Are types of functions used in your classes– Accessor fucntions (get) Return values of instance

fields (class properties)– Mutator functions (set) Change the value of the

instance fields (class properties• The private keyword makes sure that the only

methods that can access these instance fields• No outside method can read or write to these

fields.

Page 22: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Instance Fields

Page 23: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Final Instance Fields

Page 24: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Static Fields and Methods

Page 25: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Initializing Fields

Page 26: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Method Parameters

Page 27: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Packages

• Java allows you to group classes in a collection called a package.

• Packages are convenient for organizing your work– separating your work from code libraries provided

by others• The main reason for using packages is to

guarantee the uniqueness of class names.– You wouldn’t want two copies of Bicycle class

Page 28: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Packages Cont.’

Page 29: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Static Imports

Page 30: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Static Imports Cont.’

Page 31: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Extending A Class

Page 32: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Dynamic Binding

Page 33: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

super

• Whenever you want to access a instance field of your super class inside of your sub class you have to use the keyword super

• See eclipse for example

Page 34: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Final Classes

Page 35: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Final Methods

Page 36: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise

Assignments 3 & 4

• Due Dec 4th – posted to class website