CSE 114 Computer Science I Objects Lake Superior, Michigan.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Bryce Canyon, Utah CSE 114 – Computer Science I Objects and Reference.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Working With Objects Tonga Institute of Higher Education.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
SEEM Java – Basic Introduction, Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Problem of the Day  Why are manhole covers round?
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Information and Computer Sciences University of Hawaii, Manoa
Objects as a programming concept
3 Introduction to Classes and Objects.
Classes and OOP.
Topic: Classes and Objects TALENTSPRINT | © Copyright 2012
Methods Chapter 6.
Namespaces, Scopes, Access privileges
Lecture 14 Writing Classes part 2 Richard Gesick.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 5 - Functions Outline 5.1 Introduction
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes & Objects: Examples
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 4 Writing Classes.
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Java Programming Language
Classes, Objects and Methods
Methods/Functions.
What to expect this week
Presentation transcript:

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: MyClass myObject = new MyClass(); myObject.length(); // method or variable? myObject.length; // method or variable? Constructor

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)

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

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

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

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?

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 Memory Note, these are just example memory addresses

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 Memory 3 9 Note, these are just example randomly rolled upValues

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 Memory Output: You rolled 9 & 3 YOU LOSE.

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

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