Dialogues and Wrapper Classes

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Chapter 3: Introduction to Objects and Input/Output.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
Starting Out with Java: From Control Structures through Objects
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
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.
Chapter 3 Java Input/Output.
Lecture 2 Objectives Learn about objects and reference variables.
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Dialog Boxes.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
Ihsan Ayyub Qazi Introduction to Computer Programming Recitation 3 Ihsan Ayyub Qazi.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 5.
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.
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
Arithmetic Expressions
Java Programming Lecture 2
3 Introduction to Classes and Objects.
Outline Creating Objects The String Class Packages Formatting Output
Data Types – Reference Types
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Objectives You should be able to describe: Interactive Keyboard Input
2.5 Another Java Application: Adding Integers
Multiple if-else boolean Data
Interactive Standard Input/output
Chapter 3 Java Input/Output.
Message, Input, Confirm, and Specialized Dialogs
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 2 - Introduction to Java Applications
Debugging 9/22/15 & 9/23/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Branching Chapter 5 11/14/16 & 11/15/
Chapter 3: Introduction to Objects and Input/Output
Multiple if-else boolean Data
Chapter 3 Input/Output.
Message, Input, and Confirm Dialogs
Starting Out with Java: From Control Structures through Objects
JOptionPane class.
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Dialogues and Wrapper Classes Chapter 2 - Extra 2/11/16 1 1 1

Today's Topics Use Decimal Format to control output. Use Dialog Boxes for messages and input. Use wrapper classes to convert primitives to objects Autoboxing and unboxing saves conversion work. 2 2 2

Dialogue Boxes Message Dialogue Displays message and OK button to close it. Input Dialogue Prompts user for input. Provides text box for input. OK and Cancel buttons to control it. 3 3 3

JOptionPane Class JOptionPane class allows you to quickly display a dialogue box. Must be imported: import javax.swing.JOptionPane;

Message Dialogue ShowMessageDialog method displays a message dialogue. JoptionPane.showMessageDialog(null, ”Eggs aren't supposed to be green.”);

import javax.swing.JOptionPane; public class EggsMessage { public static void main(String[] args) { JoptionPane.showMessageDialog(null, "Eggs aren't supposed to be green."); } 6 6 6

Input Dialog Inputs data as String, so we need to use a String variable to store it. String name; name = JoptionPane.showInputDialogue (“Enter your name.”);

import javax.swing.JOptionPane; import java.text.DecimalFormat; public class AcreConversion { public static void main(String[] args) { final double SQ_FT_TO_ACRES = 43560.0; String strSqFt; //Input the square feet strSqFt = JOptionPane.showInputDialog("Input Square Feet."); //Convert sq ft to number double sqFt = Double.parseDouble(strSqFt); //Figure acres double acres = sqFt/SQ_FT_TO_ACRES; //Output acres DecimalFormat formatter = new DecimalFormat("00.##"); JOptionPane.showMessageDialog(null, formatter.format(acres) + " acres"); }

import javax.swing.JOptionPane; public class AskForName { public static void main(String[] args) { String name; name = JOptionPane.showInputDialog("Enter your name."); JOptionPane.showMessageDialog(null,"Hey, " + Name + "!"); System.exit(0); }

Numeric Input Input dialog only handles String. Numeric values must be converted. Wrapper classes have methods to convert from String to numeric type. Integer.parseInt(“123”) → 123 Double.parseDouble(“4.56”) → 4.56

Write a Program An acre of land is equivalent to 43,560 square feet of land. Write a program that calculates the number of acres in a tract of land. Use an input dialog to ask the user for the square footage of the land. Use an output dialog to output the result. Could use DecimalFormat class to control output.

Class DecimalFormat Class DecimalFormat enables control of output In java.text package. Specify a string pattern to describe desired format double val = 1.23456; DecimalFormat formatter = new DecimalFormat("0.000"); System.out.println(formatter.format(val)); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Class DecimalFormat 0 – digit, leading and trailing zeros # -- digit, no leading and trailing zeros % -- percentage $ -- at beginning displays dollar sign Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Class DecimalFormat Figure 4-7 Examples of patterns used with class DecimalFormat Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

More on Wrapper Classes A wrapper class is a class that is “wrapped around” a primitive data type. Allows you to create objects instead of primitive variables. Not used a lot.

Wrapper classes Integer for the int type Double for the double type Mostly used for the methods, like Double.parsedouble( )

Autoboxing Can create objects of a wrapper class by passing a primitive to a constructor Integer number = new Integer(7); Another way is to initialize object with the number, autoboxing happens. Integer number; Number = 7;

Unboxing Opposite of autoboxing. Converting wrapper class object to a primitive type. Integer myInt = 5;//Autobox 5 int primitiveNo; primitiveNo = myInt;//Unbox object

Wrapper Classes ArrayList, for example, only takes objects. ArrayList<Integer> list = new ArrayList<Integer>; Can wrap ints to put them on the list. Integer num5 = new Integer(5); list.add(num5); Not really necessary because autoboxing takes care of the conversion. list.add(5);

Participation Write a java program to use an input dialog to ask the user for his/her current annual salary. Give them a 5% raise. Use a message dialog to output the new salary. Use a DecimalFormat to control the look of the output.