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

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Advertisements

Chapter 3 Introduction to Classes, Objects, Methods, and Strings
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
 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.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 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.
Classes and Instances. Introduction Classes, Objects, Methods and Instance Variables Declaring a Class with a Method and Instantiating an Object of a.
Java: How to Program Methods Summary Yingcai Xiao.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 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.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
 2005 Pearson Education, Inc. All rights reserved. 1 A class A class is the blueprint from which objects are generated. In other words, if we have six.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
 2008 Pearson Education, Inc. All rights reserved 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
Topics Instance variables, set and get methods Encapsulation
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 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
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
Object Oriented Programming in java
Visual Programming Lecture 4.
Introduction to Classes and Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Classes and Objects Systems Programming.
Presentation transcript:

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

 2005 Pearson Education, Inc. All rights reserved. 2 Classes, Objects, Methods and Instance Variables Class provides one or more methods - Method represents task in a program Classes contain one or more attributes – Specified by instance variables – Carried with the object as it is used

 2005 Pearson Education, Inc. All rights reserved. 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.

 2005 Pearson Education, Inc. All rights reserved. 4 Common Programming Error Declaring more than one public class in the same file is a compilation error.

 2005 Pearson Education, Inc. All rights reserved. 5

6 Class GradeBookTest Java is extensible – Programmers can create new classes 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

 2005 Pearson Education, Inc. All rights reserved. 7

8 Declaring a Method with a Parameter Scanner methods – nextLine reads next line of input – next reads next word of input

 2005 Pearson Education, Inc. All rights reserved. 9

10 // GradeBookTest.java import java.util.Scanner; public class GradeBookTest { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); GradeBook myGradeBook = new GradeBook(); System.out.println( "Please enter the course name:" ); String nameOfCourse = input.nextLine(); // read a line of text System.out.println(); myGradeBook.displayMessage( nameOfCourse ); }

 2005 Pearson Education, Inc. All rights reserved. 11 Instance Variables, set Methods and get Methods Variables declared in the body of method – Called local variables – Can only be used within that method Variables declared in a class declaration – Called fields or instance variables – Each object of the class has a separate instance of the variable

 2005 Pearson Education, Inc. All rights reserved. 12 public class GradeBook { private String courseName; public void setCourseName( String name ) { courseName = name; } public String getCourseName() { return courseName; } public void displayMessage() { System.out.println( "Welcome to the grade book for\n“ + getCourseName() ); }

 2005 Pearson Education, Inc. All rights reserved. 13 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 – When a program creates (instantiates )an object of class GradeBook variable courseName is encapsulated (hidden) in the object and can be accessed only by methods of the objects class

 2005 Pearson Education, Inc. All rights reserved. 14 GradeBookTest Class That Demonstrates Class GradeBook Default initial value – Provided for all fields not initialized – Equal to null for String s

 2005 Pearson Education, Inc. All rights reserved. 15 set and get methods 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

 2005 Pearson Education, Inc. All rights reserved. 16

 2005 Pearson Education, Inc. All rights reserved. 17

 2005 Pearson Education, Inc. All rights reserved. 18 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 parentheses – When you declare a class you can provide your own constructor to specify custom initialization for objects of your class

 2005 Pearson Education, Inc. All rights reserved. 19

 2005 Pearson Education, Inc. All rights reserved. 20

 2005 Pearson Education, Inc. All rights reserved. 21 Call constructor to create first grade book object Create second grade book object

 2005 Pearson Education, Inc. All rights reserved. 22 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

 2005 Pearson Education, Inc. All rights reserved. 23 Outline Dialog1.java

 2005 Pearson Education, Inc. All rights reserved. 24 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

 2005 Pearson Education, Inc. All rights reserved. 25 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. 26