Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.

Slides:



Advertisements
Similar presentations
Chapter 21 Implementing lists: array implementation.
Advertisements

Why not just use Arrays? Java ArrayLists.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
Arrays CS177 (Week 06). Announcements ● Project 2 due today ● Project 3 will be posted tomorrow.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Road Map Introduction to object oriented programming. Classes
IAT 800 Foundations of Computational Art and Design Lecture 2.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
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.
CS Winter 2011 Introduction to the C programming language.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Primitive Variables.
CS107 References and Arrays By Chris Pable Spring 2009.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
1 CIS 2168 Data Structures and Algorithms in JAVA Brief Introduction to JAVA.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
LCC 6310 Computation as an Expressive Medium Lecture 2.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
Program Organization Sequential Execution: One line done after the other Conditional Execution: If a test is true, one section is done, otherwise another.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Written by: Dr. JJ Shepherd
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Week 9 - Friday.  What did we talk about last time?  Static method practice  Finished the Game of Life.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Object Orientated Programming in Perl Simulated Models of Termites.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Week 2 - Wednesday CS 121.
Introduction to Primitive Data types
Interfaces.
Java Programming Language
IAT 800 Foundations of Computational Art and Design
LCC 6310 Computation as an Expressive Medium
Introduction to Primitive Data types
Presentation transcript:

Week 10 - Monday

 What did we talk about last time?  Method overloading  Lab 9

 Write a complete program that simulates betting on a roulette wheel  You start with $1000 in your bank  Each turn, you bet a dollar amount and either red ( r ) or black ( b )  Include a method with the following prototype:  public static boolean spin(char guess)  This method randomly picks a number between 1 and 38 (where 37 is 0 and 38 is 00) and determines whether or not your guess (r or b) was correct  Remember, there are 38 numbers on an American roulette wheel:  1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 are red  2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35 are black  0 and 00 are green (neither red nor black)

 There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types:  String  arrays  An infinite number of others…

Primitive types:  Have a fixed amount of storage  Think of them as a box designed to hold a particular kind of data  Have basic operations used to manipulate them  int, double ( +, -, *, /, % )  boolean ( ||, &&, ^, ! )

Object types  Hold arbitrarily complex kinds of data of any kind  Do not have a prespecified amount of storage  Think of them as arrows pointing to some concrete thing that holds primitive data  Use methods for interaction instead of operators  For example, String objects use length(), charAt(), etc.

 Variables that hold object types are called references  Unfortunately, I have lied to you:  References do not work the same as primitive variables  Up to this point, we have tried to ignore this difference  A primitive variable holds a value  A reference variable merely points to the location of the object

 Picture a ham…  Imagine that this ham is actually a Java object  You may want a reference of type Ham to point at this ham  Let’s call it ham1 ham1

 Now, what if we have another Ham reference called ham2  What happens if we set ham2 to have the same value as ham1 using the following code? ham1 Ham ham2 = ham1; ham2

 When you assign an object reference to another reference, you only change the thing it points to  This is different from primitive types  When you do an assignment with primitive types, you actually get a copy int x = 37; int y = x; int x = 37; int y = x; y 37 x

 Since reference variables are only pointers to real objects, an object can have more than one name  These names are called aliases  If the object is changed, it doesn’t matter which reference was used to change it

 Thus, if we tell ham2 to take a bite away, it will affect the ham pointed at by ham1  Remember, they are the same ham ham1 ham2.bite(); ham2

 We have int s x and y, both with value 37  If we change x, it only affects x  If we change y, it only affects y int x = 37; int y = x; x++; y--; int x = 37; int y = x; x++; y--; y 37 x 38 36

 If you declare a lot of references, you have not created any objects, just lots of arrows Eggplant aubergine; DumpTruck truck1; Idea thought; Eggplant aubergine; DumpTruck truck1; Idea thought; aubergine truck1thought

 When you first declare a reference variable, those arrows point to null  null is a Java keyword that just means nothingness  If you try to do something with null, thinking it is a real object, you can break your program

 To make those arrows point at a new object, you must call a constructor  A constructor is a kind of method that creates an object  Some constructors allow you to specify certain attributes of the object you are creating  The default constructor does not let you specify anything

 To call a constructor, you use the new keyword with the name of the class followed by parentheses:  Perhaps there is a Ham constructor that lets you take a double that is the number of pounds that the ham weighs: Ham ham1 = new Ham(); //default constructor Ham ham2 = new Ham( 4.2 ); //weight constructor

 The objects you are most familiar with by now are String s  They break a few rules:  It is possible to create them without explicitly using a constructor  They can be combined using the + operator  You can still create a String object using a constructor: String s = new String("Help me!");

 More on classes and objects  Start review if there's time

 Exam 2 is next Monday  Review is on Wednesday and Friday  Start working on Project 4