Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change.

Slides:



Advertisements
Similar presentations
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Advertisements

Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Lecture 04 agenda Go over QT exam. Discuss OO How to discover objects, properties, and methods Tetris game Other examples.
Java Language and SW Dev’t
Inner and Anonymous Classes Though you are captive in the OO paradigm, you can use inner classes and anonymous classes to get around this constraint and.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
BPJ444: Business Programming using Java Java basics Tim McKenna
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
The Java Programming Language
Exceptions: Checked versus Unchecked Exceptions.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change.
10-Nov-15 Java Object Oriented Programming What is it?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Inheritance A Review of Objects, Classes, and Subclasses.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CSC 205 Java Programming II Defining & Implementing Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Working With Objects Tonga Institute of Higher Education.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
GoodOO Programming Practice in Java © Allan C. Milne v
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Lecture 04 agenda Go over QT exam. Discuss OO How to discover objects, properties, and methods Tetris game Other examples.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Object-Oriented Programming Important step forward in programming because it allows you to model real world systems which is what programming is all about.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Lecture 04 agenda Go over QT exam.
University of Central Florida COP 3330 Object Oriented Programming
Interfaces.
Quizz Results hw1 quizz mean std-dev
Elipses int... passed into method as int[]
Presentation transcript:

Creating a class

Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change value i.e. they're variable. If a field is not variable, then you should exclude it, or make it a constant (public final static). Any static member belongs to the CLASS -- only one. A field is private. This is what is meant by encapsulation. You are hiding all but the 'public interface' (not to be confused with Interface). There is NO limit as to how many instances (objects) of a class you can have; and often you will have more than one.

getters and setters public type getField() public void setField(type typName) They are public! The form the 'public interface' of the class. Use Eclipse to generate them for you.

Constructors Constructors are optional. If your class is just a static driver; i.e. it just has a static main method, then no need to instantiate it; and no need for constructor. If your class is a "java bean" -- a class that models a real world object (noun) in your system, then it should have a constructor. House, Car, Person, etc. If you don't provide a constructor, a no-args constructor is provided automatically. You can, and often want, more than one constructor with different signatures. You call a constrcutor with the 'new' keyword.

Methods public non-static methods may be called from the implicit parameter of an object (once instantiated in memory space). public static methods may be called directly from the class -- WITHOUT the need to instantiate in memory. Example: public static void main(). Java doesn't instantiate your methods over and over, just the fields. They are compiled in the.class file, only once.

style conventions don't use variable/reference names like x,y, i, and j. That's very difficult to read. The exception is a counter, and even then, it should look like this: nC. cntrl-shift-f will format your code with indents. Use it. some programmers use underscore to denote fields; and you may as well. All contemporary IDEs highlight fields (in Eclipse they're blue).

style conventions Example: y verus dPrice? Example: name or strFullName Example: today or greToday Example countries or strCountries (append an s for data structures, the type should be first) Communicate meta-data! – prefix conveys type – prefix conveys variable or object reference (one and three letters in prefix respectively) – postFix 's' conveys data-structure or array

style conventions Make it up! just be consistent throughout that same class. Example: StringTokenizer stoDates If you just need a quick-and-dirty object reference defined locally inside a method, use str, gre, scn, etc. So long as it's not a keyword like int.

Model a system Object-oriented programming is about modelling a system. Write the problem out as requirements -- this will essentially be a math world problem (your favorite kind to solve!) Your computer objects map directly to real- world objects (nouns). Your methods map directly to real-world actions (verbs).

Write a very simple application for a fictitious real estate company that displays the houses it currently offers. Some of the data they'd like to display include; address, market_value, an image of the house, and if the house has been foreclosed. 1/ display all the houses to the console (use char[][] for image). 2/ Apply a 7% discount on all of our houses. Then display all of our houses to the console (use char[][] for image) 3/ Determine the most expensive house and display it to the console. All houses are by default for sale, and one they're sold, they are removed from the display list. There is plenty of other data like, weeks on market, the owners of the house, cube footage, etc. that we don't care about for our simple app.

Class Objects

Surprise

Exceptions How do you know if a method throws an exceptions? -- look at the javaDocs in the API Do I need to catch all exceptions? -- in a professional environment, yes. In this class, no. Will the compiler warn me about possible thrown exceptions? --sometimes. What's catch and throw? --see example...

A couple of homework probs YourDate and java library Date Reorder

nVals 2031 nOrds [0] [1] [2] [3]

Reorder nVals 2310 nOrds [0] [1] [2] [3] int[] nResults = new int[nVals.length]; for (int nC = 0; nC < nResults.length; nC++) nResults[nOrds[nC]] = nVals[nC]; 0000 nResults [0] [1] [2] [3]

Reorder nVals 2310 nOrds [0] [1] [2] [3] nOrds[3] == 0, nVals[3] == 600. We need to assing 600 to the nVals[0] nSwap = nVals[0], so now nSwap is 4. nVals[nC] = nVals[nB], so 4, 18, -1, 4. Then nVals[nB] = nSwap. so 600, 18, -1, 4 do the same for orders. break. O(n^2)

Reorder (from hw1) int nSwapVal; for (int nB = 0; nB < nOrders.length; nB++) { for (int nC = 0; nC < nOrders.length; nC++) { if (nOrders[nC] == nB) { //swap vals nSwapVal = nVals[nC]; nVals[nC] = nVals[nB]; nVals[nB] = nSwapVal; //swap ords nSwapVal = nOrders[nC]; nOrders[nC] = nOrders[nB]; nOrders[nB] = nSwapVal; break; }

YourDate (from hw1)

Deep Copy

Auto-Boxing

Elipses

Interfaces A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods. Interface names often end with "able" to imply that they add to the capabilty of the class. An interface is a contract; it defines the methods

Interfaces A class implements an interface rather than extends it. Any class that implements the interface must override all the interface methods with it's own methods. Interface names often end with "able" to imply that they add to the capabilty of the class.