CSE 114 – Computer Science I Lecture 1: Introduction

Download Report

Transcript CSE 114 – Computer Science I Lecture 1: Introduction

CSE 114 Computer Science I
Objects
Lake Superior, Michigan
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?
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.
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
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
'.'
• 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);
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()
Methods vs. Instance Variables
• Method calls always have parenthesis ()
• References to public instance variable never have
parenthesis:
Constructor
MyClass myObject = new MyClass();
myObject.length(); // method or variable?
myObject.length;
// method or variable?
Example Class (Die.java)
public class Die
{
private int numFaces;
private int upValue;
Instance variables
(data items)
Accessibility Modifiers (for classes, variables, and methods)
public – directly accessible by all classes
private – directly accessible only inside methods from this class
Die.java (continued)
// Methods can return values back
// to whomever calls them
Method doesn’t return any data to program that calls this method
public void roll()
{
upValue = ((int)(Math.random() * numFaces)) + 1;
}
Type of returned value
NOTE: Methods to be used
public int getUpValue()
by instantiated objects
{
cannot be static
return upValue;
}
A variable or expression of the same type
}
as specified in the header
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:
Constructors do not require a return type
public Die()
{
numFaces = 6;
upValue = 1;
}
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:
Supplied by programmer w/ call to new
public Die(int faces)
{
numFaces = faces;
upValue = 1;
}
public class Die
{ private int numFaces;
private int upValue;
public Die()
{
numFaces = 6;
upValue = 1;
}
public Die(int faces)
{
numFaces = faces;
upValue = 1;
}
Updated Die.java
Notice none of these
methods use the
static keyword!
So how do we make a Die?
public void roll()
{
upValue = ((int)(Math.random() * numFaces)) + 1;
}
public int getUpValue() {
}
return upValue;
}
Making an Object
• We can use a constructor
Memory
• Let’s make two, ex:
Die die1 = new Die();
Die die2 = new Die(20);
1
20
1
6
@28
@20
• What happens when we do this?
Note, these are
just example
memory addresses
Using the Die
public class DoublesGame
Memory
{
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();
Note, these are
just example
randomly rolled
upValues
19
20
13
6
@28
@20
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.");
}
}
Output:
You rolled 9 & 3
YOU LOSE.
Memory
9
20
3
6
@28
@20
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();
In some other class
die1.setUpValue(3);
public void setUpValue(int setValue)
{
if (setValue > 0 && setValue <= numFaces)
upValue = setValue;
}
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?
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