mastabri
Nov 8, 2002, 02:18 AM
can you tell me what's wrong? it always goes to the error statement
package program3;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
FormattedInput keyboard = new FormattedInput();
int a,b,c,d;
double x = 0;
System.out.println("Enter the integer coefficients:");
a = keyboard.intRead();
b = keyboard.intRead();
c = keyboard.intRead();
d = keyboard.intRead();
/* Check for non-cubic or trival answer */
if( check_input(a,b,c,d) == false )
// If it is a non cubic or has trival answer, quit the program.
System.exit(0);
/*
double y;
y=cubic(a,b,c,d,x);
double z;
z=cubic_slope(b,c,d,x);
*/
int count=1;
while(count<=1000){
x= x-(cubic(a,b,c,d,x)/cubic_slope(b,c,d,x));
if(verify_root(a,b,c,d,x)==true)
System.out.println(a+ " +" + b + " *x+" + c + " *x^2+" + d + "*x^3 has root" + x);
else
count++;
}
System.out.println(a+ " +" + b + " *x+" + c + " *x^2+" + d + " *x^3 does not converge");
}
public static double cubic(int a, int b, int c, int d,double x){
double cubic;
cubic= a + b*x + c*(x*x) + d*(x*x*x);
return cubic;
}
public static double cubic_slope(int b, int c, int d,double x){
double cubic_slope;
cubic_slope= b + 2*c*x + 3*d*(x*x);
return cubic_slope;
}
public static boolean check_input(int a, int b, int c, int d){
if (a==0){
System.out.println(a+ " +" + b + " *x+" + c + " *x^2+" + d + " *x^3 has a trivial solution");
return false;
}
if (d==0){
System.out.println(a+ " +" + b + " *x+" + c + " *x^2+" + d + " *x^3 is not a cubic");
return false;
}
return true;
}
public static boolean verify_root(int a, int b, int c, int d,double x){
boolean verify_root;
if((a + b*x + c*(x*x) + d*(x*x*x))==0){
verify_root=true;
return true;
}
return false;
}
package program3;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
FormattedInput keyboard = new FormattedInput();
int a,b,c,d;
double x = 0;
System.out.println("Enter the integer coefficients:");
a = keyboard.intRead();
b = keyboard.intRead();
c = keyboard.intRead();
d = keyboard.intRead();
/* Check for non-cubic or trival answer */
if( check_input(a,b,c,d) == false )
// If it is a non cubic or has trival answer, quit the program.
System.exit(0);
/*
double y;
y=cubic(a,b,c,d,x);
double z;
z=cubic_slope(b,c,d,x);
*/
int count=1;
while(count<=1000){
x= x-(cubic(a,b,c,d,x)/cubic_slope(b,c,d,x));
if(verify_root(a,b,c,d,x)==true)
System.out.println(a+ " +" + b + " *x+" + c + " *x^2+" + d + "*x^3 has root" + x);
else
count++;
}
System.out.println(a+ " +" + b + " *x+" + c + " *x^2+" + d + " *x^3 does not converge");
}
public static double cubic(int a, int b, int c, int d,double x){
double cubic;
cubic= a + b*x + c*(x*x) + d*(x*x*x);
return cubic;
}
public static double cubic_slope(int b, int c, int d,double x){
double cubic_slope;
cubic_slope= b + 2*c*x + 3*d*(x*x);
return cubic_slope;
}
public static boolean check_input(int a, int b, int c, int d){
if (a==0){
System.out.println(a+ " +" + b + " *x+" + c + " *x^2+" + d + " *x^3 has a trivial solution");
return false;
}
if (d==0){
System.out.println(a+ " +" + b + " *x+" + c + " *x^2+" + d + " *x^3 is not a cubic");
return false;
}
return true;
}
public static boolean verify_root(int a, int b, int c, int d,double x){
boolean verify_root;
if((a + b*x + c*(x*x) + d*(x*x*x))==0){
verify_root=true;
return true;
}
return false;
}
