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??
1
Expert's answer
2017-03-16T10:08:06-0400
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);
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS