Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS

Slides:



Advertisements
Similar presentations
Classes And Objects II. Recall the LightSwitch Class class LightSwitch { boolean on = true; boolean isOn() { return on; } void switch() { on = !on; }
Advertisements

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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
Logic & program control part 2: Simple selection structures.
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Object Reference. Variables: Class Type vs. Primitive Type What does a variable hold? –primitive type value of the variable –class type memory address.
Intro to CS – Honors I Inheritance and Polymorphism GEORGIOS PORTOKALIDIS
Chapter 41 Defining Classes and Methods Chapter 4.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Information Hiding and Encapsulation
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
Intro to CS – Honors I Classes and Methods GEORGIOS PORTOKALIDIS
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,
Parameter Passing with Java See also the examples: Parameters1.java, Parameters2.java, Parameters3.java and Parameters4.java © Juhani Välimäki 2003.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
Arrays as pointers and other stuff COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
Week 12 Methods for passing actual parameters to formal parameters.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Programming in JAVA – II (A Whirlwind Tour of Java) Course Lecture Slides.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Defining Classes and Methods
Test 2 Review Outline.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
User Interaction and Variables
Classes, Objects, and Methods
An Introduction to Java – Part II
An Introduction to Java – Part I, language basics
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Recap Week 2 and 3.
AKA the birth, life, and death of variables.
Defining Classes and Methods
Defining Classes and Methods
Names of variables, functions, classes
Presentation transcript:

Intro to CS – Honors I Class Types and Object References GEORGIOS PORTOKALIDIS

A Name and A Value Remember the Java primitive types ◦byte, short, int, long, float, double, boolean, char And class types ◦Like the class Dog we defined in previous lectures Declaring a variable of any type is always the same ◦TYPE VariableName1, VariableName2; But not all names are the same! Back to basics: What is a variable, what is its purpose? Where are data stored? How are data referenced/accessed? Variables are used to store data Data 2 Data 1 Data 3 Data 2 Data 1 Data 3 Data are stored in memory A1 A2 A3 Each piece of data has an address [] accesses the contents of an address

Primitive Data Types VariableName1 VariableName2 VariableName3 The names of primitive variables directly access the contents stored in them Data 2 Data 1 Data 3 A1 A2 A3 Memory

Object Class Data Types They are declared the same, but … VariableName1 VariableName2 VariableName3 The names of class variables can refer to an object or not Data 2 Data 1 Data 3 A1 A2 A3 Memory Not an object A variable’s data refer can refer to an object This variables are references to objects

What Does This Mean For Me? When you declare a variable of a class type ◦Example: Dog scooby; When you create a new object of type Dog ◦Example: new Dog(); When an object is assigned to variable ◦Example: scooby = new Dog(); scooby A1 Memory Data 1 A reference pointing nowhere A new object is created Object

Species klingonSpecies, earthSpecies; klingonSpecies.setSpecies("Klingon ox", 10, 15); earthSpecies.setSpecies("Black rhino", 11, 2); earthSpecies = klingonSpecies; earthSpecies.setSpecies("Elephant", 100, 12); System.out.println("earthSpecies:"); earthSpecies.writeOutput(); System.out.println("klingonSpecies:"); klingonSpecies.writeOutput(); - name: String - population: int - growthRate: double Species + setSpecies(String newName, int newPopulation, double newGrowthRate): void + getName(): String + getPopulation(): int + getGrowthRate( ): growthRate + writeOutput(): void

References So both variables klingonSpecies and earthSpecies store references to Species objects What does the == then do on references? klingonSpeciesearthSpecies Species object So class type variables essentially contain memory addresses The address is always called a reference in Java SpeciesFourthTry klingonSpecies = new SpeciesFourthTry(); SpeciesFourthTry earthSpecies = new SpeciesFourthTry(); klingonSpecies.setSpecies("Klingon ox", 10, 15); earthSpecies.setSpecies("Klingon ox", 10, 15); if (klingonSpecies == earthSpecies) System.out.println("They are EQUAL."); else System.out.println("They are NOT equal.");

Actually Comparing Two Objects How do we compare strings? You can define an equals() method public boolean equals(Species otherObject) { return (this.name.equalsIgnoreCase(otherObject.name)) && (this.population == otherObject.population) && (this.growthRate == otherObject.growthRate); } if (klingonSpecies.equals(earthSpecies)) System.out.println("They are EQUAL."); else System.out.println("They are NOT equal."); Always name the method testing for equality equals

Other Methods Returning boolean Use names that make sense in English when used with if..else statements, while loops, etc. public Boolean isExtinct() { return population == 0; } if (s1.isExtinct()) { // Yes, it’s extinct }

Class Types are Passed Using call-by-reference Defining method parameters of primitive types is equivalent to declaring a local variable public boolean equals(Species otherObject) { otherObject = new Species(); return (this.name.equalsIgnoreCase(otherObject.name)) && (this.population == otherObject.population) && (this.growthRate == otherObject.growthRate); }

Class Types are Passed Using call-by-reference Let’s introduce a new Species class method public boolean eatSpecies(Species unluckySpecies) { unluckySpecies.population = 0; this.growthRate += 5; } Species tyrannosaurus = new Species(), elephant = new Species(); tyrannosaurus.setSpecies(“Dinosaur", 10, 15); elephant.setSpecies(“Mamal", 11, 2); tyrannosaurs.eatSpecies(elephant); elephant.writeOutput(); Which object is updated? Why is this accessible?

Lost Objects Species tyrannosaurus = new Species(), elephant = new Species(); tyrannosaurus.setSpecies(“Dinosaur", 10, 15); elephant.setSpecies(“Mamal", 11, 2); tyrannosaurus = elephant;

Unit Testing Testing our program with different inputs or even all possible inputs is good! ◦However not possible for larger programs Test smaller parts of your program/class individually ◦AKA Unit Testing Species testSpecies = new Species(); // Test the setSpecies method testSpecies.setSpecies("Tribbles", 100, 50); if (testSpecies.getName().equals("Tribbles") && (testSpecies.getPopulation() == 100) && (testSpecies.getGrowthRate() >= 49.99) && (testSpecies.getGrowthRate() <= 50.01)) { System.out.println("Pass: setSpecies test."); } else { System.out.println("FAIL: setSpecies test."); } Since getGrowthRate returns a double we should not attempt testSpecies. getGrowthRate() == 50