Java Classes Using Java Classes Introduction to UML.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Fundamentals of Software Development 1Slide 1 Review: Why start with WordGames? You wrote your first lines of code in this course in WordGames.You wrote.
Inheritance Inheritance Reserved word protected Reserved word super
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
UML Basics & Access Modifier
Object Oriented Software Development
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Writing Classes (Chapter 4)
Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software.
Introduction to Programming Writing Java Beginning Java Programs.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
4.1 Instance Variables, Constructors, and Methods.
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.
Object-Oriented Analysis and Design An Introduction.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object-Oriented Programming in C++
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Information Systems Engineering
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Working With Objects Tonga Institute of Higher Education.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
ITIP © Ron Poet Lecture 14 1 Responsibilities. ITIP © Ron Poet Lecture 14 2 Two Sections of Code  If we ask two people to work together to do a job,
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Software development For large and complex software use divide and conquer rule. Software design methods: Structured design Object-Oriented design.
Data Structures Using Java1 Chapter 1 Software Engineering Principles and Java Classes.
Unified Modeling Language (UML)
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Comp1004: Building Better Objects II Encapsulation and Constructors.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Introduction to Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
CompSci 230 Software Construction
Corresponds with Chapter 7
Building Java Programs
Encapsulation and Constructors
CSE 1020:Programming by Delegation
Building Java Programs
Today’s topics UML Diagramming review of terms
User-Defined Classes and ADTs
Chapter 8 Classes User-Defined Classes and ADTs
Week 4 Lecture-2 Chapter 6 (Methods).
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Presentation transcript:

Java Classes Using Java Classes Introduction to UML

Java and object-orientation Java is an object-oriented language a Java program consists of one or more classes, each of which contain attributes and methods Java programs create and use instances of these classes called objects we can use UML (Unified Modelling language) to design and document our object-oriented designs UML class diagrams can easily be translated into Java code

Why classes? convenient way of packaging together related data and functionality this is called encapsulation Example - Spike

Spike's data colour of body and features position (x and y coordinates) height

Spike's functionality construct a new Spike object and initialise its data draw move to a given position get its current position (x or y coordinate) change its height to a given value

Java classes in Java we use the following terms: attributes for the data methods for the functionality attributes are usually private other objects can't read or change them public methods are used to interact with the object typically methods could: read an attribute set (change) an attribute do something (eg paint) using attribute values

Why classes? once we have defined a class we can create one or more instances (objects) of that class class is the blueprint object is the actual instance each object has its own data can be different to that of other objects of the same class methods are used to interact with individual objects

How to create and use objects declare a variable of the desired class Spike spike1; create objects of that class using the constructor and the keyword new spike1 = new Spike(); use the name of the object to call its methods spike1.moveTo(50, 100); spike1.drawSpike();

Other classes you’ve seen Console readString(), readInt()…. methods System.out print() and println() methods You also used the String class Take a look at your first Java project again.

UML class diagrams UML – Universal Modelling Language standard notation for designing and documenting object-oriented systems a UML class diagram is a box with 3 compartments top: class name middle: attributes bottom: methods

More detailed class diagrams attribute visibility + for public - for private type of attribute, return type of method after : type and name of method parameters in brackets after method name

Using classes very often in Object-Oriented programming we will use classes written by someone else part of Java language in libraries, like Console written by our colleagues (Spike) (hopefully) these classes will be well designed, tested and documented we can use them with confidence this is called software reuse allows rapid building of robust systems now we will go through another example of using a library (someone else's) class

Day part of corejava library encapsulates all information about a date private attributes: day month year all integers why not have a dayOfWeek attribute?

Day methods what do you think the other methods do? hint: it is good programming practice to give methods meaningful names! can refer to documentation for more information hopefully the class developer wrote meaningful comments!!

Constructors all classes have a constructor method that is called when an object is first created all constructors must have the same name as the class and are public constructors never have a return type Java provides a default constructor creates space in memory for the object and all its attributes classes can provide their own version(s) to do any initialisation steps required which methods are the Day constructors and what do you think they do?

Day class two constructor methods: public Day() creates a Day object with today’s date public Day(int yyyy, int m, int d) creates a Day object with given date

get- methods often need a method to return the value of a private attribute way of reading the attribute without changing it conventional to use a get- method method getX( ) returns the attribute X public so other classes can use it return type is the same type as the attribute Day has three get- methods what do you think they do?

set- methods set- methods are used to change the value of given attribute give write- access to a private attribute method setX(type a) sets the value of attribute X to the new value a public so other classes can use it parameter type is the same type as the attribute return type is usually void Day does not have any set- methods why do you think this is?

toString() method all Java classes have a default toString() method returns a String containing information about the object the default is name of hash code of the object for example many classes override this method to return something more meaningful in this case Day[2004,9,22] name of class, then year, month, day in square brackets

Day other methods of interest: advance(int n) advances the date by n days not as easy as it looks – have to consider end of months and years int weekday() returns the day of the week 0 for Sunday to 6 for Saturday int daysBetween(Day b) calculates and returns the number of days between 2 Day objects

Information hiding the logic within these methods is quite complicated fortunately we don't need to know how they are implemented as long as we know what they do, we can confidently use them without knowing how they do it in OO terms this is called information hiding

Using the Day class we'd now like to write a program that uses some of the features of the Day class before we write our program we need to think about what it will do Scenario - a small application program is required which can ask the user for a number and tell them what date it will be in that many day's time

How can we do this? could create a Day object it encapsulates all the information about a date what date should we represent? today's date use default constructor how can we get input from the user? use Console.readInt() method how can we advance the date? use the Day object's advance() method luckily we don't need to write it ourselves!!!

How can we do this? how can we output the results? use the Day object's toString() method, and print the returned String to the console how should we structure the program? put code in one class DayInterface.java small, so put all code in main() method import corejava.* to access Console and Day classes

DayInterface.java import corejava.*; public class DayInterface { public static void main(String[] args) { Day theDate; int amount; theDate = new Day(); amount = Console.readInt("How many days from now? "); theDate.advance(amount); System.out.println(amount + " days from today is " + theDate.toString()); }

Output How many days from now? days from today is Day[2005,7,26]

Summary have looked at classes why we use them - encapsulation what they are made of (attributes and methods) how we can document them with UML class diagrams how we can use library classes in our own programs information hiding – we don't need to know how they work! tutorial design some class diagrams write some more applications using other people's classes