Using enum, returning null
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:
Code :
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:
Code :
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.
Re: Using enum, returning null
So here is what I'm seeing that could be causing some issues. First the Driver class:
1. You set world[count] to a new instance of World regardless of whether or not one needs to be created.
2. count never gets incremented after a world is created.
3. Right now there is no check for whether or not the count is maxed out or not.
The World class:
1. You have it set so that each instance of this class will create 2 arrays of length 5, I do not see any purpose in this. Each instance only needs one avgTemp and one terrain.
2. The count variable never gets incremented, if you want it to get +1 for each instance created then you will want it to be static. However, if you get rid of the arrays then it isn't needed.
3. the equals method you use returns a boolean, it does not set the value of the variable.
Start out with that and see what you can do. Hope this helped.
Re: Using enum, returning null
Wow. Very obvious this was the first assignment of the semester. Not sure what I was thinking. Thanks.
Re: Using enum, returning null
Took your suggestions on the World class and they worked perfectly. Started from scratch on the driver since it was garbage, lol. Thanks again.