The question says The Fast Freight Shipping Company charges the following rates:
Weight of Package Rate per 500 Miles Shipped
2 pounds or less $1.10
Over 2 pounds but not more than 6 pounds $2.20
Over 6 pounds but not more than 10 pounds $3.70
Over 10 pounds $3.80
The shipping charge per 500 miles are not prorated. For example, if a 2-pound package is shipped 550 miles, the charges would be $2.20. Write a program that asks the user to enter the weight of a package and then displays the shipping charges.
My problem is that I keep receiving two different answers everytime I put in a weight and distance. For example when I enter the weight as 2 pounds and the distance as 500 miles I get the answers $0.0 and $3.8 which are both incorrect answers. It looks like some weights that I enter are correct answers and others I enter give me incorrect answers. Heres my program:
//import java utilities for scanner class
import java.util.Scanner;
public class ShippingCharge
{
public static void main (String[] args)
{
//Declare and initialize variable to hold the entered weight.
int weight = 0;
//Declare and initialize variable to hold the entered distance.
double distance = 0.0;
//This variable will hold the calculated rate.
double rate;
//This will decide if the shipping charge will advance up one level.
int distanceMultiplier = (int)distance / 500;
//This will hold the increments of the shipping charge.
int distanceRemainder;
//Create a Scanner object for the input.
Scanner input = new Scanner(System.in);
//Get the weight of the package.
System.out.println("What is the weight of the package (in pounds)?");
weight = input.nextInt();
//Get the shipping distance of the package.
System.out.println("What is the shipping distance (in miles)?");
distance = input.nextDouble();
distanceRemainder = (int)distance % 500;
if (distanceRemainder == 0)
{
if (weight <= 2)
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 1.10));
}
else if (weight > 2 && weight <= 6)
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 2.20));
}
else if (weight > 6 && weight <= 10)
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.70));
}
else
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.80));
}
if (distanceRemainder != 0)
{
if (weight <= 2)
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 1.10);
}
else if (weight > 2 && weight <= 6)
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 2.20);
}
else if (weight > 6 && weight <= 10)
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.70);
}
else
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.80);
}
//end program
System.exit(0);
}//end main
}//end class
This will work for you
public static void main(String[] args) {
int weight = 0;
double distance = 0.0 , distanceExtra ;
Scanner in = new Scanner(System.in);
System.out.println("Weight ? ");
weight = in.nextInt();
System.out.println("Distance ? ");
distance = in.nextDouble();
distanceExtra = distance / 500;
distanceExtra = Math.ceil(distanceExtra);
if (weight <= 2) {
System.out.printf("charge is :" , (distanceExtra * 1.10));
}
else if (weight > 2 && weight <= 6)
{
System.out.printf("charge is :" , (distanceExtra * 2.20));
}
else if (weight > 6 && weight <= 10)
{
System.out.printf("charge is :" , (distanceExtra * 3.70));
}
else if (weight > 10)
{
System.out.printf("charge is :" , (distanceExtra * 4.80));
}
}
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
int weight = 0;
double distance = 0.0 ;
Scanner keyboard =new Scanner(System.in);
System.out.println("Enter the Distance");
distance = keyboard.nextDouble();
System.out.println("Enter the Weight");
weight = keyboard.nextInt();
if (weight <= 2) {
System.out.println("charge is : " + "$"+1.10);
}
else if (weight > 2 && weight <= 6)
{
System.out.println("charge is : " + "$"+2.20);
}
else if (weight > 6 && weight <= 10)
{
System.out.println("charge is : " + "$"+3.70);
}
else if (weight > 10)
{
System.out.println("charge is :" + "$"+4.80);
}
}
}
Related
The first part of the code is working, the program takes inputs as height and weight as displays an output message. When I press n I can go to the second part and take inputs for height in inches and the weight in pounds. As soon as I take them the program outputs the resulting bmi is: Nan. and the variable bmi might not have been initialized. i want to convert the height and weight from inches and pounds to meters and then show the resulting bmi This is my code:
package uly14th;
import java.util.Scanner;
public class BmiCalculator {
private static float BMI2;
public static void main(String[] args) {
char Y;
char y;
char Q;
char n;
char N;
float heightInMeters, weightInKilograms, BMI;
float heightInInches, weightInpounds;
float heightInMeters2 = 0, weightInKilograms2 = 0;
Scanner input = new Scanner(System.in);
System.out.println("Please state whether you are going to use kilograms & meters or, inches & pounds");
System.out.println("If you want to use the former please press Y or if you want to use the latter please press N");
Q = input.next().charAt(0);
if(Q == 'y' || Q == 'Y' ) {
System.out.println("Please enter the height in meters: ");
heightInMeters = input.nextFloat();
System.out.println(" and weight in kilograms: ");
weightInKilograms = input.nextFloat();
BMI = weightInKilograms / (heightInMeters * heightInMeters);
System.out.println("The resulting BMI of the person is: " +BMI);
if (BMI < 18.5){
System.out.println("The personis underweight");
}
else if ((BMI<=25.0) && (BMI >= 18.5)){
System.out.println("The person is normal");
}
else if ((BMI >= 25.0) && (BMI <=30.0 )){
System.out.println("The personis obese");
}
else if(BMI>=30.0) {
System.out.println("The person is obese and should exercise");
}
}
if ((Q == 'n') || (Q == 'N')){
System.out.println("Please enter the height in inches: ");
heightInInches = input.nextFloat();
System.out.println("Please enter the weight in pounds: ");
weightInpounds = input.nextFloat();
heightInInches = 0.0254f * heightInMeters2;
weightInpounds = 0.453592f * weightInKilograms2;
BMI = weightInKilograms / (heightInMeters * heightInMeters);
System.out.println("The resulting BMI of the person is: " +BMI);
if (BMI < 18.5){
System.out.println("The personis underweight");
}
else if ((BMI<=25.0) && (BMI >= 18.5)){
System.out.println("The person is normal");
}
else if ((BMI >= 25.0) && (BMI <=30.0 )){
System.out.println("The personis obese");
}
else if(BMI>=30.0) {
System.out.println("The person is obese and should exercise");
}
}
else {
System.out.println("Please make sure you run the program again and then only use the characters n,N,y,Y. ");
}
}
}
This is the error message: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable BMI might not have been initialized
at uly14th.BmiCalculator.main(BmiCalculator.java:82)
At the row (line 82)
BMI = weightInKilograms / (heightInMeters * heightInMeters);
you use the variables weightInKilograms and heightInMeters which has never been initialized, hence your error.
I suspect you want to do the conversion to meters and kilograms from inches and pounds before calculating the BMI. You can do this by simply putting the following lines before the BMI calculation
heightInMeters = 0.0254f * heightInInches;
weightInKilograms = 0.453592f * weightInPounds;
BMI = weightInKilograms / (heightInMeters * heightInMeters); //Now you can calculate the BMI using your variables since they have been assigned a value and is therefore initialized
heightInMeters = 0.0254f * heightInInches ;
weightInKilograms = 0.453592f * weightInpounds ;
BMI = weightInKilograms / (heightInMeters * heightInMeters);
should do the trick (... in case the constants are correct)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to find a java code to my calculation below,
can't find how to increment the value by 53 and 79 ,
public static void main(String[] args) {
int weight, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Weight : ");
weight = (int) sc.nextDouble();
b = calculateLandingRate(weight);
System.out.println("Total price : " + b);
}
static int calculateLandingRate(int weight) {
int rate = 26;
if (weight<= 25) {
if (weight > 1) {
int totalPrice = 26 * weight;
} else if (weight> 25 && weight < 75) ;`
total price= (the weight 26kg ) + 53 ,
the calculation should be like the value is incremented by 53 ,from the weight =26kg
/* int rate = +53;
Total price at 26kg = 650 + 53 =703
Total price at 27kg = 703 + 53=756
Total price at 28kg = 756 + 53 =809
Total price at 29kg = 809 + 53 =862
Total price at 30kg = 862 + 53 =915
*
*
*
Total price at 75kg = 3247 + 53 =3300
*/
}
else if (weight > 75) ;
the value increment by 79
int rate = +79;
Total price at 76kg = 3300 + 79 =3379
*
*
*
*
Total price at 624 kg = 46592 + 79 =46671
I think what you are after is this
static int calculateLandingRate(int weight) {
int lowRate = 26;
int midRate = 53;
int highRate = 79;
int lowRateLimit = 25;
int midRateLimit = 75;
int totalPrice = 0;
if (weight > 1 && weight <= lowRateLimit) {
totalPrice = lowRate * weight;
} else if (weight > lowRateLimit && weight <= midRateLimit) {
totalPrice = lowRate * lowRateLimit + midRate * (weight - lowRateLimit);
} else if (weight > midRateLimit) {
totalPrice = lowRate * lowRateLimit + midRate * (midRateLimit - lowRateLimit) + highRate * (weight - midRateLimit);
}
return totalPrice;
}
Remarks:
Your elseif statement else if (weight> 25 && weight < 75) is always false
you define rate but do not use it
Do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int weight, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the weight : ");
weight = sc.nextInt();
b = calculateLandingRate(weight);
System.out.println("Total price : " + b);
}
static int calculateLandingRate(int weight) {
final int RATE = 26;
final int LOWERLIMIT = 25;
final int LOWERLIMITADDER = 53;
final int UPPERLIMIT = 75;
final int UPPERLIMITADDER = 79;
// Base price
int price = LOWERLIMIT * RATE;
if (weight > 25 && weight <= 75) {
price += LOWERLIMITADDER * (weight - LOWERLIMIT);
} else if (weight > 75) {
price += LOWERLIMITADDER * (UPPERLIMIT - LOWERLIMIT);
price += UPPERLIMITADDER * (weight - UPPERLIMIT);
}
return price;
}
}
A sample run:
Enter the weight : 24
Total price : 650
Another sample run:
Enter the weight : 25
Total price : 650
Another sample run:
Enter the weight : 40
Total price : 1445
Another sample run:
Enter the weight : 74
Total price : 3247
Another sample run:
Enter the weight : 75
Total price : 3300
Another sample run:
Enter the weight : 80
Total price : 3695
Another sample run:
Enter the weight : 624
Total price : 46671
I am new to Java.
How do I make a condition that could possibly combine the said "plans" in the program and add their prices? For now the program is only set for one plan and price. Which can only output one plan, and price.
Like for example if I input:
Enter number of talk minutes: 125
Enter number of text messages sent: 225
Enter number of gigabytes used: 1.3
The outcome should be like
Recommended Cellular Plans: Plan B and Plan E
Total Bill: $134.00
import java.text.DecimalFormat;
import java.util.Scanner;
public class HorizonPhones {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of talk minutes: ");
int minutes = scanner.nextInt();
System.out.print("Enter amount of text messages sent: ");
int texts = scanner.nextInt();
System.out.print("Enter amount of gigabytes used: ");
double gigaBytes = scanner.nextDouble();
double planA = 49.00;
double planB = 55.00;
double planC = 61.00;
double planD = 70.00;
double planE = 79.00;
double planF = 87.00;
if (minutes <= 500 && texts == 0 && gigaBytes == 0 ) {
System.out.println("Recommended Cellular Plans: Plan A");
System.out.println("Total Bill: " + planA);
return;
}
if (minutes < 500 && texts > 1 && gigaBytes == 0) {
System.out.println("Recommended Cellular Plans: Plan B");
System.out.println("Total Bill: " + planB);
return;
}
if (minutes > 500 && texts <= 100 && gigaBytes == 0) {
System.out.println("Recommended Cellular Plans: Plan C");
System.out.println("Total Bill: " + planC);
return;
}
if (minutes > 500 && texts >= 100 && gigaBytes == 0) {
System.out.println("Recommended Cellular Plans: Plan D");
System.out.println("Total bill: " + planD);
return;
}
if (minutes > 500 && texts >= 1 && gigaBytes < 2) {
System.out.println("Recommended Cellular Plans: Plan E");
System.out.println("Total bill: " + planE);
return;
}
if (minutes > 500 && texts > 1 && gigaBytes > 2) {
System.out.println("Recommended Cellular Plans: Plan F");
System.out.println("Total bill: " + planF);
return;
}
}
}
Before you if(){} blocks start declare two things one String[] (for storing plan's name) and another int sum=0; variable for storing total sum. And remove return from each if(){} block. Then in each block just do this.
String[] plansName = new String[];
int sum = 0;
if (minutes <= 500 && texts == 0 && gigaBytes == 0 ) {
plansName.push("planA");
sum += planA;
}
...
Similarly do for all if(){} blocks and at the end print plansName and sum values.
I am getting a NaN error when I compile this, i can't figure out what I am doing wrong? I tried moving the variables around to see if I could get them working but nothing. Notice the variable I put of type double i used for bmi after inches = keyboard.nextInt(); I think its a divide by zero error but i dont know what i am dividing by zero.
import java.util.Scanner;
public class BodyMassIndex {
public static void main(String[] args) {
// TODO Auto-generated method stub
int pounds =0;
int feet = 0;
int inches = 0;
double heightMeters = ((feet * 12) + inches) * .0254;
double mass = pounds / 2.2;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your weight in pounds");
pounds = keyboard.nextInt();
System.out.println("Enter how many feet you are");
feet = keyboard.nextInt();
System.out.println("Enter how many inches after feet");
inches = keyboard.nextInt();
double bmi = mass / (heightMeters * heightMeters);
System.out.println(mass);
System.out.println(bmi);
if(bmi < 18.5){
System.out.println("Underweight");
}
else if ((bmi >= 18.5) && (bmi < 25)){
System.out.println("Normal weight");
}
else if ((bmi >= 25) && (bmi < 30))
System.out.println("Above weight");
else
System.out.println("Obese");
}
}
It's important to understand the dynamic nature of your variables, such that when you type
double heightMeters = ((feet * 12) + inches) * .0254;
This assignment is immediately evaluated using the current values of feet and inches (which are 0 at that moment), before any of the keyboard entry is performed. To perform these calculations with the values entered by the keyboard, these calculations need to be performed and their results assigned to their corresponding variables after the keyboard entry is done, when the current values of pounds, feet and inches are what you just entered. Because heightMeters is still zero from its initialization and hasn't been changed since, you're getting a divide by zero.
You're getting the exception because the value of heightMeters is 0 here:
double bmi = mass / (heightMeters * heightMeters);
The only time that heightMeters is calculated, the result of it is 0:
double heightMeters = ((feet * 12) + inches) * .0254;
It looks like you want to use this calculation, so I'd split it into a separate function:
public static double getHeightInMeters(int feet, int inches)
{
return 0.0254 * ((feet * 12) + inches);
}
Then you can call it later:
double heightMeters = this.getHeightInMeters(feet, inches);
double bmi = mass / (heightMeters * heightMeters);
My code compiles correctly. When I run my program the output asks me to enter weight of package. If I enter a negative number the program asks me to enter weight again, but won't stop to let me enter another number.
I think the problem is in the "while" statement but I'm not sure.
Any help would be appreciated.
import java.util.Scanner;
public class dcrawford_Shipping
{
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
int weight, distance, distancex;
double rate, price;
rate = 0.00;
System.out.print("Please enter package weight: ");
weight = input.nextInt();
while (weight <= 0 || weight >= 61)
{
System.out.print("Please enter package weight: ");
}
if (weight <= 10 && weight >= 1 )
{
rate = 5.01;
}
else if ( weight <= 20 && weight >= 11 )
{
rate = 7.02;
}
else if ( weight <= 30 && weight >= 21 )
{
rate = 9.03;
}
else if ( weight <= 40 && weight >= 31 )
{
rate = 11.04;
}
else if ( weight <= 60 && weight >= 41)
{
rate = 15.00;
}
System.out.print("Please enter distance: ");
distance = input.nextInt();
while ( distance <= 0 )
{
System.out.print("Please enter distance: ");
}
distancex = ( distance / 100 ) + 1;
price = ( distancex * rate );
System.out.printf("Your total shipping cost for %d miles is $%.2f\n", distance, price);
}
}
You need to ask the user to enter the weight again inside of the while loop.
while (weight <= 0 || weight >= 61) {
System.out.print("Please enter package weight: ");
weight = input.nextInt();
}
You could also use a do-while loop:
do {
System.out.print("Please enter package weight: ");
weight = input.nextInt();
} while (weight <= 0 || weight >= 61);
If you use the do-while loop, you can remove the first time you ask and the while loop. It's a slightly more compact way of doing it.