Data Types – Reference Types

Slides:



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

Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
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.
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.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
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
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
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.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
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.
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.
 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.
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.
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.
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.
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.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
 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.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
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.
Sixth Lecture ArrayList Abstract Class and Interface
Java Programming Lecture 2
Formatting Output & Enumerated Types & Wrapper Classes
3 Introduction to Classes and Objects.
Outline Creating Objects The String Class Packages Formatting Output
Introduction to Classes and Objects
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
“Form Ever Follows Function” Louis Henri Sullivan
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
Message, Input, Confirm, and Specialized Dialogs
INPUT STATEMENTS GC 201.
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Chapter 3: Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
Chapter 3: Introduction to Objects and Input/Output
Chapter 3 Input/Output.
Message, Input, and Confirm Dialogs
Introduction to Java Programming
Outline Creating Objects The String Class The Random and Math Classes
JOptionPane class.
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To differentiate between Java defined types and user defined types To have a deeper understanding of the String class To get acquainted with the Math class To get acquainted with the Wrapper classes

Data Types – Reference Types Reference types - any data type that is composed of primitive data types as its based type. In other words, it is an aggregate of primitive types. Reference types in Java are: The array data structure, and The class data type. In this section we will briefly discuss the concept of array. We will discuss the class type to the extent of the fundamental classes of Java.

Reference Types - Array An array is a set of storage locations set aside to hold one type of data. It is simply a means where by we can store values of the same type by using one generic name. The list of items are stored linearly Hence the items can be accessed by their relative position in the list. Arrays are real objects in Java. Storage space is allocated for an array during execution time. The concept will be discussed fully in future lesson

Reference Types - Array Data arr 10 20 30 40 50 60 70 80 2 1 3 4 5 6 7 indeces This is the most we will say about arrays for now

Reference Types - Class The concept of class as you know is the fundamental construct upon which Java is built. A class serves as a blueprint or a template for a certain type of object. All data types other than the primitive type or the array must be addressed in terms of class. This is evident in the way that we have been using class. For instance, going back to the class Book. In order to use this class we had to declare variables of the type: That is, Book b1; for instance

Reference Types - Standard Packages Java has a vast and rich collection of classes. These classes are stored in respective directories and subdirectories. Each directory or subdirectory is called, a package. Programmers can use these classes in their specific applications. There are several packages in Java. Two of the most important packages in the language are java and javax.

Java Standard Packages java and javax packages, along with their sub-packages. Some of the sub-packages also contain sub-packages of their own The sub-package lang is a special package is for general programming The sub-package javax is designed for Graphical User Interface programming (GUI)

Java Standard Package- lang The java.lang package contains classes that are fundamental to Java. All of the classes in this package are available in your program automatically. That is, as soon as the Java compiler is loaded, all of these classes are loaded into every Java source file.

Java Standard Package- lang

Java Standard Package- lang Chief among these fundamental classes are the following: Object String Math System The wrapper classes - for each primitive type there is a corresponding class. Byte Short Integer Long Character Float Double Boolean Void

The class Object See the Application Programmers’ Interface (API) Object String Math Wrapper classes - Byte, Short, Integer, Long, Character, Float, Double, Boolean, and Void.

Using == with Reference Types class Test { public static void main(String[] arg) String s1 = new String("Hello"); String s2 = new String("Hello"); System.out.println(s1 == s2); } Answer: __________________

Using == with Reference Types class Test { public static void main(String[] arg) String s1 = new String("Hello"); String s2 = new String("Hello"); System.out.println(s1.equals(s2)); }

Input and Output Operations There are three other important operations that are performed on data. Input operations Output operations, and Formatting operations. We have been generating outputs already but it was not dealt with formally. Input Operations - There are several ways to input data into a program: Java Input classes The DOS prompt The Scanner Class JOptionPane Class

JOptionPane Input/Output Dialog Boxes Capabilities Create input dialog boxes Create output windows

JOptionPane Input Dialog Boxes There are four kinds of standard dialogs: Message dialog shows a message and waits for the user to click OK. Confirmation dialog shows a question and ask for confirmation such as OK or Cancel. Option dialog shows a question and gets the user’s response from a set of options. Input dialog shows a question and gets the user’s input from a text field, a combo box, or list.

Enhance Your Output with Scrollable Window

JOptionPane – Input Dialog You must import the class JOptionPane import javax.swing.JOptionPane; The class contains: Several constructors, and Several class methods that deals with dialog boxes. The general construct of these methods is as follows: showXXXXDialog( parameter_list) Where XXXX is any of the four dialog box types. We will only be concerned with the input dialog type.

Using JOptionPane for Input import javax.swing.JOptionPane; class optionPane { public static void main(String[] arg) String str = JOptionPane.showInputDialog( "Read data” ); }

Using JOptionPane for Input Type the value 123.45 This value is returned as a String String str = JOptionPane.showInputDialog( "Read data” ); That is, str = “123.45”

Using the Wrapper class - Double Convert the string value to a double by using the parseDouble method import javax.swing.JOptionPane; class convert_string { public static void main(String[] arg) String str = JOptionPane.showInputDialog("Read data"); double x = Double.parseDouble(str); }

Build a Single Class to be used for Input import javax.swing.JOptionPane; class GetData { static String str; static double getDouble(String s) str = JOptionPane.showInputDialog(s); return Double.parseDouble(str); } static int getInt(String s) return Integer.parseInt(str); static String getWord(String s) return JOptionPane.showInputDialog(s);

Build a Single Class for Input (Another view) import javax.swing.JOptionPane; class GetData { static double getDouble(String s) return Double.parseDouble( getWord(s) ); } static int getInt(String s) return Integer.parseInt(getWord(s)); static String getWord(String s) return JOptionPane.showInputDialog(s);

Using the GetData Class class TestGetData { public static void main(String[] arg) int x = getData.getInt("Type an integer value"); double y = getData.getDouble("Type a double value"); String name = getData.getWord("Enter a Name"); System.out.println("Your name is: " + name + "\nYour age is: " + x + "\nYou have $" + y); }

Three Dialog Boxes Dialog box when Line 5 is executed.

The output

Output Operation We have seen output operations using System.out.println() Now we will look at output operations using the JOPtionPane class. We use the showMessageDialog method to out result. The general for of the showMessageDialog is shown in Figure below

Output Operation import javax.swing.JOptionPane; class output_pane { public static void main(String[] arg) JOptionPane.showMessageDialog(null, "Your \noutput string", "Your title", JOptionPane.INFORMATION_MESSAGE); } Notice the keywords, constants and features: null signifies that this dialog box stands alone. Your \noutput string - The string to be displayed in the message dialog box. Your title – The title you want to give to the output. JOptionPane.INFORMATION_MESSAGE – This specifies the type of dialog box. Notice the letter cases. They must be written as shown here.

Output Operation – Only String can be Accommodated import javax.swing.JOptionPane; class OutputPane { public static void main(String[] arg) int x = getData.getInt("Type an integer value"); double y = getData.getDouble("Type a double value"); String name = getData.getWord("Enter a Name"); String s = "Your name is: " + name + "\nYour age is: " + x + "\nYou have $" + y; JOptionPane.showMessageDialog(null, s, "Personal Data", JOptionPane.INFORMATION_MESSAGE); }

Output Operation – Only String can be Accommodated

Output Operation In this situation the value to be outputted must be a string. This means that you must first construct the string before calling the method. This requires you to: Know the format of the output ahead of time, and Be able to use the string concatenation features along with tabs, new line, and space to make the entire string.

Format Decimal values In order to format decimal values do the following: Import the following classes: java.text.NumberFormat; java.text.DecimalFormat; Setup the format pattern: NumberFormat nf = NumberFormat.getInstance(); DecimalFormat df = (DecimalFormat)nf; df.applyPattern("#,##0.0000; -#,##0.0000");

import java.text.NumberFormat; import java.text.DecimalFormat; import javax.swing.JOptionPane; class Format { public static void main(String arg[]) double pi = Math.PI; String s = "Value of PI \nBefore formatting: " + pi; NumberFormat nf = NumberFormat.getInstance(); DecimalFormat df = (DecimalFormat)nf; df.applyPattern("#,##0.0000; -#,##0.0000"); s = s + "\nAfter formatting: " + df.format(pi); display(s); } static void display(String s) JOptionPane.showMessageDialog(null, s, "Format numbers", JOptionPane.INFORMATION_MESSAGE);

Output

Using a Scrollable Pane The above method has a major disadvantage A very long string will create a long the dialog box. Some data maybe lost. To avoid this from happening, do the following: Place the string (text) in a JTextArea object Place the JTextArea in a JScrollPane object Finally, place the JScrollPane object in the showMessageDialog method of the JOptionPane class.

import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JScrollPane; class OutputPane { public static void main(String[] arg) int x = getData.getInt("Type an integer value"); double y = getData.getDouble("Type a double value"); String name = getData.getWord("Enter a Name"); String s = "Your name is: " + name + "\nYour age is: " + x + "\nYou have $" + y; s = s + "\nThat other person does not remember his name\nnor his age"; s = s + "\nlet alone how much money he has"; JTextArea text = new JTextArea(s, 10, 20); JScrollPane pane = new JScrollPane(text); JOptionPane.showMessageDialog(null, pane, "Personal Data", JOptionPane.INFORMATION_MESSAGE); }

Using a Scrollable Pane