Data Types - PowerPoint PPT Presentation

About This Presentation
Title:

Data Types

Description:

Data Types Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 35
Provided by: smith194
Learn more at: http://users.cs.fiu.edu
Category:
Tags: data | types | week

less

Transcript and Presenter's Notes

Title: Data Types


1
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

2
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.

3
Reference Types - Array
  • 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

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

6
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.

7
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)

8
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.

9
Java Standard Package- lang
10
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

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

12
Using with Reference Types
  1. class Test
  2. public static void main(String arg)
  3. String s1 new String("Hello")
  4. String s2 new String("Hello")
  5. System.out.println(s1 s2)

Answer __________________
13
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))

14
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

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

16
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
    users response from a set of options.
  • Input dialog shows a question and gets the users
    input from a text field, a combo box, or list.

17
Enhance Your Output with Scrollable Window
18
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.

19
Using JOptionPane for Input
  1. import javax.swing.JOptionPane
  2. class optionPane
  3. public static void main(String arg)
  4. String str JOptionPane.showInputDia
    log( "Read data )

20
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

21
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.showInputDi
    alog("Read data")
  • double x Double.parseDouble(str)

22
Build a Single Class to be used for Input
  1. import javax.swing.JOptionPane
  2. class GetData
  3. static String str
  4. static double getDouble(String s)
  5. str JOptionPane.showInputDialog(s)
  6. return Double.parseDouble(str)
  7. static int getInt(String s)
  8. str JOptionPane.showInputDialog(s)
  9. return Integer.parseInt(str)
  10. static String getWord(String s)
  11. return JOptionPane.showInputDialog(s)

23
Build a Single Class for Input (Another view)
  1. import javax.swing.JOptionPane
  2. class GetData
  3. static double getDouble(String s)
  4. return Double.parseDouble( getWord(s)
    )
  5. static int getInt(String s)
  6. return Integer.parseInt(getWord(s))
  7. static String getWord(String s)
  8. return JOptionPane.showInputDialog(s)

24
Using the GetData Class
  1. class TestGetData
  2. public static void main(String arg)
  3. int x getData.getInt("Type an
    integer value")
  4. double y getData.getDouble("Type a
    double value")
  5. String name getData.getWord("Enter
    a Name")
  6. System.out.println("Your name is "
    name "\nYour age is "

  7. x "\nYou have " y)

25
Three Dialog Boxes
  • Dialog box when Line 5 is executed.
  • Dialog box when Line 6 is executed.
  • Dialog box when Line 7 is executed.

26
The output

27
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

28
Output Operation
  1. import javax.swing.JOptionPane
  2. class output_pane
  3. public static void main(String arg)
  4. JOptionPane.showMessageDialog(null,
    "Your \noutput string",
  5. "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.

29
Output Operation Only String can be Accommodated
  1. import javax.swing.JOptionPane
  2. class OutputPane
  3. public static void main(String arg)
  4. int x getData.getInt("Type an
    integer value")
  5. double y getData.getDouble("Type
    a double value")
  6. String name getData.getWord("En
    ter a Name")
  7. String s "Your name is "
    name "\nYour age is " x
  8. "\nYou have " y
  9. JOptionPane.showMessageDialog(nul
    l, s, "Personal Data",
  10. JOptionPane.INFORMATION_MESSAGE)

30
Output Operation Only String can be Accommodated

31
Output Operation
  • In this situation the value to be outputed 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.

32
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 avid 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.

33
  1. import javax.swing.JOptionPane
  2. import javax.swing.JTextArea
  3. import javax.swing.JScrollPane
  4. class OutputPane
  5. public static void main(String arg)
  6. int x getData.getInt("Type an
    integer value")
  7. double y getData.getDouble("Type
    a double value")
  8. String name getData.getWord("Enter
    a Name")
  9. String s "Your name is " name
    "\nYour age is " x
  10. "\nYou have "
    y
  11. s s "\nThat other person does
    not remember his name\nnor his age"
  12. s s "\nlet alone how much money
    he has"
  13. JTextArea text new JTextArea(s,
    10, 20)
  14. JScrollPane pane new
    JScrollPane(text)
  15. JOptionPane.showMessageDialog(null,
    pane, "Personal Data",

  16. JOptionPane.INFORMATION_MESSAGE)

34
Using a Scrollable Pane
Write a Comment
User Comments (0)
About PowerShow.com