Question

In: Computer Science

Create a moderately complex java program that utilises 2 or more classes. Within these classes: -...

Create a moderately complex java program that utilises 2 or more classes. Within these classes:
- have one that defines an exception
- have that exception throw(n) in one method and handled in another
-has the program continue even if the user inputs incorrect data
-be creative/unique in some way

Solutions

Expert Solution

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


class MyNegativeNumberException extends RuntimeException{
        public MyNegativeNumberException(String m) {
                super(m);
        }
}
public class TestEx {
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter numbers (0 to exit): ");
                int n=sc.nextInt();
                int sum=0;
                while(n!=0) {
                        try {
                                checkIfNegative(n);
                                sum+=n;
                        }
                        catch (InputMismatchException e) {
                                sc=new Scanner(System.in);
                        }
                        catch (MyNegativeNumberException e) {
                                System.out.println(e);
                        }
                        n=sc.nextInt();
                }
                System.out.println("Sum = "+sum);
        }

        private static void checkIfNegative(int n) throws MyNegativeNumberException{
                if(n<0)
                        throw new MyNegativeNumberException("Invalid number");
        }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
The following program will be written in JAVA. Create a class called complex performing arithmetic with...
The following program will be written in JAVA. Create a class called complex performing arithmetic with complex numbers. Write a program to test your class.                         Complex numbers have the form:                         realPart + imaginaryPart * i                                               ___                         Where i is sqrt(-1)                                                 Use double variables to represent data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Create a program that accepts in a string of 2 or more words. The program then...
Create a program that accepts in a string of 2 or more words. The program then copies the entered string changing the alpha characters into digits representing characters with acsenders, descenders and nonascender/desender characters; uppercase characters should be treated as lower case. The characters with descenders (gjpqy) should be replaced with a 1. The characters with ascenders (dbfhklt) should be replaced with a 2. The rest of the alpha characters should be replaced with a 3. The converted string should...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in...
Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add...
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create...
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create your own user defined exception, COMMENT it well, and add code so that it is thrown.
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
This is in Java 1. Create an application called registrar that has the following classes: a....
This is in Java 1. Create an application called registrar that has the following classes: a. A student class that minimally stores the following data fields for a student:  Name  Student id number  Number of credits  Total grade points earned             And this class should also be provides the following methods:  A constructor that initializes the name and id fields  A method that returns the student name field  A method that returns the student...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT