Defining Classes. Why is Java designed this way? Improving our class Creating our own class Using our class Implementing our class.

Slides:



Advertisements
Similar presentations
Written by: Dr. JJ Shepherd
Advertisements

Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 4 Object Oriented Programming in Java Class Design Process.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
OOP & JAVA. HelloWorld.java /** * The HelloWorld class is an application that * displays "Hello World!" to the standard output. */ public class HelloWorld.
Classes and Objects in Java
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Announcements Attendance sheet is going around – be sure you sign it! First part of.
J. Michael Moore Object Oriented Programming: An Introduction CSCE 110.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Object Oriented Software Development
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
1 Chapter 3 and 6– Classes, Objects and Methods Object-Oriented Programming Concepts – Read it from Java TutorialJava Tutorial Definition: A class is a.
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.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes and Objects in Java
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Chapter 5 Introduction to Defining Classes
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
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.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Introduction to Object-oriented Programming
Written by: Dr. JJ Shepherd
Re-Intro to Object Oriented Programming
Objects as a programming concept
Objects as a programming concept
University of Central Florida COP 3330 Object Oriented Programming
Haidong Xue Summer 2011, at GSU
University of Central Florida COP 3330 Object Oriented Programming
Introduction to Object-oriented Program Design
Interfaces.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Object-oriented Design in Processing
Prototype Pattern 1.
Implementing Classes Chapter 3.
Object-oriented Design in Processing
Object-oriented Design in Processing
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Object-oriented Design in Processing
Object-Oriented Programming and class Design
Classes and Objects Systems Programming.
SPL – PS3 C++ Classes.
Object-Oriented Programming and class Design
Introduction to Classes and Objects
Presentation transcript:

Defining Classes

Why is Java designed this way? Improving our class Creating our own class Using our class Implementing our class

Why did they design it that way? Java has some awkward compositions: To read from the keyboard we need: System.in object InputStreamReader object A BufferedReader object A String object To read from a disk file we need: A FileReader object A BufferedReader object A String object Complicated! But powerful

Solving the problem We can create our own classes that provide the required behaviour I want to describe a class that Java doesn’t contain It will do something I need

Woof I want to keep track of dogs

Steps to create your own class 1.Decide on the behaviour that the class will provide 2.Determine the way the class will be used by others (its interface) Determine the prototypes of the methods 3.Write a sample program using the class Test the design 4.Write a skeleton of the class definition Class boilerplate with prototypes and empty method bodies

1. Determining the behavior Actions What can it do? If we had a class called Dog we would want it to perform these actions:

Characteristics / Attributes Think about attributes or as they are also called instance variables Important information about the object E.g. a chair: 4 legs?, colour?, material? Instance variables are available to ALL methods in the class!

What attributes do we want our dog to know?

Attributes of the dog

2. Interface and Prototypes Interface: way in which one can use an object of a particular class With Java we need the ability to: a.Declare a Dog as follows: Dogfluffie;

2. Interface and Prototypes…2 b.Instantiate (create) a new Dog object: fluffie = new Dog( >); Does this object need any arguments (or parameters) to set it up?

c.Gather additional information about itself : d.Change its attributes: 2. Interface and Prototypes…3

2. Interface and Prototypes…4 d.Send the dog a message to do these things:

2. Interface and Prototypes…5 Develop prototypes for the methods Make sure method names follow java conventions

2. Interface and Prototypes…6 constructor method: Defines the “default” look or state of an object E.g. a student object must have a first name, last name, student number (and other items)

2. Interface and Prototypes…7 Constructor (continued) public Dog( >) constructor has same name as class In our example it may need arguments Age? Name? Fur colour?

2. Interface and Prototypes…8 Returning information Send information to the dog Get it to bark Get it to roll over

2. Interface and Prototypes…9 Getting information Ask the object (Dog) to give us this information What’s your name dog?

2. Interface and Prototypes…10 Change information Ask the dog to change some information Change your dog’s name

3. Sample program using class Program serves two purposes Help clarify how our new class is to be used Checks our interface design to see if it is satisfactory Will it do what we need it to do?

4. Class definition skeleton class Dog { //instance variables, if needed … // constructor no return type public Dog( >) { statements }

Implementing the Triangle Class

Implementing the Triangle class Implementation: writing the method bodies and declaring any instance variables Start with any method Start with the easy methods!

For your class… Determine it’s behaviour Determine it’s attributes Design 3 ‘get’ methods Design 3 ‘set’ methods Make sure you include any required arguments (stuff to make the method work e.g. Color c)