 2005 Pearson Education, Inc. All rights reserved. 1 3 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.
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.
 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.
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.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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.
CISC6795 Spring 11 Fordham Univ. Introduction to Classes and Objects 1.
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.
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.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
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.
Jozef Goetz,  2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved. expanded by J. Goetz, 2015 credits:
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
 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
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
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
Classes, Objects, Methods and Strings
Introduction to Java Applications
Object Oriented Programming in java
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Objects Systems Programming.
Presentation transcript:

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects

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

3 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 file – that has the same name as the class and – ends with the.java file-name extension.

4 Class GradeBook keyword public is an access modifier Class declarations include: – Access modifier – Keyword class – Pair of left and right braces Method declarations – Keyword public indicates method is available to public – Keyword void indicates no return type – Access modifier, return type, name of method and parentheses comprise method header View Source Code

5 Class GradeBookTest Java is extensible – Programmers can create new classes – Test program uses the GradeBook class Class instance creation expression – Keyword new – Then name of class to create and parentheses Calling a method – Object name, then dot separator (. ) – Then method name and parentheses View Source Code of GradeBookTest View Source Code of GradeBookTest

6 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 separately (or use Compile Project command) Requires a project in some IDE’s – Project created in JCreator Project created in JCreator

7 3.4 Declaring a Method with a Parameter Method parameters – Additional information passed to a method – Supplied in the method call with arguments Scanner methodsScanner methods – nextLine reads next line of input – next reads next word of input See new improved GradeBook class, Figure 3.7 and GradeBookTest program, Figure 3.8 Figure 3.7 Figure 3.8

8 Outline GradeBook.java Call printf method with courseName argument View Fig. 3.5 GradeBook test program View Fig. 3.5 GradeBook test program

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

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

11 Access Modifiers public and private private keyword – Used for most instance variables – private variables and methods are accessible only to methods of the class in which they are declared – Declaring instance variables private is known as data hiding Return type – Indicates item returned by method – Declared in method header

12 GradeBookTest GradeBookTest Class That Demonstrates Class GradeBook Default initial value – Provided for all fields not initialized – Equal to null for String s Note private instance variables – Cannot be accessed directly by clients of the object – Use set methods to alter the value – Use get methods to retrieve the value

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

Initializing Objects with Constructors Constructors – Initialize 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 new is followed by the class name and parenthesesCalled when keyword new Constructors which validate “incoming” initial valueConstructors which validate

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

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

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

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

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects