codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  changing program into three seperate ones  sidewayson2wheels at 13:30 on Saturday, February 11, 2006
 

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);


}
}



  Re: changing program into three seperate ones  crwood at 06:01 on Monday, February 13, 2006
 


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;
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  setSize and setTitle
•  web publishing problem
•  C++ and Passwording Files
•  Disabling/enabling the textbox of EditCommandColumn based on value
•  Re: Character Arrays
•  Subject: regarding making a setup using Package and Deployment Wizard
•  java script
•  Re: how to extract applet class ?
•  Wrong number of arguments or invalid property assignment


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


© Copyright codetoad.com 2001-2006