Java LAB 10
Java LAB 10
Java LAB 10
LAB # 10
OBJECTIVE:
To get familiar with wrapper classes; Scanner class, and Array class.
THEORY:
1. They convert primitive data types into objects. Objects are needed if
we wish to modify the arguments passed into a method (because
primitive types are passed by value).
2. The classes in java.util package handles only objects and hence
wrapper classes help in this case also.
3. Data structures in the Collection framework, such
as ArrayList and Vector, store only objects (reference types) and not
primitive types.
4. An object is needed to support synchronization in multithreading.
byte bv = byteobj;
int iv = intobj;
float fv = floatobj;
double dv = doubleobj;
char cv = charobj;
Output:
The Byte, Short, Integer, and Long classes are wrappers for byte, short,
int, and long integer types, respectively. Their constructors are shown
here:
As you can see, these objects can be constructed from numeric values or
from strings that contain valid whole number values. The methods defined
by these classes are shown in given Table. They define methods for
converting strings back into integers. Variants of these methods allow you
to specify the radix, or numeric base, for conversion. Common radixes are
2 for binary, 8 for octal, 10 for decimal, and 16 for hexadecimal. The
following constants are defined:
MIN_VALUE Minimum value
MAX_VALUE Maximum value
TYPE The Class object for byte, short, int, or long
Character Class:
class CharacterClassExample {
public static void main(String[] args) {
char ch1, ch2;
ch1 = '9';
ch2 = 'V';
Output:
Let’s take the obvious example of a source from which you might want to
interpret data. To obtain a Scanner object that will scan input from the
keyboard, you could use the following statement:
Using a Scanner:
Here’s a simple example that just reads a variety of input from the
standard input stream and displays what was read:
import java.util.Scanner;
class TryScanner {
public static void main(String[] args) {
Scanner kbScan = new Scanner(System.in); // Create the scanner
int selectRead = 1; // Selects the read operation
int MAXTRIES = 3; // Maximum attempts at input
int tries = 0; // Number of input attempts
while(tries<MAXTRIES) {
switch(selectRead) {
case 1:
System.out.println("Enter an Integer:");
System.out.println("You entered: "+ kbScan.nextLong());
++selectRead; // Select next read operation
tries = 0; // Reset count of tries
case 2:
System.out.print("Enter a floating-point value: ");
System.out.println("You entered: "+ kbScan.nextDouble());
++selectRead; // Select next read operation
tries = 0; // Reset count of tries
case 3:
System.out.print("Enter a boolean value(true or false): ");
System.out.println("You entered: "+ kbScan.nextBoolean());
}
break;
}
}
}
Output:
class Hierarchy:
java.lang.Object
↳ java.util.Arrays
Class Declaration:
public class Arrays
extends Object
Syntax to use Array:
Arrays.<function name>;
import java.util.Arrays;
Output:
LAB TASK
1. Write a program to take the integer value from the user and convert an
integer into binary, hexadecimal, and octal with the help of methods
defined in wrapper class of Integer and Long.
3. Write a program to take 10 integer values from the user and display the
odd values with their indexes and copy of the original array by using
the search and copy method respectively, define in array class.
Example:
Array= {11,22,33,44,55,66,77,88,99,111}
Sample Output:
11 found at index= 1
33 found at index= 3
55 found at index= 5
77 found at index= 7
111 found at index= 9
Array= {11,22,33,44,55,66,77,88,99,111}