Question

In: Computer Science

Write a complete java program that Define a class named sample containing: A method named division...

Write a complete java program that Define a class named sample containing:

  • A method named division that receives two integers x and y and returns the division of the two numbers. It must handle any possible exceptions.
  • A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index. It must handle any possible exceptions.
  • main method that recievs input from user and repeat if wrong input. Then it should call the other methods.
  • A sample output:

Please enter two numbers to divide: 10 Hi

You entered wrong input. Try again.

Please enter two numbers to divide: 15 0

Division could not be completed.

Enter 5 double numbers: 2.5 3.1 5.0 9.3 10.0

Which element you want to print? (Choose a number between 0-4): 7

Wrong choice. Try again.

Which element you want to print? (Choose a number between 0-4): 3

The element in position 3 = 9.3

Thank you for using this program.

Solutions

Expert Solution

Sample.java

import java.util.Scanner;


//Write a complete java program that Define a class named sample containing:
public class Sample
{
   //A method named division that
   //receives two integers x and y and returns the division of the two numbers.
   double division(double a, double b)
   {
       //denominator cannot be 0
       if(b==0)
       {
           System.out.println("Division could not be completed");
           return 0.0;
       }
       double result = a/b;
  
   //It must handle any possible exceptions.
       return result;
   }
  
   //A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index.
   double printArray(double arr[], int index)
   {
       double result = 0.0;
       if(index < 0 || index > 4)
       {
           throw new NumberFormatException();
       }
       result = arr[index];
       //It must handle any possible exceptions.
      
       return result;
   }

  
   //main method that recievs input from user and repeat if wrong input. Then it should call the other methods.
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       Sample s = new Sample();
         
       while(true)
       {
         
           System.out.println("Please enter two numbers to divide");
           String first = sc.next();
           String second = sc.next();
           try
           {
               double num1 = Double.parseDouble(first);
               double num2 = Double.parseDouble(second);
               double result = s.division(num1, num2);
               if(num1 != 0.0 && result == 0.0)
               {
                 
               }
               else
               {
                   System.out.println("Division of "+num1+" by "+num2+" is "+result);
               }
               break; // this line will come if both num1 and num2 are valid numbers
           }
           catch (NumberFormatException nfe)
           {
           System.out.println("You entered wrong input. Try again");
           }
       }
         
       while(true)
       {
         
           System.out.println("Enter 5 double numbers:");
           String first = sc.next();
           String second = sc.next();
           String third = sc.next();
           String fourth = sc.next();
           String fifth = sc.next();

           try
           {
               double arr[] = new double[5];
               arr[0] = Double.parseDouble(first);
               arr[1] = Double.parseDouble(second);
               arr[2] = Double.parseDouble(third);
               arr[3] = Double.parseDouble(fourth);
               arr[4] = Double.parseDouble(fifth);
               System.out.println("Which element you want to print? (Choose a number between 0-4): ");
               String option = sc.next();
               int i = Integer.parseInt(option);
                 
               double result = s.printArray(arr, i);
               System.out.println("The element in position "+i+" = "+result);
               break; // this line will come if all numbers in array are valid numbers
           }
           catch (NumberFormatException nfe)
           {
           System.out.println("Wrong choice. Try again");
           }
       }
         
       System.out.println("Thank you for using this program");
         
       sc.close();
      
   }
}

Sample I/O and output


Related Solutions

write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method:...
JAVA (Tree height) Define a new class named BSTWithHeight that extends BST with the following method: /** Return the height of this binary tree */ public int height() Class Name: Exercise25_01
Write a complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT