In: Computer Science
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table:
| Compass Direction | Integer ID | 
|---|---|
| North | 0 | 
| Northeast | 1 | 
| East | 2 | 
| Southeast | 3 | 
| South | 4 | 
| Southwest | 5 | 
| West | 6 | 
| Northwest | 7 | 
Further details are in the comments of Compass.java.
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 the direction corresponding to the given
    //     number. A snippet of code has been provied starting
    //     this implementation.
    //
    // 4.) Define an instance method named randomDirection
    //     which generates a valid random direction number
    //     and returns a String representing that direction.
    //     You MUST call numberToDirection to perform this
    //     determination. The number for the parameter to
    //     numberToDirection should be derived from the
    //     Random instance created in the constructor.
    //
    // As a hint, the overall structure of this file is
    // based on the structure used in ScoreDice.java from
    // the previous lab. You may wish to consult that file
    // as an example.
    //
    
    // DO NOT MODIFY main!
    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);
    }
}
hey there ! i am done with the code ,, Please give me a like to appreciate my work and efforts...
here is the code -
package Compass;
import java.util.Random;
import java.util.Scanner;
public class Compass {
    long seed;
// You will need to do the following:
    //
    // 1.) Define a private instance variable which
can
    //     hold a reference to a
Random object.
  
    Random random;
    // 2.) Define a constructor which takes a seed
value.
    //     This seed will be
used to initialize the
    //     aforementioned Random
instance variable.
    private Compass(long seed) {
        this.seed=seed;
        random=new
Random();
      
    }
  
    //
    //
    // 3.) Define a static method named
numberToDirection
    //     which takes a
direction number and returns a String
    //     representing the
direction corresponding to the given
    //     number. A snippet of
code has been provied starting
    //     this
implementation.
    //
    public static String numberToDirection(long
directionno)
        {
           
if(directionno==0)
           
{
               
return "North";
           
}
           
else if(directionno==1)
           
{
               
return "Northeast";
           
}
           
else if(directionno==2)
           
{
               
return "East";
           
}
           
else if(directionno==3)
           
{
               
return "Southeast";
           
}
           
else if(directionno==4)
           
{
               
return "South";
           
}
           
else if(directionno==5)
           
{
               
return "Southwest";
           
}
           
else if(directionno==6)
           
{
               
return "West";
           
}
           
else if(directionno==7)
           
{
               
return "Northwest";
           
}
          
              
           
else
              
               
return "";
        }
// 4.) Define an instance method named randomDirection
    //     which generates a
valid random direction number
    //     and returns a String
representing that direction.
    //     You MUST call
numberToDirection to perform this
    //     determination. The
number for the parameter to
    //     numberToDirection
should be derived from the
    //     Random instance
created in the constructor.
    private String randomDirection() {
        int directionno
=random.nextInt((int) seed);
       
System.out.println(directionno);
        String
direction=numberToDirection(directionno);
        return direction;
    }
//
    // As a hint, the overall structure of this file
is
    // based on the structure used in ScoreDice.java
from
    // the previous lab. You may wish to consult
that file
    // as an example.
    //
  
    // DO NOT MODIFY main!
    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);
    }
  
}
and the snapshot of the output is --


Thank You ! Dont forget to like the code !