Java-problems - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Java-problems (2537 Views)
| Java-problems by Knownpal(op): 10:47am On Nov 05, 2013 |
Hey guys, this thread is created by me, for learning and posting the problems en-counted by me during my personal programming lectures ..I would be posting the difficulty I'm having while learning the Almighty JAVA, and I hope I would be helped. The book I'm currently using is INTRODUCTION TO JAVA PROGRAMMING 8TH EDITION Y Daniel Liang. The problems I had so far are: 1: Solved!! How do you generate a random integer i such that 0 <= i < 20. Answer: I invoked this statement double i = Math.random()*20; System.out.println((int) i); 2. Unsolved: How do you generate a random integer i such that 10 <= i < 20 [Do not Use conditional Statement]. 3. Unsolved: How do you generate a random integer i such that 10 <= i <= 50 [Do not use conditional Statement]. Please do any one have the idea? if so, kindly explain how, on this thread. |
| Re: Java-problems by codeaddict(m): 12:31pm On Nov 05, 2013*. Modified: 12:53pm On Nov 05, 2013 |
Known_pal: 2. Unsolved:10 + (int) (Math.random() * 10) Known_pal: 3. Unsolved:10 + (int) (Math.random() * 41) |
| Re: Java-problems by Knownpal(op): 2:53pm On Nov 05, 2013*. Modified: 3:43pm On Nov 05, 2013 |
codeaddict: 10 + (int) (Math.random() * 10)Wow, worked. Thanks man. The '10' before them should mean the starting point of possible numbers to be printed. Thanks for your time. Please note that I would be posting any Java problems I have on this thread than creating a new thread, please follow this thread. |
| Re: Java-problems by Nobody: 7:45am On Nov 07, 2013 |
codeaddict: 10 + (int) (Math.random() * 41)Sorry i do not understand how this is so. Where did 41 come from? Thanks |
| Re: Java-problems by codeaddict(m): 10:38am On Nov 07, 2013 |
Johnpaul88: Sorry i do not understand how this is so. Where did 41 come from?Math.random() returns a floating point number in the range 0 <= x < 1 So the highest integer value you get, when you multiply with 41 is 40, since int casting truncates the decimal digits. |
| Re: Java-problems by Knownpal(op): 3:35pm On Nov 09, 2013*. Modified: 3:51pm On Nov 09, 2013 |
Unresolved!: import java.util.Scanner; class AlgebraCalc { public static void main (String args[]){ int a,b,c; double r1,r2; Scanner input = new Scanner (System.in) ; System.out.print("Enter a , b , c respectively: " ;a = input.nextInt(); b = input.nextInt(); c = input.nextInt(); r1 = -b + Math.pow(b * b - 4 * a * c, 0.5)/ 2 * a; r2 = -b - Math.pow(b * b - 4 * a * c, 0.5) /2 * a; if ((b * b - 4 * a * c) > 0) System.out.println("The roots are " + r1 + " and " + r2 ); else if ((b * b - 4 * a * c) < -1) System.out.println("The root is " + r2 ); else System.out.println("No Real Roots" ); } } After Compiling, the result I got was "The root is NaN" though I used 1 2 3 as my a b c. Any Help and corrections? |
| Re: Java-problems by codeaddict(m): 4:26pm On Nov 09, 2013 |
A lot of things could cause that. One of the more prominent causes in your code is trying to find the root of a negative number |
| Re: Java-problems by jboy01(m): 9:37am On Nov 10, 2013 |
Known_pal: Unresolved!:If u do the calculation with hand, u will see that with ur input 1,2,3, the Math.pow(...) Will find d squareRoot of (-eight) which is NOT A NUMBER (NAN). Try to change the input value to like ( a is 10, b is 1 and c is 2) to comfirm. |
| Re: Java-problems by Knownpal(op): 4:51pm On Nov 10, 2013 |
Thanks guys, would give you feed back. Wanna try solving it with paper and pencil then compile it. |
| Re: Java-problems by Knownpal(op): 9:39am On Nov 11, 2013 |
I've corrected it. I changed the (< -1) to (<0) so it print out the The equation has no real roots (1 2 3). And I changed the pattern Java uses by overriding the precedence with parenthesis. Thanks codeaddict and jboy. |
Most Of Your Java Problems Solved • 2 • 3 • 4
Can I Be A Good Programmer Without Studying Computer Science In The University? • Final Year Project Topic In Mechanical Software Application • Checkout This Nairaland Client I'm Working On
;