int single = 0, doub=0, triple=0, homer=0, atbats=0, totalbase, totalhits;
double slug, battingavg;
Scanner sc = new Scanner(System.in);
System.out.print("Enter singles (-1 to end): ");
single = sc.nextInt();
while (single != -1)
{
System.out.print("Enter doubles: ");
doub = sc.nextInt();
System.out.print("Enter triples: ");
triple = sc.nextInt();
System.out.print("Enter home runs: ");
homer = sc.nextInt();
System.out.print("Enter total at bats: ");
atbats = sc.nextInt();
System.out.print("Enter the player's name: ");
String name = sc.next();
totalbase = (single + doub * 2 + triple * 3 + homer * 4);
slug = totalbase / atbats;
battingavg = (single + doub + triple + homer) / atbats;
System.out.println("Player's name is " + name);
System.out.printf("The slugging percentage is %.3f\n", + slug);
System.out.printf("The batting percentage is %.3f\n", + battingavg);
System.out.print("Enter singles (-1 to end): ");
single = sc.nextInt();
}
This program will only output a 1 or a 0 after the calculations. Everything else works fine, but It just doesn't seem to do the calculations.
The problem is that when you do integer divisions, you'll get integer values. Instead use double or float data types. I see slug and battingavg are already doubles, but you're assigning the result of an integer division to them. If you cast at least one of the values in your calculations to a double you should get the output you expect. Example:
slug = totalbase / (double) atbats;
battingavg = (single + doub + triple + homer) / (double) atbats;
When you divide integer values and store the result in a double that is a widening conversion, but the value was calculate as an integer and thus you're widening the integer value.
Change this,
slug = totalbase / atbats;
battingavg = (single + doub + triple + homer) / atbats;
to something like this,
slug = ((double) totalbase / atbats);
battingavg = ((double) (single + doub + triple + homer) / atbats);
to get double values into your double variables.
Related
I am attempting to write a program that will list the amount of kilograms, grams, and milligrams in a given input. Ex. in the given input of 1050042mg, the output will say that there is 1 kilogram, 50 grams, and 42 milligrams.
import java.util.Scanner;
class milligrams {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int milligram;
double kilo, gram;
System.out.println("Enter the weight in milligrams: ");
milligram = in.nextInt();
kilo = milligram / 1000000;
gram = milligram / 10000;
milligram = milligram / 1;
System.out.println("The weight is " + kilo + "kilos, " + gram + "grams" + milligram + " milligrams");
}
}
The part I am struggling with is I believe I must change the 10-12 lines with code that will read the given user input and then divide the input by the appropriate number to get kg, g, and mg but I cannot figure out how to do it as I am new to java. I am aware the division numbers are incorrect but I don't believe that is the issue. If this is not the right approach please guide me to the right approach.
You can use the modulus (clock arithmetic) operator
const KG = 1000000;
const GRAM = 1000;
Scanner in = new Scanner(System.in);
int milligram;
double kilo, gram;
System.out.println("Enter the weight in milligrams: ");
milligram = in.nextInt();
kilo = milligram / KG;
//use the modulus here (and below)
milligram = milligram % KG;
gram = milligram / GRAM;
// 2nd use of modulus, this time with GRAN
milligram = milligram % GRAM;
System.out.println("The weight is " + kilo + "kilos, " + gram + "grams" + milligram + " milligrams");
}
I am trying to write a payment calculator program, but am receiving "NaN" as my result. The program asks for input on the loan amount and length of loan (in months). The program should then calculate out the monthly payments for each APR (3% -10%). I'm not sure if I have done something wrong with my calculation.
double L, payment;
double APR = 0;
int n;
Scanner input = new Scanner(System.in);
System.out.println("Loan calculator");
System.out.print("Enter the loan amount: ");
L = input.nextDouble();
System.out.print("Enter the number of payments: ");
n = input.nextInt();
double t = APR/1200.0;
for (APR = 3; APR <= 10; APR += .25){
payment = L * (t * (Math.pow((1.0 + t), n)) / (Math.pow((1.0 + t), n) - 1.0));
System.out.println(APR + "\t" + payment);
Define or at least assign t within the loop. Otherwise it will only be computed once before the loop using APR, which is 0 at that time.
for (APR = 3; APR <= 10; APR += .25){
double t = APR/1200.0;
payment = L * (t * (Math.pow((1.0 + t), n)) / (Math.pow((1.0 + t), n) - 1.0));
System.out.println(APR + "\t" + payment);
Problem with my code for calculator - output values not correct
Here is my code, any response would be appreciated.
import java.util.Scanner;
public class Savings {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
//ask for initial amount
System.out.print("What is the initial savings amount? ");
double initialAmount = console.nextDouble();
// ask for number of months
System.out.print("What is the number of months to save? ");
int months = console.nextInt();
//ask for interest rate
System.out.print("What is the annual interest rate? ");
double interestRate = console.nextDouble();
//calculate total
double monthlyInterest = ((interestRate/100)*(1/12));
double number1 = (monthlyInterest + 1);
double number2 = Math.pow(number1, months);
double total = (initialAmount*number2);
System.out.println("$" + initialAmount + ", saved for " + months + " months at " + interestRate + "% will be valued at $" + total);
console.close();
}
}
final value ends up being the same value as initial
Change this:
double monthlyInterest = ((interestRate/100)*(1/12));
to
double monthlyInterest = (interestRate / 100) * (1.0 / 12.0);
You're trying to do integer division in floating-point context, so in monthlyInterest you are essentially multiplying interestRate / 100 with 0.
Add d with the numbers to convert them into double and keep the decimal value, this way -
double monthlyInterest = ((interestRate/100d)*(1/12d));
If you do 1/12 with integers, the output will be 0, but with 1/12d it will be 0.08333333333333333
Also, you can get rid of the extra parenthesis -
double monthlyInterest = (interestRate/100d)*(1/12d);
...
double number1 = monthlyInterest + 1;
...
double total = initialAmount * number2;
I have some inputs say x1,x2,x3. I have to multiply each input with some weight and add them. Like alpha * x1 + beta * x2 + gamma * x3.
Range of x1,x2,x3 is 0 to 1.
Range of weights is also 0 to 1.
I want some algorithm to give more weight to higher values and less weight to lower values.
Like if x1 has higher value among three then 60% weight age can be given to x1.
x2 has 2nd highest value 30% to it and 20% to last value completing 100%.
What I have tried so far is given below but values are static, I am giving weight 0.6 to highest value the 0.3 and 0.2 respectively. Is there any way to give weight dynamically according to value of input?
if (x1>x2 and x1>x3 ) {
if (x2>x3) {
sum= 0.6*x1 + 0.3*x2 + 0.2*x3;
}
if (x3>x2) {
sum= 0.6*x1 + 0.2*x2 + 0.3*x3;
}
}
if (x2>x3 and x2>x1 ) {
if (x1>x3) {
sum= 0.3*x1 + 0.6*x2 + 0.2*x3;
}
if (x3>x1) {
sum= 0.2*x1 + 0.6*x2 + 0.3*x3;
}
}
if (x3>x2 and x3>x1 ) {
if (x2>x1) {
sum= 0.2*x1 + 0.3*x2 + 0.6*x3;
}
if (x1>x2) {
sum= 0.3*x1 + 0.2*x2 + 0.6*x3;
}
}
You're saying but values are static.
If by that you mean that you hardcoded values like:
double x1 = 1.0;
double x2 = 2.0;
double x3 = 3.0;
then the solution is to get them by user input:
Scanner scanner = new Scanner(System.in);
System.out.print("x1=");
double x1 = scanner.nextDouble();
System.out.print("x2=");
double x2 = scanner.nextDouble();
System.out.print("x3=");
double x3 = scanner.nextDouble();
scanner.close();
Then you make all the calculations.
I've been a lot of trouble figuring this class problem. My due date is tomorrow and I still don't know how to do it. I made a code when the input put by the user is converted into binary, octal, and hexadecimal. Now, the problem is that now they are asking me to modify the code in a way that the user inputs a Double or Floating point and convert it into decimal. My greatest problem is working with the decimal numbers; for example, 5*.987*. I will leave the code I already created. I would be very grateful is someone could help! thanks :)
import java.util.Scanner;
class EncodingTester {
public static void main(String args[]) {
byte largestPositiveByte = 127;
short largestPositiveShort = 32767;
int largestPositiveInt = 2147483647;
long largestPositiveLong = 9223372036854775807L;
long largestPositiveLongPlusOne = 9223372036854775807L;
Scanner in = new Scanner(System.in);
System.out.println("Next number (0 to stop): ");
long nextNumber = in.nextLong();
while (nextNumber != 0) {
int radix;
double logBase2 = Math.log(nextNumber) / Math.log(2);
double absoluteNumOfBits = 1 + Math.floor(logBase2);
double maxBits = Math.max(8, absoluteNumOfBits);
double numBits = Math.log(maxBits) / Math.log(2);
int newNumBits = (int) Math.pow(2, Math.ceil(numBits));
System.out.println("The absolute number of bits is: " +absoluteNumOfBits +". The final number of bits is: " +newNumBits +".");
radix = 10;
System.out.println("Decimal: " + String.format("%s", Double.toString(nextNumber)).replace(' ','0'));
radix = 2;
System.out.println("Binary: " + String.format("%"+newNumBits+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
radix = 8;
System.out.println("Octal: " + String.format("%"+((int) Math.ceil(newNumBits/3.0))+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
radix = 16;
System.out.println("Hexadecimal: 0x" + String.format("%"+newNumBits/4+"s", Long.toString(nextNumber,radix)).replace(' ','0'));
System.out.println("");
System.out.println("Next number: (0 to stop)");
nextNumber = in.nextLong();
}
System.out.println("Good Bye");
}
}