Docsity

Java Programming: Objects, Input/Output, and String Methods, Slides of Object Oriented Programming

Partial preview of the text

Download Java Programming: Objects, Input/Output, and String Methods and more Slides Object Oriented Programming in PDF only on Docsity!

Java Programming: From

Problem Analysis to

Program Design, 3e

Chapter 3

Introduction to Objects and

Input/Output

Chapter Objectives

 (^) Learn about objects and reference variables  (^) Explore how to use predefined methods in a program  (^) Become familiar with the class String  (^) Learn how to use input and output dialog boxes in a program

Object and Reference

Variables

 (^) Declare a reference variable of a class type  (^) Allocate memory space for data  (^) Instantiate an object of that class type  (^) Store the address of the object in a reference variable

Object and Reference Variables

(continued)

int x; //Line 1

String str; //Line 2

x = 45; //Line 3

str = "Java Programming"; //Line 4

Object and Reference

Variables (continued)

  • (^) Primitive type variables directly store data into

their memory space

  • (^) Reference variables store the address of the

object containing the data

  • (^) An object is an instance of a class and the

operator new is used to instantiate an object

Objects and Reference Variables (continued)

Using Predefined Classes and

Methods in a Program

(continued)

 (^) To use a method you must know:  (^) Name of the class containing method (Math)  (^) Name of the package containing class (java.lang)  (^) Name of the method - (pow), it has two parameters  (^) Math.pow(x, y) = xy

Using Predefined Classes and

Methods in a Program

(continued)

 (^) Example method call import java.lang; //imports package Math.pow(2, 3); //calls power method // in class Math  (^) Dot (.) Operator: used to access the method in the class

The class String

(continued)

 (^) A String object is an instance of class String  (^) The address of a String object with the value "Lisa Johnson" is stored in name  (^) String methods are called using the dot operator

Some Commonly Used String Methods

Some Commonly Used String Methods

(continued)

Some Commonly Used String Methods

(continued)

Formatting Output with printf

 (^) A syntax to use the method printf to produce output on the standard output device is: System.out.printf(formatString); or System.out.printf(formatString, argumentList);  (^) formatString is a string specifying the format of the output and argumentList is a list of arguments

Formatting Output with

printf (continued)

 (^) argumentList is a list of arguments that consists of constant values, variables, or expressions  (^) If there is more than one argument in argumentList, then the arguments are separated with commas