Question

CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The...

CIT 149 Java 1 programming question

Use DrJava to compile the following try-catch program

? The Assignment

? Specifications

General

  • Structure your file name and class name on the following pattern:
    • The first three letters of your last name (begin with upper case.).
    • Then the first two letters of your first name (begin with upper case.).
    • Follow this with the name of the program: TryCatch.
    • For a student called ’John Doe,’ the class name and file name would be: DoeJoTryCatch
  • Use good comments in the body of the program.
  • Think about the problem and decide what type each of the input numbers should be. Also, think about the calculations and decide what type the variables should be that will hold the results of the calculations.
  • Use camel casing for variable names, and use descriptive variable names. Do not use general variable names like "tax" or "total."

Make sure you have the following comments at the beginning of your program:

/*
 * Program Name:   The program name.
 * Author:         Your first and last name.
 * Date Written:   The current date.
 * The class name: CIT 149 Java 1
 * Description:    Meaningful description of the purpose of the program.
 */
  • Take a look at the method below.
    This method will cause the program to crash.

  • Example 1: A method that tries to perform an impossible task

public static void watchThis()
{
    int theFirstNumber = 5;
    int theSecondNumber = 0;
    int theAnswer = theFirstNumber / theSecondNumber;  // <1>
    return;
}

<1> This is where the program crashes. I hope that at this point you understand why.

  • Here is another example of a method that asks for an int and displays the number entered.
    A good way to handle a situation like this is to have a method (think one task) that will return an int from the user, but will continue to ask for input until an int is received. This would prevent a type mismatch and cause the program to end.
    You could use this method any time you needed an int from the user. You might also create a method for doubles as well.
    You would not put the prompting in the method because the prompting would be different in each program.

Example 2: A method that will return an int.

public static int getInt()
{
    while(true)    //  <1>
    {
        try
        {
            return keyboard.nextInt();   // <2>
        }
        catch(InputMismatchException e)
        {
            keyboard.next();             // <3>
            System.out.print("That is not and integer. Please try again: ");
        }
    }
}

<1> Using while(true) will create an infinite loop. The return statement will take it out of the loop.
<2> This is a normal int input statement. You could set this to a variable as well, and then return it. As it is, you need to have a variable set to store it in when it is returned.
<3> The next() statement discards the previous input. That is its only purpose.

? The Problem

? You will have a good bit of leeway in completing this problem.
You may create any problem you like, as long as it meets the specifics below. It can be written to display the results in the terminal, or it can be written in GUI style.

  • The program must use OOP style and have more than one class containing methods.
  • The program must at some point ask the user for the input of numbers.
  • There must be methods to handle the input of the numbers to insure that type mismatches should not crash the program.
  • The program must do at least one calculation. If it is possible to have a division by zero, there should be a try-catch to catch it and prevent the program from crashing.
  • There must be at least one try-catch statement in your program.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

==========================

JAVA Program

==========================

/////DoeJoTryCatch.JAVA//////////////////

import java.util.InputMismatchException;

import java.util.Scanner;

/**

* Program Name: CIT 149 Java1 Programming

* Author: John Doe

* Date Written: 11/27/2019

* The class Name: DoeJoTryCatch

* Description: The program will take 2 numbers from

* user and try to divide number1 by number 2 and display the result

*

*/

public class DoeJoTryCatch {

//private static field keyBoard, which is a scanner to read input from keyboard

private static Scanner keyBoard = new Scanner(System.in);

public static void main(String[] args){

System.out.println("Enter number1: ");

int num1 = getInt();//call getInt() to read a valid int from user

System.out.println("Enter number2: ");

int num2 = getInt();//call getInt() to read a valid int from user

System.out.println("Doing division of "+num1+" by "+num2+"...");

division(num1,num2);//call division() method to divide num1 by num2

}

/**

* Get a valid int from user.

* Take input from user until a valid integer value is not provided

* @return

*/

public static int getInt(){

while(true){//start infinite while loop

try{

return keyBoard.nextInt();//return int as read from scanner

}catch(InputMismatchException ime){//exception will be caught if proper input not given

keyBoard.next();//clear the previous input

System.out.println("This is not an integer. Please try again.");

}

}

}

/**

* Divide number1 by number2 and return integer result

* print error message if there is division by zero issue.

* @param number1

* @param number2

*/

public static void division(int number1, int number2){

int result = 0;

try{

result = number1/number2;

System.out.println("Result of division is = "+result);

}catch(ArithmeticException ae){//if division by 0, exception will be caught

System.out.println("Result undefined : division by zero error.");

}

}

}

=========================

OUTPUT

=========================

RUN1

-----

Enter number1:

25

Enter number2:

a

This is not an integer. Please try again.

5d

This is not an integer. Please try again.

5

Doing division of 25 by 5...

Result of division is = 5

RUN2

-----

Enter number1:

xyz

This is not an integer. Please try again.

68

Enter number2:

ys

This is not an integer. Please try again.

#

This is not an integer. Please try again.

4

Doing division of 68 by 4...

Result of division is = 17

RUN3

-----

Enter number1:

50

Enter number2:

0

Doing division of 50 by 0...

Result undefined : division by zero error.

Add a comment
Know the answer?
Add Answer to:
CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The...
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
  • Java 1 Some help Please...... 1. Before You Begin Anticipate where things can go wrong Consider...

    Java 1 Some help Please...... 1. Before You Begin Anticipate where things can go wrong Consider how to gracefully shut down the program and save the users data and close files properly. Typically there are two scenarios to make a distinction between: The first is one that which you have absolutely no control over; such as if all the files are where they should be, and that the user has not deleted one or accidentally moved it rather than copied...

  • CIT 149 JAVA 1 programming question? There will be two files for this problem. The first...

    CIT 149 JAVA 1 programming question? There will be two files for this problem. The first one, called FutureValue will hold the main method. The second file called FinancialUtils will hold the two methods that will display what the program will do, and perform the calculations. In the main method, which controls the logic of the program, you will first call the displayInstructions method which will display a statement of what the program will do. You will write this description...

  • use java program, create a class with conditions below. 1. asking user to type int number...

    use java program, create a class with conditions below. 1. asking user to type int number 2.you have to receive it use next String (not next int) 3.use try catch , if user input is not int, ask them to retype int number. (have to print out message that said only type int number)

  • I am required to use the try - catch block to validate the input as the...

    I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning...

    PLEASE INCLUDE COMMENTS In java Create a Java Program Add the following comments at the beginning of the file: Your name. The name of the class(es) used in the program. The core concept (found below) for this lesson. The date the program was written. Include a recursive method separate from the main method that will add together all of the even numbers between and including 1 and the value the user supplies. For instance, if the user enters 10 then...

  • Java I: Create a Java program that will accept the price of an item and the...

    Java I: Create a Java program that will accept the price of an item and the quantity being purchased of that item. After accepting the input, calculate the amount of the purchase (based on the price and quantity), calculate the sales tax on the purchase, then output the product price, quantity, subtotal, sales tax, and the total sale based on the output format shown below. Structure your file name and class name on the following pattern: The first three letters...

  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total...

    This is java and make simple program. CPSC 1100. Thanks CSPS 1100 Lab 4 ay total Also I would need a new name for the daubles. An example here might be double tRtalD-caradd xD, YD) 1. (a) We are going to begin with some simple math, reading input, and formatted output. Create a class called MuCalsustec Yau will not have an instance variable. Since you have no instance variables to initialize, you do nat need a constructar. Do NOT create...

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