Download presentation
Presentation is loading. Please wait.
Published byGeorgia Cross Modified over 8 years ago
1
CSE 114 Computer Science I Objects Lake Superior, Michigan
2
Primitives vs. Objects So far we have seen mostly primitives –they store single values –Ex: int i = 6; Now we will define/use our own complex types –Classes/Objects –they can store multiple values –Ex: Die die; What objects have we used already?
3
Object vs. Class What’s the difference? Class: –defines a type, it’s a blueprint for making objects Object –a variable of type Class Ex: String s = "Hello"; –What’s the class name? –What’s the object name? All String objects have the same instance variables (but different values) and methods.
4
Die Object STATE (What information describes it?) Number of Faces – maximum numbers on dice Value facing up – number on upward face BEHAVIOR (What can we do with it?) Initialize – initializes state Roll – choose a random up-face value Read the value – get current up-face value
5
The Basics of Defining Classes General format of a class: public class ClassName { instance variable declaration(s) method definition(s) } Instance variables == state Methods == behavior
6
'.' When you make an object, you have access to: –its instance variables –its methods, which may use its instance variables '.' – used to access public instance variables and methods for a given object variable –soon we’ll see we have options other than public Example: String name = "Richard"; String nickname = name.substring(0, 4);
7
But what does '.' really do? An object is a block of data An object variable stores the memory address '.' says “using the block of variables at this memory address” –access an instance variable: obj.x –change an instance variable: obj.x = 5 –run a method: obj.updateX()
8
Methods vs. Instance Variables Method calls always have parenthesis () References to public instance variable never have parenthesis: MyClass myObject = new MyClass(); myObject.length(); // method or variable? myObject.length; // method or variable? Constructor
9
Example Class (Die.java) public class Die { private int numFaces; private int upValue; Accessibility Modifiers (for classes, variables, and methods) public – directly accessible by all classes private – directly accessible only inside methods from this class Instance variables (data items)
10
Die.java (continued) // Methods can return values back // to whomever calls them public void roll() { upValue = ((int)(Math.random() * numFaces)) + 1; } public int getUpValue() { return upValue; } Type of returned value A variable or expression of the same type as specified in the header Method doesn’t return any data to program that calls this method NOTE: Methods to be used by instantiated objects cannot be static
11
Initializing using Constructors To create/initialize an object variable, we use new and a Constructor method from that class. A constructor is only executed when an object is created. Constructors always have the same name as their class, so we would add the method below to the Die class: public Die() { numFaces = 6; upValue = 1; } Constructors do not require a return type
12
Initializing using Constructors You may also define constructor methods that take arguments for initialization purposes We could also add the method below to the Die class: public Die(int faces) { numFaces = faces; upValue = 1; } Supplied by programmer w/ call to new
13
Updated Die.java public class Die {private int numFaces; private int upValue; public Die() {numFaces = 6; upValue = 1; } public Die(int faces) {numFaces = faces; upValue = 1; } public void roll() {upValue = ((int)(Math.random() * numFaces)) + 1; } public int getUpValue() {return upValue;} } Notice none of these methods use the static keyword! So how do we make a Die?
14
Making an Object We can use a constructor Let’s make two, ex: Die die1 = new Die(); Die die2 = new Die(20); What happens when we do this? 6 1 20 1 @20 @28 Memory Note, these are just example memory addresses
15
Using the Die public class DoublesGame { public static void main(String[] args) { Die die1, die2; // CREATE A PAIR OF DICE die1 = new Die(); die2 = new Die(20); // ROLL BOTH DICE die1.roll(); die2.roll(); 6 1 20 1 @20 @28 Memory 3 9 Note, these are just example randomly rolled upValues
16
Using the Die // GET THE RESULTS int dieValue1 = die1.getUpValue(); int dieValue2 = die2.getUpValue(); // PRINT THE RESULTS System.out.println("You rolled " + dieValue1 + " & " + dieValue2); if (dieValue1 == dieValue2) System.out.println("YOU WIN!"); else System.out.println("YOU LOSE."); } 6 3 20 9 @20 @28 Memory Output: You rolled 9 & 3 YOU LOSE.
17
Methods and Parameters Some methods require data in order to run: String sentence = "Hello there."; char firstLetter = sentence.charAt(0); Let’s write a setUpValue method to set the die to a specific value (not random like roll) Die die1 = new Die(); die1.setUpValue(3); public void setUpValue(int setValue) { if (setValue > 0 && setValue <= numFaces) upValue = setValue; } In some other class
18
OOP – What’s the point? Combine various types of data in a single variable –A single object variable may have many instance variables, including other object variables Abstraction –Write a class definition once, use objects of that class type in a million different programs in a million different ways –Provides consistent behavior –Only worry about what the object can do (methods), not how it is done Good rule to follow: make class definitions general –Allows for more usages –Custom behavior can be done using inheritance –How did we make the Die class general?
19
OOP approach Step 1: –Divide up all necessary data into separate classes as instance variables Step 2: –Decide what methods are required by each class to manipulate instance variables Warning: –Avoid making multiple instance variables that represent the same data – potential consistency problems
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.