-2

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, the 405 services the 5, and the 290 services the 90.

Write (define) a public static method named printHighwayInfo, that takes as its only argument a highway number (the arguments will be an int). When this method is called, it should print out a String indicating if the highway is a primary or auxiliary, and if the highway goes east/west or north/south (see examples below). This method should have a return void return type.

Examples: 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.

printHighwayInfo(90) will print The 90 is primary, going east/west. printHighwayInfo(290) will print The 290 is auxiliary, serving the 90, going east/west. printHighwayInfo(185) will print The 185 is auxiliary, serving the 85, going north/south. You may wish to write some additional code to test your method

Hints:

You can use the modulus operator (%) to compute the reminder of an integer division operation.

For example: 290 % 100 will evaluate to 90, because the remainder after dividing 290 by 100 is 90.

As another example: 185 % 2 is 1, because 2 goes into 185, 92 times with 1 left over. Also, note that for any integer x, x % 2 will always evaluate to 1 when x is odd, and it will always evaluate to 0 when x is even.

** this is my first week using java. we've been learning python to this point and I'm having the hardest time with it. I'm getting tons of errors and I don't know why. my class notes and lectures are not helpful like at all.

    import java.util.Scanner;
public class Main {
  static Scanner scnr = new Scanner(System.in);
}
   public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int highwayNumber;
        int primaryNumber;
       {
    public static int printHighwayInfo(int a);
    }
        if (printHighwayInfo >= 1 && highwayNumber <= 999) {
            if (printHighwayInfo <= 99) {
                if (printHighwayInfo % 2 == 0) {
                    System.out.println(printHighwayInfo + " is a primary highway, going east/west");
                } else {
                    System.out.println(printHighwayInfo + " is a primary highway, going north/south");
                }
            } else {
                primaryNumber = highwayNumber;
                highwayNumber %= 100;
                System.out.println(printHighwayInfo + " is an auxiliary highway, serving primary highway " + highwayNumber);
            }
        } else {
            System.out.println(printHighwayInfo + " is not a valid interstate highway number.");
        }
    }
}
{
System.out.println(printHighwayInfo(90));           
System.out.println(printHighwayInfo(290));  
System.out.println(printHighwayInfo(185));
}
}

//these are the errors I'm getting back

HighwayInfo.java:5: error: class, interface, or enum expected
   public static void main(String[] args) {
                 ^
HighwayInfo.java:7: error: class, interface, or enum expected
        int highwayNumber;
        ^
HighwayInfo.java:8: error: class, interface, or enum expected
        int primaryNumber;
        ^
HighwayInfo.java:9: error: class, interface, or enum expected
       {
       ^
HighwayInfo.java:10: error: class, interface, or enum expected
    public static int printHighwayInfo(int a);
                  ^
HighwayInfo.java:11: error: class, interface, or enum expected
    }
    ^
HighwayInfo.java:16: error: class, interface, or enum expected
                } else {
                ^
HighwayInfo.java:18: error: class, interface, or enum expected
                }
                ^
HighwayInfo.java:21: error: class, interface, or enum expected
                highwayNumber %= 100;
                ^
HighwayInfo.java:22: error: class, interface, or enum expected
                System.out.println(printHighwayInfo + " is an auxiliary highway, serving primary highway " + highwayNumber);
                ^
HighwayInfo.java:23: error: class, interface, or enum expected
            }
            ^
HighwayInfo.java:26: error: class, interface, or enum expected
        }
        ^
HighwayInfo.java:31: error: class, interface, or enum expected
System.out.println(printHighwayInfo(290));  
^
HighwayInfo.java:32: error: class, interface, or enum expected
System.out.println(printHighwayInfo(185));
^
HighwayInfo.java:33: error: class, interface, or enum expected
}
^
15 errors
PapayaFrame
  • 1
  • 1
  • 3
  • 1
    you need to write your public static void main inside the class. currently it is out of your class scope. – Hemant Metalia Nov 03 '20 at 03:49
  • 2
    _Remove_ the `}` bracket in the line after your `Scanner` definition. You are prematurely closing off your class definition. That aside, most of your actual logic looks correct to me at a glance. – Tim Biegeleisen Nov 03 '20 at 03:51
  • Do not post a question prior to searching for your answer. If you had searched, including on this site, you would have run into multiple answers pointing out exactly what your error means and how to fix it. You're trying to learn to code now, but the actual lesson here is to think like an engineer. An engineer researches before asking because that's how you learn. – MarsAtomic Nov 03 '20 at 04:42

1 Answers1

0

You would need to make multiple changes in your code.

  1. All the methods should be inside the Main class.
  2. There is no use of scanner, so remove it.
  3. Method printHighwayInfo should have void return type.
  4. Method printHighwayInfo has only one return type so one one variable should be used.
  5. Need to remove unnecessary brackets.

I am not making it logically correct, just helping you to proceed further by removing compilation issue. Further you can work with logics.

public class Main {

  public static void main(final String[] args) {

    printHighwayInfo(90);
    printHighwayInfo(290);
    printHighwayInfo(185);
  }

  public static void printHighwayInfo(final int highwayNumber) {

    if (highwayNumber >= 1 && highwayNumber <= 999) {
      if (highwayNumber <= 99) {
        if (highwayNumber % 2 == 0) {
          System.out.println(highwayNumber + " is a primary highway, going east/west");
        } else {
          System.out.println(highwayNumber + " is a primary highway, going north/south");
        }
      } else {
        System.out.println(highwayNumber + " is an auxiliary highway, serving primary highway " + highwayNumber);
      }
    } else {
      System.out.println(highwayNumber + " is not a valid interstate highway number.");
    }
  }
}
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47