Implementing Classes Chapter 3.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Looking inside classes Fields, Constructors & Methods Week 3.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
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.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1. 2 Introduction to Methods  Method Calls  Parameters  Return Type  Method Overloading  Accessor & Mutator Methods  Student Class: Revisited.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
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.
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
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.
Chapter 8: User-Defined Classes and ADTs
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
Creating Your Own Classes
Lecture 3 John Woodward.
3 Introduction to Classes and Objects.
Examples of Classes & Objects
Haidong Xue Summer 2011, at GSU
Software Development Java Classes and Methods
Class Definitions and Writing Methods
Road Map Introduction to object oriented programming. Classes
CompSci 230 Software Construction
Introduction to Object-oriented Program Design
Can perform actions and provide communication
Chapter 3 Introduction to Classes, Objects Methods and Strings
Can perform actions and provide communication
Defining Your Own Classes
An Introduction to Java – Part II
Classes & Objects: Examples
Chapter 8: User-Defined Classes and ADTs
Can perform actions and provide communication
CS2011 Introduction to Programming I Objects and Classes
Object-oriented Design in Processing
CIS16 Application Development and Programming using Visual Basic.net
JAVA CLASSES.
Java Programming with BlueJ Objectives
Classes CS 21a: Introduction to Computing I
Dr. R Z Khan Handout-3 Classes
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

Implementing Classes Chapter 3

What is a Java Class? A Java class describes the behaviors and attributes of an object. There are at least two java classes associated with a Java project. A class implementation .java file. This class contains instance variables and methods for accomplishing a task. A tester class .java file. The tester class instantiates an object of the class and calls the member functions described in the implementation class.

Syntax of a Java Class Declaration public class ClassName { Instance variables - data or information an object contains visible by the methods of a class. accessibility data type variable name; private double balance; Constructor methods() }

Accessibility Accessibility refers to the information or data exposure to other classes. public variables are exposed to all classes private variables are only exposed to classes to which they belong.

Method Return Types A method return type refers to how a method performs an action. A void method performs an action without storing a value. A void method does not require a return statement public void greeting()//this method simply displays text output. It does not store the outcome of a computation. { System.out.println(“Hello!”); }

Methods that return values pubic int getValue()//this method will return the information contained in a class object. { return value; Return statement }

Syntax of declaring class methods public return type method name (parameter list) {accessibility identifies the method data type of the method results.(void, int, double, string, etc) arguments- information a method needs to perform a task }

Example of a Java implementation class //This class will simulate pressing a counting device. public class Clicker{ private int value // an instance variable to store the number of clicks. public void click()// a mutator method to add 1 each time the method is called { value = value +1; } }

Clicker class continued…. //This method accesses the number of times the click method has been called. public int getValue() { return value; }

Clicker class continued... //a mutator method to reset the number of clicks stored in value back to 0. //This method does not return a value. public void reset() { value=0; }

Tester Class //This class will instantiate an object of the Clicker class and call the methods of the class. public class ClickerTester{ int numClicks(); Clicker myClicker = new Clicker(); //instantiate an object of a Clicker class. clas Object myClicker.click() // the object calls the click method. //The numClicks variable is increased by 1; System.out.println(myClicker.getValue());//displays the value of the numClicks variable myClicker.click()// another method call to the click method. }