Question

CM245 Lab 4 (Chapter 12) Submit online through D2L Implement solutions using the appropriate concepts from Chapter 12. There

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: I am submitting two programs...i will do remaining also.thank u

_____________________

12.2)

// InputMismatchExceptionDemo.java

import java.util.InputMismatchException;
import java.util.Scanner;

public class InputMismatchExceptionDemo {

   public static void main(String[] args) {

       // Declaring variables
       double a, b, sum = 0;

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       while (true) {
           try {
               // Getting the input entered by the user
               System.out.print("Enter Number a:");
               a = sc.nextDouble();
               System.out.print("Enter Number b:");
               b = sc.nextDouble();
               sum = a + b;
               System.out.println("Sum of " + a + " abd " + b + " is :" + sum);
               break;
           } catch (InputMismatchException e) {
               sc.nextLine();
               System.out.println("Exception :"+e);
              
               continue;
           }
       }

   }

}
____________________

Output:

Enter Number a:3.4
Enter Number b:hello
Exception :java.util.InputMismatchException
Enter Number a:4.5
Enter Number b:5.5
Sum of 4.5 abd 5.5 is :10.0

_____________________

12.3)

// ArrayIndexOutOfBoundsExceptionDemo.java

import java.util.Random;
import java.util.Scanner;

public class ArrayIndexOutOfBoundsExceptionDemo {

   public static void main(String[] args) {
       int indx;
       int arr[] = new int[100];

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       // Creating an random class object
       Random r = new Random();
       for (int i = 0; i < arr.length; i++) {
           arr[i] = r.nextInt(100) + 1;
       }

       while (true) {
           try {
               System.out.print("Enter Index to search :");
               indx = sc.nextInt();

               System.out.println("The number present at index " + indx
                       + " is :" + arr[indx]);

               break;
           } catch (ArrayIndexOutOfBoundsException e) {
               System.out.println("Exception :" + e);
               continue;
           }
       }
   }

}
________________________

Output:

Enter Index to search :105
Exception :java.lang.ArrayIndexOutOfBoundsException: 105
Enter Index to search :98
The number present at index 98 is :19

________________________

12.4)

Add a comment
Know the answer?
Add Answer to:
CM245 Lab 4 (Chapter 12) Submit online through D2L Implement solutions using the appropriate concepts from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly...

    (ArrayIndexOutOfBoundsException) Write a program that meets the following requirements: ■ Creates an array with 100 randomly chosen integers. ■ Prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds. Following modifications: * Use either a Scanner object or JOptionPane.showInputDialog() for input. * Use either System.out.println() or JOptionPane.showMessageDialog() for output. * In addition to ArrayIndexOutOfBoundsException, also catch InputMismatchException or NumberFormatException, and...

  • java punu ile 20 maze from chapter 12. 2. StringToolong.java → Write a program that creates...

    java punu ile 20 maze from chapter 12. 2. StringToolong.java → Write a program that creates an exception class called StringTooLongException, designed to be thrown when a string is discovered that too many characters in it. In the main driver of the program, read strings from th user until the user enters "DONE". If a string is entered that has too many chara (say 20), throw the exception. Allow the thrown exception to terminate the program.

  • C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and...

    C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and x,y pair object) 24. Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception. 25. Write a function that dynamically allocates a block of memory and returns a char pointer to...

  • Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling...

    Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling and Input/Output. Using try/catch/finally statement to handle exceptions, or declare/throw an exception as needed, create the following program: Create an array of 25 random numbers between 0 & 250 formatted to a field width of 10 & 4 decimal places (%10.4f). Use the formula Math.random() * 250. Display the array of random numbers on the console and also write to a file. Prompt the...

  • Programming Lab 4 (Chapter 4) There is one checkpoint, make sure you call your professor to...

    Programming Lab 4 (Chapter 4) There is one checkpoint, make sure you call your professor to get checked. 4.1 Write a program that prompts the user for two integers and then prints The sum The difference The product The average The distance (absolute value of the difference) The maximum (the larger of the two) The minimum (the smaller of the two) Hint: The max, min, and absolute value methods are declared in the Math class. Lookup https://docs.oracle.com/javase/8/docs/api/java/lang/Math. html the API...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT