Question

In: Computer Science

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 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);
    }
}

Solutions

Expert Solution


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.
                private Random r;
        
    // 2.) Define a constructor which takes a seed value.
    //     This seed will be used to initialize the
    //     aforementioned Random instance variable.
                public Compass(long seed) {
                        r=new Random(seed);
                }
    // 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(int n){
                        if(n==1)
                                return "EAST";
                        if(n==2)
                                return "WEST";
                        if(n==3)
                                return "NORTH";
                        return "SOUTH";
                }
    //
    // 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.
    //
                public String randomDirection(){
                        return numberToDirection(r.nextInt()%4+1);
                }
    // 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);
    }
}


Related Solutions

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: ");...
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...
import java.util.Scanner; public class Months { // You will need to write a method that makes...
import java.util.Scanner; public class Months { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // 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 = input.nextInt(); String output = monthAsString(month); System.out.println(output); } }
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...
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 =...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: "); input = in.nextLine(); int a = 0; int b = 0; a = Integer.parseInt(input); System.out.println("Enter the second number: "); input = in.nextLine(); b = Integer.parseInt(input); int result = 0; result = a/b; System.out.println("a/b : " + result); } Copy the code to your Main.java file...
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;...
import java.util.Scanner; public class LetterGrade { // The method you write will return a String representing...
import java.util.Scanner; public class LetterGrade { // The method you write will return a String representing a letter // grade (e.g., "A", "A-", "B+", etc.). // The letter grade is determined by the given percentage, according // to the scale specified on page 2 of the class syllabus (available // here: https://kyledewey.github.io/comp110-spring18/syllabus.pdf). // You may assume that the given percentage is between 0.0 and 100.0 // TODO - write your code below this comment.    public static String letterGrade(double percentage){...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT