QuestionQuestion

Intro to Computer Science
Handle exceptions
Assignment:
The next lab assignment is another exercise in the use of the exception and handling of exceptions. There is another simple calculation program listed below, which takes decimal values (doubles) as input for the a, b and c coefficients of a quadratic equation and calculates the two values for X. The program should always display two values as the result; a quadratic equation always has two values as the solution even when they are identical.
The program should repeat itself if the user wishes; this makes it easier to test. Values for A and B will be cleared to 0 (zero) after each calculation.
However it does nothing to handle the situation where a user enters data which is non-double or even non numeric. Your task today is to modify that program to handle the bad input situation with the use of exception handling. Bad inputs are:
Non-numeric values where numeric values are expected
Any input that causes the output to be “NaN” (Not a Number)
Any input that causes the output to be “INFINITY”

The program should not terminate but should inform the user of the specific error and should re-request the input value which was in error
Testing:
Test your program with the following input;
a. Non-numeric value when double is expected.
b. A = 0
c. Values which result in “NaN” or “INFINITY” for answers.
d. Correct input

In all cases the program should ask for only the input values that were invalid and should continue with the values that were correct. The request for a corrected value should be repeated as many times as the user inputs illegal values.
The program should never crash!

Binary-sort tree
Problem Description:
The problem to be solved is the building of a binary tree in RAM using methods that recursively call themselves to construct the tree. If the values are stored correctly, they can be retrieved in numeric order by merely traversing the tree and checking the values for numeric order. It is not the world’s best sort algorithm but it has two points in its favor: (a) it can be used to insert values that occur leisurely but that can be retrieved in numeric order at a moment’s notice, and (b) it will give you some practice using recursion.
The program will have 3 classes; (1) the class containing the “main” method, (2) a class named treeSort and (3) a class named node. Objects of the node class will be used as nodes in the sort-tree while an object of the treeSort class will receive long integer numeric values and create nodes to contain them in the tree. You can think of the tree as a “two-dimensional linked-list” and like any linked list, it exists in RAM but is built dynamically. The “main” method will generate the random values and send them to the sortTree by calling a method.
SortTree will then have at least four methods:
public boolean insert(long value)
This method will be called by “main” and will insert the value into the tree. If the value to be inserted is a duplicate of one already in the tree, the insertion will be terminated without any node being created and the value “true” will be returned to the caller. Otherwise insert will return the value “false”.
public long [] retrieve( )
This method will create an array of type “long” and will fill it with the values in sorted order from the tree. Since the insert method can count the number of values currently in the tree, creating the array should be able to use this value to size the array exactly.

public boolean isPresent(long value)
This method will search the tree for the argument value and if found, will return “true” If the value is not found, “false” will be returned.
public int getCount( )
This method will return the number of values currently in the tree. It will count only the actual values, not the duplicates for which “insert” returned “false”.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Arrays;

public class BinarySortTree {

    public static void main(String[] args) {
      
       TreeSort treeSort = new TreeSort();
      
       long[] array = new long[280];
       for (int i = 0; i < array.length; i++) {
            array[i] = (long) (Math.random() * Long.MAX_VALUE);
            treeSort.insert(array[i]);
            if (treeSort.isPresent(array[i]) == false) {
                System.err.println("Error: "+array[i]+" inserted incorrectly.");
            }
       }
       System.out.println("Total values: "+treeSort.getCount()+"\n");
       System.out.println("Array:\t"+Arrays.toString(array)+"\n");
       System.out.println("TreeSort:\t"+Arrays.toString(treeSort.retrieve()));
    }
   
}

class TreeSort {

    Node root;
   
    public TreeSort() {
       root = new Node();
    }
   
    public boolean insert(long value) {
       return root.insert(value);
    }
   
    public long[] retrieve() {
       return root.retrieve();
    }
   
    public boolean isPresent(long value) {
       return root.isPresent(value);
    }
   
    public int getCount() {
       return root.getCount();
    }
   
}

class Node {
   
    Long value...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$50.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats