Having trouble with a java program dealing with returning and receiving methods. I have no clue how to call my method.
The Scanner variable is the only global variable. The variable size will be local to 2 methods.
setOcean(): This method prompts for the ocean and returns what is captured from the keyboard.
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).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.- 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. - 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.
- 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
- Largest
- Second Largest
- Third Largest
- Fourth Largest
- 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
- Largest
- Second Largest
- Third Largest
- Fourth Largest
- Smallest
From the above list, select the size of the Arctic Ocean: 6
Try again! Re-enter selection.
- Largest
- Second Largest
- Third Largest
- Fourth Largest
- Smallest
From the above list, select the size of the Arctic Ocean: 0
Try again! Re-enter selection.
- Largest
- Second Largest
- Third Largest
- Fourth Largest
- 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!