0

Having trouble with a java program dealing with returning and receiving methods. I have no clue how to call my method.

  1. The Scanner variable is the only global variable. The variable size will be local to 2 methods.

  2. setOcean(): This method prompts for the ocean and returns what is captured from the keyboard.

  3. setSize(): This method receives the name of the ocean, prompts for a descriptive size of the
    ocean and returns what is captured. A list of descriptive sizes is displayed (see sample output).

  4. The user selects from the list. Based on the selection a switch is used to determine the
    descriptive size: Largest, Second Largest, Third Largest, Fourth Largest, Smallest or display the error message: Try again! Re-enter selection. Use a do/while to re-prompt if the choice is anything less than 1 or greater than 5. There'll be 2 variables: one to store the selection from the list and the other to store the size as "Largest" or "Second Largest", etc. The switch uses the first variable to determine the value in the second variable or print the error message.

  5. setKilometers(): This method receives the name of the ocean, prompts for the total area of the
    ocean in kilometers and returns what is captured from the keyboard.
  6. calcTotalAreaMiles(): This method receives the number of kilometers and the conversion factor of 0.621, converts the kilometers into miles, and returns the total area calculated as miles.
  7. printOceanInfo(): This method receives the ocean name, the size, and the total area in miles and
    prints this information (see sample output).

SAMPLE OUTPUT

THE 5 WORLD OCEANS

Enter an ocean: Southern

  1. Largest
  2. Second Largest
  3. Third Largest
  4. Fourth Largest
  5. Smallest

From the above list, select the size of the Southern Ocean: 4

Enter the total area for the Southern Ocean in kilometers: 20.327

OCEANS OF THE WORLD

Ocean: Southern Size: Fourth Largest Total Square Mile: 12.623

Enter another ocean? y

Enter an ocean: Arctic

  1. Largest
  2. Second Largest
  3. Third Largest
  4. Fourth Largest
  5. Smallest

From the above list, select the size of the Arctic Ocean: 6

Try again! Re-enter selection.

  1. Largest
  2. Second Largest
  3. Third Largest
  4. Fourth Largest
  5. Smallest

From the above list, select the size of the Arctic Ocean: 0

Try again! Re-enter selection.

  1. Largest
  2. Second Largest
  3. Third Largest
  4. Fourth Largest
  5. Smallest

From the above list, select the size of the Arctic Ocean: 5

Enter the total area for the Arctic Ocean in kilometers: 14.056

OCEANS OF THE WORLD

Oceans: Arctic Size: Smallest Total Square Miles: 8.729 million

Enter another ocean? n

This is what I have so far.

import java.util.Scanner;

public class OceanMLE52


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




  public static void main(String[] args)

  {//BEGIN main()
    char answer = 'Y';

    do
    {
      System.out.printf("\n\nThe 5 World Oceans");


      setOcean();

      setSize(ocean);

      setKilometers(ocean);

      calcTotalAreaMiles(oceanKilo, conversion);

      printOcean(ocean, oceanSizeName, totalSqMiles);

      input.nextLine();

      System.out.printf("%nEnter another ocean?  ");
      answer = input.nextLine().charAt(0);

    } while (Character.toUpperCase(answer)=='Y');

    System.exit(0);
  } //END OF MAIN()

  public static String setOcean() //Prompts for ocean and stores it in a variable that the other methods can access
  {
    String ocean = "";
    System.out.printf("%nEnter an ocean :");
    ocean = input.nextLine();

    return ocean;
  }


  public static void setSize(String ocean) //Prompts for descriptive size of ocean and stores it in a variable
                               //accessible to the other methods. The user selects from the list.
                               //Based on the selection a switch is used to determine the descriptive
                               //Error message is displayed if one
  {
    int selection = 0;
    do
    {

      System.out.printf("%n1. Largest %n2. Second Largest %n3. Third Largest %n4. Fourth Largest"
                          + "%n5. Smallest" + "%nFrom the above list, select the size of the 5 Ocean:  ", ocean);
      selection = input.nextInt();
      String oceanSizeName = "";

      switch(selection)
      {
        case 1: oceanSizeName = "Largest";
        break;
        case 2: oceanSizeName = "Second Largest";
        break;
        case 3: oceanSizeName = "Third Largest";
        break;
        case 4: oceanSizeName = "Fourth Largest";
        break;
        case 5: oceanSizeName = "Smallest";
        break;
        default: System.out.printf("%nTry Again! Re-enter selection.  %n");
      }

    } while (selection < 1 || selection > 5); 

  }

  public static double setKilometers(String ocean)
  {
      double oceanKilo = 0;

    System.out.printf("%nEnter total area for the %s Ocean in kilometers:  ", ocean);
    oceanKilo = input.nextDouble();

    return oceanKilo;
  }

  public static double calcTotalAreaMiles(double oceanKilo, double conversion )
  {
   double totalSqMiles = 0;
    totalSqMiles = oceanKilo * conversion; 

    return totalSqMiles;
  }

  public static void printOcean(String ocean, String oceanSizeName, double totalSqMiles)
  {

    System.out.printf("\n\nOCEANS OF THE WORLD" +
                      "\n\nOcean:  %s" + 
                      "\n\nSize: %s" + 
                      "%nTotal Square Miles: %.3f million", ocean, oceanSizeName, totalSqMiles);


  }


}

Im not sure how to call the different methods.

Please help!

Mel
  • 21
  • 2
  • What do you mean with `I'm not sure how to call the different methods`? You're calling methods in your program already. Btw, please read this question: [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx). Your mix of `input.nextLine()`, `input.nextInt()` and `input.nextDouble()` can cause some problems. – Tom Nov 10 '14 at 00:19
  • I mean that every time I try to compile I continue to get the error code "Cannot Find Symbol" "Symbol: variable ocean" "Location: class OceanLE52" and that error continues for all of my method calls. – Mel Nov 10 '14 at 00:26
  • 1
    Your variable `ocean` is declared and initialized inside your method `setOcean()` which means it only exists in there. You can't use it in `main` like this `setSize(ocean);`. You need something like `String s = setOcean();` then use `setSize(s);`. – takendarkk Nov 10 '14 at 00:31

2 Answers2

0

The main issue I'm seeing is that you're returning things from your methods but not assigning them to any variables to be passed into your other methods. For example, your setOcean method returns a string, you then pass a variable called ocean into setSize but there's no local variable called ocean in your main method. You should assign the method to a string variable called ocean, i.e. String ocean = setOcean(); Any other methods that return something that you need to pass into another method should be called and assinged similarly.

dogwin
  • 183
  • 2
  • 14
0

You need to properly use variables to get this to work. I also used what Tom said about combining input.nextInt() and input.nextLine() and here you go:

import java.util.Scanner;

public class OceanMLE52 {

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


public static void main(String[] args)

{//BEGIN main()
    char answer = 'Y';
    String ocean = "";
    String oceanSizeName = "";
    double oceanKilo = 0;
    double conversion = .38610; // convert km^2 to mi^2 
    double totalSqMiles = 0;

    do
    {
        System.out.printf("\n\nThe 5 World Oceans");

        ocean = setOcean();

        oceanSizeName = setSize(ocean);

        oceanKilo = setKilometers(ocean);

        totalSqMiles = calcTotalAreaMiles(oceanKilo, conversion);

        printOcean(ocean, oceanSizeName, totalSqMiles);

        input.nextLine();

        System.out.printf("%nEnter another ocean?  ");
        answer = input.nextLine().charAt(0);

    } while (Character.toUpperCase(answer)=='Y');

    System.exit(0);
} //END OF MAIN()

public static String setOcean() //Prompts for ocean and stores it in a variable that the other methods can access
{
    String ocean = "";
    System.out.printf("%nEnter an ocean :");
    ocean = input.nextLine();

    return ocean;
}


public static String setSize(String ocean) //Prompts for descriptive size of ocean and stores it in a variable
//accessible to the other methods. The user selects from the list.
//Based on the selection a switch is used to determine the descriptive
//Error message is displayed if one
{
    int selection = 0;
    String oceanSizeName = "";
    do
    {

        System.out.printf("%n1. Largest %n2. Second Largest %n3. Third Largest %n4. Fourth Largest"
                + "%n5. Smallest" + "%nFrom the above list, select the size of the 5 Ocean:  ", ocean);
        selection = input.nextInt();

        switch(selection)
        {
        case 1: oceanSizeName = "Largest";
        break;
        case 2: oceanSizeName = "Second Largest";
        break;
        case 3: oceanSizeName = "Third Largest";
        break;
        case 4: oceanSizeName = "Fourth Largest";
        break;
        case 5: oceanSizeName = "Smallest";
        break;
        default: System.out.printf("%nTry Again! Re-enter selection.  %n");
        }

    } while (selection < 1 || selection > 5); 
    input.nextLine(); // consume the newline
    return oceanSizeName;
}

public static double setKilometers(String ocean)
{
    double oceanKilo = 0;

    System.out.printf("%nEnter total area for the %s Ocean in kilometers:  ", ocean);
    oceanKilo = input.nextDouble();
    input.nextLine(); // consume the newline
    return oceanKilo;
}

public static double calcTotalAreaMiles(double oceanKilo, double conversion )
{
    double totalSqMiles = 0;
    totalSqMiles = oceanKilo * conversion; 

    return totalSqMiles;
}

public static void printOcean(String ocean, String oceanSizeName, double totalSqMiles)
{

    System.out.printf("\n\nOCEANS OF THE WORLD" +
            "\n\nOcean:  %s" + 
            "\n\nSize: %s" + 
            "%nTotal Square Miles: %.3f million", ocean, oceanSizeName, totalSqMiles);


}

}

Ben Minton
  • 118
  • 5