patternjavaModerate
Completed Town Generator
Viewed 0 times
towngeneratorcompleted
Problem
I am a beginner, and I believe I made a simple and efficient program to do it in. I can add more methods for more detail for future updates, but it's a workable bit.
```
import java.util.Random;
import java.util.Scanner;
public class TownGenerator
{
private static Scanner k;
public static void main (String [] args)
{
System.out.println("Ayyy, here's a town for you. Want a City, Town, or Village?");
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
k = new Scanner(System.in);
String TownType = k.nextLine();
if (TownType.equalsIgnoreCase("City"))
{
System.out.println("You chose a City, here it is!");
String name1 = city();
String name2 = city2();
System.out.println("Then name of the city is: "+name1+name2+"!");
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
String name3 = percent();
String name4 = demo();
String name5 = demo2();
System.out.println("The populas is comprised "+name3+" "+name4+" and the next leading majority being "+name5);
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
String name6 = status();
System.out.println("This Cities general behaviour is"+name6);
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
String name7 = geo();
String name8 = geo2();
System.out.println("The Cities geography is"+name7+name8);
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
if (TownType.equalsIgnoreCase("Town"))
{
System.out.println("You chose a Town, here it is!");
String name1 = town();
String name2 = town2();
System.out.println("Then name of the town is: "+name1+name2+"!");
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
System.out.println("
```
import java.util.Random;
import java.util.Scanner;
public class TownGenerator
{
private static Scanner k;
public static void main (String [] args)
{
System.out.println("Ayyy, here's a town for you. Want a City, Town, or Village?");
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
k = new Scanner(System.in);
String TownType = k.nextLine();
if (TownType.equalsIgnoreCase("City"))
{
System.out.println("You chose a City, here it is!");
String name1 = city();
String name2 = city2();
System.out.println("Then name of the city is: "+name1+name2+"!");
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
String name3 = percent();
String name4 = demo();
String name5 = demo2();
System.out.println("The populas is comprised "+name3+" "+name4+" and the next leading majority being "+name5);
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
String name6 = status();
System.out.println("This Cities general behaviour is"+name6);
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
String name7 = geo();
String name8 = geo2();
System.out.println("The Cities geography is"+name7+name8);
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
if (TownType.equalsIgnoreCase("Town"))
{
System.out.println("You chose a Town, here it is!");
String name1 = town();
String name2 = town2();
System.out.println("Then name of the town is: "+name1+name2+"!");
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
System.out.println("
Solution
There are two major concepts your code needs.
Arrays
This code can be easily rewritten if the Strings are stored in an array:
Each and every one of your methods that return a random string can be rewritten like that.
Although you should know that it is better to re-use the
You can make the
Then all methods can access it, without creating their own.
Objects
Your towns and villages and cities are essentially the same. I don't want to give you too much information right now, but I would write a
I would strongly recommend reading this tutorial on the subject: http://docs.oracle.com/javase/tutorial/java/javaOO/
You could make one class for a
You seem to know how to make methods but you need to learn how to make non-static methods. When a method is non-static it belongs to an object instead of in a "global" space.
Once you have improved your code a bit, I would recommend coming back here and post a new, follow-up, question
- Arrays
- Objects
Arrays
public static String status()
{
Random r = new Random();
int a = 1+r.nextInt(10);
switch (a)
{
case 1:
return " joyus and filled with spirit.";
case 2:
return " in a state of unrest.";
case 3:
return " hostile towards strangers.";
case 4:
return " open to newcommers and glad";
case 5:
return " varied. Depending on who you ask.";
case 6:
return " Strange and unsetting.";
case 7:
return " disgusting and obnoxious.";
case 8:
return " afraid of strange people, fear is in them.";
case 9:
return " happy and full of cultured indeviduals who love sharing their works of art.";
case 10:
return " proud, they love their home.";
default:
throw new IllegalStateException("Something went wrong!");
}
}This code can be easily rewritten if the Strings are stored in an array:
public static String status() {
String[] values = { " joyus and filled with spirit.",
" in a state of unrest.",
" hostile towards strangers.",
" open to newcommers and glad",
" varied. Depending on who you ask.",
" Strange and unsetting.",
" disgusting and obnoxious.",
" afraid of strange people, fear is in them.",
" happy and full of cultured indeviduals who love sharing their works of art.",
" proud, they love their home.",
};
Random r = new Random();
return values[r.nextInt(values.length)];
}Each and every one of your methods that return a random string can be rewritten like that.
Although you should know that it is better to re-use the
Random object. You should not create a new one each and every time as that reduces the "randomness".You can make the
Random like this once in your class:public static final Random random = new Random();Then all methods can access it, without creating their own.
Objects
Your towns and villages and cities are essentially the same. I don't want to give you too much information right now, but I would write a
class to represent a Town/Village/City.I would strongly recommend reading this tutorial on the subject: http://docs.oracle.com/javase/tutorial/java/javaOO/
You could make one class for a
Town, one for Village and one for City, although essentially as it's the same thing in the core you could make just one class for it all.You seem to know how to make methods but you need to learn how to make non-static methods. When a method is non-static it belongs to an object instead of in a "global" space.
Once you have improved your code a bit, I would recommend coming back here and post a new, follow-up, question
Code Snippets
public static String status()
{
Random r = new Random();
int a = 1+r.nextInt(10);
switch (a)
{
case 1:
return " joyus and filled with spirit.";
case 2:
return " in a state of unrest.";
case 3:
return " hostile towards strangers.";
case 4:
return " open to newcommers and glad";
case 5:
return " varied. Depending on who you ask.";
case 6:
return " Strange and unsetting.";
case 7:
return " disgusting and obnoxious.";
case 8:
return " afraid of strange people, fear is in them.";
case 9:
return " happy and full of cultured indeviduals who love sharing their works of art.";
case 10:
return " proud, they love their home.";
default:
throw new IllegalStateException("Something went wrong!");
}
}public static String status() {
String[] values = { " joyus and filled with spirit.",
" in a state of unrest.",
" hostile towards strangers.",
" open to newcommers and glad",
" varied. Depending on who you ask.",
" Strange and unsetting.",
" disgusting and obnoxious.",
" afraid of strange people, fear is in them.",
" happy and full of cultured indeviduals who love sharing their works of art.",
" proud, they love their home.",
};
Random r = new Random();
return values[r.nextInt(values.length)];
}public static final Random random = new Random();Context
StackExchange Code Review Q#77360, answer score: 17
Revisions (0)
No revisions yet.