Java programming I need help with this problem below: Now consider the formula w
ID: 662674 • Letter: J
Question
Java programming
I need help with this problem below:
Now consider the formula waxbyczd, where each of a, b, c, and d is one of the 17 numbers {-5, -4, -3, -2, -1, -1/2, -1/3, -1/4, 0, 1/4, 1/3, 1/2, 1, 2, 3, 4, 5}. The "charming theory" asserts that the with your four personal numbers can be used to approximate ? within a fraction of 1% relative error. For example, suppose you choose to approximate the number: ? = 238,900. Then you select the numbers 14, 102,329,1936 and 13. Then the value of 14-5102329119361/2134 is about 239,103, which is within about 0.08% of ?.
Your job is to create a Java program that asks the user what constant ? should be approximated, and then asks in turn for each of the four personal numbers w, x, y, and z. The program should then calculate and report the values of the exponents a, b, c, and d that bring the de Jager formula as close as possible to ?, as well as the value of the formula waxbyczd and the relative error of the approximation to the nearest hundredth of one percent.
Create a class called 123What1.
Edit 123What1.java to satisfy the problem requirements stated above, as well as the following additional requirement: use only while loops for iteration.
Copy 123What1.java to create 123What2.java. Change it so the while loops are replaced by for loops, and so it uses at least one private static method.
My code is so far this:
import java.util.Scanner;
public final class 123What1 {
public static void main(String[] args) {
double u;
double w;
double x;
double y;
double z;
double exp[] = {-5, -4, -3, -2, -1, -1.0/2, -1.0/3, -1.0/4, 1.0/4,
1.0/3, 1.0/2, 1, 2, 3, 4, 5};
double a = 0;
double b = 0;
double c = 0;
double d = 0;
int acount = 0;
int bcount = 0;
int ccount = 0;
int dcount = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number related to something that fascinates you: ");
u = in.nextDouble();
System.out.println("Lets see how closely you are related to "+u+" !!");
System.out.println("Please your favorite number: ");
w = in.nextDouble();
System.out.println("You entered: "+w);
System.out.println("Now, please enter your street number: ");
x = in.nextDouble();
System.out.println("Cool. So far you have entered: "+w+" & "+x);
System.out.println("Now please the year you were born: ");
y = in.nextDouble();
System.out.println("Great! Your numbers so far are: "+w+", "+x+" & "+y);
System.out.println("Lastly, please Math.pow(a,exp);enter your birth month/day (MMDD): ");
z = in.nextDouble();
System.out.println("Epic!! Your numbers are: "+w+", "+x+", "+y+" & "+z);
while (acount < exp.length){
bcount = 0;
while(bcount < exp.length){
ccount = 0;
while(ccount < exp.length){
dcount = 0;
while(dcount < exp.length){
Math.pow(a,exp);
Math.pow(b,exp);
Math.pow(c,exp);
Math.pow(d,exp);
}
dcount++;
}
ccount++;
}
bcount++;
}
acount++;
}
}
I cant figure out how to code the rest of it, and I continue to get an error on my .pow s. How can I transfer it into a for loop instead?
I really appreciate your time?
Explanation / Answer
Please see the corrected code below:
import java.util.Scanner;
public final class 123What1 {
public static void main(String[] args) {
double u;
double w;
double x;
double y;
double z;
double[] exp = {-5, -4, -3, -2, -1, -1/2, -1/3, -1/4, 1/4, 1/3, 1/2, 1, 2, 3, 4, 5};
double a = 0;
double b = 0;
double c = 0;
double d = 0;
int acount = 0;
int bcount = 0;
int ccount = 0;
int dcount = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a number related to something that fascinates you: ");
u = in.nextDouble();
System.out.println("Lets see how closely you are related to "+u+" !!");
System.out.println("Please your favorite number: ");
w = in.nextDouble();
System.out.println("You entered: "+w);
System.out.println("Now, please enter your street number: ");
x = in.nextDouble();
System.out.println("Cool. So far you have entered: "+w+" & "+x);
System.out.println("Now please the year you were born: ");
y = in.nextDouble();
System.out.println("Great! Your numbers so far are: "+w+", "+x+" & "+y);
System.out.println("Lastly, please Math.pow(a,exp);enter your birth month/day (MMDD): ");
z = in.nextDouble();
System.out.println("Epic!! Your numbers are: "+w+", "+x+", "+y+" & "+z);
while (acount < exp.length){
bcount = 0;
while(bcount < exp.length){
ccount = 0;
while(ccount < exp.length){
dcount = 0;
while(dcount < exp.length){
Math.pow(a,exp[dcount]);
Math.pow(b,exp[dcount]);
Math.pow(c,exp[dcount]);
Math.pow(d,exp[dcount]);
}
dcount++;
}
ccount++;
}
bcount++;
}
acount++;
}
}
You were passing double array exp in the while loop, instead of passing each element in each iteration whereas pow method takes one element at a time. I have modified the codes here:
while(dcount < exp.length){
Math.pow(a,exp[dcount]);
Math.pow(b,exp[dcount]);
Math.pow(c,exp[dcount]);
Math.pow(d,exp[dcount]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.