Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference...
Transcript of Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference...
![Page 1: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/1.jpg)
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
![Page 2: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/2.jpg)
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.
![Page 3: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/3.jpg)
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
![Page 4: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/4.jpg)
Reference Types - Array
10 20 30 40 50 60 70 80
arr
indeces
0 1 2 3 4 5 6 7
Data
This is the most we will say about arrays for now
![Page 5: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/5.jpg)
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
![Page 6: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/6.jpg)
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.
![Page 7: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/7.jpg)
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)
![Page 8: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/8.jpg)
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.
![Page 9: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/9.jpg)
Java Standard Package- lang
![Page 10: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/10.jpg)
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
![Page 11: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/11.jpg)
The class Object
• See the Application Programmers’ Interface (API)
• Class
• String
• Math
• Wrapper classes - Byte, Short, Integer, Long, Character, Float, Double,
Boolean, and Void.
![Page 12: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/12.jpg)
Using == with Reference Types
1. class Test
2. {
3. public static void main(String[] arg)
4. {
5. String s1 = new String("Hello");
6. String s2 = new String("Hello");
7.
8. System.out.println(s1 == s2);
9. }
10. }
Answer: __________________
![Page 13: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/13.jpg)
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/Output classes
The DOS prompt
The Scanner Class
JOptionPane Class
![Page 14: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/14.jpg)
JOptionPane Input/Output Dialog Boxes
Capabilities
1. Create input dialog boxes
2. Create output windows
![Page 15: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/15.jpg)
JOptionPane Input Dialog Boxes
There are four kinds of standard dialogs:
1. Message dialog shows a message and waits for the user to click OK.
2. Confirmation dialog shows a question and ask for confirmation such as OK
or Cancel.
3. Option dialog shows a question and gets the user’s response from a set of
options.
4. Input dialog shows a question and gets the user’s input from a text field, a
combo box, or list.
![Page 16: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/16.jpg)
Enhance Your Output with Scrollable Window
![Page 17: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/17.jpg)
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.
![Page 18: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/18.jpg)
Using JOptionPane for Input
1. import javax.swing.JOptionPane;
2. class optionPane
3. {
4. public static void main(String[] arg)
5. {
6. String str = JOptionPane.showInputDialog( "Read data” );
7. }
8. }
![Page 19: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/19.jpg)
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”
![Page 20: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/20.jpg)
Using the Wrapper class - Double
• Convert the string value to a double by using the parseDouble method
1. import javax.swing.JOptionPane;
2. class convert_string
3. {
4. public static void main(String[] arg)
5. {
6. String str = JOptionPane.showInputDialog("Read data");
7. double x = Double.parseDouble(str);
8. }
9. }
![Page 21: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/21.jpg)
Build a Single Class for Input
1. import javax.swing.JOptionPane;
2. class GetData
3. {
4. static String str;
5. static double getDouble(String s)
6. {
7. str = JOptionPane.showInputDialog(s);
8. return Double.parseDouble(str);
9. }
10. static int getInt(String s)
11. {
12. str = JOptionPane.showInputDialog(s);
13. return Integer.parseInt(str);
14. }
15. static String getWord(String s)
16. {
17. return JOptionPane.showInputDialog(s);
18. }
19. }
![Page 22: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/22.jpg)
Build a Single Class for Input (Another view)
1. import javax.swing.JOptionPane;
2. class GetData
3. {
4. static double getDouble(String s)
5. {
6. return Double.parseDouble( getWord(s) );
7. }
8. static int getInt(String s)
9. {
10. return Integer.parseInt(getWord(s));
11. }
12. static String getWord(String s)
13. {
14. return JOptionPane.showInputDialog(s);
15. }
16. }
![Page 23: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/23.jpg)
Using the GetData Class
1. import java.text.NumberFormat;
2.
3. class TestGetData
4. {
5. public static void main(String[] arg)
6. {
7. NumberFormat nf = NumberFormat.getCurrencyInstance();
8. int x = GetData.getInt("Type an integer value");
9. double y = GetData.getDouble("Type a double value");
10. String name = GetData.getWord("Enter a Name");
11. System.out.println("Your name is: " + name + "\nYour age is: "
12. + x + "\nYou have " + nf.format(y));
13. }
14. }
![Page 24: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/24.jpg)
yy
• Dialog box when Line 5 is executed.
• Dialog box when Line 6 is executed.
• Dialog box when Line 7 is executed.
![Page 25: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/25.jpg)
The output
• The output
![Page 26: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/26.jpg)
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
![Page 27: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/27.jpg)
Output Operation
1. import javax.swing.JOptionPane;
2. class output_pane
3. {
4. public static void main(String[] arg)
5. {
6. JOptionPane.showMessageDialog(null, "Your \noutput string",
7. "Your title", JOptionPane.INFORMATION_MESSAGE);
8. }
9. }
Notice the keywords, constants and features:
1. null signifies that this dialog box stands alone.
2. Your \noutput string - The string to be displayed in the message dialog box.
3. Your title – The title you want to give to the output.
4. JOptionPane.INFORMATION_MESSAGE – This specifies the type of dialog
box. Notice the letter cases. They must be written as shown here.
![Page 28: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/28.jpg)
Output Operation – Only String can be Accommodated
1. import javax.swing.JOptionPane;
2. class OutputPane
3. {
4. public static void main(String[] arg)
5. {
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.
10. String s = "Your name is: " + name + "\nYour age is: " + x +
11. "\nYou have $" + y;
12. JOptionPane.showMessageDialog(null, s, "Personal Data",
13. JOptionPane.INFORMATION_MESSAGE);
14. }
15. }
![Page 29: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/29.jpg)
Output Operation – Only String can be Accommodated
![Page 30: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/30.jpg)
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.
![Page 31: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/31.jpg)
Format Decimal values
In order to format decimal values do the following:
1. Import the following classes:
• java.text.NumberFormat;
• java.text.DecimalFormat;
2. Setup the format pattern:
• NumberFormat nf = NumberFormat.getInstance();
• DecimalFormat df = (DecimalFormat)nf;
• df.applyPattern("#,##0.0000; -#,##0.0000");
![Page 32: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/32.jpg)
• 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);
• }
• }
![Page 33: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/33.jpg)
Output
![Page 34: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/34.jpg)
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:
1. Place the string (text) in a JTextArea object
2. Place the JTextArea in a JScrollPane object
3. Finally, place the JScrollPane object in the showMessageDialog
method of the JOptionPane class.
![Page 35: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/35.jpg)
1. import javax.swing.JOptionPane;
2. import javax.swing.JTextArea;
3. import javax.swing.JScrollPane;
4. class OutputPane
5. {
6. public static void main(String[] arg)
7. {
8. int x = GetData.getInt("Type an integer value");
9. double y = GetData.getDouble("Type a double value");
10. String name = GetData.getWord("Enter a Name");
11. String s = "Your name is: " + name + "\nYour age is: " + x +
12. "\nYou have $" + y;
13. s = s + "\nThat other person does not remember his name\nnor his age";
14. s = s + "\nlet alone how much money he has";
15. JTextArea text = new JTextArea(s, 10, 20);
16. JScrollPane pane = new JScrollPane(text);
17. JOptionPane.showMessageDialog(null, pane, "Personal Data",
18. JOptionPane.INFORMATION_MESSAGE);
19. }
20. }
![Page 36: Data Types – Reference Typessmithjo/classnotes_2xxxx/lesson_04.2.pdf · Data Types –Reference Types • Reference types - any data type that is composed of primitive data types](https://reader034.fdocuments.in/reader034/viewer/2022050718/5e1808868671d9743a0a1c46/html5/thumbnails/36.jpg)
Using a Scrollable Pane