Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.*; public class LabProgram { public static void main(String[] args) { //Initializing variables with 0 int packages=0; int destinations=0; int routes=0; Scanner sc =

import java.util.*;

public class LabProgram { public static void main(String[] args) { //Initializing variables with 0 int packages=0; int destinations=0; int routes=0; Scanner sc = new Scanner(System.in); // if input entered for packages is a character then the catch block executes // else catch block is ignored (when input is an integer) try { System.out.println("Please enter the number of packages:"); packages = sc.nextInt(); } // Error message is prompted as mention in the print statement // Number of routes is 0 as character is entered // then program terminates catch(InputMismatchException e) { System.out.println("Error! Please enter only numbers."); System.out.println("Number of routes: 0"); System.exit(0); } // if input entered for destinations is a character then the catch block executes // else catch block is ignored (when input is an integer) try { System.out.println("Please enter the number of destinations:"); destinations=sc.nextInt(); } // Error message is prompted as mention in the print statement // Number of routes is 0 as character is entered // then program terminates catch(InputMismatchException e) { System.out.println("Error! Please enter only numbers."); System.out.println("Number of routes: 0 "); System.exit(0); } // if destinations input is 0 then devision fails // which leads to execution of catch block // if destinations input is not 0 then // routes are calculated and value is propmpted try{ System.out.println("Calculating routes..."); routes = packages/destinations; System.out.println("Number of routes: " + routes); } // Error message is prompted as mention in the print statement // Number of routes is 0 as division fails // then program terminates catch(ArithmeticException c){ System.out.println("Error! Cannot have zero destinations."); System.out.println("Number of routes: 0"); System.exit(0); } } }

What have I performed wrong in this lab? and what can i do to to complete this lab....I am confused and lost

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions