Question

In: Computer Science

This programming assignment involves learning about some of the common exceptions that occur in Java programs.

This programming assignment involves learning about some of the common exceptions that occur in Java programs. Consider the following exception types: NullPointerException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (i.e., is thrown). Write programs that demonstrate each type of exception being thrown (one program per exception) and provide a screen capture of the output. You should write your code so that each exception type is forced to occur. Name your programs as follows: NullPointerExceptionThrown, etc. Then, write a program that catches each type of thrown exception and display an error message indicating some of the details as to what that exception is. Write one program per type of exception. Name your programs as follows: NullPointerExceptionCatch, etc.

Solutions

Expert Solution

The problem of accessing an array element beyond the size of an array is represented by a class called ArrayIndexOutOfBoundsException. Let’s take an example- suppose marks of 20 students are stored in an array. A particular student marks are known by taking the roll number (it is index number) from the keyboard. The array index is from 0 to 19. If the user enters 20, there is no element in an array with index 20. JVM terminates the execution. Before terminating, it gives a proper message of the problem. This example is like this mentioned example. The index is permitted 0 to 2 only but the user is giving 3, it is beyond the array size.

import java.io.*;
public class ArrayIndexOutofBoundThrown
{
public static void main(String args[])
{
int marks[] = { 40, 50, 60 };
System.out.println("Hello 1");

int m1 = marks[3];
System.out.println("Marks are " + m1);

System.out.println("Hello 2");
System.out.println("Hello 3");
}
}

NullPointerException indicates, if an object points to null and further used in the code, the JVM throws NullPointerException. In the following program, string str is assigned with Null. The Null variable str is compared with “Hello”.The JVM reacts by throwing NullPointerException.

import java.io.*;
public class NullPointerExceptionThrown
{
public static void main(String args[])
{
String str = null;
System.out.println(str.equals("hello"));
}
}

As the name indicates, this exception is raised by JVM when it is unable to cast two objects which are done in the program by the programmer against the rules of java casting. In this program, test object f is explicitly cast to ClassCastExceptionThrow and assigned. It compiles, but at execution time throws ClassCastException. Why did it raise? The rule says “before doing explicit casting, there must be implicit casting done earlier”. But in this code, it is not done.

import java.io.*;
class test {
}
public class ClassCastExceptionThrown extends test
{
public static void main(String args[])
{
test f = new test();;
ClassCastExceptionThrown r = new ClassCastExceptionThrown();
              
// f = r;

r = (ClassCastExceptionThrown) f;
}
}

It is thrown by Color constructor when wrong parameters are passed. The RGB values should be within the range of 0 to 255. If other values are passed Color constructor throws IllegalArgumentException.

import java.awt.*;
public class IllegalArgumentExceptionThrown
{
public static void main(String args[])
{
Color clr1 = new Color(300, 150, 200);
}
}


Related Solutions

*OBJECT ORIENTED PROGRAMMING* GOAL: will be able to throw and catch exceptions and create multi-threaded programs....
*OBJECT ORIENTED PROGRAMMING* GOAL: will be able to throw and catch exceptions and create multi-threaded programs. Part I Create a class called Animal that implements the Runnable interface. In the main method create 2 instances of the Animal class, one called rabbit and one called turtle. Make them "user" threads, as opposed to daemon threads. Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts a false....
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create...
JAVA programming - please answer all prompts as apart of 1 java assignment. Part A Create a java class InventoryItem which has a String description a double price an int howMany Provide a copy constructor in addition to other constructors. The copy constructor should copy description and price but not howMany, which defaults to 1 instead. In all inheriting classes, also provide copy constructors which chain to this one. Write a clone method that uses the copy constructor to create...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node...
JAVA PROGRAMMING For this assignment, review the successor method in BST. The successor of a node is the node with the next highest value in the tree. The successor of the node with the largest value in a tree, is null. The algorithm to find the successor of a node is straight forward: if the node has a right subtree, the successor is the smallest node in that subtree (for that we use method minNode). Otherwise, we traverse the tree...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code outputs the programmer’s name , prompts the user to enter two numbers (integers) and outputs their sum. Note: When you run the program, it expects you to enter two integer values (a counting or whole number like 1, 216, -35, or 0) Make the following changes/additions to the code: On line 11, change the String myName from “your full name goes here!!!” to your...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT