BMI Calculator Java incorrect output [duplicate] - java

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 5 years ago.
I am trying to code a basic command line BMI Calculator in Java and for some reason every time I run the code and enter my height and weight, 0 is outputted. Please help me understand where I have made a mistake.
import java.util.Scanner;
public class Chap2 {
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.println("Enter your height in inches: ");
int myHeight = reader.nextInt();
System.out.println("Enter your weight in lbs: ");
int myWeight = reader.nextInt();
int Bmi = (myWeight/myHeight/myHeight)*703;
System.out.println("Your BMI is " + Bmi + ".");
}
}
And my output is as follows:
Enter your height in inches:
68
Enter your weight in lbs:
180
Your BMI is 0.

You are dividing int with int, thus the result is a rounded int.
Cast to double before calculation to get exact results.
double bmi = ((double) myWeight / myHeight / myHeight) * 703;

Related

Calculate BMI using command line arguments inputs

first question here. I have to use a previous assignment of calculating BMI, and reformat it to accept command line arguments as inputs for height and weight.
"Your program shall obtain the weight and the height via main(String[] args), i.e,, when you run your program you must do the following:
java MyProgramName 180 5 7
where MyProgramName is the name of your program, 180 is the weight in pounds, 5 is the feet and 7 is the inch values.
The program shall output the BMI value in the terminal window as it was before (item f below)."
I am confused on how to call the arguments into the code while performing operands on them.
Here is my original code:
'int weight;
int heightInInches;
int bmi;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter your weight in pounds: ");
weight = keyboard.nextInt();
System.out.print("Enter your height in inches: ");
heightInInches = keyboard.nextInt();
bmi = ((weight * 703)/(heightInInches * heightInInches));
System.out.println("Your height is " + heightInInches + " and your
weight is: " + weight + " pounds");
System.out.println("Your BMI is " + bmi);'
I have seen something like this for just adding two numbers, but am confused how to alter it to the BMI formula.
int sum = 0;
for (int i = 0; i < args.length; i++) {
sum = sum + Integer.parseInt(args[i]);
}
System.out.println("The sum of the arguments passed is " + sum);
Thanks
Your String[] args would look something like the s: ["180","5","7"]
So args[0] would be your weight.
args[1] would be the heightInFeet (multiply by 12 to get heightInInches)
args[2] would be the inches part of the height
So the code becomes:
int weight = Integer.parseInt(args[0]);
int heightInInches = Integer.parseInt(args[1])*12 + Integer.parseInt(args[2]);
bmi = ((weight * 703)/(heightInInches * heightInInches));

Simple way to limit decimal places to two places [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 5 years ago.
public static void main(String[] args) {//begin main
Scanner worksheet = new Scanner(System.in);
{
double radi;
double area;
double circum;
double pie = 3.14;
System.out.println("Enter the radius: ");
radi = worksheet.nextDouble();//enter 1.2
area = pie * radi * radi;
Area comes out as 4.521599999999999, but I want it to be 4.52.
System.out.println("The area is:" + area);
circum=2*pie*radi;
circum comes out as 7.536.
want it to be 7.53
System.out.println("The circumference is:"+circum);
}
How can I truncate the value to 2 decimal places without a method.
You can try
DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("The circumference is:"+df.format(circum));
or you can simply do this
int temp = (int)(circum*100);//452
circum = temp/100d;//4.52
Both of these will limit the value up to 2 decimal points.
Wish it will solve the problem.
Reference (Take a look at this)

JAVA - How to generate Random number between 2 integers? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
i'm trying to read in two integer numbers from the console and then display a random integer number generated between these two numbers.
this is my code.
import java.awt.*;
import java.io.*;
import java.util.*;
import java.math.*;
public class Question8{
public static void main(String[] args)
{
int first, second;
Scanner myScanner = new Scanner(System.in);
System.out.println("Enter first integer: ");
int numOne;
numOne = myScanner.nextInt();
System.out.println("You have keyed in " + numOne);
System.out.println("Enter second integer: ");
int numTwo;
numTwo = myScanner.nextInt();
System.out.println("You have keyed in " + numTwo);
Random generator = new Random();
//int num = generator.nextInt(numOne) + numTwo;
System.out.println("Random number: " + numOne + generator.nextInt(numTwo - numOne));
}
}
after i execute this program..
i do not get the random number that's between the 2 integers.
this is my output:
Enter first integer:
20
You have keyed in 20
Enter second integer:
30
You have keyed in 30
Random number: 204
hope you guys can help me in any way you can (:
Make sure that first is always the smaller value and use new Random().nextInt(second - first) + first;. first is in the range of possible generated numbers, second is exclusive.
A random generator (Method in java.lang.Math?) will generate a random double x, where 0 <= x <= 1. Just multiply x with the range and add any offset to the product.

What is wrong with the formula in my code? [duplicate]

This question already has answers here:
Why double width = 50/110000; the output is 0.000000000000000?
(3 answers)
Closed 9 years ago.
We've been set a task to calculate user input. My code compiles, however when I test it my output for the refund is always 0 :(
Users are meant to enter their distance and self contribution when prompted, but what exactly is wrong with my formula for refund? Can anybody shed some light on this for me?
import java.util.*;
public final class UserCalc {`
public static void main(String[] args) {
Scanner scanner = new Scanner(System. in );`
System.out.print("Please enter the distance ");
int distance = scanner.nextInt();
System.out.print("Please enter the percentage of self contribution ");
int selfcon = scanner.nextInt();
int trainprice = (75 + (2 / 10)) * distance;
int carprice = (26 + (7 / 10)) * distance;
int refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();
}
}
Because result of ((100 - selfcon)/100) is always zero and you works with integers you should use float or double.
You are using integer arithmetic rather than floating point arithmetic, so expressions like 2/10 evaluate to 0.
double distance = scanner.nextDouble();
System.out.print("Please enter the percentage of self contribution ");
double selfcon = scanner.nextDouble();
double trainprice = (75 + (2 / 10)) * distance;
double carprice = (26 + (7 / 10)) * distance;
doulbe refund = (Math.min(trainprice, carprice)) * ((100 - selfcon) / 100);
System.out.print("You get a refund of " + refund + " pounds");
int age = scanner.nextInt();

how to get two variables to print with one in decimal form

I am very new. apologies in advance for my coding. I need to print a table that shows year and then a tab over, and then the value with a next line. The value has to be in decimal form.
I have been reading and searching and mixing my code around. I have found it for 1 variable but not for two in same line. I have tried the printf, I have tried the good ol 100 / 100.0 and I either get errors or the decimal never goes to 2 places. I do not need it rounded, just with 2 spaces after. I am obviously going wrong somewhere. I would appreciate any assistance.
import java.util.Scanner;
public class Investment1 {
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){
double principal = 0.0;
double futureInvestmentValue = 0;
for (years = 1; years <=30; years++){
//calculate futureInvestmentValue
futureInvestmentValue = (investmentAmount * (Math.pow (1 + monthlyInterestRate, years * 12)));
System.out.print(years + "\t" + futureInvestmentValue + "\n");
}//end for
return futureInvestmentValue;
}//end futureInvestmentValue
public static void main(String[] args){
Scanner input = new Scanner (System.in);
//obtain Investment amount
System.out.print("Enter Investment amount: ");
double investmentAmount = input.nextDouble();
//obtain monthly interest rate in percentage
System.out.print("Enter annual interest rate in percentage: ");
double annualInterestRate = input.nextDouble();
double monthlyInterestRate = (annualInterestRate / 1200);
int years = 30;
System.out.println("Years\t" + "Future Value");
System.out.print(years + "\t");
System.out.print(years + "\t" + ((int)(futureInvestmentValue(investmentAmount, monthlyInterestRate, years))) + "\n");
}//end main
}//end Investment
You can use system.out.format():
System.out.format("%d \t %.2f", years, futureInvestmentValue);
you should read about format strings, heres a simple usage example:
System.out.println(String.format("%d %.2f",myint,myfloat));
myint will be printed as an integer (even if it's a floating point value) due to the use of the %d in the format string.
myfloat will be printed as a decimal number with 2 digits after the decimal point, thanks to the %f.2 part in the format string.

Categories