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.

Slides:



Advertisements
Similar presentations
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Java: How to Program Methods Summary Yingcai Xiao.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
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.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3: Introduction to Objects and Input/Output.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
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.
Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Using Classes BCIS 3680 Enterprise Programming. Overview 2  Using Classes  Using premade classes for input and output  Display output: System, JOptionPane.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and 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.
Object Oriented Programming Object and Classes Lecture 3 MBY.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
3 Introduction to Classes and Objects.
Outline Creating Objects The String Class Packages Formatting Output
Data Types – Reference Types
Introduction to Classes and Objects
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Yanal Alahmad Java Workshop Yanal Alahmad
AP Java 10/4/2016.
2.5 Another Java Application: Adding Integers
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
Chapter 3: Introduction to Objects and Input/Output
AP Java 10/4/2016.
Introduction to Classes and Objects
Using java libraries CGS3416 spring 2019.
JOptionPane class.
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

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 output –Package is javax.swing – contains classes for creation of GUIs. –JOptionPane class provides prepackaged dialog boxes called message dialogs –showMessageDialog() – displays a dialog box displaying a message. Two parameters, position of the dialog box and the message.

Displaying Text in a Dialog Box // Printing multiple lines in dialog box. import javax.swing.JOptionPane; // import class JOptionPane public class Dialog1 { public Dialog1() { // display a dialog with the message JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); } public static void main( String args[] ) { new Dialog1(); } // end main } // end class Dialog1 Import class JOptionPane Show a message dialog with text

Displaying Text in a Dialog Box showMessageDialog() – displays a dialog box displaying a message. Two parameters, position of the dialog box and the message. showMessageDialog() is a class method of JOptionPane class, it is a static method. Such methods often define frequently used tasks that do not explicitly require the creation of an object. A static method typically is called by using its class name dot operator and the method name. JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); Note we did not have to create an instance of the class JoptionPane to use the showMessageDialog() method.

Entering Text in a Dialog Box Input dialog –Allows user to input information –Created using method showInputDialog from class JOptionPane

Entering Text in a Dialog Box // Basic input with a dialog box. import javax.swing.JOptionPane; public class NameDialog { public NameDialog() { // prompt user to enter name String name =JOptionPane.showInputDialog( "What is your name?" ); // create the message String message = String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); } public static void main( String args[] ) { new NameDialog(); } } // end class NameDialog

Entering Text in a Dialog Box showInputDialog() – parameter is the prompt The user types characters in the text field, – User enters OK - showInputDialog() returns a String. – User enters Cancel - showInputDialog() returns null static String method format() –returns a formatted String similar to printf

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 output –Package is javax.swing – contains classes for creation of GUIs. –JOptionPane class provides prepackaged dialog boxes called message dialogs –showMessageDialog() – displays a dialog box displaying a message. Two parameters, position of the dialog box and the message.

Displaying Text in a Dialog Box // Printing multiple lines in dialog box. import javax.swing.JOptionPane; // import class JOptionPane public class Dialog1 { public Dialog1() { // display a dialog with the message JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); } public static void main( String args[] ) { new Dialog1(); } // end main } // end class Dialog1 Import class JOptionPane Show a message dialog with text

Displaying Text in a Dialog Box showMessageDialog() – displays a dialog box displaying a message. Two parameters, position of the dialog box and the message. showMessageDialog() is a class method of JOptionPane class, it is a static method. Such methods often define frequently used tasks that do not explicitly require the creation of an object. A static method typically is called by using its class name dot operator and the method name. JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" ); Note we did not have to create an instance of the class JoptionPane to use the showMessageDialog() method.

Entering Text in a Dialog Box Input dialog –Allows user to input information –Created using method showInputDialog from class JOptionPane

Entering Text in a Dialog Box // Basic input with a dialog box. import javax.swing.JOptionPane; public class NameDialog { public NameDialog() { // prompt user to enter name String name =JOptionPane.showInputDialog( "What is your name?" ); // create the message String message = String.format( "Welcome, %s, to Java Programming!", name ); // display the message to welcome the user by name JOptionPane.showMessageDialog( null, message ); } public static void main( String args[] ) { new NameDialog(); } } // end class NameDialog

Entering Text in a Dialog Box showInputDialog() – parameter is the prompt The user types characters in the text field, – User enters OK - showInputDialog() returns a String. – User enters Cancel - showInputDialog() returns null static String method format() –returns a formatted String similar to printf

Exercises Adapt our Hello World and Leap Year programs to include Swing Input and Output

Java packages Java Packages available with the JDK –java.io – for system input and output –java.math – for integer and decimal arithmetic –java.text - for handling text, dates, numbers … –java.util – for collections, date & time facilities also new class Scanner for Data input –java.net – for implementing networking apps –java.sql – for accessing data in a data source –and many more…

Using Math Class Math Class is in the package java.lang All methods of the Math class are class methods Must use the class name to invoke the methods –Math.round(x), Math.pow(x,y), Math.sqrt(y) – … A utility type class A class field is associated with the class in which it is defined rather than with each instance of the class

Class fields and class methods A class field is associated with the class in which it is defined rather than with each instance of the class. –public static final double PI = The static modifier says that the field is a class field. The final modifier indicates that the value in the field does not change ie a constant

Class fields and class methods A class methods are declared with the static modifier, they are associated with the class, rather than the instance. Public static double radinsToDegrees(double rads) { return rads *180/PI;} System.out.println(c+"\t"+ Math.round(c*9.0/5 + 32)); A class method can use any class fields and other class methods of its own class.

Using Math Class All methods of the Math class are class methods Must use the class name to invoke the methods –Math.round(x), Math.pow(x,y), Math.sqrt(y) – … A utility type class A class field is associated with the class in which it is defined rather than with each instance of the class

Class fields and class methods A class field is associated with the class in which it is defined rather than with each instance of the class. –public static final double PI = The static modifier says that the field is a class field. The final modifier indicates that the value in the field does not change ie a constant

Class fields and class methods A class methods are declared with the static modifier, they are associated with the class, rather than the instance. Public static double radinsToDegrees(double rads) { return rads *180/PI;} System.out.println(c+"\t"+ Math.round(c*9.0/5 + 32)); A class method can use any class fields and other class methods of its own class.

static Methods in Class Math static method (or class method) –Applies to the class as a whole instead of a specific object of the class –Call a static method by using the method call: ClassName.methodName( arguments ) –All methods of the Math class are static example: Math.sqrt( )

TemperatureTable class TemperatureTable { TemperatureTable() { System.out.println("Temperature Conversion Table"); System.out.println("============================"); System.out.println(); System.out.println("C\tF"); for (int c = 5; c <= 20; c++) { System.out.println(c+"\t"+ Math.round(c*9.0/5 + 32)); } } public static void main ( String[] args) { new TemperatureTable (); } }

static Fields and Class Math Constants –Keyword final –Cannot be changed after initialization static fields (or class variables) –Are fields where one copy of the variable is shared among all objects of the class Math.PI and Math.E are final static fields of the Math class

Math class methods.

static Method main Method main –main is declared static so it can be invoked without creating an object of the class containing main –Any class can contain a main method The JVM invokes the main method belonging to the class specified by the first command-line argument to the java command

Account example Account class –Interest rate – have the same interest rate for all accounts –Declare it as a class field / attribute public class Account { private int accountno; private String name; private String address; private double balance; static double interestrate = 3.5; //class field static void changeRate(double rate){ // class method interestrate = rate;} –To change the interest rate, we need to create a class method to update it.

Account example AccountTest class –To change the interest rate, we use the class method. –Use the Class name. Class method name Account.changeRate(4.5); //class method How would you update the account class to automatically allocate a new account number for each account created.

Examples of Vectors

import java.io.*; import java.util.*; class VectorApp{ public static void main(String[] args){ Vector v = new Vector(20); Integer j = null; int i;

System.out.println("Generating random integers between 1 and 100, wrapping it as integer objects and adding to a vector object"); for(i=0;i<10;i++){ j = new Integer((int)(Math.random() * 100)); v.addElement(j); System.out.println("Added Integer object elements " + j); } System.out.println("size of the vector "+v.size()); System.out.println("capacity of the vector "+v.capacity()); Enumeration enum = v.elements(); while(enum.hasMoreElements()) System.out.println("Enumeration of the elements in the vector container "+(Integer)enum.nextElement()); System.out.println(""); }