|
I have a program that works, what it does is ask you for a name of a cat and will keep doing till untill you input no.
Then it prints the cats names with a random age between 0 and 20.
The problem is i need to use imports,required instance variables and methods!!
it must used three files called cat, catcreator and gamehelper.
gamehelper should use the method getuserinput(string).
I dont have a clue where to start changing my program.
My code is below.
import javax.swing.*;
public class gametest{
public static void main(String[] args) {
int i;
i=0;
int max = 20;
String cat[]=new String[5];
String string1="no";
cat=JOptionPane.showInputDialog("enter a name");
while(!cat.equals(string1)){
i++;
cat=JOptionPane.showInputDialog("enter a name");
}
System.out.print("You entered");
System.out.print( i);
System.out.print(" cats. They are:\n");
java.util.Random rand = new java.util.Random();
for (int counter=0;counter<i;counter++)
{System.out.print(cat[counter]);
System.out.print( " is ");
System.out.print(rand.nextInt(max + 1));
System.out.print( " years old\n");
}
System.exit(0);
}
}
|
|
|
public class GameHelper
{
public static void main(String[] args)
{
CatCreator cc = new CatCreator();
int size = 5;
int count = 0;
Cat[] cats = new Cat[size];
String exitSignal = "no";
Cat cat;
do
{
cat = cc.getUserInput();
if(exitSignal.equalsIgnoreCase(cat.name))
break;
cats[count++] = cat;
}
while(count < size);
for(int j = 0; j < cats.length; j++)
{
cat = cats[j];
if(cat != null)
System.out.println("cat " + cat.name +
" is " + cat.age + " years old");
}
}
}
|
|
public class Cat
{
String name;
int age;
public Cat(String s, int age)
{
name = s;
this.age = age;
}
}
|
|
import java.util.Random;
import javax.swing.JOptionPane;
public class CatCreator
{
Random seed = new Random();
final int max = 20;
public Cat getUserInput()
{
String name = JOptionPane.showInputDialog(null, "enter a name");
int age = seed.nextInt(max+1);
Cat cat = new Cat(name, age);
return cat;
}
}
|
|
|
|
|
|
|
|
|
|