Outline Creating Objects The String Class Packages Formatting Output

Slides:



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

Introduction to Objects and Input/Output
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
Chapter Day 6. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 DUE Problem set 2 posted 
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Chapter 3: Introduction to Objects and Input/Output.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Outline Last Time: Creating Objects The GregorianCalendar Class.
Using Classes and Objects Chapters 3 Section 3.3 Packages Section 3.4 Random Class Section 3.5 Math Class Section 3.7 Enumerated Types Instructor: Scott.
COS 312 DAY 3 Tony Gauvin. Ch 1 -2 Agenda Questions? Assignment 1 DUE right now Assignment 2 Posted – Due Feb 5 prior to class Using Classes and Objects.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
Chapter 3 Using Classes and Objects. Chapter Scope Creating objects Services of the String class The Java API class library The Random and Math classes.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Objectives ► Become familiar with the class String ► ► Learn how to use input and output dialog boxes in a program ► ► Learn how to tokenize the input.
Chapter 3 Java Input/Output.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
Lecture 2 Objectives Learn about objects and reference variables.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
Programming in Java (COP 2250) Lecture 7 Chengyong Yang Fall, 2005.
ELC 312 Day 4. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Agenda Questions? Problem set one due Problem set two Posted in WebCT  Due Sept.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
1 CMPT 126 Java Basics Using Classes and Objects.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
Variables in Java A variable holds either
Java Programming Lecture 2
Formatting Output & Enumerated Types & Wrapper Classes
3 Introduction to Classes and Objects.
Data Types – Reference Types
Introduction to Classes and Objects
Using Classes and Objects
2.5 Another Java Application: Adding Integers
Dialogues and Wrapper Classes
Chapter 3 Java Input/Output.
Classes, Libraries & Packages
Chapter 3: Using Classes and Objects
Message, Input, Confirm, and Specialized Dialogs
Unit-2 Objects and Classes
Chapter 3: Introduction to Objects and Input/Output
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Chapter 2 - Introduction to Java Applications
Chapter 3: Introduction to Objects and Input/Output
Chapter 3 Input/Output.
Message, Input, and Confirm Dialogs
Outline Creating Objects The String Class The Random and Math Classes
Presentation transcript:

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

Formatting output It is often necessary to format values output looks appropriate when printed or displayed NumberFormat part of java.text package Provides generic formatting capabilities is not instantiated using the new operator instead by requesting an object From one of the static methods invoked thru the class name NumberFormat fmt = NumberFormat.getCurrencyInstance();

Creating NumberFormat instance NumberFormat objects are created using getCurrencyInstance() invoked thru class name returns a formatter for monetary values getPercentInstance() invoked thru class name returns an object that formats a percentage are used to format numbers using method format() Refer to Purchase.java NumberFormat fmt = NumberFormat.getCurrencyInstance() double subtotal=19.35; System.out.println(fmt.format(subtotal) ); Output: $19.35

DecimalFormat class DecimalFormat part of java.text package allows to format values based on a pattern To determine how many digits should be printed To the right of the decimal point (for instance) is instantiated in the traditional way using the new operator Its constructor DecimalFormat takes a String That represents a pattern for the formatted number Refer to CircleStats.java

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

Enumerated types Java allows you to define an enumerated type Which can then be used to declare variables as the type of a variable establishes all possible values for a variable of that type By listing, or enumerating the values Where the values are identifiers, and can be anything desired enum Season {winter, spring, summer, fall} There is no limit to the number of listed values Any number of values can be listed

Declaring and using an enumerated type Once a type is defined A variable of that type can be declared enum Grade {A, B, C, D, F}; Grade score; And it can be assigned a value Thru the name of the type score = Grade.A; Enumerated types are type-safe You cannot assign any value other than those listed

Ordinal values Internally, each value of an enumerated type is stored as an integer, called its ordinal value The first value has an ordinal value of zero The second one, and so on You cannot assign a numeric value to enumerated type, even if it corresponds to an ordinal value

Enumerated types: methods The declaration of an enumerated type is a special type of class And each variable of that type is an object methods associated with enumerated objects The ordinal() method returns the numeric value Of an enumerated type The name() returns the name of the value Refer to IceCream.java

Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

Wrapper classes A wrapper class is used to wrap a primitive value into an object Ex: create an object that serves as a container to hold an int represents a particular primitive type Ex: Integer class represents a simple integer value instantiated, stores a single primitive type value Ex: Integer object store a single int value its constructor accept the primitive value to store Ex: Integer ageObj = new Integer (40);

Wrapper classes in the JAVA class library For each primitive type in JAVA There exists a corresponding wrapper class (java.lang) Primitive type Wrapper class byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean

Wrapper classes methods manages the associated primitive type Ex: Integer provides methods returning the int stored in it Some methods of the Integer class Integer (int value) Constructor: creates an new Integer object storing value float floatValue() returns the value of this integer as the float type static int parseInt (String str) Returns the int corresponding to the value in str

Autoboxing/Unboxing Autoboxing is the automatic conversion between Primitive value and corresponding wrapper object Integer obj1; int num1 = 69; Obj1 = num1; // automatically creates an Integer object Unboxing is the reverse condition Integer obj2 = new Integer (69); int num2; num2 = Obj2; // automatically extracts the int value Refer to wrapper_error.java

Using Dialog Boxes for Input/Output Another way to gather input is to use a GUI such as JOptionPane offered by Java Which allows a programmer to use GUI for I/O Making I/O more efficient and the program more attractive JOptionPane Contained in the package javax.swing Offers two methods: ShowInputDialog: allows user to input a string from keyboard ShowMessageDialog: allows user to display results

Syntax for ShowInputDialog To display a dialog box Containing the string in the object stringExpression Storing the input data in the String object str The syntax to use the method is: str = JOptionPane.showInputDialog(stringExpression); A dialog appears on the screen prompting user to enter data Data is returned as a string and assigned to variable str The user enters the data in the white area Called text field

Syntax for showMessageDialog The syntax to use showMessageDialog is JoptionPane.showMessageDialog(null, messageStringExpression, boxTitleString, messageType). messageStringExpression:evaluated and value appears in box boxTitleString: the title of the dialog box messageType: type of icon appearing in the dialog box JoptionPane.ERROR_MESSAGE JoptionPane.INFORMATION_MESSAGE JoptionPane.Question_MESSAGE JoptionPane.PLAIN_MESSAGE JoptionPane.WARNING_MESSAGE