Can't figure out this Error in Eclipse Java - java

My program keeps hitting a brickwall when I try to run the code. I get the error "Exception in thread "main" java.lang.Error: Unresolved compilation problem: DfinalResult cannot be resolved to a variable". I have tried to read other solutions but so far no luck. Any adivce on what im doing wrong to get this code to run. Also, it should be noted that the issue occurs when I try to call the result from one of my methods to a println.
-Liam
P.s. If the numbers dont work out right now that's okay I havent been able to test it since I cant run it lol.
import java.util.Scanner;
public class mathMecklenburg {
//note: i have a very good idea don't forget it
public static void main(String[] args) {
System.out.println("Enter an integer between 1 and 10 to be used as the first value: ");
Scanner scanint = new Scanner(System.in);
int x = scanint.nextInt();
System.out.println("Enter an integer between 1 and 10 to be used as the second value: ");
int y = scanint.nextInt();
System.out.println("Enter an integer between 1 and 10 to be used as the third value: ");
int z = scanint.nextInt(); //x,y,z will be input values from person
scanint.close();
double A1 = operationOne(x); //A1 is just operation 1 only using x. made double maybe for later difficulty
System.out.println("The answer to part one of the problems is: " + A1);
double A2 = operationTwo(x,y); // A2 is operation 2 using x and y
System.out.println("The answer to part two of the problems is: " + A2);
double A3 = operationThree(x,y,z); //A3 is operation 3 is using x,y,z. make something difficult here
System.out.println("The answer to part three of the problems is: " + A3); //these print text then A3
int IfinalResult = (int) DfinalResult;
System.out.println("The double trouble answer is " + IfinalResult);
//summary of the mentioned declared stuff in this part:
//x,y,z are all input numbers from the person
//A1, A2, A3 are the holders for each operations output i
//currently all of them are same data types maybe make more confusing later
//final answer should be fun also make a method for final answer later to clean up code
// finalAnswer = (Create an equation that utilizes all of the arithmetic operators and all three answers)
//for me to check that numbers work out
//System.out.println("The final answer is:" + IfinalResult); //put the result from the final method here for print);
}
//side note: make sure all the first 3 operations give whole numbers no decimals yet
public static int operationOne(int x) {
int answerOne;
answerOne = x+2;
// answerOne = (Create an equation that utilizes all of the arithmetic operators with the one input parameter)
return answerOne;
}
public static int operationTwo(int x, int y) {
int answerTwo;
answerTwo = x + y;
// answerOne = (Create an equation that utilizes all of the arithmetic operators with both input parameters)
return answerTwo;
}
public static int operationThree(int x, int y, int z) {
int answerThree;
answerThree = x + y + z;
// answerThree = (Create an equation that utilizes all of the arithmetic operators using all three input parameters)
return answerThree;
}
public static double doubleTrouble(int x, int y, int z, int A1, int A2, int A3, int IfinalResult) { //i thought the method name was funny
double Fx = Math.log(A1 - (A2 + A3)); //oops :) final answer x (Fx)
return (int) Fx;
double Fy = Math.exp(7); //final answer y (Fy)
return Fy;
double Fz = Math.asin(Fy)/(200); //final answer z (Fz)
return Fz;
double Fz1 = Math.exp(Fx+Fy+Fz);
double F1 = (Fx+Fy+Fz)*(java.lang.Math.PI)+A1;
double F2 = F1/(x)+(y)/(x)+5*(x+y);
double F3 = F1+F2-A1;
double DfinalResult = F1+F2+2*(F3*Fz1);
}
}

The variable DfinalResult cant be seen outside of the method doubleTrouble and you are using in on the main method.

there is 4 errors in your program
1: there is no variable called. DfinalResult
2: I don't know if you know but when you use the command return it dosn't continue with the code
public static double doubleTrouble(int x, int y, int z, int A1, int A2, int A3, int IfinalResult) {
double Fx = Math.log(A1 - (A2 + A3)); //oops :) final answer x (Fx)
return (int) Fx; "Here you return so the next line wouldn't be executed"
double Fy = Math.exp(7); //final answer y (Fy)
return Fy;
double Fz = Math.asin(Fy)/(200); //final answer z (Fz)
return Fz;
double Fz1 = Math.exp(Fx+Fy+Fz);
double F1 = (Fx+Fy+Fz)*(java.lang.Math.PI)+A1;
double F2 = F1/(x)+(y)/(x)+5*(x+y);
double F3 = F1+F2-A1;
double DfinalResult = F1+F2+2*(F3*Fz1);
}

doubleTrouble() method contains a lot of errors.
You can't write return statements in between.
You didn't declare DfinalResult variable in the main method.
That's why the code didn't compile.
I have fixed the error. Update to this :
public class Sample {
public static void main(String[] args) {
System.out.println("Enter an integer between 1 and 10 to be used as the first value: ");
Scanner scanint = new Scanner(System.in);
int x = scanint.nextInt();
System.out.println("Enter an integer between 1 and 10 to be used as the second value: ");
int y = scanint.nextInt();
System.out.println("Enter an integer between 1 and 10 to be used as the third value: ");
int z = scanint.nextInt(); // x,y,z will be input values from person
scanint.close();
double A1 = operationOne(x); // A1 is just operation 1 only using x. made double maybe for later difficulty
System.out.println("The answer to part one of the problems is: " + A1);
double A2 = operationTwo(x, y); // A2 is operation 2 using x and y
System.out.println("The answer to part two of the problems is: " + A2);
double A3 = operationThree(x, y, z); // A3 is operation 3 is using x,y,z. make something difficult here
System.out.println("The answer to part three of the problems is: " + A3); // these print text then A3
int IfinalResult = (int) doubleTrouble(x, y, z, A1, A2, A3);
System.out.println("The double trouble answer is " + IfinalResult);
}
// side note: make sure all the first 3 operations give whole numbers no
// decimals yet
public static int operationOne(int x) {
int answerOne;
answerOne = x + 2;
// answerOne = (Create an equation that utilizes all of the arithmetic operators
// with the one input parameter)
return answerOne;
}
public static int operationTwo(int x, int y) {
int answerTwo;
answerTwo = x + y;
// answerOne = (Create an equation that utilizes all of the arithmetic operators
// with both input parameters)
return answerTwo;
}
public static int operationThree(int x, int y, int z) {
int answerThree;
answerThree = x + y + z;
// answerThree = (Create an equation that utilizes all of the arithmetic
// operators using all three input parameters)
return answerThree;
}
public static double doubleTrouble(int x, int y, int z, double a1, double a2, double a3) {
double Fx = Math.log(a1 - (a2 + a3)); // oops :) final answer x (Fx)
double Fy = Math.exp(7); // final answer y (Fy)
double Fz = Math.asin(Fy) / (200); // final answer z (Fz)
double Fz1 = Math.exp(Fx + Fy + Fz);
double F1 = (Fx + Fy + Fz) * (java.lang.Math.PI) + a1;
double F2 = F1 / (x) + (y) / (x) + 5 * (x + y);
double F3 = F1 + F2 - a1;
double DfinalResult = F1 + F2 + 2 * (F3 * Fz1);
return DfinalResult;
}
}

Related

(Beginner) - How to I write decimal calc and print results?

this is the question.
Write a Java program to declare two integers and demonstrate the use of
addition, subtraction, multiplication, division and modulo operators.
Repeat the calculations from task 1 using decimal numbers.
So I've done part one:
public static void main(String[] args)
{
float a = 4 + 1;
float b = 484 - 48;
float c = 484 * 49;
float d = 32 / 93;
float e = 55 % 787;
System.out.print(a + "\n" + b + "\n" + c + "\n" + d + "\n" + e);
}
but I'm stuck in part two.
Integers are of type int. You are using float and declaring 5 variables. Decimal numbers are type double
public static void main(String[] args)
{
int a = 1;
int b = 2;
int out = a*b;
System.out.print(out);
}
Repeat using doubles
double a = 1.5;
double b = 2.5;

Generating random quadratic equations imposing condition for real numbers as roots

i deveoped a program to generate random quadratic equations and show their solutions. i took integers from an array containing numbers from -9 to 9, avoiding 0. I chose index by using Random object. but, i get invalid equations a lot , as square of B becomes more than 4AC, the solution is not a real number and i get "NaN" as my solutions. I want to set a condition such as square of B will always be greater than 4AC and the numbers will be taken from the array in such a manner.
my codes are:
import java.util.Random;
class number{
String equation, result;
public void set(){
int[] n = {-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9};
Random r = new Random();
int x = r.nextInt(17);
int xx = r.nextInt(17);
int xxx = r.nextInt(17);
int a = n[x];
int b = n[xx];
int c = n[xxx];
double b1 = 0-b; double ac = 4*a*c ; double b2 = b*b; double rt1 = b2-ac;
double rt = Math.sqrt(rt1); double px1 = b1 + rt ; double px2 = b1 - rt;
double a1 = 2*a; double x1 = px1/a1; double x2 = px2/a1;
equation = String.format("The equation is (%d)X^2 + (%d)X + (%d) ",
a,b,c):
result = String.format("Roots are %.3f and %.3f" , x1, x2);
}
public String geteq(){
return equation; }
public String getres(){
return result; }
then in another class I just assigned them in JTextField in actionListener class of JButton.
Is there any way that, upon clicking the buttton, it will automatically repeat the set() method until square of B is greater than 4AC ?
You can try this:
do {
a = n[r.nextInt(17)];
b = n[r.nextInt(17)];
c = n[r.nextInt(17)];
} while (b*b<=4*a*c);
This way, you can only have real solutions

Java Invoking Methods and returning data

I am brand new to Java… I have an assignment that is discussing Methods. For some reason when I invoke my methods and pass the data back to my main everything is "-Infinity" or "0". I have been trying to resolve this for two days straight and I can't seem to find an appropriate solution.
I'm only including the code for the first portion of the assignment because I have a feeling that whatever mistake I am making… I'm making it throughout the entire assignment. So if someone could assist me with this portion it would allow me to hopefully correct my other issues too.
The first method returns: -Infinity, but when I take the code apart and run it without the use of methods… I get 11.8, which is correct.
Any assistance would be greatly appreciated!
/*
* Anthony Vincenzo Laginess
* CIT 130
* Oct. 12th, 2016
* HMW 07 - Methods
* Time Needed:
*/
package cit130hmw07_laginess;
import java.util.Scanner;
public class CIT130HMW07_Laginess {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//*************************************************
//****************** Method 1 *********************
//*************************************************
//This method calculates body fat. It takes your gender as a parameter
//and outputs your bodyfat.
System.out.println("Please enter your gender...");
String gender = input.nextLine();
System.out.println("Please enter your weight: ");
int bodyWeight = input.nextInt();
System.out.println("Please enter your waist measurement: ");
int waistSize = input.nextInt();
if(gender.equalsIgnoreCase("male")) {
bodyFatMale(bodyWeight, waistSize);
double bodyFatPercentage = bodyFatMale(bodyWeight, waistSize);
System.out.println("Your body fat is: " + bodyFatPercentage); }
else if(gender.equalsIgnoreCase("female")) {
System.out.println("Please enter your waist size: ");
int waist = input.nextInt();
System.out.println("Please enter your hip size: ");
int hips = input.nextInt();
System.out.println("Please enter your forearm size: ");
int forearms = input.nextInt();
bodyFatFemale(bodyWeight, waistSize, waist, hips, forearms);
double answer = bodyFatFemale(bodyWeight, waistSize, waist, hips,
forearms);
System.out.println("Your body fat is: " + answer); }
else
//enter an error message
}//main
//METHOD 1: Bodyfat calculations
public static double bodyFatMale(int bodyWeight, int waistSize) {
int weight = 0;
int waist = 0;
double A1;
double A2;
double B;
double actualBodyFat;
double bodyFatPercentage;
A1 = (weight * 1.082) + 94.42;
A2 = waist * 4.15;
B = A1 - A2;
actualBodyFat = weight - B;
bodyFatPercentage = actualBodyFat * 100 / weight;
return bodyFatPercentage; }
public static double bodyFatFemale(int bodyWeight, double wristSize, double waistSize, double hipSize, double forearmSize) {
int weight = 0;
double wrist = 0;
double waist = 0;
double hips = 0;
double forearms = 0;
double A1, A2, A3, A4, A5, B;
double actualBodyFat;
double bodyFatPercentage;
A1 = (weight * .732) + 8.987;
A2 = wrist / 3.14;
A3 = waist * 0.157;
A4 = hips * 0.249;
A5 = forearms * 0.434;
B = A1 + A2 - A3 - A4 + A5;
actualBodyFat = weight - B;
bodyFatPercentage = actualBodyFat * 100 / weight;
return bodyFatPercentage; }
}//class
In each of your methods, you are setting all of your variables equal to 0, so the calculations inside the methods are being performed with 0s. Instead, you need to assign the variables to the values that you're passing in as parameters.
So instead of
int weight = 0;
try
int weight = bodyWeight;
Hint: In the bodyFat methods, you have two variables for the body Weight and waist size. And you are using the wrong one; i.e. the one that you have initialized to ero. You should only have one.
You are not actually using your method parameters in your calculations.
Here would be the fixed version of your first method:
//METHOD 1: Bodyfat calculations
public static double bodyFatMale(int bodyWeight, int waistSize) {
double A1;
double A2;
double B;
double actualBodyFat;
double bodyFatPercentage;
A1 = (bodyWeight* 1.082) + 94.42;
A2 = waistSize* 4.15;
B = A1 - A2;
actualBodyFat = bodyWeight- B;
bodyFatPercentage = actualBodyFat * 100 / bodyWeight;
return bodyFatPercentage;
}
public static double bodyFatMale(int bodyWeight, int waistSize) {
int weight = 0; // Introducing `0` into all calculations
.....
A1 = (weight * 1.082) + 94.42; // Should be `bodyWeight`?
A2 = waist * 4.15; // Should be `bodyWeight`?
B = A1 - A2;
actualBodyFat = weight - B; // Should be `bodyWeight`?
bodyFatPercentage = actualBodyFat * 100 / weight; // Should be `bodyWeight`?

Does Java have an exponential operator?

Is there an exponential operator in Java?
For example, if a user is prompted to enter two numbers and they enter 3 and 2, the correct answer would be 9.
import java.util.Scanner;
public class Exponentiation {
public static double powerOf (double p) {
double pCubed;
pCubed = p*p;
return (pCubed);
}
public static void main (String [] args) {
Scanner in = new Scanner (System.in);
double num = 2.0;
double cube;
System.out.print ("Please put two numbers: ");
num = in.nextInt();
cube = powerOf(num);
System.out.println (cube);
}
}
There is no operator, but there is a method.
Math.pow(2, 3) // 8.0
Math.pow(3, 2) // 9.0
FYI, a common mistake is to assume 2 ^ 3 is 2 to the 3rd power. It is not. The caret is a valid operator in Java (and similar languages), but it is binary xor.
To do this with user input:
public static void getPow(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter first integer: "); // 3
int first = sc.nextInt();
System.out.println("Enter second integer: "); // 2
int second = sc.nextInt();
System.out.println(first + " to the power of " + second + " is " +
(int) Math.pow(first, second)); // outputs 9
The easiest way is to use Math library.
Use Math.pow(a, b) and the result will be a^b
If you want to do it yourself, you have to use for-loop
// Works only for b >= 1
public static double myPow(double a, int b){
double res =1;
for (int i = 0; i < b; i++) {
res *= a;
}
return res;
}
Using:
double base = 2;
int exp = 3;
double whatIWantToKnow = myPow(2, 3);
There is the Math.pow(double a, double b) method. Note that it returns a double, you will have to cast it to an int like (int)Math.pow(double a, double b).
you can use the pow method from the Math class. The following code will output 2 raised to 3 (8)
System.out.println(Math.pow(2, 3));
In case if anyone wants to create there own exponential function using recursion, below is for your reference.
public static double power(double value, double p) {
if (p <= 0)
return 1;
return value * power(value, p - 1);
}

Getting accurate roots of quadratic equation

This is the code that I have thus far. The goal of the project is to have the user enter any integers for a, b, c for the ax^2+bx+c equation. For some reason I am not getting the correct roots for any numbers that are input into the program. Can anyone point out my wrong doings?
import java.util.*;
public class Quad_Form {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double a = 0;
double b = 0;
double c = 0;
double discrim = 0;
double d = 0;
System.out.println("Enter value for 'a'");
String str_a = sc.nextLine();
a = Integer.parseInt(str_a);
System.out.println("Enter value for 'b'");
String str_b = sc.nextLine();
b = Integer.parseInt(str_b);
System.out.println("Enter value for 'c'");
String str_c = sc.nextLine();
c = Integer.parseInt(str_c);
double x1 = 0, x2 = 0;
discrim = (Math.pow(b, 2.0)) - (4 * a * c);
d = Math.sqrt(discrim);
if(discrim == 0){
x1 = (-b + d) / (2* a);
String root_1 = Double.toString(x1);
System.out.println("There is one root at: " + root_1);
}
else {
if (discrim > 0)
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);
String root_1 = Double.toString(x1);
String root_2 = Double.toString(x2);
System.out.println("There are two real roots at:" + root_1 + "and" + root_2);
}
if (discrim < 0){
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);
String root_1 = Double.toString(x1);
String root_2 = Double.toString(x2);
System.out.println("There are two imaginary roots at:" + root_1 + "and" + root_2);
}
}
}
#Smit is right about one of the issues, but there's a second one as well.
Math.sqrt(discrim) won't work when discrim is negative. You should be taking Math.sqrt(Math.abs(discrim)) instead.
a, b, c, d are double and you are parsing them as Integer. So this could be one of problem.
Use
Double.parseDouble();
Another problem is you can not make square root of negative numbers. This will result in NaN. For that use following, but you should handle that properly to get exact result.
Math.sqrt(Math.abs());
Moreover you should use following formula for getting roots
Taken from Wikipedia Quadratic equation
Class Double
Class Math

Categories