So this was a homework problem, but it's already been turned in. My instructor just hinted that we would be expanding on this program later, so I'm hoping to fix it. We were supposed to create a program that used a World class to create new worlds. I chose to use an enum and a random number generator to randomly choose a terrain for each world, then assign it a random temperature. Problem is, everything returns null. I get no errors from the compiler (eclipse), so it's obviously a logical error. I hate using enums, and could have done it easily several other ways, but now I'm frustrated and would appreciate any help fixing this so I can finally learn enums properly. Here is the World class:
import java.util.Random; public class World { // attributes of every world private final int MAX_WORLDS = 5; private String[] terrain = new String[MAX_WORLDS]; private String[] name = new String[MAX_WORLDS]; private int[] avgTemp = new int[MAX_WORLDS]; private int count = 0; // random number generator Random rand = new Random(); // enum to pick random terrains private enum terrainType {MOUNTAINOUS, DESERT, PLAINS, COASTAL}; // method will create a world with random terrain and temp public void createWorld() { terrainType ter = terrainType.values()[rand.nextInt(4)]; //pickTerrain(ter); terrain[count] = new String(); //int avgTemp[count] = new int[5]; // assigns an average temp based on terrain switch(ter) { case MOUNTAINOUS: terrain[count].equals("Mountainous"); avgTemp[count] = rand.nextInt(40)+50; break; case DESERT: terrain[count].equals("Desert"); avgTemp[count] = rand.nextInt(30)+90; break; case PLAINS: terrain[count].equals("Plains"); avgTemp[count] = rand.nextInt(40)+50; break; case COASTAL: terrain[count].equals("Coastal"); avgTemp[count] = rand.nextInt(20)+60; break; default: System.out.println("error"); break; } count++; } // displays worlds created public String displayWorld(int a) { return "Terrain: " + terrain[a] + "\nAverage Temp: " + avgTemp[a]; } /*private terrainType pickTerrain(terrainType ter) { int terrain = rand.nextInt(4); return terrainType.values()[terrain]; }*/ }
And here is the driver I used to test it:
import java.util.Scanner; public class Driver { // ------------------------------------------ // The main allows the user to create worlds // ------------------------------------------ public static void main(String[] args) { int choice = 3; int count = 0; World[] world = new World[5]; // executes until the user decides to quit while(choice != 0) { Scanner scan = new Scanner(System.in); System.out.println("Enter 1 to create a world\nEnter 2 to Display current world\n0 to quit"); choice = scan.nextInt(); world[count] = new World(); // evaluates user decision if(choice == 1) { world[count].createWorld(); } // prints current world else if(choice == 2) { System.out.println(world[count].displayWorld(count)); } } } }
I know it's pretty sloppy, but a lot of it was improvised trying to fix errors. Any help would be much appreciated.