CSC 212 – Data Structures Lecture 12: Java Review.

Slides:



Advertisements
Similar presentations
Written by: Dr. JJ Shepherd
Advertisements

CSC 212 – Data Structures Lecture 10: More Inheritance & Exceptions.
LECTURE 7: INHERITANCE CSC 212 – Data Structures.
Road Map Introduction to object oriented programming. Classes
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Understanding class definitions Looking inside classes.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
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.
Writing Classes (Chapter 4)
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Classes CS 21a: Introduction to Computing I First Semester,
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,
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,
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
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.
10-Nov-15 Java Object Oriented Programming What is it?
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, 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.
Written by: Dr. JJ Shepherd
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Problem of the Day  Why are manhole covers round?
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Variable Scope & Lifetime
Objects as a programming concept
Lecture 3: Fields, Methods, & Constructors
CS230 Tutorial Week 3.
Creating Your OwnClasses
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.
How Objects Behave Chapter 4.
OO Design with Inheritance
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Methods/Functions.
CSG2H3 Object Oriented Programming
Presentation transcript:

CSC 212 – Data Structures Lecture 12: Java Review

Fields Fields defined in class, but not in method  Instances share fields; have different values Definition must also include a data type  Can be primitive or reference type  Fields behave like variables of that type would Fields should be private or protected  In general, should be private  protected allows subclasses to access field

javadoc Example /** This class represents individual cars that people drive. */ public class Car { /** * Name of company that made the car and name of the car model */ String makeAndModel; /** Color of the car. */ String color; /** How full the gas tank is, expressed as * a percentage. */ float tankLevel; /** Number of miles recorded on the odometer */ int odometerReading;

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

/** * Drive around town and look for the hot spots to hit. distance Number of miles traveled gasUsed Amount of fuel used on this trip */ public void crusin(int distance, float gasUsed) { int newDistance = odometerReading + distance; adjustFuel(gasUsed); odometerReading = newDistance; } /** * Find out if the car will start. Assumes that you will not hotwire the car. haveKey True if we the key to this car; else it is false True if the engine would start, false otherwise */ public 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; } }

Visibility Modifiers Going to class naked is common nightmare  Try keeping certain details private  Want control over our state  Fields represent state of an object Fields normally declared private private int fieldName; Only access the field from class methods Use protected when subclass also accesses

Accessor Methods Field used in other classes’ methods  Done via accessor methods (“getters”) Getter usually named getField public field_type getField() {... }  Other classes must use accessor method  Includes code that can limit access to field  Do not copy code to each field use!

Mutator Methods Set field’s value in other classes’ methods  Use mutator methods (“setters”) for this Setter usually named setField public void setField(field_type param) {... }  Changes to field normally via mutators  Check that only legal values assigned to field  Makes changing or updating checks easier

Visibility of Methods Methods normally act on or with object  Use active verb as name  Declare as public Replace multiple copies of code with single method  Perform common test, update, or calculation  These should be private or protected  Simplifies using and editing the class

Fields Each class instance has fields  Field values in separate instances may * differ  Static fields shared by all instances Objects’ fields initialized at instantiation  Initial value is 0, false, ‘’, or null Should define as few fields as possible  Do not include fields that can be computed  Unneeded fields makes coding harder

Parameters Variables named in method description  Created & initialized each time method called  “Live” until method completes Initialized with value of argument  Primitives  copy value into parameter  References  parameter aliases argument Documented as part of method comments  Done paramName paramDescript

Locals Variables declared inside a method  Required to assigned value before using  “Live” only for code block in which declared Cannot be included in javadoc  Local variables help a method complete  Only comment as part of describing how method works

Tracing Parameters public void badTransfer(BankAccount source, float amount) { if (amount >= source.balance) { amount = source.balance(); source = null; } else { source.balance -= amount; } balance += amount; } check.badTransfer(richOne, ); owner balance  0.05 identifier  “2408” check haxx0r owner balance  identifier  “2408” richOne this source amount  balance  

Tracing Parameters public void badTransfer(BankAccount source, float amount) { if (amount >= source.balance) { amount = source.balance(); source = null; } else { source.balance -= amount; } balance += amount; } check.badTransfer(richOne, Float.MAX_VALUE); owner balance  identifier  “2408” check haxx0r owner balance  identifier  “2408” richOne this source amount  ~3.4·10 38  

Inherited Fields & Methods Subclass inherits superclass’s methods and fields  Non-private fields and methods used as if in subclass Can reuse names in subclasses  Fields are hidden  Methods are overriden Method called according to instance type Fields accessed by variable type

this & super Keywords this keyword is rarely used in Java  Access field with same name as parameter or local  From constructor call another defined in class Usage of super within constructor  Must be first statement in constructor  Calls constructor defined in superclass Usage of super elsewhere in class  Call overriden method as defined in superclass: super.methodName(…);  Use hidden field as defined in superclass: super.fieldName;

Before Next Lecture… Start week #5 assignment Continue programming assignment #1  It is due Friday Start reviewing for Midterm #1  It will be 1 week from today Wednesday’s lecture talks about linked lists  Read Sections of the book