It's my first program with several methods
1st has to convert height to inches
2nd calculate BMI
3rd receive BMI and and return the status
4th which is the main, has to call for input and generate output
The problem is that it doesnt calculate the BMI - it outputs 0.
When I run it in just one method, it works fine. What might be wrong?
package bmiCalculator;
java.util.Scanner;
public class BmiCalculator {
public static double bmi;
public static int height;
public static int feet;
public static int inches;
public static int weight;
public static String status;
public static void convertToInches (){
height = feet * 12 + inches;
}
public static void bmiCalculator (){
bmi = (weight * 703) / (height * height);
}
public static void weightStatus () {
if (bmi < 18.5){
status = "underweight";
}
else if (bmi <= 24.9){
status = "normal";
}
else if (bmi <= 29.9){
status = "overweight";
}
else if (bmi >= 30){
status = "obese";
}
}
public static void main (String[] args){
System.out.println("Put your height in ft and inches");
Scanner sc = new Scanner(System.in);
feet = sc.nextInt();
inches = sc.nextInt();
System.out.println("Put your weight in pounds");
weight = sc.nextInt();
System.out.println("Height: " + feet + " feet, " + inches + " inches");
System.out.println("Weight: " + weight + " pounds");
System.out.println("Your BMI is " + bmi + "category" + status);
}
}
Declaring those method does not mean all will execute. you need to call those methods from main accordingly.
for example:
...
System.out.println("Put your weight in pounds");
weight = sc.nextInt();
System.out.println("Height: " + feet + " feet, " + inches + " inches");
System.out.println("Weight: " + weight + " pounds");
// call corresponding method to calculate:
convertToInches();
bmiCalculator();
weightStatus();
// now all of those method are executed.
System.out.println("Your BMI is " + bmi + "category" + status);
Declaring all those methods and properties as static is not a good practice. Please learn how OOP works.
first, you need to call the methods after the user enter values like this..
...
System.out.println("Put your weight in pounds");
weight = sc.nextInt();
convertToInches();
bmiCalculator();
weightStatus();
System.out.println("Height: " + feet + " feet, " + inches + " inches");
...
The order of calling the methods is important because there are dependences between them.
Also, you need to convert the divisor and dividend to double before the BMI division because int/int = int and java round the value.
public static void bmiCalculator() {
bmi = (double)(weight * 703) / (double)(height * height);
}
Related
I have to create a game that prompts the user to put in a starting balance and then place a bet. Then they win or lose and their balance is added or subtracted accordingly. My problem is that I don't know how to make the program remember the final balance before starting over in the while loop if the player wants to play again. My balance stays as the original first balance entered when the user is prompted. I'm sorry if this is a repeat, but I couldn't find a question like this and I've been searching for over an hour. Thanks in advance.
import static java.lang.Math.*;
import java.util.Scanner;
public class BetGame
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
int die1, die2;
boolean looping = true;
System.out.println("Please enter your starting balance in whole dollars:");
double balance = in.nextDouble();
System.out.println("Your beginning balance is " + balance + ", good luck!!");
int guess = 0;
double bet2 = 0;
double endingBalance = 0;
while(looping) {
while (guess < 3) {//to allow the user to do this only 3 times
System.out.println("Enter a valid bet:");
double bet = in.nextDouble();
if (bet >= balance || bet >= endingBalance){
System.out.println("That is more than your balance.");
}
else if (bet < balance || bet < endingBalance) {
bet2 = 0 + bet;
break;
}
guess++;
if (guess == 3) {
looping = false;
System.out.println("You have entered three invalid bets in a row. Please leave the casino.");
}
}
die1 = RollDie();
die2 = RollDie();
int sum = die1 + die2;
if (2 <= sum && sum <= 6) {
System.out.println("The roll is " + die1 + " and " + die2 + " for a " + sum + " for a win!");
endingBalance = balance + bet2;
}
else if (7 <= sum && sum <= 12) {
System.out.println("The roll is " + die1 + " and " + die2 + " for a " + sum + " for a lose!");
endingBalance = balance - bet2;
}
System.out.println("Your balance is " + endingBalance);
System.out.println("Do you want to roll again?");
String answer = in.next();
if ((answer.equals("n")) || (answer.equals("N"))) {
looping = false;
}
}
System.out.println("Your balance is " + endingBalance + ". Better luck next time! Have a wonderful evening!");
}
static int RollDie()
{
int min = 1;
int max = 6;
return myRandomInteger(min, max);
}
static int myRandomInteger(int min, int max)
{
double range = max - min + 1.0;
int randomNum = (int)(range * Math.random()) + min;
return randomNum;
}
}
You can store the balance in a variable
double balance = in.nextDouble();
double originalBalance = balance;
So I'm making a program and I want an exception, so the program wont crash.
If the user puts a string that is not in the else if statements then it would not crash.
Also, I tried doing that for the integer, so if someone tries to write something that is not an integer it wont crash. And the program will catch it and would say that it isn't an integer.
How can I get exception in my try catch in java.
Thank for your help
Here is the code:
import java.util.InputMismatchException;
import java.util.Scanner;
public class WeightOnADifferentPlanet {
public static void main ( String[] args ){
Scanner scan = new Scanner ( System.in );
System.out.println("Where do you want to travel:?");
try{
String planetName = scan.nextLine();
}
catch(/*need help here*/){
System.out.println("Please check your spelling");
}
System.out.println("Please enter your weight:");
try{
int weight = scan.nextInt();
}
catch(InputMismatchException e)
{
System.out.println("That is not an integer");
}
double earthCalculation = weight * 1.0;
double jupiterCalculation = weight * (21.0 / 8.0); //check
double marsCalculation = weight * (3.0 / 8.0);
double mercuryCalculation = weight * (3.0 / 10.0);
double neptuneCalculation = weight * (11.0 / 10.0); //check
double plutoCalculation = weight * (7.0 / 10.0);
double saturnCalculation = weight * (6.0 / 5.0); //check
double uranusCalculation = weight * (9.0 / 10.0);
double venusCalculation = weight * (7.0 / 8.0);
if (planetName.equalsIgnoreCase("Earth"))
{
System.out.println("Your weight on "+planetName+" is: "+earthCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Jupiter"))
{
System.out.println("Your weight on "+planetName+" is: "+jupiterCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Mars"))
{
System.out.println("Your weight on "+planetName+" is: "+marsCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Mercury"))
{
System.out.println("Your weight on "+planetName+" is: "+mercuryCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Neptune"))
{
System.out.println("Your weight on "+planetName+" is: "+neptuneCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Pluto"))
{
System.out.println("Your weight on "+planetName+" is: "+plutoCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Saturn"))
{
System.out.println("Your weight on "+planetName+" is: "+saturnCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Uranus"))
{
System.out.println("Your weight on "+planetName+" is: "+uranusCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Venus"))
{
System.out.println("Your weight on "+planetName+" is: "+venusCalculation+" pounds.");
}
}
}
}
I believe scan.nextInt(); will only scan an integer anyways, so there shouldn't be any need to catch non integers
There are several things wrong with your program:
String planetName needs to be declared outside of the try block.
You shouldn't have planetName = scan.nextLine(); in a try/catch. You should figure out a way to keep asking the user for a planet name until they get a proper one.
Also, the int weight needs to be declared outside of the try block.
Same thing here, you need to figure out a way to keep asking the user for an integer if they don't give you one (e.g. you get an exception).
EDIT: As suggested by MasterBlaster, you should also close out your scanner with scan.close()
You don't really need to use exceptions. You can enter anything for planet and it won't crash, since you are checking for nextLine(). For weight, just check if scan.hasNextInt() before setting the weight.
import java.util.Scanner;
public class WeightOnADifferentPlanet {
public static void main ( String[] args ) {
Scanner scan = new Scanner(System.in);
System.out.print("Where do you want to travel? ");
String planetName = scan.nextLine();
System.out.print("Please enter your weight: ");
int weight = 0;
if (scan.hasNextInt()) {
weight = scan.nextInt();
double earthCalculation = weight * 1.0;
double jupiterCalculation = weight * (21.0 / 8.0); //check
double marsCalculation = weight * (3.0 / 8.0);
double mercuryCalculation = weight * (3.0 / 10.0);
double neptuneCalculation = weight * (11.0 / 10.0); //check
double plutoCalculation = weight * (7.0 / 10.0);
double saturnCalculation = weight * (6.0 / 5.0); //check
double uranusCalculation = weight * (9.0 / 10.0);
double venusCalculation = weight * (7.0 / 8.0);
if (planetName.equalsIgnoreCase("Earth")) {
System.out.println("Your weight on " + planetName + " is: " + earthCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Jupiter")) {
System.out.println("Your weight on " + planetName + " is: " + jupiterCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Mars")) {
System.out.println("Your weight on " + planetName + " is: " + marsCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Mercury")) {
System.out.println("Your weight on " + planetName + " is: " + mercuryCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Neptune")) {
System.out.println("Your weight on " + planetName + " is: " + neptuneCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Pluto")) {
System.out.println("Your weight on " + planetName + " is: " + plutoCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Saturn")) {
System.out.println("Your weight on " + planetName + " is: " + saturnCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Uranus")) {
System.out.println("Your weight on " + planetName + " is: " + uranusCalculation + " pounds.");
} else if (planetName.equalsIgnoreCase("Venus")) {
System.out.println("Your weight on " + planetName + " is: " + venusCalculation + " pounds.");
} else {
System.out.println("Planet not recognized");
}
} else {
System.out.println("Invalid weight");
}
scan.close();
}
}
So I ended up fixing it and here is the result.
import java.util.InputMismatchException;
import java.util.Scanner;
public class WeightOnADifferentPlanet {
static Scanner scan = new Scanner ( System.in );
public static void main ( String[] args ){
System.out.println("What planet do you want to travela:?");
String planetName = scan.nextLine();
System.out.println("Please enter your weight:");
int weight = Integer();
//int weight = scan.nextInt();
double earthCalculation = weight * 1.0;
double jupiterCalculation = weight * (21.0 / 8.0); //check
double marsCalculation = weight * (3.0 / 8.0);
double mercuryCalculation = weight * (3.0 / 10.0);
double neptuneCalculation = weight * (11.0 / 10.0); //check
double plutoCalculation = weight * (7.0 / 10.0);
double saturnCalculation = weight * (6.0 / 5.0); //check
double uranusCalculation = weight * (9.0 / 10.0);
double venusCalculation = weight * (7.0 / 8.0);
if (planetName.equalsIgnoreCase("Earth"))
{
System.out.println("Your weight on "+planetName+" is: "+earthCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Jupiter"))
{
System.out.println("Your weight on "+planetName+" is: "+jupiterCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Mars"))
{
System.out.println("Your weight on "+planetName+" is: "+marsCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Mercury"))
{
System.out.println("Your weight on "+planetName+" is: "+mercuryCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Neptune"))
{
System.out.println("Your weight on "+planetName+" is: "+neptuneCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Pluto"))
{
System.out.println("Your weight on "+planetName+" is: "+plutoCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Saturn"))
{
System.out.println("Your weight on "+planetName+" is: "+saturnCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Uranus"))
{
System.out.println("Your weight on "+planetName+" is: "+uranusCalculation+" pounds.");
}
else if (planetName.equalsIgnoreCase("Venus"))
{
System.out.println("Your weight on "+planetName+" is: "+venusCalculation+" pounds.");
}
else {
System.out.println("Planet not recognized");
}
}
public static int Integer(){
while (true)
{
try
{
return scan.nextInt();
}
catch (InputMismatchException e)
{
scan.next();
System.out.print("That’s not an integer. Try again: ");
}
}
}
}
I have attempted using a nested if in the following code. I have initialized variables but the compiler is telling me that the variable named 'bill' is not initialized even though it has been. Why is the compiler not recognizing the value assigned to the variable? Please see the notes in the code below.
package killMe;
import java.util.Scanner;
public class Kill_Me {
static Scanner console = new Scanner(System.in);
static double PREMIUM_SERVICE = 55.00;
static double PREMIUM_DAY_OVERTIME_MIN = 0.20;
static double PREMIUM_NIGHT_OVERTIME_MIN = 0.15;
static double REGULAR_SERVICE = 30.00;
static double REGULAR_OVERTIME_MIN = 0.40;
public static void main(String[] args) {
int acctNumber;
double premiumDayMin;
double premiumNightMin;
double bill;
double minutes;
String name;
String premium = "PREMIUM";
String regular = "REGULAR";
System.out.println("What is the Account Number? ");
acctNumber = console.nextInt();
System.out.println("What is the Customer Name? ");
name = console.next();
System.out.println("Is the Service Code Premium or Regular? ");
String strService = console.next();
String strServiceCAP = strService.toUpperCase();
if(strServiceCAP.compareTo(premium) == 0)
{
System.out.println("How many Day Minutes were used? ");
premiumDayMin = console.nextDouble();
System.out.println("How many Night Minutes were used? ");
premiumNightMin = console.nextDouble();
if(premiumDayMin <0 && premiumNightMin <0)
{
System.out.println("Minutes cannot be less than 0 ");
}
else if(premiumDayMin <= 75 && premiumNightMin <= 100)
{
bill = PREMIUM_SERVICE;
}
else bill = PREMIUM_SERVICE + (premiumDayMin - 75) * PREMIUM_DAY_OVERTIME_MIN + (premiumNightMin - 100)
* PREMIUM_NIGHT_OVERTIME_MIN;
minutes = premiumDayMin + premiumNightMin;
System.out.println("Customer Name: " + name);
System.out.println("Account Number: " + acctNumber);
System.out.println("Service Type: " + strServiceCAP);
System.out.println("Minutes Premium Service Used (Day): " + premiumDayMin);
System.out.println("Minutes Premium Service Used (Night): " + premiumNightMin);
System.out.println("Amount Due: " + bill); // I get an error here stating, "The local variable 'bill' may not have been initialized".
}
else if(strServiceCAP.compareTo(regular) == 0)
{
System.out.println("How many minutes were used? ");
minutes = console.nextDouble();
bill = REGULAR_SERVICE + (minutes - 50) * REGULAR_OVERTIME_MIN;
System.out.println("Customer Name: " + name);
System.out.println("Account Number: " + acctNumber);
System.out.println("Service Type: " + strServiceCAP);
System.out.println("Minutes Regular Service Used: " + minutes);
System.out.println("Amount Due: " + bill); // I DO NOT receive an error message here.
}
else
{
System.out.println("Invalid Service Type");
}
} // End of main
} // End of class
No, bill has not been initialized in all cases.
Understand this: the Java compiler will never, ever, evaluate boolean expressions; Simplified version:
double bill;
if (c1) {
bill = v1;
} else if (c2) {
bill = v2;
}
// try and use bill here
Even if, according to your logic, boolean expressions c1 and c2 may cover all possible cases, the compiler cannot ensure that this is the case.
This is the root cause of your error, however deep your if/else, switch, etc statements may be nested.
They were some problems with else statement and variable declarations.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
double PREMIUM_SERVICE = 25.00;
double PREMIUM_DAY_OVERTIME_MIN = 0.10;
double PREMIUM_NIGHT_OVERTIME_MIN = 0.05;
double REGULAR_SERVICE = 10.00;
double REGULAR_OVERTIME_MIN = 0.20;
int acctNumber;
double premiumDayMin;
double premiumNightMin;
double bill = 0.0;
double minutes;
String name;
String premium = "PREMIUM";
String regular = "REGULAR";
System.out.println("What is the Account Number? ");
acctNumber = console.nextInt();
System.out.println("What is the Customer Name? ");
name = console.next();
System.out.println("Is the Service Code Premium or Regular? ");
String strService = console.next();
String strServiceCAP = strService.toUpperCase();
if(strServiceCAP.compareTo(premium) == 0)
{
System.out.println("How many Day Minutes were used? ");
premiumDayMin = console.nextDouble();
System.out.println("How many Night Minutes were used? ");
premiumNightMin = console.nextDouble();
if(premiumDayMin <0 && premiumNightMin <0)
{
System.out.println("Minutes cannot be less than 0 ");
}
else if(premiumDayMin <= 75 && premiumNightMin <= 100)
{
bill = PREMIUM_SERVICE;
}
else
{
bill = PREMIUM_SERVICE + (premiumDayMin - 75) * PREMIUM_DAY_OVERTIME_MIN + (premiumNightMin - 100)
* PREMIUM_NIGHT_OVERTIME_MIN;
}
minutes = premiumDayMin + premiumNightMin;
System.out.println("Customer Name: " + name);
System.out.println("Account Number: " + acctNumber);
System.out.println("Service Type: " + strServiceCAP);
System.out.println("Minutes Premium Service Used (Day): " + premiumDayMin);
System.out.println("Minutes Premium Service Used (Night): " + premiumNightMin);
System.out.println("Amount Due: " + bill); // I get an error here stating, "The local variable 'bill' may not have been initialized".
}
else if(strServiceCAP.compareTo(regular) == 0)
{
System.out.println("How many minutes were used? ");
minutes = console.nextDouble();
bill = REGULAR_SERVICE + (minutes - 50) * REGULAR_OVERTIME_MIN;
System.out.println("Customer Name: " + name);
System.out.println("Account Number: " + acctNumber);
System.out.println("Service Type: " + strServiceCAP);
System.out.println("Minutes Regular Service Used: " + minutes);
System.out.println("Amount Due: " + bill); // I DO NOT receive an error message here.
}
else
{
System.out.println("Invalid Service Type");
}
} // End of main
}
I'm not sure why it gets this error, but try initialising bill as 0.00 when you declare the variable.
Also,
if(premiumDayMin <0 && premiumNightMin <0)
should probably be changed to
if(premiumDayMin <0 || premiumNightMin <0)
Because you want to make sure that either minutes is not less then zero. You're program should then probably handle this error, because the rest of the program still executes. But maybe you're getting on to that :-P.
I don't recall what I did to stop getting an error message (sorry) but I removed the code if(premiumDayMin <0 && premiumNightMin <0) and replaced it with if(premiumDayMin <= 75 && premiumNightMin <= 100) to stop the code from being redundant. That may have fixed things. I also added another else if to clean the logic up further.
i am trying to get my program to just show the grade and letter grade i know its a mess but i just need it to print but the Netbean says that JOptionPane requires double, double, double, double
package garrett_sprunger_a5;
import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
*
* #author Garrett
*/
public class Garrett_sprunger_A5 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
String inputString; // For reader's input
double TestScore1, //Define TestScore 1
TestScore2, //Define TestScore 2
TestScore3, //Define TestScore 3
AverageScore; //Define AverageScore
Scanner keyboard = new Scanner(System.in); //To hold the users grade
// (somehow i am able to use
// keyboard but can't get the
// varible to match correctly)
DecimalFormat formatter =
new DecimalFormat("#,##0.0"); //format the scores
Scanner Keyboard = new Scanner(System.in);
inputString=
JOptionPane.showInputDialog("\t\nPlease enter Test Score 1");
TestScore1 = Double.parseDouble(inputString);
// input TestScore2
inputString=
JOptionPane.showInputDialog("\t\nPlease enter Test Score 2");
// Convert the input to a double
TestScore2 = Double.parseDouble(inputString);
//input TestScore3
inputString=
JOptionPane.showInputDialog("\t\nPlease enter Test Score 3");
// Convert the input to a double
TestScore3 = Double.parseDouble(inputString);
//Calculate the average score for the tests
AverageScore = Calcaverage(TestScore1, TestScore2, TestScore3);
//AverageScore = (AverageScore +0.5);// applying midpoint roudning
// rule not needed with formated
// rounding
//Display Average test Score
{
if(TestScore1 <0 && TestScore1 >100)
JOptionPane.showMessageDialog(null, "Please enter a Correct" +
" data range between 0 and 100");
}
JOptionPane.showMessageDialog(null, "\t\nYour Test Score 1 is : "
+ formatter.format(TestScore1)
+"\t Grade: "
+ getLetterGrade(TestScore1)
+ "\t\nYour Test Score 2 is : "
+ formatter.format(TestScore2)
+ "\t Grade: "
+ getLetterGrade(TestScore2)
+ "\t\nYour Test Score 3 is : "
+ formatter.format(TestScore3)
+ "\t Grade: "
+ getLetterGrade(TestScore3)
+ "\t\nYour Average Score is : "
+ formatter.format(AverageScore)
+ "\t Grade: "
+ getLetterGrade(AverageScore));
}//End main method
public static double Calcaverage(double TestScore1,
double TestScore2, double TestScore3 ) {
double AverageScore = ((TestScore1 + TestScore2 + TestScore3)/3);
return AverageScore;
}
// Determine the letter grade
public static char getLetterGrade(double TestScore1,
double TestScore2,double TestScore3, double AverageScore) {
if (AverageScore >=90) {
return 'A';
} else if (AverageScore >= 70 && AverageScore < 90) {
if (TestScore3 > 90)
return 'A';
} else
return 'B';
if(AverageScore >=50 && AverageScore <70) {
if(((TestScore2 + TestScore3)/2.0) > 70)
return 'C';
} else
return 'D';
if (AverageScore < 50)
return 'f';
else
return '0';
}
public static void displaygrade( double AverageScore,
double TestScore1, double TestScore2,
double TestScore3, char getLetterGrade) {
DecimalFormat formatter = new DecimalFormat("#,##0.0");
JOptionPane.showMessageDialog(null, "\t\nYour Test Score 1 is : " +
formatter.format(TestScore1) +
"\t Grade: " + getLetterGrade(
TestScore1) +
"\t\nYour Test Score 2 is : " +
formatter.format(TestScore2) +
"\t Grade: " + getLetterGrade(
TestScore2) +
"\t\nYour Test Score 3 is : " +
formatter.format(TestScore3) +
"\t Grade: " + getLetterGrade(
TestScore3) +
"\t\nYour Average Score is : " + formatter.format(AverageScore)+
"\t Grade: " + getLetterGrade(
AverageScore));
}
}
I suggest you change the getLetterGrade() method to something which resembles this:
public static char getLetterGrade(double testScore) {
if (testScore >= 90) {
return 'A';
}
else if (testScore >= 80) {
return 'B';
}
//continue using if statements to determine the letter grade
}
Note that the method above only has one parameter, double testScore, which should be more helpful than inputing four doubles at once.
Would also like to point out that this if statement in your code will never run. Should be easy to figure out why:
if(TestScore1 <0 && TestScore1 >100)
JOptionPane.showMessageDialog(null, "Please enter a Correct data range between 0 and 100");
so i have these two classes called shipment and insurance, one calculates the price for shipping and the other adds the insurance. The shipment logic is working just fine but for some reason the if statements in Insurance class is not working i dont know whats going on. The output for the Insurance cost is always 2.45. why does it do this?
Shipment class:
package theshipment;
public class Shipment extends Main {
protected double weight = Double.parseDouble(savedArgs[1]);
protected double shippingCost;
protected double methodCost;
protected String method = savedArgs[2];
public void calculateShippingCost(){
if (weight<=10||weight>=1){
if (method.equalsIgnoreCase("T"))
methodCost = weight * 3.00;
else if (method.equalsIgnoreCase("A"))
methodCost = weight * 4.00;
else if (method.equalsIgnoreCase("M"))
methodCost = weight * 2.00;
else{}
}else if (weight<=20||weight>=10.1){
if (method.equalsIgnoreCase("T"))
methodCost = weight * 2.45;
else if (method.equalsIgnoreCase("A"))
methodCost = weight * 3.00;
else if (method.equalsIgnoreCase("M"))
methodCost = weight * 1.75;
else{}
}else if (weight>20){
if (method.equalsIgnoreCase("T"))
methodCost = weight * 1.95;
else if (method.equalsIgnoreCase("A"))
methodCost = weight * 2.50;
else if (method.equalsIgnoreCase("M"))
methodCost = weight * 1.55;
else{}
}else{}
}
}
And the insurance class:
package theshipment;
public class Insurance extends Shipment{
private void calculateInsurance(){
if (methodCost<=10.0||methodCost>=0.0)
shippingCost = methodCost + 2.45;
else if(methodCost<=30.0||methodCost>=10.1)
shippingCost = methodCost + 3.95;
else if(methodCost>=30.01)
shippingCost = methodCost + 5.55;
else{}
}
public void run(){
calculateShippingCost();
calculateInsurance();
}
public String displayOrder(){
return ("Method cost: " + methodCost + " " + "Insurance cost: " +
(shippingCost-methodCost) + " Total shipping cost: " + shippingCost);
}
}
methodCost<=10.0||methodCost>=0.0 that means it will be true when methodCost will be bigger than 0 OR smaller than 10,
I believe you want range not all numbers change it to methodCost<=10.0 && methodCost>=0.0