Java Module III-Lab Exercises

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Java Module III - Lab Exercises

Topic: Exception Handling – Built-in Exceptions


1. Write Java programs to handle the following exceptions:
a. InputMismatchException
b. NumberFormatException
c. ArrayIndexOutofBoundsException
d. NullPointerException

2. Write an application that throws and catches an ArithmeticException when you attempt to
take the square root of a negative value. Prompt the user for an input value and try the
Math.sqrt() method on it. The application either displays the square root or catches the
thrown Exception and displays an appropriate message.
Topic: Exception Handling – User-defined Exceptions
3. Write a Java program based on the following statements:
• Create a CourseException class that extends Exception and whose constructor
receives a String that holds a college course’s department (for example, CIS), a
course number (for example, 101), and a number of credits (for example, 3). Save
the file as CourseException.java.
• Create a Course class with the same fields and whose constructor requires values
for each field. Upon construction, throw a CourseException if the department does
not consist of three letters, if the course number does not consist of three digits
between 100 and 499 inclusive, or if the credits are less than 0.5 or more than 6.
Save the class as Course.java.
• Write an application that establishes an array of at least six Course objects with
valid and invalid values. Display an appropriate message when a Course object is
created successfully and when one is not.

4. Write a Java program based on the following statements:


• Create a UsedCarException class that extends Exception; its constructor receives a
value for a vehicle identification number (VIN) that is passed to the parent
constructor so it can be used in a getMessage() call. Save the class as
UsedCarException.java.
• Create a UsedCar class with fields for VIN, make, year, mileage, and price. The
UsedCar constructor throws a UsedCarException when the VIN is not four digits;
when the make is not Ford, Honda, Toyota, Chrysler, or Other; when the year is not
between 1997 and 2017 inclusive; or either the mileage or price is negative. Save
the class as UsedCar.java.
• Write an application that establishes an array of at least seven UsedCar objects and
handles any Exceptions. Display a list of only the UsedCar objects that were
constructed successfully. Save the file as ThrowUsedCarException.java.

Page 1 of 4
Topic: Collections API – List
5. Write an application for Cody’s Car Care Shop that shows users a list (ArrayList) of
available services: oil change, tire rotation, battery check, or brake inspection. Allow the
user to enter a string that corresponds to one of the options, and display the option and its
price as $25, $22, $15, or $5, accordingly. Display an error message if the user enters an
invalid item.

6. Write an application using LinkedList that contains Flower names Rose, Lily, Dalia and
Jasmine. Perform following set of operations on LinkedList:
a. Add a new flower Lotus.
b. Verify whether the LinkedList is empty or not.
c. Remove the flower Dalia from the LinkedList.
d. Display all the elements in the LinkedList.
Topic: Collections API – Set
7. Write a Java program based on the following conditions:
a. Create a HashSet and add these strings: "dog", "ant", "bird", "elephant", "cat".
b. Use an Iterator to print the items in the set.
c. Add a new element into the set.
d. Delete an element from the set.
e. Check whether “ant” is present in the set.

8. Write a Java program for the statements given below:


a. Create a class Employee with attributes empid, name, age, designation and salary.
Include a parameterized constructor to assign the values to each attribute. Create
get and set methods for all the fields. Include a toString() method.
b. The Employee class must implement Comparable interface and override the
compareTo() method to sort the employees based in the empid.
c. Create a TreeSet to store five employees given below and perform the following
operations:
Empid Name Age Designation Salary
7624 SMITH 23 CLERK 10000
7640 ALLEN 35 SALESMAN 25000
7625 JONES 27 MANAGER 50000
7641 MARTIN 30 SALESMAN 30000
7601 TURNER 28 SALESMAN 27000
• Display all the employees.
• Display the empid of the employees who are managers.
• Display the name and age of all the employees who are salesman.
• Display the designation of the employees who earn more than Rs. 25000.

Page 2 of 4
Topic: Collections API – Map
9. Create a HashMap that consists of Fruit names Apple, Mango, Grapes and Papaya with the
key values 301, 302, 303 and 304 respectively. Develop a program to perform basic
operations on HashMap:
a. Add a new fruit Strawberry with a key 305.
b. Display the fruit name for the key 302.
c. Check whether the map has a key 303 or not.
d. Remove an element from the map.
e. Display all the elements of the HashMap.

10. Write a Java program for the statements given below:


a. Create a class Basket with attributes item and price. Include a parameterized
constructor to assign the values to each attribute. Create get and set methods for all
the fields. Include a toString() method.
b. Create a TreeMap to store five items given below and perform the following
operations:
Key (String) Value (Basket Object)
Banana Basket [item=Banana, price=40.0]
Apple Basket [item=Apple, price=105.0]
Mango Basket [item=Mango, price=75.0]
Orange Basket [item=Orange, price=55.0]
Papaya Basket [item=Papaya, price=25.0]
• Display all the elements in the map.
• Check whether the map contains any item with price Rs. 25.0.
• Check whether the map contains the key “Orange”.
• Remove an entry from the map.
• Display all the values in the map.
• Display all the keys in the map.

Topic: Wrapper Classes


11. Write a Java program to use Integer wrapper class for the following tasks:
a. Convert a string representing a financial figure "1234" to an Integer object for
further calculations.
b. Parse a string "5678" representing a transaction amount to a primitive int for a
calculation function.
c. Compare the values of two Integer objects representing two different account
balances (e.g., 10 and 20) and determine which is larger.
d. For a security feature, calculate the number of one-bits in the binary
representation of an account ID (e.g., 29).
e. Convert an Integer representing a total transaction count (e.g., 100) to a String for
a report.

Page 3 of 4
12. Write a Java program to use Character wrapper class for the following tasks:
a. Verify if a given character (e.g., 'a') from user input is a letter, ensuring that only
alphabetic characters are used in certain fields.
b. Check if a given character (e.g., '1') from a numeric input field is a digit.
c. Convert a lowercase character (e.g., 'b') to uppercase for standardized display, and
vice versa (e.g., 'G' to lowercase).
d. Check if a given character (e.g., ' ') in user input is a whitespace, to validate
proper word spacing in text fields.

Page 4 of 4

You might also like