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`?
Related
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;
}
}
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
I made a simple grade system for fun. I'm trying to apply the total of grades and add it to an equation in my getApercent() method. However, I keep getting errors and don't know what to do.
package gradesystem;
import java.util.Scanner;
public class Gradesystem {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Gradesystem gs = new Gradesystem();
// TODO Auto-generated method stub
int Acount,Bcount,Ccount,Dcount,Fcount;
double ap,bp,cp,dp,fp;
System.out.println("Enter the amount of A's");
Acount = keyboard.nextInt();
System.out.println("Enter the amount of B's");
Bcount = keyboard.nextInt();
System.out.println("Enter the amount of C's");
Ccount = keyboard.nextInt();
System.out.println("Enter the amount of D's");
Dcount = keyboard.nextInt();
System.out.println("Enter the amount of F's");
Fcount = keyboard.nextInt();
int grades;
ap = getApercent(Acount);
System.out.println(ap);
bp = getBpercent(Bcount);
System.out.println(bp);
cp = getCpercent(Ccount);
System.out.println(cp);
dp = getDpercent(Dcount);
System.out.println(dp);
fp = getFpercent(Fcount);
System.out.println(fp);
}
public static void Totalgrades(int acount, int bcount, int ccount, int dcount, int fcount){
int totalofgrades = acount + bcount + ccount + dcount + fcount;
System.out.print(totalofgrades);
}
public static double getApercent(int a){
double ap;
ap = (a/a * 100) + 0.5;
return Math.round(ap);
}
public static double getBpercent(int b){
double bp;
bp = (b/b * 100) + 0.5;
return Math.round(bp);
}
public static double getCpercent(int c){
double cp;
cp = (c/c * 100) + 0.5;
return Math.round(cp);
}
public static double getDpercent(int d){
double dp;
dp = (d/d * 100) + 0.5;
return dp;
}
public static double getFpercent(int f){
double fp;
fp = (f/f * 100) + 0.5;
return fp;
}
}
A little bit of guessing here. But the methods that calculate the percentage seem off. Again, assuming; but to calculate the percentage of a whole you would use the following formula percentage = part/whole * 100
e.g.
we have 9 grades and 3 are A's, 3 are B's, 2 are C's, 1 is D's, and 0 are E's.
Then I'd expect the percentages to be as follows:
33% A // 3 / 9 * 100
33% B // 3 / 9 * 100
22% C // 2 / 9 * 100
11% D // 1 / 9 * 100
0% E // 0 / 9 * 100
One other thing to point out is the the operator / with two ints does integer division. So 3 / 9 == 0.
You could replace all the specific methods with a more generic version.
public static double getPercentage(int gradeCount, int totalNumberOfGrades) {
double percentage = ( gradeCount / (double) totalNumberOfGrades * 100);
return Math.round(percentage);
}
that prompts the user for the number of pennies, nickels, dimes, and quarters, and then displays their total dollar amount. The application should include a getDollarAmount() method that has 4 int parameters corresponding to the number of pennies, nickels, dimes, and quarters and returns a String that corresponds to the dollar value of the coins.
the application output should look similar to:
Enter you total coins:
Quarters:3
Dimes:2
Nickels:1
Pennies:8
Total: $1.08
and this is my attempt:
package ch7e5;
import java.util.Scanner;
public class Ch7E5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n1, p, d, n, t;
double Q1, D1, N1, P1;
System.out.println("Enter your total coins:");
System.out.print("Quarters:");
n1 = input.nextInt();
System.out.print("Dimes:");
d = input.nextInt();
System.out.print("Nickles:");
n = input.nextInt();
System.out.print("Pennies:");
p = input.nextInt();
double Q1 = (pennies * 0.01);
private static double calctotal(double Q1, double D1, double P1, double N1) {
double dbltotal;
dbltotal = (Q1 + D1 + P1 + N1);
return dbltotal;
}
}
This is my 2nd attempt with the help of your comments:
package chapter7ex5;
import java.util.Scanner;
public class Chapter7ex5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your total coins:");
System.out.print("Quarters:");
int Q1 = input.nextInt();
System.out.print("Dimes:");
int D1 = input.nextInt();
System.out.print("Nickles:");
int N1 = input.nextInt();
System.out.print("Pennies:");
int P1 = input.nextInt();
}
public static double calctotal(int Q1, int D1, int N1, int P1) {
double total;
total=((0.25 * Q1) + (0.1 * D1) + (0.05 * N1) + (0.01 * P1));
return (total);
}}
I think my attempts are over:
package chapter7ex5;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Chapter7ex5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your total coins:");
System.out.print("Quarters:");
int Q1 = input.nextInt();
System.out.print("Dimes:");
int D1 = input.nextInt();
System.out.print("Nickles:");
int N1 = input.nextInt();
System.out.print("Pennies:");
int P1 = input.nextInt();
DecimalFormat fmt = new DecimalFormat("$#,###.##");
System.out.println("Total:"+fmt.format(calctotal(Q1, D1, N1,
P1)));
}
public static double calctotal(int Q1, int D1, int N1, int P1) {
double total;
total=((0.25 * Q1) + (0.1 * D1) + (0.05 * N1) + (0.01 * P1));
return (total);
}}
Well, one quarter is usually worth 0.25. A dime is worth ten cents and so on. Also, you seem to have swapped nickels and pennies. And your numbers will always be int. Finally, a method does not go within the body of another method. So, move calctotal outside of main() and calculate the values of your coins with something like
private static double calctotal(int Q1, int D1, int N1, int P1) {
return ((0.25 * Q1) + (0.1 * D1) + (0.05 * N1) + (0.01 * P1));
}
You should probably call flush() if you use System.out.print(), it isn't automatic without a newline. Then you could use a DecimalFormat to format your calculate total like
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your total coins:");
System.out.print("Quarters:");
System.out.flush();
int quarters = input.nextInt();
System.out.print("Dimes:");
System.out.flush();
int dimes = input.nextInt();
System.out.print("Nickles:");
System.out.flush();
int nickels = input.nextInt();
System.out.print("Pennies:");
System.out.flush();
int pennies = input.nextInt();
DecimalFormat fmt = new DecimalFormat("$#,###.##");
System.out.println(fmt.format(calctotal(quarters, dimes, nickels,
pennies)));
}
Not bad. Something I noticed is that you only calculate the double value of the pennies, not the quarters, dimes, and nickels. That conversion should also be done inside your function:
private static double calcTotal(int pennies, int nickels, int dimes, int quarters)
{
return ((pennies * 0.01) + (nickels * 0.05) + (dimes * 0.10) + (quarters * 0.25));
}
Outputting the amount is easy too, just use something like this in your main function:
System.out.printf("%.2f",calcTotal(p, n, d, q));
The doubles have have created in your main aren't necessary, and you should change how you get your amounts of coins to:
System.out.print("Quarters: ");
q = Integer.parseInt(input.nextLine());
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);
}