In: Computer Science
import java.util.Random;
import java.util.Scanner;
public class Compass {
public Random r;
public Compass(long seed){
r = new Random(seed);
}
public static String numberToDirection(int a){
if(a==0)
return "North";
if(a==1)
return "NorthEast";
if(a==2)
return "East";
if(a==3)
return "Southeast";
if(a==4)
return "South";
if(a==5)
return "Southwest";
if(a==6)
return "West";
if(a==7)
return "Northwest";
return "Invalid Direction" ;
}
public String randomDirection(){
return numberToDirection(r.nextInt()% 4 + 1);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter seed: ");
long seed = input.nextLong();
Compass compass = new Compass(seed);
String direction = compass.randomDirection();
System.out.println("Random direction: " + direction);
}
}
NEED HELP WITH TEST FILE
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CompassTest {
@Test
public void test0IsNorth() {
assertEquals("North", Compass.numberToDirection(0));
}
@Test
public void testOutOfRange8() {
assertEquals("Out of range: 8",
Compass.numberToDirection(8));
}
@Test
public void testSeed123() {
Compass c = new Compass(123l);
assertEquals("Southwest",
c.randomDirection());
}
// TODO - write your code below this comment.
// You will need to write at least SEVEN tests for
// your numberToDirection method you wrote in
// Compass.java. Each test should test a different
// behavior of numberToDirection. While you may
// tests which are redundant with the tests above,
// you'll still need to be sure to test each
// behavior.
//
// If you're not sure you're testing all the
// behaviors, don't hesitate to ask!
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CompassTest {
@Test
public void test0IsNorth() {
assertEquals("North", Compass.numberToDirection(0));
}
@Test
public void testOutOfRange8() {
assertEquals("Out of range: 8", Compass.numberToDirection(8));
}
@Test
public void testSeed123() {
Compass c = new Compass(123l);
assertEquals("Southwest", c.randomDirection());
}
@Test
public void testSeedNorthEast() {
Compass c = new Compass(123l);
assertEquals("NorthEast", c.randomDirection());
}
@Test
public void testSeedEast() {
Compass c = new Compass(123l);
assertEquals("East", c.randomDirection());
}
@Test
public void testSeedSouthEast() {
Compass c = new Compass(123l);
assertEquals("Southeast", c.randomDirection());
}
@Test
public void testSeedSouth() {
Compass c = new Compass(123l);
assertEquals("South", c.randomDirection());
}
@Test
public void testSeedSouthwest() {
Compass c = new Compass(123l);
assertEquals("Southwest", c.randomDirection());
}
@Test
public void testSeedwest() {
Compass c = new Compass(123l);
assertEquals("West", c.randomDirection());
}
@Test
public void testSeedNorthWest() {
Compass c = new Compass(123l);
assertEquals("Northwest", c.randomDirection());
// TODO - write your code below this comment.
// You will need to write at least SEVEN tests for
// your numberToDirection method you wrote in
// Compass.java. Each test should test a different
// behavior of numberToDirection. While you may
// tests which are redundant with the tests above,
// you'll still need to be sure to test each
// behavior.
//
// If you're not sure you're testing all the
// behaviors, don't hesitate to ask!
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me