Java Class Syntax CSIS 3701: Advanced Object Oriented Programming.

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

Structures Spring 2013Programming and Data Structure1.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Interfaces A Java interface is a collection
Chapter 14: Overloading and Templates
Class Relationships. In systems with multiple classes, it can become difficult to keep track of relationships  e.g. the Student class requires the Course.
CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Enhancing classes Visibility modifiers and encapsulation revisited
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Lecture 9 Concepts of Programming Languages
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Object interaction Creating cooperating objects. 28/10/2004Lecture 3: Object Interaction2 Main concepts to be covered Abstraction Modularization Class.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
ITP © Ron Poet Lecture 13 1 Helper Objects. ITP © Ron Poet Lecture 13 2 Console Helper Object  We have used Console objects for a while, let’s see what.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Chapter 8: User-Defined Classes and ADTs
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
Agenda Object Oriented Programming Reading: Chapter 14.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Unit Testing CSIS 3701: Advanced Object Oriented Programming.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
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 ◦
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Pointers and Classes.
Andrew(amwallis) Classes!
Classes and Objects.
Java Programming: Guided Learning with Early Objects
Inheritance and Encapsulation
Lecture 9 Concepts of Programming Languages
User-Defined Classes and ADTs
Object Based Programming
בניית מחלקות.
Chapter 3 Introduction to Classes, Objects Methods and Strings
CSC 113: Computer programming II
Chapter 8: User-Defined Classes and ADTs
Basics of OOP A class is the blueprint of an object.
Object-Oriented Programming
Java Programming Language
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
C++ data types.
SPL – PS3 C++ Classes.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Class Decomposition Often decompose complex class into simpler support classes Why? –Easier to write/debug/maintain group of simple classes than one complex class –Simpler classes can be refactored into general purpose tools usable by others Refactoring: redesigning classes after an implementation stage to improve modularity, efficiency, etc.

UML Universal Modeling Language (UML) –Excellent tool for analysis and design Features: –Graphical in nature How we tend to do design –Simple enough for customer to understand –Well-defined enough to allow developers to create system –Common design specification language Experienced developer should be able to immediately understand system from diagram

UML and Decomposition Class Diagram –Represents relationships between classes Composition relationship –One class composed of others as member variables Class Support class

Clock Example ClockProject package Main.java visual application Stores current hour and minute Allows hour and minute to be set Increments to next second or minute Clock.java Clock.java business logic clock

Basic Class Structure package ClockProject; public class Clock { … } Package declaration Class name must be same as name of file Makes class available for use by other classes Only one class per file All code for class in file (no separate headers and code)

Member Variables public class Clock { private int hour; private int minute; } Internal representation may not be directly accessed by other objects – must use methods instead Represent current state of the object Exist for lifetime of object “Shared” by all methods and constructors Each object maintains own copy, possibly with different values hour: 12 minute: 15 hour: 3 minute: 42 c1c2

Constructors Called when object created with new Clock c = new Clock(); –Sets initial state of member variables –Must have same name as class public class Clock { … public Clock() { hour = 0; minute = 0; } hour and minute initially both 0 Note that variables not redeclared!

Methods Generally manipulate member variables –May also have local variables like a function public class Clock { … public void nextMinute() { minute++; if (minute > 59) { minute = 0; hour++; if (hour > 23) { hour = 0; } } } Note that methods and constructors usually public so may be called by other objects

Methods Often provide direct access to state variables public void setMinute(int m) { minute = m; } public void setHour(int h) { hour = h; } public int getMinute() { return minute; } public int getHour() { return hour; } “Setter” methods “Getter” methods Note that h, m are local variables

Methods Often return information about state of object in form helpful to user –Example: toString returns state in HH:MM forma t public String toString() { String result = ""; if (hour < 10) result += "0" + hour; else result += hour; result += ":"; if (minute < 10) result += "0" + minute; else result += minute; return result; }

Overloading Giving multiple definitions to same name Most often done with constructors –Required to have same name as class –May need to construct objects in different ways Legal if compiler can disambiguate based on parameters –Number of parameters –Type of parameters

Overloading Example: Overloaded constructor to set initial time public class Clock { … public Clock() { hour = 0; minute = 0; } public Clock(int h, int m) { hour = h; minute = m; } 0-parameter “default” constructor 2-parameter “overloaded” constructor

Overloading Easy for compiler to disambiguate: Clock c1 = new Clock(); Must be default constructor Clock c2 = new Clock(12, 47); Must be overloaded constructor Note: Must use () in constructor call even if no parameters (unlike C++)

The “ this ” object Reference from object to itself –Stores address of object –Implicit state variable in all objects Internal use of state variables implicitly use “ this ” public void setHour(int h) { this.hour = h; } public int getHour() { return this.hour; } 37A4 this 37A4 Manipulate the hour member variable of “this” object

Using a Clock Object Business logic objects used by other classes

Using a Clock Object Other objects composed of support objects –Contain object as member variable –Construct that object (often when it is constructed) –Call its methods as needed public class Main … { … private Clock clock; … public Main() { … clock = new Clock(); }

Using a Clock Object

Aggregation Often keep track of many objects simultaneously Simplest idea: Array of objects –Must construct array and all objects in array –Use loop to call method for all objects in array Basic syntax: for (int i = 0; i < A.length; i++) { A[i].method(params)

UML for Aggregation Give number of entities aggregate type contains –Can be range (1..4) –Can be unlimited *  any number from 0 to ∞ +  any number from 1 to ∞ Car Wheel 4 Spoke String char *

Aggregation Example Goal: Display list of times in each time zone Solution: Array of 4 clock objects

Aggregation Example Construct array of Clocks Use loop to construct each array element –Use different initial hour for each

Aggregation Example Use loop to call nextMinute and toString for each Clock in array –Print tabs between each clock time, newline at end