3 Introduction to Classes and Objects.

Slides:



Advertisements
Similar presentations
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 2 - Introduction to Java Applications
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
 Pearson Education, Inc. All rights reserved. 3 3 Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java How to Program, Late Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Introduction to Classes and Objects
4 Introduction to Classes and Objects.
Introduction to Classes and Objects
Defining Classes and Methods
Classes, Objects, Methods and Strings
Introduction to Java Applications
Object Oriented Programming in java
Java Programming Language
Defining Classes and Methods
Defining Classes and Methods
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Objects Systems Programming.
Presentation transcript:

3 Introduction to Classes and Objects

3.2 Classes, Objects, Methods and Instance Variables Class provides one or more ______________ Method represents task in a program Describes the _____________ that actually perform its tasks Hides from its user the complex tasks that it performs Method _____________ tells method to perform its task Classes contain one or more attributes Specified by instance variables Carried with the object as it is used

Each class declaration that begins with keyword public 3.3 Declaring a Class with a Method and Instantiating an Object of a Class Each class declaration that begins with keyword public must be stored in a _______________ that has the same ___________ as the class and ends with the .__________ file-name extension.

Class GradeBook keyword public is an ___________ modifier Class declarations include: Access modifier Keyword class Pair of left and right braces Method declarations Keyword ____________ indicates method is available to public Keyword void indicates _______________ Access modifier, return type, name of method and parentheses comprise method header

Class GradeBookTest Java is ______________________ Programmers can create new classes Test program the GradeBook class Class instance creation expression Keyword _____________ Then name of class to create and parentheses Calling a method Object _______________, then dot separator (.) Then method name and parentheses

Compiling an Application with Multiple Classes Note we have two classes Gradebook GradeBookTest Compiling multiple classes Use the compile command in the IDE Compile each file ___________ (or use Compile Project command) Requires a ____________ in some IDE’s

3.4 Declaring a Method with a Parameter Additional information passed to a method Supplied in the method call with _________ Scanner methods _______________ reads next line of input next reads next ___________ of input See new improved GradeBook class, Figure 3.4 and GradeBookTest program, Figure 3.5

Outline GradeBook.java

Notes on Import Declarations __________________ is implicitly imported into every program Default package Contains classes compiled in the ________________ Implicitly imported into source code of other files in directory Packages unnecessary if fully-qualified names are used

3.5 Instance Variables, set Methods and get Methods Variables declared in the body of method Called _______________ variables Can only be used within that method Variables declared in a class declaration Called fields or _______________ variables Each object of the class has a separate instance of the variable Note set and get methods

Access Modifiers public and private private keyword Used for most instance variables private variables and methods are accessible only to methods of the ____________________________________ Declaring instance variables private is known as data ____________________ Return type Indicates item returned by method Declared in method header

GradeBookTest Class That Demonstrates Class GradeBook Default initial value Provided for all fields not initialized Equal to _____________ for Strings Note private instance variables Cannot be accessed directly by ____________ of the object Use set methods to ____________ the value Use ______________ methods to retrieve the value

Primitive Types vs. Reference Types Types in Java Primitive boolean, byte, char, short, _______, long, float, _______________ Reference (sometimes called ____________ types) Objects Default value of null Used to invoke an object’s methods

3.7 Initializing Objects with Constructors _____________ an object of a class Java requires a constructor for every class Java will provide a default no-argument constructor if none is provided Called when keyword _________ is followed by the class name and parentheses Constructors which “incoming” initial value

AccountTest Class to use Class Account Note use of Scanner object Input floating point number Note use of ____________________ %f Used to output floating-point numbers Place a decimal and a number between the percent sign and the f to mandate a ________________

Displaying Text in a Dialog Box Windows and dialog boxes Many Java applications use these to display output ________________ provides prepackaged dialog boxes called message dialogs Program to use dialog box

Displaying Text in a Dialog Box Package javax.swing Contains classes to help create _______________________________ (GUIs) Contains class JOptionPane Declares static method showMessageDialog for displaying a message dialog

Entering Text in a Dialog Box Input dialog Allows user to input information Created using method _____________________ from class JOptionPane