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.

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Road Map Introduction to object oriented programming. Classes
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Understanding class definitions Looking inside classes.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
Programming Languages and Paradigms Object-Oriented Programming.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
CSC 212 – Data Structures Lecture 12: Java Review.
Introduction to Object-Oriented Programming
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
CSC 212 Object-Oriented Programming and Java Part 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Problem of the Day  Why are manhole covers round?
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Problem of the Day  Why are manhole covers round?
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!
ECE122 Feb. 22, Any question on Vehicle sample code?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
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 4 Introduction to Classes, Objects, Methods and strings
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSC 212 Object-Oriented Programming and Java Part 2.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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!
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
CSC 212 – Data Structures Lecture 6: Static and non-static members.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Problem of the Day  Why are manhole covers round?
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Question of the Day  How do you add 5 matches to these 6 & make 9?
Defining Your Own Classes II
Lecture 3: Fields, Methods, & Constructors
Java Primer 1: Types, Classes and Operators
Java Programming Language
Implementing Non-Static Features
Building Java Programs
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Recap Week 2 and 3.
Building Java Programs
Java Programming Language
Defining Classes and Methods
Introduction to Object-Oriented Programming
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Corresponds with Chapter 5
Presentation transcript:

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 problems due before class  Remove rust from summer and get back into coding  Problems designed to help learn new material, too

Classes vs. Objects  Classes are blueprints describing data type  Classes (usually) cannot do anything on their own  Objects are instances of a class  New objects created (instantiated) using new  Fields describe state of an object  Object’s behavior represented by methods

Instance Variables  All of class's instances have same fields…  … but values can differ between each instance  In a class, each field must have unique name  Different classes can duplicate names of fields  Field declaration also includes data type  Will act like variables of that type  Can be primitive, enum, or reference type

Class Example public class Car { /** What kind of car & who made it */ private String makeAndModel; /** Color of the car. */ private String color; /** Percent full the gas tank is */ private float tankLevel; /** Miles recorded on the odometer */ private int odometerReading; /* Definition continues from here */

Using Fields (1) Car profHertzCar = new Car(); profHertzCar.makeAndModel = “BMW Z4”; profHertzCar.color = “Deep Green Metallic”; profHertzCar.tankLevel = 1.0; profHertzCar.odometerReading = 10000; Car actualCar = new Car(); actualCar.makeAndModel = “Subaru Outback"; actualCar.color = “Brown”; actualCar.tankLevel = ; actualCar.odometerReading = 47634;

Using Fields (2) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; dreamCar.tankLevel = 1.0; dreamCar.odometerReading = 10000;

Methods  Define how objects act, behave, & change  Need unique name & parameters (“signature”)  Can share name or parameter list, but not both  Determines which code used at any method call  Methods also define return type  Return type is not part of signature  Cannot help determine which method to call

Method Return Types  Can be primitive, reference, array, or void  void methods cannot return data  All other methods must return data  Method execution stops immediately at return  Will NOT execute further in method  After return, calling method executes immediately  Compilation error occurs when:  Method includes executing code after return  Non- void method can end without return statement

Car Example /** Reset the fuel tank to be full. */ void fillTank() { tankLevel = 1.0; } /** Change amount of fuel by some means levelDelta Change in fuel level */ void adjustFuel(float levelDelta) { tankLevel += levelDelta; /* Check that tank level makes sense. */ if (tankLevel > 1.0) { tankLevel = 1.0; } else if (tankLevel < 0.0) { tankLevel = 0.0; } }

Useful Car Methods void crusin(int distance, float gasUsed) { int newDistance = odometerReading + distance; adjustFuel(gasUsed); odometerReading = newDistance; } boolean willStart(boolean haveKey) { if (!haveKey || tankLevel == 0.0) { return false; } else if (makeAndModel.startsWith(“Jaguar”)){ java.util.Random rnd = new Random(); return rnd.nextBoolean(); } else { return true; } }

Calling Methods Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; dreamCar.tankLevel = 1.0; dreamCar.odometerReading = 10000; if (dreamCar.willStart(true)) { System.out.println(“Vroom vroom”); } dreamCar.crusin(400, 1.0); if (dreamCar.willStart(true)) { System.out.println(“Vroom vroom”); } else { System.out.println(“*sigh*”); }

Constructors  Special methods called to instantiate object  Have identical name as class  No return type (not even void ) allowed  If parameters differ, multiple constructors possible  Parameters to new must match 1 constructor  If no constructor defined, implicit one used for class  No parameters included in implicit constructor

Tracing with Objects public class Account { public String owner; public float balance; public String identifier; Account check = new Account(“haxx0r”, 1.25, “0001”); Account richOne = new Account(“billG”, ,“2408”); check.balance -= 1.20; // Bought a coffee check.identifier = “2408”; // 1 st theft attempt richOne.owner = “haxx0r”; // 2 nd theft attempt

Tracing Parameters  Traced like variable created at method call  New variable created each time method called  Variable lives until completion of method  Initialized with value of argument  Works like ran parameter = actual  Primitives copy value into parameter  Parameter is aliased if a reference

Tracing Parameters public void badTransfer(Account source,float amount){ if (amount >= source.balance) { amount = source.balance(); source = null; } else { source.balance -= amount; } balance += amount; } check.badTransfer(richOne, Float.MAX_VALUE);

Locals  Variables declared inside a method  Must be assigned value before using  “Live” only for code block in which declared  Local always better than instance variable  Easy trick to simplify design, algorithm, & code  Reduces opportunities for bugs dramatically  Added bonus, creates fewer comments to write!  Declare local in smallest enclosing block

Your Turn  Get into your groups and complete activity

For Next Lecture  Reading from AF 7.11 for Wednesday  What does the static keyword mean?  How do static fields & static methods work?  Why did 10 out of 12 students get this wrong?  There is weekly assignment problem on Angel  Due before Wednesday’s lecture (via )  Get back into the swing of writing Java code