In: Computer Science
String inputStr;
double number;
try
{
inputStr =
JOptionPane.showInputDialog(null,
"Please input a number.");
if (inputStr.equals(""))
throw new IllegalArgumentException("Please enter a number");
number =
Double.parseDouble(inputStr);
JOptionPane.showMessageDialog(null,
"The square of the number is " + number * number);
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null,
"Please enter a valid number to square");
}
catch (IllegalArgumentException e)
{
JOptionPane.showMessageDialog(null,
e.getMessage());
}
finally
{
System.exit(0);
}
1) First,Why must the two catch blocks be listed in the order that they appear? What is the expected output if the user does not enter anything into the input dialog? What is the expected output if the user enters “ten” into the input dialog? And then-What is the purpose of the throw statement located within the try block? What is the purpose of the finally block at the end of the code? Now assume the user must enter only positive numbers. Provide a very brief example of a customly thrown exception when the user enters a negative number. Then,What is the relationship between binary files and performing a random file access? Given a binary file consisting of a sequence of integers (int in Java), how must the file pointer be managed to correctly move to the start of each integer? Then, Briefly describe the process of object serialization and object deserialization. And How would you determine the size of an object in bytes?
All parts must be answered for question 1 to receive any credit.
Why must the two catch blocks be listed in the order that they appear?
NumberFormatException is a sub class of IllegalArgumentException
If you will user IllegalArgumentException first then NumberFormatException,
it will never execute NumberFormatException because it will caught by IllegalArgumentException.
What is the expected output if the user does not enter anything into the input dialog?
It will display "Please enter a number" message
Because the condition inputStr.equals("") becomes true
What is the expected output if the user enters “ten” into the input dialog?
It will display message "Please enter a valid number to square"
Because while converting to double using Double.parseDouble(inputStr); will generate NumberFormatException.
What is the purpose of the throw statement located within the try block?
Will through an exception to it respective catch block.
In your case throw new IllegalArgumentException("Please enter a number"); statement will transfer the control to
following catch block:
catch (IllegalArgumentException e)
{
JOptionPane.showMessageDialog(null,
e.getMessage());
}
What is the purpose of the finally block at the end of the code?
If exception is generated control will transfer to respective catch block and then will execute finally block.
If exception is not generated will calculate and display the result and then will execute finally block.
Now assume the user must enter only positive numbers.
Assume user enters 3
then you will get the following output:
The square of the number is 9.0
Provide a very brief example of a customly thrown exception when the user enters a negative number.
if(number < 0)
throw new IllegalArgumentException("Please enter a positive number.");
If user will enter -3 then you will get the following message:
Please enter a positive number.
What is the relationship between binary files and performing a random file access?
1) In binary file there are two different methods of storing integers and floating-point numbers in memory, depending on the processor:
For 4-byte int (Example: 5E2):
The first of the four bytes in memory holds the most significant byte: 00 00 05 E2 (big-endian mechanism)
Otherwise start with the least significant byte values: E2 05 00 00
2) The writeInt() method of DataOutput interface writes an integer as a 4 - bytes binary quantity regardless of the number of digits to write.
The writeDouble() method of DataOutput interface writes a double as an 8 - bytes binary quantity regardless of the number.
Random access allows us to read or write data anywhere in a file.
Random access file provides a file pointer which indicates the position of the next byte to be read or write.
Given a binary file consisting of a sequence of integers (int in Java), how must the file pointer be managed to correctly move to the start of each integer?
Scanner keyboard = new Scanner(new File("Numbers.txt"));
int [] num = new int[200];
int counter = 0;
while(keyboard.hasNextInt())
num[counter++] = keyboard.nextInt();
Briefly describe the process of object serialization and object deserialization.
Serialization converts the object into byte stream, and also stores information about object type and the type of data stored in the object.
Where as De-serialization converts byte stream to actual object.
How would you determine the size of an object in bytes?
import java.lang.instrument.Instrumentation;
public class ObjectSize
{
private static volatile Instrumentation ins;
public static void premain(final String strArgs,
final Instrumentation inst)
{
ins = inst;
}
public static long getObjectSize(final Object obj)
{
return ins.getObjectSize(obj);
}
}
public class Demo
{
int x;
public static void main(String [] args)
{
System.out.println(
ObjectSize.getObjectSize(new Demo()));
}
}