Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

Introduction to Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
Chapter 2 - Introduction to Java Applications
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.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
● Basic Computation Recitation – 09/(04,05)/2008 CS 180 Department of Computer Science, Purdue University.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.
Chapter Chapter 2 Getting Started with Java.
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
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction to scripting
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.
A First Program Using C#
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
Strings & things Introduction to the Java API. The Java API API = Applications Programming Interface –Rich set of predefined classes and objects for use.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
Introduction to Programming with RAPTOR
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
Introduction to Programming
XP Tutorial 8 Adding Interactivity with ActionScript.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
SE-1020 Dr. Mark L. Hornick 1 Graphical User Interfaces.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
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.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
2-1 Types of data  Java has two types of data  primitive types that store one value char boolean byte int long float double  reference types to store.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Classes and Objects
2.5 Another Java Application: Adding Integers
Section 64 – Manipulating Data Using Methods – Java Swing
Message, Input, Confirm, and Specialized Dialogs
Variables and Arithmetic Operations
Chapter 2 - Introduction to Java Applications
Message, Input, and Confirm Dialogs
Introduction to Java Programming
Getting Started with Java
JOptionPane class.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Programming with Java standard classes

Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into your programs – readymade solutions for a variety of tasks Detailed documentation of Java standard classes freely available on web:

JOptionPane: an I/O class Output: display something on an output device (i.e. the computer’s monitor) –display results of computation –communicate with the user Most Java programs employ a graphical user interface (GUI) with two types of windows: –frame window: the type of window created by our first example program –dialog window: allows communication with user; most commonly used to prompt for & receive input, but we’ll begin by looking at just output

JOptionPane output example Displays a small window in the center of the screen containing the words in the string literal and a button labeled “OK” This is an example of a message that calls a class method of the JOptionPane class – we are not making a request to an object, but rather to the class itself JOptionPane.showMessageDialog(null, “Look at me!”);

JOptionPane.showMessageDialog example Arguments to the method: –the first argument, null, indicates that there is no existing frame object upon which this dialog should appear; if we want the dialog to appear in an existing frame window, we would pass the name of the frame object as the first argument, instead of null – for example: JFrame myWindow; … // review: what goes here? JOptionPane.showMessageDialog (myWindow, “It’s my window”); –the second argument, a string literal, indicates the text we want to display in the window

Example program // Sample program #2: displaying messages // using JOptionPane.showMessageDialog import javax.swing.*; class Example2 { public static void main (String [ ] args) { JFrame myWindow; myWindow = new JFrame(); myWindow.setSize (300, 200); myWindow.setVisible(true); JOptionPane.showMessageDialog(myWindow, “It’s my window”); JOptionPane.showMessageDialog(null, “and I’ll cry \n if I want to”); }

Some notes on the example Displaying multiple lines of text: the special character ‘\n’ represents the control character you get when you press the Enter key on the keyboard (n = new line) Later on, we’ll introduce another JOptionPane class method that allows us to take keyboard input from the user

The String class We have already seen several instances of string literal values – a.k.a. string constants – that is, sets of characters enclosed within double quotation marks The Java API contains the String class, which allows us to manipulate string data through the use of String objects

The String class We can declare and create a String object just like we do other types of objects, for example: String bigRiver = new String (“Mississippi”); In fact, the String class is an exception to the “new” rule – you can create a String object without it, as in this example: String bigRiver = “Amazon”; But you may find it less confusing to stick to the form you know for other classes, like the first example, above Bear in mind, though, that it is quite common to assign a new string literal to a String object without using the new operator, as in the following lines of code: String bigRiver = new String (“Amazon”); … // some other stuff happens bigRiver = “Mississippi”;

Operations on String objects The Java API defines many operations on String objects: we will review just a few of them here: substring: takes 2 arguments representing the beginning and ending positions of a String within a String – returns the resulting substring –Note that the first position in a String in Java is designated position 0 – so a 4-letter word would start at 0 and end at 3 –An error will result if you attempt to call the method using positions that don’t exist within the String object

Examples using substring If the String object bigRiver contains the literal value “Mississippi”: bigRiver.substring(6, 8); // returns “sip” bigRiver.substring (0, 2); // returns “Mis” bigRiver.substring (4, 5); // returns “is”

Examples using substring The method calls in the example would return the literal values indicated, but they would neither be stored nor displayed anywhere – therefore, these method calls would usually occur within the context of an assignment statement or another method call; for example: String sub = new String ( bigRiver.substring(6, 8)); // returns “sipp” and assigns it to new object sub JOptionPane.showMessageDialog(bigRiver.substring (4, 5)); // displays “is” in a small window with an OK button

More String methods The length method returns the length (in characters) of the String object; for example, if String bigRiver contains the value “Mississippi” then bigRiver.length() // returns 11 The indexOf method returns a number indicating the position of the beginning of the first occurrence of the substring specified in the message’s argument; examples: bigRiver.indexOf(“Miss”) // returns 0 bigRiver.indexOf(“is”) // returns 1 bigRiver.indexOf(“sis”) // returns 3

String concatenation String concatenation: an operation that makes one String from two, using the ‘+’ operator; for example: String phrase, word; phrase = new String (“old man river”); word = new String (“ that ”); phrase = phrase + word + phrase; // phrase now contains “old man river that old man river”

Program example import javax.swing.*; class Ch2StringProcessing { public static void main( String[] args ) { String fullName, firstName, lastName, space; fullName = new String("Decafe Latte"); space = new String(" "); firstName = fullName.substring(0, fullName.indexOf(space)); lastName = fullName.substring(fullName.indexOf(space) + 1, fullName.length()); JOptionPane.showMessageDialog(null, "Full Name: " + fullName); JOptionPane.showMessageDialog(null, "First: " + firstName); JOptionPane.showMessageDialog(null, "Last: " + lastName); JOptionPane.showMessageDialog(null, "Your last name has "+lastName.length( )+" characters."); }

Reading input In order to receive input data from a user, we need two things: –a mechanism by which to read the data –a place to store the data We can use another method, showInputDialog, of the JOptionPane class for the first part; we can use a String object for the second part.

Using showInputDialog for reading input The syntax for showInputDialog is almost identical to the previous JOptionPane method we looked at, showMessageDialog. You may recall from a previous example program the following lines of code: JOptionPane.showMessageDialog(myWindow, “It’s my window”); JOptionPane.showMessageDialog(null, “and I’ll cry \n if I want to”); In general, the syntax for both showMessageDialog and showInputDialog is: JOptionPane.methodName (WindowObject, MessageObject);

I/O dialog windows We know from the examples that the first argument, the WindowObject, can either be a JFrame object we have declared and initialized or null; we also know that the second argument can be a String literal value. The difference between the two methods is that showMessageDialog merely displays a window containing the specified message, but showInputDialog method displays the message as a prompt, followed by a space for the user to enter input.

Example: showInputDialog The following code fragment produces a dialog window like the one shown below: String daddy; daddy = JOptionPane.showInputDialog (null, “Who’s your daddy?”);