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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
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.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
C-1 University of Washington Computer Programming I Lecture 3: Variables, Values, and Types © 2000 UW CSE.
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.
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.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
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 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
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.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
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.
CS107 References and Arrays By Chris Pable Spring 2009.
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.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
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.
Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur.
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.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Written by: Dr. JJ Shepherd
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Topic 4 Expressions and variables Based on slides bu Marty Stepp and Stuart Reges from "Once a person has understood.
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 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Object Orientated Programming in Perl Simulated Models of Termites.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Week91 APCS-A: Java Problem Solving November 2, 2005.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Week 2 - Wednesday CS 121.
Building Java Programs
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.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Interfaces.
Java Programming Language
Building Java Programs
Presentation transcript:

Week 10 - Monday

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

 Recall that C = (5/9)(F – 32)  C is the temperature in degrees Celsius  F is the temperature in degrees Fahrenheit  Write a method that takes a double value in Fahrenheit and returns the answer in Celsius

 Write a method with the following header: public static void rightTriangle( double a, double b, double c)  This method uses the Pythagorean equation (a 2 + b 2 = c 2 ) to find the lengths of the sides of a right triangle  This method will compute the value for a, b, or c if the other two values are known  A user calls this method with the unknown value set to be zero  If no values are zero, check to make sure that the answer is correct  If exactly one value is zero, compute it, and print the answer  If two or more values are zero, print that it is impossible to determine the lengths

 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

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