chapter 4 objects and classes. assignment 2 solution in class exercise
TRANSCRIPT
![Page 1: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/1.jpg)
Chapter 4
Objects and Classes
![Page 2: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/2.jpg)
Assignment 2 Solution
• In class exercise
![Page 3: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/17.jpg)
Relationships Between Classes
![Page 18: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/18.jpg)
Constructors
![Page 19: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/19.jpg)
Constructors Cont’
![Page 20: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/20.jpg)
this
![Page 21: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/22.jpg)
Instance Fields
![Page 23: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/23.jpg)
Final Instance Fields
![Page 24: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/24.jpg)
Static Fields and Methods
![Page 25: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/25.jpg)
Initializing Fields
![Page 26: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/26.jpg)
Method Parameters
![Page 27: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/28.jpg)
Packages Cont.’
![Page 29: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/29.jpg)
Static Imports
![Page 30: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/30.jpg)
Static Imports Cont.’
![Page 31: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/31.jpg)
Extending A Class
![Page 32: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/32.jpg)
Dynamic Binding
![Page 33: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/34.jpg)
Final Classes
![Page 35: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/35.jpg)
Final Methods
![Page 36: Chapter 4 Objects and Classes. Assignment 2 Solution In class exercise](https://reader036.vdocuments.net/reader036/viewer/2022062423/56649e2d5503460f94b1db8c/html5/thumbnails/36.jpg)
Assignments 3 & 4
• Due Dec 4th – posted to class website