Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

Looking inside classes Fields, Constructors & Methods Week 3.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 10 Classes Continued
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Introduction to Defining Classes
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Classes, Interfaces and Packages
Topics Instance variables, set and get methods Encapsulation
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.
Class Fundamentals BCIS 3680 Enterprise Programming.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
1 Guide gus; Creates a “mailbox” to hold the address of a Guide object. null gus.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Classes (Part 1) Lecture 3
Java Primer 1: Types, Classes and Operators
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Encapsulation & Visibility Modifiers
Corresponds with Chapter 7
Defining Classes and Methods
Object Oriented Programming in java
Java Programming Language
Classes and Objects Systems Programming.
Chapter 7 Objects and Classes
Presentation transcript:

Introduction to Defining Classes

Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class and a model class. Use visibility modifiers to make methods visible to clients and restrict access to data within a class.

Introduction to Defining Classes Objectives: Write appropriate mutator methods, accessor methods, and constructors for a class. Understand how parameters transmit data to methods. Use instance variables, local variables, and parameters appropriately.

The Internal Structure of Classes and Objects An object is a run-time entity that contains data and responds to messages. A class is a software package or template that describes the characteristics of similar objects. These characteristics are of two sorts: Variable declarations that define an objects data requirements (instance variables). Methods that define its behavior in response to messages.

The Internal Structure of Classes and Objects The combining of data and behavior into a single software package is called encapsulation. An object is an instance of its class, and the process of creating a new object is called instantiation.

The Internal Structure of Classes and Objects Three Characteristics of an Object An object has behavior as defined by the methods of its class An object has state, which is another way of saying that at any particular moment its instance variables have particular values. Typically, the state changes over time in response to messages sent to the object.

A Card Class The Card Class stores a card that contains two variables: a suit and a value. It also has 6 methods: 1. Card () – a constructor function 2. getSuit() – a method that returns a number that represents a suit 3. getValue() – a method that returns the face value of a card 4. getSuitAsString() – a method that returns the suit as a string. 5. getValueAsString() – a method that returns the suit as a string.

Using Card Objects Some portions of code illustrate how a client instantiates and manipulates student objects. First we declare several variables, including two variables of type Card. As usual, we do not use variables until we have assigned them initial values. We assign a new card object to myCard & dealerCard using the operator new. Card myCard; Card dealerCard; myCard = newCard() // Instantiate a card dealerCard = newCard();

A Card Class It is important to emphasize that the variable myCard is a reference to a card object and is not a card object itself. A card object keeps track of the suit and value of a card. What are these values?

A Card Class That depends on the classs internal implementation details, but we can find out easily by sending messages to the card object via its associated variable myCard: mySuit = myCard.getSuit(); System.out.println (mySuit); // yields a random number from 0 to 3 myValue = myCard.getValue(); System.out.println (myValue); // yields a random value from 1 to 13;

A Card Class Messages that change an objects state are called mutators. To see if the mutators worked correctly, we use other messages to access the objects state (called accessors): String mySuit = myCard.getSuitAsString(); System.out.println (mySuit); // prints out either Hearts, Clubs, Diamonds or Spades String myValue = myCard.getValueAsString(); System.out.println (myValue); // prints out a number 2 to 10 or Ace, Jack, Queen or King.

A Card Class Objects, Assignments, and Aliasing We close this demonstration by associating a card object with the variable myCard2. Rather than instantiating a new card, we assign myCard to myCard2; myCard2 = myCard; // myCard & myCard2 now refer to the same card

A Card Class The variables myCard and myCard2 now refer to the same card object. This might come as a surprise because we might reasonably expect the assignment statement to create a second card object equal to the first, but that is not how Java works. To demonstrate that myCard and myCard2 now refer to the same object, we get a new suit & value for myCard and display both cards suit and value

// Black Jack Template // Author: Ms. Waldron1/08 import cs1.Keyboard; public class BlackJack { public static void main(String[] args) { Card myCard; Card myCard2; myCard = new Card(); myCard.getValue(); myCard.getSuit(); System.out.println ("myCard: " + myCard.getSuitAsString() + " " + myCard.getValueAsString()); myCard2 = myCard; System.out.println ("myCard2: " + myCard2.getSuitAsString() + " " + myCard2.getValueAsString()); }

A Card Class The Structure of a Class Template All classes have a similar structure consisting of four parts: The classs name and some modifying phrases A description of the instance variables One or more methods that indicate how to initialize a new object (called constructor methods) One or more methods that specify how an object responds to messages The order of these parts can be varied arbitrarily provided (the classs name) comes first.

A Card Class The following is an example of a class template: public class extends { // Declaration of instance variables private ;... // Code for the constructor methods public () { // Initialize the instance variables... }... // Code for the other methods public ( ){... }... }

A Card Class Class definitions usually begin with the keyword public, indicating that the class is accessible to all potential clients. public class Class names are user-defined symbols, and must adhere to the rules for naming variables and methods. It is common to start class names with a capital letter and variable and method names with a lowercase letter.

A Card Class Instance variables are nearly always declared to be private. This prevents clients from referencing to the instance variables directly. Making instance variables private is an important aspect of information hiding. private

A Card Class Methods are usually declared to be public, which allows clients to refer to them. public The clauses private and public are called visibility modifiers. Omitting the visibility modifier is equivalent to using public.

A Card Class In the Card example all the methods, except the constructor method, have a return type, although the return type may be void, indicating that the method in fact returns nothing. In summary when an object receives a message, the object activates the corresponding method. The method then manipulates the objects data as represented by the instance variables.

A Card Class Constructors The principal purpose of a constructor is to initialize the instance variables of a newly instantiated object. Constructors are activated when the keyword new is used and at no other time. A constructor is never used to reset instance variables of an existing object.

A Card Class A class template can include more than one constructor, provided each has a unique parameter list; however, all the constructors must have the same name- that is, the name of the class. Constructors with empty parameter lists and are called default constructors. If a class template contains no constructors tha JVM provides a primitive default constructor. This constructor initializes numeric variables to zero and object variables to null, thus indicating that the object variables currently reference no objects.

Editing, Compiling, and Testing the Card Class Once the Card class is compiled, applications can declare and manipulate student objects provided that: The code for the application and Card.class are in the same project

Editing, Compiling, and Testing the Card Class The following is a small program that uses and tests the Card class. import cs1.Keyboard; public class BlackJack { public static void main(String[] args) { Card myCard; Card dealerCard; myCard = new Card(); dealerCard = new Card(); System.out.println ("myCard: " + myCard.getSuitAsString() + " " + myCard.getValueAsString()); System.out.println ("dealerCard: " + dealerCard.getSuitAsString() + " " + dealerCard.getValueAsString()); }