Answer to Question #66328 in Java | JSP | JSF for freddy
Question #66328
Write a new public method in your Beach class called addTurtle(). This method should pick a random x/y location (e.g., using Random.generator()) on the beach and add a single Turtle there.
How do I do this??
How do I do this??
Expert's answer
import java.io.*;
import java.util.Random;
import java.util.Vector;
public class Main
{
public static void main(String[] args) throws IOException
{
//BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
Beach beach = new Beach();
beach.addTurte();
}
}
class Turtle
{
private int mX;
private int mY;
public Turtle(int x, int y)
{
mX = x;
mY = y;
}
public int x()
{
return mX;
}
public int y()
{
return mY;
}
}
class Beach
{
private Vector<Turtle> mVectorTurtle;
public Beach()
{
mVectorTurtle = new Vector<Turtle>();
}
public void addTurte()
{
Random rand = new Random();
Turtle turtle = new Turtle(rand.nextInt(), rand.nextInt());
mVectorTurtle.add(turtle);
}
}
import java.util.Random;
import java.util.Vector;
public class Main
{
public static void main(String[] args) throws IOException
{
//BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
Beach beach = new Beach();
beach.addTurte();
}
}
class Turtle
{
private int mX;
private int mY;
public Turtle(int x, int y)
{
mX = x;
mY = y;
}
public int x()
{
return mX;
}
public int y()
{
return mY;
}
}
class Beach
{
private Vector<Turtle> mVectorTurtle;
public Beach()
{
mVectorTurtle = new Vector<Turtle>();
}
public void addTurte()
{
Random rand = new Random();
Turtle turtle = new Turtle(rand.nextInt(), rand.nextInt());
mVectorTurtle.add(turtle);
}
}
Need a fast expert's response?
Submit orderand get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Comments
Leave a comment