CS0007
Good day, I am in an entry-level Java class and need assistance completing an assignment. Below is the assignment criteria and the source code from the previous assignment to build from. Any help would be appreciated, thank you in advance.
Source Code from the previous assignment to build from shown below:
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
// Project1.java
import java.util.Random;
import java.util.Scanner;
public class Project1 {
static final int SEED = 1;
static Random rng = new Random(SEED);
static double size = 1.0;
// method to fetch number of darts from user. modified the signature to
// accept number of attempts user has
public static int getNumberOfDarts(int attempts_remaining) {
// proceeding only if attempts_remaining>0
if (attempts_remaining > 0) {
Scanner sc = new Scanner(System.in);
// asking and reading an integer
System.out.println("How many darts do you want to throw? (1-2000)");
int n = sc.nextInt();
// validating
if (n >= 1 && n <= 2000) {
// valid, returning
return n;
} else {
// invalid, displaying error and calling and returning
// getNumberOfDarts() passing one less attempts_remaining value
System.out
.println("The number must be in the interval of 1-2000");
return getNumberOfDarts(attempts_remaining - 1);
}
}
// returning -1 to indicate that user did not enter valid value even
// after all attempts
return -1;
}
// method to throw count number of darts and return number of hits
public static double throwDarts(int count) {
// proceeding if count is positive, else returning 0
if (count > 0) {
// storing number of hits for one less number of throw counts,
// recursively
double hits = throwDarts(count - 1);
// generating x,y coordinates between (-size,size)
double x = ((2 * size) * rng.nextDouble()) - size;
double y = ((2 * size) * rng.nextDouble()) - size;
// finding distance betweeen this point and origin (0,0) which is
// the center of circle
double dist = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
// if this distance is less than or equal to radius, returning
// 1+hits, else returning hits
if (dist <= size) {
return 1 + hits;
}
return hits;
} else {
return 0;
}
}
public static void main(String[] args) {
// reading number of darts, passing 5 as number of attempts
int n = getNumberOfDarts(5);
// if returned value is -1, displaying error and exiting
if (n == -1) {
System.out.println("Invalid size.");
System.exit(0);
}
// otherwise finding number of hits
double hits = throwDarts(n);
// displaying results
System.out.println(hits + " points out of " + n
+ " are inside the circle.");
System.out.println("The pi estimate is " + (4.0 * (hits / n)));
}
}
/*OUTPUT*/
How many darts do you want to throw? (1-2000)
-2
The number must be in the interval of 1-2000
How many darts do you want to throw? (1-2000)
20000
The number must be in the interval of 1-2000
How many darts do you want to throw? (1-2000)
1000
798.0 points out of 1000 are inside the circle.
The pi estimate is 3.192
CS0007 Good day, I am in an entry-level Java class and need assistance completing an assignment....
I am in an entry-level Java class and need assistance solving questions 20-25 for a homework assignment. Any help is appreciated. Thank you. Q20 Read the code and answer the question 6 Points Read each of the following code snippets and determine if they are equivalent. I.e. they produce the same output. Q20.1 Are these snippets equivalent? 2 Points Snippet 1: int i=0; while (i<10) System.cut.print(i); i++; 1 Snippet 2: for(int i-0; i<10; i++) System.out.print(i); } Yes O No Q20.2...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this: public static double divide(double operand1, double operand2) { return operand1 / operand2; } The random method is supposed to return a double within a lower limit and...
Here is the assignment: Fibonacci or Prime number iterator: Design a menu driven program that performs the user’s choice of the following functions the program exits should also be a user’s choice. Menu Item 1: Fibonacci number iterator, here you are to define an iterator class named FibonacciIterator for iterating Fibonacci numbers. The constructor takes an argument that specifies the limit of the maximum Fibonacci number. For example, prompt the user for size, use the size to call FibonacciIterator(“user input”)...
Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...
Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...
*JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> { private int top, size; private E arrS[]; private static final int MAX_STACK_SIZE = 10; public ArrayStack() { this.arrS = (E[]) new Object[MAX_STACK_SIZE]; this.top = size; this.size = 0;...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
I have spent so much time on this and I am having trouble getting all the methods to work together correctly to run the code right, and I am not sure what is wrong with it. /* You will recreate one or two versions of the logic game shown in the Algorithms movie and you will ask which one the user would like to play. Upon completion of the game, display who won and ask if they would like to...