Answer to Question #47621 in Java | JSP | JSF for Rajeev Kumar Tadepalli




 Part I (Chapter 4 &amp; 5): Guess a number from 1 to 100  

Example of Run-time Output Console   

Welcome to the Guess the Number Game  

++++++++++++++++++++++++++++++++++++  

  

I'm thinking of a number from 1 to 100.  

Try to guess it.  

  

Enter number: 50  

You got it in 1 tries.  

Great work! You are a mathematical wizard.  

  

Try again? (y/n): y  

  

I'm thinking of a number from 1 to 100.  

Try to guess it.  

  

Enter number: 50  

Way too high! Guess again.  

  

Enter number: 30  

Too high! Guess again.  

  

Enter number: 15  

Too low! Guess again.  

  

Enter number: 23  

Too high! Guess again.  

  

Enter number: 19  

Too low! Guess again.  

  

Enter number: 21  

Too high! Guess again.  

  

Enter number: 20  

You got it in 7 tries.  

Not too bad! You've got some potential.  

  

Try again? (y/n):  

Error! This entry is required. Try again.  

Try again? (y/n): x  

Error! Entry must be 'y' or 'n'. Try again.  

Try again? (y/n): n  

  

Bye - Come back soon!  

  

Press any key to continue . . .  

Operation  

• Write a class called GuessGame with all business logics for guess game, Use the Validator class in
The answer to the question is available in the PDF file https://www.assignmentexpert.com/https://www.assignmentexpert.com/homework-answers/programming-answer-47621.pdf
Here, this should do it. I just replaced the

 
```
System.out.println(isPrime(n));

```
 part, which would print true or false, with an if

 
```
if (isPrime(n))
    System.out.println("The number is prime.\n");
else 
    System.out.println("The number is not prime.\n");

```
 Here's the code:

 
```
import java.util.Scanner;
public class PrimeNumber{
    public static void main(String args[]){
        int n;
        Scanner Prime = new Scanner(System.in);
        System.out.println(" Please enter a number: ");
        n = Prime.nextInt();
        if (isPrime(n))
            System.out.println("The number is prime.\n");
        else 
            System.out.println("The number is not prime.\n");
    }

    static boolean isPrime(int n) {
        if (n%2==0)return false;
        for(int i=3;i*i<=n;i+=2) {
            if(n%i==0)
                return false;
        }
        return true;
    }
} 

```
 This is a trivial task. You should probably read your coursebook/tutorials, I bet you'll find the answers there. I will preface this by saying that I don't know Java at all. I touched upon it in school 20+ years ago, but I've been doing PHP ever since.


However, on line 27, you say that if the input passed in is a double, execute this code block *blah*.


The user enters -1.


Is -1 not a double? It is a valid double, and therefore the code block executes, setting the priceFromUser to be -1 and adding it to the list.


Then, when the loop starts back up again on line 23, priceFromUser is now -1, so the loop breaks and prints the tally.


Also, note you're definining this constant SENTINEL variable, but you're not actually using it anywhere in your code.


Perhaps do something like this instead, changing the while loop into a do-while loop:



```
System.out.println("Please enter item price to start, since there needs to be at least 1 item:");

do
{

        // Keep asking for a price until the user enters a valid price
        while (!in.hasNextDouble())               
        {
                trash=in.nextLine(); //throw away the input 
                System.out.println("You did not enter a valid price. Please try again:");
        }

        priceFromUser=in.nextDouble(); //read value from user
        list.add(priceFromUser); //add valid price to the list            

          System.out.println("Please enter another item price, or enter -1 to quit.");

} while (priceFromUser != SENTINEL)
```

Note this isn't valid code because I'm not a Java programmer, but hopefully it helps you out a bit. start with 50 easys. if you solve those quickly, then move into mediums. hards are pretty tricky tbh i just attempt then peek answers/memorize lol Does he ever get a Heap Space exception? Does he ever even fill at least have of the Heap Space? If not, it may just be that the JVM hasn't felt that a GC run has been necessary yet. Also, even if it has, Java only reserves from the system the min heap space, then, as that is surpassed will periodically reserve more until the Maximum Heap Space has been reached. ***But*** the JVM *never* releases that memory back to the System. So, even though the memory usage of the JVM *seems* to go up and up and up, from the System view, that doesn't mean there is any kind of memory problem. Find a profiler and monitor the actual Heap Space, not the system memory usage of the JVM. Reason: `s` starts at 1 and is incremented on first call, but is never reset. On seconds and subsequent calls `s` no longer starts at 1. `s` is actually unnecessary as you can use the loop counter `i` instead.



A more conventional way to do this sort of thing would be with a single function, not embedded in an object, and to control the detailed behaviour by passing parameter(s).



```
function loadGame(level){
	var values;
	switch(level){
		case 'hard':
			values: [7,8,9],
		break;
		case 'normal':
			values: [4,5,6],
		break;
		default://easy
			values: [1,2,3],
	}
	for(i=0;i<=this.values.length;i++){
		document.getElementById('n'+i).innerHTML=this.values[i];
	}
}
```


```
<div id="n0"></div>
	<div id="n1"></div>
	<div id="n2"></div>
	<input type="button" value="easy" onClick="loadGame('easy')"/>
	<input type="button" value="normal" onClick="loadGame('normal')"/>
	<input type="button" value="hard" onClick="loadGame('hard')"/>
```

**Airshow** `stream`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

of `PriorityBlockingQueue`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

follows the order `Iterator`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

, which according to the [documentation](https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=) :



>  The iterator provided in the iterator () method is not guaranteed to traverse the PriorityBlockingQueue elements in any particular order.
> 
> 
> 


If you want order of precedence, you need `poll`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

items from `PriorityBlockingQueue`
  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  

.



  




 (adsbygoogle = window.adsbygoogle || []).push({});
 
  


```
PriorityBlockingQueue<Integer> pq = new PriorityBlockingQueue<>();
pq.add(5);
pq.add(8);
pq.add(3);

System.out.println("-- Try 1 --");
pq.stream().forEach(System.out::println);

System.out.println("-- Try 2 --");
IntStream.range(0, pq.size()).map(i -> pq.poll()).forEach(System.out::println);

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
```

Conclusion (this may depend on Java implementation):



```
-- Try 1 --
3
8
5
-- Try 2 --
3
5
8

        

        
        
        
 (adsbygoogle = window.adsbygoogle || []).push({});
 
        

    
``` and what about guessing? are you good at guessing? try to guess if i am or not, if you guess right you choose your reward ;p Your explanation is not consistent. You state that the user is to enter a range as in "find me all numbers between x and y that are divisible by both 3 and 4." Later you state that you "input value 120 10 times". That is inconsistent with the problem so either the problem is stated incorrectly or you are not inputting the expected data. Decide what is the correct problem then write it up in pseudo-code before you write the actual code.


If a range is expected, make sure you test that it is a valid range (x <= y). > StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
> 
> 


(API doc for StringTokeniser)



You should do a lot better by reading one whole line at a time then splitting it (String split method) on the tab delimiters to get a String array. I haven't tested this but you should get array elements of "" where there are consecutive tabs, although it may be better to trim() the elements to get rid of any spurious blanks. 1 a c d
2 a
3 c
4 b
5 c
i got a 100%