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
Road Map Introduction to object oriented programming. Classes
Advertisements

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.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
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 Chapter 4 Defining Your Own Classes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Creating a class. Fields (instance variables) Fields are the data defined in the class and belonging to each instantiation (object). Fields can change.
Lecture 04 agenda Go over QT exam. Discuss OO How to discover objects, properties, and methods Tetris game Other examples.
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,
Best Practices. Contents Bad Practices Good Practices.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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
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,
Code Conventions Tonga Institute of Higher Education.
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”
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.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
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.
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
GoodOO Programming Practice in Java © Allan C. Milne v
CS1101 Group1 Discussion 10 Lek Hsiang Hui comp.nus.edu.sg
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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
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.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
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
Chapter 3 Introduction to Classes, Objects Methods and Strings
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 modeling 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

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? --If it's a checked exception, then yes. 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