Java Module III-Lab Exercises
Java Module III-Lab Exercises
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.
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.
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.
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