Question

In: Computer Science

import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...

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

Solutions

Expert Solution

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


Related Solutions

import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
(Using Random Class) The following UML Class Diagram describes the java Random class: java.util.Random +Random() +Random(seed:...
(Using Random Class) The following UML Class Diagram describes the java Random class: java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object with the current time as its seed. Constructs a Random object with a specified seed. Returns a random int value. Returns a random int value between 0 and n (exclusive). Returns a random long value. Returns a random double value between 0.0 and 1.0 (exclusive). Returns...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
I need a pseudocode and UML for this program. import java.util.Random; public class SortandSwapCount {   ...
I need a pseudocode and UML for this program. import java.util.Random; public class SortandSwapCount {    public static void main(String[] args) {        //Generate random array for test and copy that into 3 arrays        int[] arr1=new int[20];        int[] arr2=new int[20];        int[] arr3=new int[20];        int[] arr4=new int[20];        Random rand = new Random();        for (int i = 0; i < arr1.length; i++) {            //Generate random array...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT