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...
(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...
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]...
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;...
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) {       ...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // As a hint, you should not have to use the name of each // month more than once. // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT