230k views
3 votes
java Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or 95) go north/south, and evens (like the 10 or 90) go east/west. Auxiliary highways are numbered 100-999, and service the primary highway indicated by the rightmost two digits. Thus, I-405 services I-5, and I-290 services I-90. Given a highway number, indicate whether it is a primary or auxiliary highway. If auxiliary, indicate what primary highway it serves. Also indicate if the (primary) highway runs north/south or east/west. Ex: If the input is: 90 the output is: I-90 is primary, going east/west. Ex: If the input is: 290

User Stoleg
by
5.7k points

1 Answer

4 votes

Answer:

Here is the JAVA program:

import java.util.Scanner; // to accept input from user

public class Main{ // class name

public static void main(String[] args) { // start of main function

Scanner input = new Scanner(System.in); // creates Scanner class object to take and scan input from user

int highwayNum; //to hold value of highway number

int primaryNum; //to hold value of primary number

highwayNum = input.nextInt(); //scans and takes input value of highwayNum from user

if(highwayNum<1 || highwayNum>999) //if highway number is less than 1 or greater than 999

System.out.println(highwayNum+" is not a valid interstate highway number."); //display this message if above if statement is true

else{ //when above if statement is false

if(highwayNum>=1 && highwayNum<=99){ //checks if highway number is greater than equal to 1 and less than equal to 99

System.out.print("I-"+highwayNum+" is primary, going ");

if(highwayNum%2==0) //checks for the even highway number

System.out.println("east/west."); //prints this if highway number is even

else //if highway number is odd

System.out.println("north/south."); } //prints this if highway number is odd

else{ // if auxiliary number

primaryNum = highwayNum%100; //computes primary number

System.out.print("I-"+highwayNum+" is auxillary, serving I-"+primaryNum+", going "); //displays the auxiliary number and which primary number it is serving

if(primaryNum%2==0) //if primary number is even

System.out.println("east/west."); //displays this if primaryNum is even

else //when primaryNum is odd

System.out.println("north/south."); } } }}//displays this if primaryNum is odd

Step-by-step explanation:

I will explain the program with an example:

Suppose user enters 90 as highway number So

highwayNum = 90

if(highwayNum<1 || highwayNum>999)

This if condition evaluates to false because highwayNum is 90 and its not less than 1 or greater than 999.

So else part executes which has an if statement:

if(highwayNum>=1 && highwayNum<=99)

This statement evaluates to true because highwayNum is 90 so it lies between 1 and 99 inclusive. So the statement inside this if condition executes:

System.out.print("I-"+highwayNum+" is primary, going ");

This statement prints the following message on output screen:

I-90 is primary going

Now next statement if(highwayNum%2==0) checks if highwayNum is completely divisible by 2 and is an even number. This takes the mode of highwayNum by 2 and returns the remainder. 90%2 is 0 because 90 is completely divisible by 2 and hence it is an even number. So this if condition evaluates to true and program control moves to the statement:

System.out.println("east/west."); which prints east/west on the output screen. Hence the output of the entire program is:

I-90 is primary, going east/west.

Screenshot of the program along with its output is attached.

java Primary U.S. interstate highways are numbered 1-99. Odd numbers (like the 5 or-example-1
User Eeva
by
6.4k points