The number of calls received per minute at a Help Desk has been estimated to be between 5 and 10.
Write a simulation program that simulates calls arriving at the Help Desk for a period of 12 hours and output the frequency of calls during this period.
Sample output:
(Note: The frequencies for your program will be different from the ones shown below. Each time you run your program, you should get different frequencies)
Calls/Minute Frequency
5 155
6 172
7 148
8 123
9 62
10 60
This is what I've came up with, but cannot figure out how to split/leave a gap between calls/minute and frequency. Basically splitting it into two rows.
import java.util.Random;
public class randomCalls {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rn = new Random();
int n;
for(int i=1;i<=6;i++)
{
n = rn.nextInt(6) +5;
System.out.println("Calls/Minute" +"\n" + n);
System.out.print(' ');
System.out.println(" Frequency" + "\n"+ i);
}
}
}
I'm pretty sure you are trying to print out Calls/Minute and Frequency in columns. Here is a helpful link.
In order to use that method your need to...
Combine the lines System.out.println("Calls/Minute" +"\n" + n); and System.out.println(" Frequency" + "\n"+ i);, and format the resulting single line so that it is split into two columns.
//Example
System.out.printf("%-12.30s %-30.30s%n","Column 1","Column 2");
Place it BEFORE your for loop (or else it will print every time)
Within your for loop, format your output exactly the same as you did the line in step one.
//Example
System.out.printf("%-12.30s %-30.30s%n",n,i);
Sample output in your program would look like this:
Calls/Minute Frequency
6 1
7 2
8 3
8 4
9 5
6 6
Here is how I got it to print like described:
Random rn = new Random();
int n;
for(int i=1;i<=6;i++) {
n = rn.nextInt(6) +5;
System.out.println( n + " " + i );
Hope this helps!
Related
I'm currently working on a program that pits two simulated fighters against one another. The way I have it right now, each round is done and the output is printed all at once (virtually all at once because it is calculated so fast).
How would I take the below code and make it so that actions in the second loop occur in pseudo real time where it executes x amount of seconds where x is a random roll? Any suggestions or guidance would be great. I would even settle for the second while loop executing every three seconds or so. This is a prototype for now and the simulation will get more varied so reading the output may get more interesting.
public static void fight(Character player1, Character player2, int roundMax){
player1.setTempHitPoints(player1.getHitPoints());
player2.setTempHitPoints(player2.getHitPoints());
int r = ROUND;
while(!isFightOver(player1, player2)){
roundTimer = 0;
System.out.println("================");
System.out.println("Round " + r + " FIGHT!");
System.out.println("================");
while(roundTimer < roundMax && !isFightOver(player1, player2)){
roundTimer = roundTimer + Commands.roll(10);
round(player1, player2);
timerPrint(roundTimer, roundMax);
}
System.out.println("================");
System.out.println("Round " + r + " OVER!");
System.out.println("================");
System.out.println("");
if(!isFightOver(player1, player2)){
Commands.rest(player1);
Commands.rest(player2);
}
System.out.println("");
r++;
}
declareWinner(player1, player2);
}
You can use Thread.sleep() to slow down the processing.
E.g. Thread.sleep(1000) causes the current thread to suspend execution for a second (1000 ms). Can you use that method judiciously to put delays into the code? You can use java.util.Random to generate random numbers.
Random random = new Random();
Thread.sleep((random.nextInt(6) + 1) * 1000); // Delays from 1 - 6 secs
I am a college student and this is my first semester learning Java programming. For the past few days I've been stuck on some things when learning Java. One activity I'm stuck on for my class is this one:
Retype the statements, correcting the syntax errors.
System.out.println("Num: " + songnum);
System.out.println(int songNum);
System.out.println(songNum " songs");
Note: These activities may test code with different test values. This activity will perform two tests: the first with songNum = 5, the second with songNum = 9.
This is what I have so far:
import java.util.Scanner;
public class Errors {
public static void main (String [] args) {
int songNum;
songNum = 5;
System.out.println("Num: " + songNum);
System.out.println("5");
System.out.println("5 " + "songs");
The website we are using called Zybooks says the code above is correct for outputting:
Num: 5
5
5 songs
But I can't figure out what to do to output the same but with the number 9. I've tried doing the same 3 lines for it but it says it's not the correct way to do it. How do output both 5 and 9 for the values?
Change the 2nd and 3rd print statements to the following:
System.out.println(songNum);
System.out.println(songNum + " songs");
The problem was you hardcoded the value of 5 within a string making it impossible to change.
You cannot print the number nine with the code that you wrote. if you want to get the number nine then you have to change the code and create a new variable e.g. x = 9;
What you can do something like that:
import java.util.Scanner;
public class Errors {
public static void main (String [] args) {
int songNum;
songNum = 5;
x = 9;
System.out.println("Num: " + songNum);
System.out.println(songNum);
System.out.println(songNum + " songs");
System.out.println("Num: " + x);
System.out.println(x);
System.out.println(x+ " songs");
Your main issue is that you had cached your value (5) into a string, rendering it unchangeable.
I am coding a binary search algorithm and I want to get the count minimum guesses does it take to search the number that I provide.suppose that the number which I provide is 33, then it should count 7 steps.
Step no number guessed result range of possible values
0 1-100
1 50 too high 1-49
2 25 too low 26-49
3 37 too high 26-36
4 31 too low 32-36
5 34 too high 32-33
6 32 too low 33-33
7 33 correct
so this is my code for this
package binarySearch;
public class Binary {
int gussedNo;
public static int count =0;
void search(int lowerBound,int upperBound,int num){
gussedNo=upperBound+lowerBound/2;
count();
if(gussedNo==num){
System.out.println(count);}
else if(gussedNo>num){
upperBound=gussedNo-1;
search(lowerBound,upperBound,num);
}
if(gussedNo<num){
lowerBound=gussedNo+1;
search(lowerBound,upperBound,num);
}
}
int count(){
count=count+1;
return count;
}
}
I created a a separate method. here is my my main class..
package binarySearch;
public class MainClass {
public static void main (String[] args){
Binary search= new Binary();
search.search(1, 100,33 );
}
}
Here I have given lowerbound as 1 and uperbound as 100, and the number I want to count guesses for it is 33.
But when I execute the code I get the count as 68..but it should be 7 according to binary search
Take a look at the line where you create the next guess:
gussedNo=upperBound+lowerBound/2;
Due to mathematical operators precedence in Java, this line is the same as having:
gussedNo=upperBound+(lowerBound/2);
Which is clearly not performing a binary search, and thus, not what you wanted. You can solve this by explicitly adding the brackets:
gussedNo = (upperBound + lowerBound) / 2;
here is your problem
gussedNo=upperBound+lowerBound/2;
you forgot abour operatots order execution
it should be
gussedNo=(upperBound+lowerBound)/2;
This question already has answers here:
Java recursive Fibonacci sequence
(37 answers)
Closed 8 years ago.
I am learning Java and I have this code from the internet and running it in Eclipse:
public class Fibonacci {
public static void main (String [] args) {
for (int counter = 0; counter <= 3; counter++){
System.out.printf("Fibonacci of %d is: %d\n", counter, fibonacci(counter));
}
public static long fibonacci(long number) {
if ((number == 0) || (number == 1))
return number;
else
return fibonacci(number - 1) + fibonacci(number - 2);
}
}
I've tried to understand it but cannot get it. So I run through the code and counter gets passed in through the fibonacci method. As counter starts at 0 and this is what gets passed first, then 1 and I understand the method passes back 0 and then 1.
When it reaches 2: it will return 2-1 + 2-2 = 2 and it does return this.
When it reaches 3: it will return 3-1 + 3-2 = 3 but it does not return 3 it returns 2.
Please can someone explain to me why as I cannot figure this out?
Thanks
First, I have to tell you that this recursive version has a dramatic exponential cost. Once you understand how it works, my advice for you would be to learn about tail recursivity, write a tail-recursive solution, an iterative solution, and compare them to your current method for high values of "number".
Then, your function basically uses the mathematical definition of the Fibonacci sequence :
f0 = 1, f1 = 1, fn = fn-1 + fn-2 for all n >= 2
For example if we call fibonacci(3), this will return fibonacci(2) + fibonacci(1). fibonacci(2) will be executed first and will return fibonacci(1) + fibonnacci(0). Then fibonacci(1) will return immediately 1 since it is a terminal case. It happens the same thing with fibonnacci(0), so now we have computed fibonnacci(2) = 1 + 0 = 1. Let's go back to fibonacci(3) which has been partially evaluated at this point : 1 + fibonnacci(1). We just have to compute fibonnacci(1) and we can finally return 1 + 1 = 2.
Even in this little example, you can see that we evaluated twice fibonacci(1), that is why this version is so slow, it computes many times the same values of the sequence, and it gets worth when "number" is high.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to write the following program sequence.
The first three numbers in the sequence are 1, 1, 2. Every other number in the sequence is the sum of the three previous numbers. The program should prompt the user to enter a limit; the program will stop when the current number in the sequence is greater than or equal to this limit.
For example if I write the limit being 123 I should get: 1 1 2 4 7 13 24 44 81
I tried the following:
import jpb.*;
public class XiaolinSequence {
public static void main(String[] args) {
SimpleIO.prompt("Enter a limit on the largest number to be displayed:");
String userInput = SimpleIO.readLine();
int counter = Integer.parseInt(userInput);
int older = 1;
int old = 1;
int current = 2;
while (current < counter) {
int nextNumber = older + old + current;
older = old;
old = current;
current = nextNumber;
System.out.println(nextNumber);
}
}
}
But I am having trouble getting the sequence to print out.
You need to change how you print things.
The missing 1 1 2 are never printed because they are never stored in nextnumber, the only variable you ever print.
You will get an additional 149 because you print nextnumber without checking it its value is greater than the limit.
For me the output of the following code is 1 1 2 4 7 13 24 44 81 all on new lines.
int counter=123; // replaced IO code so I did not have to download the jar.
int older=1;
int old =1;
int current=2;
System.out.println(older); // prints the first 1
System.out.println(old); // prints the second 1
System.out.println(current); // prints the 2
while(current<counter){
int nextnumber=older+old+current;
older=old;
old=current;
current=nextnumber;
if(nextnumber <= counter)
{
System.out.println(nextnumber);
}
}
Ok since people bashed me for your SimpleIO, use whatever you want to read the input. Instead, I'm going to point out a logic flaw in your code.
For the program to function correctly, you need to print out older instead of current, like so:
while (older < counter)
{
System.out.println(older);
final int nextnumber = older + old + current;
older = old;
old = current;
current = nextnumber;
}
It works just fine.
There is no such thing as SimpleIO in java.lang.
You may want to replace String userInput = SimpleIO.readLine() with
System.out.print("Enter limit: ");
Scanner in = new Scanner(System.in);
String userInput = in.next();
then the code will work.
Oh and by the way, don't forget to print out 1 1 2 before you start the loop.