[Show all top banners]

yekloyatri
Replies to this thread:

More by yekloyatri
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 any java programmers here in sajha?
[VIEWED 3098 TIMES]
SAVE! for ease of future access.
Posted on 07-17-07 11:32 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Hi there need some basic programming help in java. If there is anyone who could write it up, i would greatly appreciate it.. So, here it goes..

Write a program to solve quadratic equations. The programe should take as input a string in the form of: ax*2+b*x+c=0
where a, b and c are real valued, non negative numbers.
Your programe should type and print the values of x that solve the equation.
Your program should use the quadratic equation formula x = (-b+sqrt(b*b-4*a*c))/2*a
By inspecting sign of the value b*b-4*a*c inside the square root , you should be able to determine if the equation has two +ve, one zero, no negative solution..
using java.util.Scanner class

if you could write it up with simple if else statement in a very simple and understandable way i would appretiate it.. Thank you in advace and one more thing there are users here would have no idea but would still like to talk garbage by saying something stupid ....please dont bother.. and use the <--- back button.. thanks..
 
Posted on 07-18-07 12:47 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

What does * mean? multiplication or to the power of?
By the way it seems like your homework assignment
, why don't you give it a try yourself. The tricky part is extracting a, b and c.
 
Posted on 07-18-07 12:50 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

import java.util.*;
import java.io.*;


public class quadraticEquation{

public static void main(String[] args){
Scanner Keyboard = new Scanner(System.in);

double a, b, c, discriminant, rootA, rootB;
System.out.println("Please enter a positive value for a. ");
a = Keyboard.nextDouble();

System.out.println("Please enter a positive value for b .");
b = Keyboard.nextDouble();

System.out.println("Please enter a positive value for c .");
c = Keyboard.nextDouble();

discriminant = (Math.pow(b, 2)) - (4*a*c);
if(discriminant == 0.0) {
rootA = (-b + Math.sqrt(discriminant)) / (2 *a);
System.out.println("There is one root and the value of the root is: " + rootA);
}
else {
if(discriminant > 0.0) {
rootA = (-b + Math.sqrt(discriminant)) / (2 * a);
rootB = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("There are two real roots and the value of the roots are:"+ rootA + rootB);
}
else {
if(discriminant < 0.0) {
rootA = (-b + Math.sqrt(-discriminant)) / (2 * a);
rootB = (-b + Math.sqrt(-discriminant)) / (2 * a);
System.out.println("There are two imaginary roots and the value of the roots are: " + rootA + rootB);
}
}
}
System.exit(0);

}
}
 
Posted on 07-18-07 12:55 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

This is what i have till now.. first of its my fault.. it is supposed to be ax^2+bx+c=0 now have you say?
 
Posted on 07-18-07 1:00 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

i've tried it but lets assume that i was to enter these values a=2, b=0 and c=2 .... as per question i am suppoed to get the value of x as

x=1.41421356237....
or
x =-1.41421.......


i tried it but it dosent really come out.. i guess i'm messing something up somewhere.. any way i've been on this for 3 hours now.. got to catch some would be here early in the morning to see the progress.. Anyway thanks guys.. atleas one em bothered to click.. lol
 
Posted on 07-18-07 7:20 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

import java.util.Scanner;
import java.io.*;


public class quadraticEquation{

public static void main(String[] args){
Scanner Keyboard = new Scanner(System.in);
String inputString=args[0];
double[] values=parseInputString(inputString);

double a, b, c, discriminant, rootA, rootB;
a = values[0];

b = values[1];

c = values[2];
discriminant = (Math.pow(b, 2)) - (4*a*c);
if(discriminant == 0.0) {
rootA = (-b + Math.sqrt(discriminant)) / (2 *a);
System.out.println("There is one root and the value of the root is: " + rootA);
}
else {
if(discriminant > 0.0) {
rootA = (-b + Math.sqrt(discriminant)) / (2 * a);
rootB = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("There are two real roots and the value of the roots are:"+ rootA + rootB);
}
else {
if(discriminant < 0.0) {
rootA = (-b + Math.sqrt(-discriminant)) / (2 * a);
rootB = (-b + Math.sqrt(-discriminant)) / (2 * a);
System.out.println("There are two imaginary roots and the value of the roots are: " + rootA + rootB);
}
}
}
System.exit(0);

}

public static double[] parseInputString(String inputString){
double[] d=new double[3];
int sign=1;
int pos1=inputString.indexOf("x*2");
int pos2=inputString.length();
String a=inputString.substring(0,pos1);
d[0]=Double.parseDouble(a);

inputString=inputString.substring(pos1+3,pos2).trim();
if(inputString.charAt(0)=='-')
sign=-1;
else
sign=1;
pos1=inputString.indexOf("x");
String b=inputString.substring(1,pos1);
d[1]=Double.parseDouble(b)*sign;

pos2=inputString.length();
inputString=inputString.substring(pos1+1,pos2).trim();
if(inputString.charAt(0)=='-')
sign=-1;
else
sign=1;
pos2=inputString.length();
String c=inputString.substring(1,pos2);
d[2]=Double.parseDouble(c)*sign;
return d;
}
}

It is basically same program but it takes input in the form of ax*2+bx+c as a
command line parameter.
To run excecute , compile and run as
java quadraticEquation 2x*2-4x+1
and son on. you can have spaces as well if you like.
 
Posted on 07-18-07 7:34 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Awesome guys! suike and divdude thanks so much. Many thanks. Suike you are rite i mean it kinda looks ugly coz i wanted to get it working in the first place rather then it looking neat and not doing aything..

Divdude, thanks for the effort bro. .i tried to compile it and it compiles just fine.. but when i run it.. i get an error.. but i'm working on it... may be i didnt some wrong.. but thanks bro..
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 7 days
Recommended Popular Threads Controvertial Threads
मन भित्र को पत्रै पत्र!
TPS Work Permit/How long your took?
Another Song Playing In My Mind
Does the 180 day auto extension apply for TPS?
Travelling to Nepal - TPS AP- PASSPORT
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters