₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,298 members, 8,421,222 topics. Date: Saturday, 06 June 2026 at 02:29 AM

Toggle theme

Java-problems - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingJava-problems (2537 Views)

1 Reply (Go Down)

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):
Known_pal: 2. Unsolved:
How do you generate a random integer i such that 10 <= i < 20 [Do not Use conditional Statement].
10 + (int) (Math.random() * 10)

Known_pal: 3. Unsolved:
How do you generate a random integer i such that 10 <= i <= 50 [Do not use conditional Statement].
10 + (int) (Math.random() * 41)
Re: Java-problems by Knownpal(op):
codeaddict: 10 + (int) (Math.random() * 10)


10 + (int) (Math.random() * 41)
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?
Thanks
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):
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: "wink ;
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!:


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: "wink ;
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?
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.
1 Reply

Most Of Your Java Problems Solved234

Can I Be A Good Programmer Without Studying Computer Science In The University?Final Year Project Topic In Mechanical Software ApplicationCheckout This Nairaland Client I'm Working On