Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.

Slides:



Advertisements
Similar presentations
Chapter 2: Using Data.
Advertisements

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.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMP 14 Introduction to Programming Miguel A. Otaduy May 17, 2004.
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 
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
COMP 14 Introduction to Programming Mr. Joshua Stough February 2, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 29, 2005.
IC211 Lecture 8 I/O: Command Line And JOptionPane.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
Static Class Members Wrapper Classes Autoboxing Unboxing.
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
Chapter 3 Introduction to Object-Oriented Programming: Using Classes.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
Topics Class Basics and Benefits Creating Objects Using Constructors Calling Methods Using Object References Calling Static Methods and Using Static Class.
 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.
Introduction to Programming
Chapter 3 Java Input/Output.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
Lecture 2 Objectives Learn about objects and reference variables.
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
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.
Review and Java Questions 13 June Classes, objects, attributes, methods In Java, by convention a class name begins with Uppercase: Coffee, String.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSC 142 H 1 CSC 142 Standard input/output. CSC 142 H 2 Console Ouput  Use the static PrintStream object out in System System.out.println("Hello, world");
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by.
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.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
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.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
Chapter 3 Introduction to Object-Oriented Programming: Using Classes.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
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.
Java Programming Lecture 2
Formatting Output & Enumerated Types & Wrapper Classes
Outline Creating Objects The String Class Packages Formatting Output
Objectives You should be able to describe: Interactive Keyboard Input
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Dialogues and Wrapper Classes
Chapter 3 Java Input/Output.
Classes, Libraries & Packages
Console Input and Output
Message, Input, Confirm, and Specialized Dialogs
Chapter 2 - Introduction to Java Applications
Chapter 3: Introduction to Objects and Input/Output
Chapter 3 Input/Output.
Message, Input, and Confirm Dialogs
Presentation transcript:

Using Classes BCIS 3680 Enterprise Programming

Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane classes  Format output: Decimal Format, NumberFormat classes  Handling Input: Wrapper classes, e.g., Integer, Double  Processing: Math class

Organization of Classes 3  Related classes are organized into packages.  Java SE comes with a number of commonly used packages.  To use Class A in Class B, you need to have an import statement for Class A at the beginning of Class B.  The only exception is the classes in the java.lang package, which can be used by all other classes without an import java.lang; statement.  Two ways to write the import statement:  Specifically import the name of the particular class you want to use, e.g., import java.util.Scanner;.  Import the entire package the class belongs to, e.g., import java.util.*;.  All other classes in that package are accessible now.

Packages of “Premade” Classes 4  Classes are grouped in packages according to functionality by using the package keyword.  The following packages ship with the Java language: PackageCategories of Classes java.langBasic functionality common to many programs, such as the String class and Math class java.awtGraphics classes for drawing and using colors javax.swingUser-interface components java.textClasses for formatting numeric output java.utilThe Scanner class, the Random class and other miscellaneous classes java.ioClasses for reading from and writing to files

The System Class 5  The System class is in the java.lang package, so it does not need to be imported.  Two useful static constants (variables with constant value) of the System class:  in represents the standard input device (the keyboard by default).  out represents the standard output device (the Java console by default).  Examples: Scanner scan = new Scanner(System.in); System.out.println("Hello");

Using System.out Example: System.out.print("The answer is "); System.out.println(3); The output is: The answer is 3 Return typeSignature voidprint( anyDataType argument ) prints argument to the standard output device voidprintln( anyDataType argument ) prints argument to the standard output device followed by a newline character

Using Dialog Boxes 7  The JOptionPane class is in the javax.swing package.  You must import it.  Static methods are provided for input and output dialog boxes.  For input dialog boxes, return value is a String, so numeric input needs to be converted (using methods such as parseInt() or parseDouble() ).

JOptionPane static Methods 8 Return valueSignature StringshowInputDialog( Component parent, Object prompt, String title, int messageType ) pops up an input dialog box, where prompt asks the user for input. voidshowMessageDialog( Component parent, Object message, String title, int messsageType ) pops up an output dialog box with message displayed.

Message Dialog Box 9  Displays a message and then waits until the user press OK: showMessageDialog(Component parent, Object message, String title, int messageType)  parent – use null for projects  mesage – the message you want to display. A string is fine as it is an object.  title – the text you want to appear on the title bar.  messageType – indicates the nature of the message box. It is of the int type but you don’t have to enter an integer. Instead, you may enter one of the following easy-to-read words and Java will translate it into an integer for you.  JOptionPane.INFORMATION_MESSAGE: Standard info icon is used.  JOptionPane.PLAIN_MESSAGE: No icon is displayed.  JOptionPane.QUESTION_MESSAGE: Question mark icon is displayed.  JOptionPane.WARNING_MESSAGE: Warning icon is displayed.

Input Dialog box 10  Displays a displays a text field into which the user can enter a string.  showInputDialog(Component parent, Object prompt, String title, int messsageType)  parent – use null for projects  prompt – the prompt you want to display. A string is fine as it’s an object.  title – the text you want to appear on the title bar.  messageType – indicates the nature of the message box. The options are:  JOptionPane.ERROR_MESSAGE  JOptionPane.INFORMATION_MESSAGE  JOptionPane.PLAIN_MESSAGE  JOptionPane.QUESTION_MESSAGE  JOptionPane.WARNING_MESSAGE

Formatting Numeric Output 11  The DecimalFormat and NumberFormat classes allow you to specify the display format of numbers - number of digits, number of places after the decimal, formatting elements such as dollar signs and percent signs, etc.  Both classes are in the java.text package.  Importing is needed.  Usage pattern:  Create a formatting object (either a DecimalFormat or a NumberFormat object).  Call the format method of the formatting object and pass the number to be formatted as the argument.  A formatting object may be used to format multiple numbers.  If numbers are to be formatted in different ways, then multiple formatting objects are needed.

DecimalFormat Class 12 Pattern characters: 0 required digit # optional digit, suppress if 0. decimal point, comma separator % multiply by 100 and display a percent sign DecimalFormat Constructor DecimalFormat( String pattern ) instantiates a DecimalFormat object with the format specified by pattern

The format( ) Method 13 Return typeSignature Stringformat( double number ) returns a formatted String representation of number

The Wrapper Classes 14  There are occasions when you want to work with a number as an object.  Numbers are primitive types. They are not objects.  Wrapper classes resolve the problem because they are objects so can be handled as such.  In the “core” of each wrapper object is a numeric value.

Wrapper Classes 15  Remember the naming convention:  Primitive types start with a lowercase letter; Class names start with an uppercase letter. Primitive Data TypeWrapper Class doubleDouble floatFloat longLong intInteger shortShort byteByte charCharacter booleanBoolean

Using Wrapper Classes 16  A handy use of wrapper class is a static method for converting textual input into numbers.  Usually, when a program obtains input from users, the input is read as strings. Even when the user enters a number, it’s still a string (of characters that stand for numbers).  Despite looking numeric ostensibly, such strings cannot be used in mathematic operations. They must be converted into primitive types first.  To perform the conversion, call the respective parseX() method of the wrapper class.  To convert a string into an integer, call parseInt().  To convert a string into a double, call parseDouble().

Integer Static Methods 17 Return valueSignature intparseInt( String s ) returns the String s as an int IntegervalueOf( String s ) returns the String s as an Integer object

Double Static Methods 18 Return valueSignature doubleparseDouble( String s ) returns the String s as a double DoublevalueOf( String s ) returns the String s as a Double object

Reading Numeric Input 19  The showInputDialog() method returns a string.  To convert the string to a numeric type, use the wrapper class methods.  Example: String input = JOptionPane.showInputDialog(null, "Please enter your age in years.", "Get Input", JOptionPane.QUESTION_MESSAGE); int age = Integer.parseInt(input);

Math Class 20  The Math class provides static constants and static methods for performing common calculations.  The Math class is in the java.lang package, so it does not need to be imported.  Methods in the Math class are static.

Methods of the Math Class 21 Return typeSignature dataTypeOfArgabs( dataType arg ) returns the absolute value of the argument arg, which can be a double, float, int or long. doublelog( double a ) returns the natural logarithm (in base e) of its argument. doublesqrt( double a ) returns the positive square root of a doublepow( double base, double exp ) returns the value of base raised to the power exp

The round() Method 22  Rounding rules:  Any factional part <.5 is rounded down  Any fractional part.5 and above is rounded up Return typeSignature longround( double a ) returns the closest integer to its argument a

The min() and max() Methods 23 Find smallest of three numbers: int smaller = Math.min( num1, num2 ); int smallest = Math.min( smaller, num3 ); Return typeSignature dataTypeOfArgsmin( dataType a, dataType b ) returns the smaller of the two arguments. The arguments can be doubles, floats, ints, or longs. dataTypeOfArgsmax( dataType a, dataType b ) returns the larger of the two arguments. The arguments can be doubles, floats, ints, or longs.