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
Related
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);
}
When I run the program everything works fine until I get to the last line where I want to to multiply the price of gas depending on the type, however that part does not print out.
{
public static void main(String[] args)
{
double R = 2.19;
double M = 2.49;
double P = 2.71;
System.out.println("What type of gas?");
System.out.println("(R)egular: 2.19");
System.out.println("(M)idgrade: 2.49");
System.out.println("(P)remium: 2.71");
String gastype;
gastype = S.nextLine();
System.out.println("How many gallons?");
double gallons;
gallons = S.nextDouble();
if ((R * gallons) == 0)
{
System.out.println("You owe: " +R * gallons+ "");
}
if ((M * gallons) == 0)
{
System.out.println("You owe: " +M * gallons+ "");
}
if ((P * gallons) == 0)
{
System.out.println("You owe: " +P * gallons+ "");
}
}
}
For your code snippet to print total price, it should be something like:
double total;
// check for user input of "gas type"
// calculate total = gas type * gallons
if (gasType.equals("R")) {
total = R * gallons;
} else if (gasType.equals("M")) {
total = M * gallons;
} else if (gasType.equals("P")) {
total = P * gallons;
} else {
System.out.println("Invalid type of gas");
return;
}
System.out.println("You owe: %f", total);
Use this
public static void main(String[] args) {
double R = 2.19;
double M = 2.49;
double P = 2.71;
System.out.println("What type of gas?");
System.out.println("(R)egular: 2.19");
System.out.println("(M)idgrade: 2.49");
System.out.println("(P)remium: 2.71");
String gastype;
gastype = S.nextLine();
System.out.println("How many gallons?");
double gallons;
gallons = S.nextDouble();
if ((gasType == 'R') {
System.out.println("You owe: " +R * gallons+ "");
}
else if (gasType == 'M') {
System.out.println("You owe: " +M * gallons+ "");
}
else if (gasType == 'P') {
System.out.println("You owe: " +P * gallons+ "");
}
else {
System.out.println("Wrong GasType")
}
}
Your if statements will only execute if gallons = 0. Not only that, but you aren’t checking to see which gas the customer wanted. You’re essentially ignoring the gastype altogether for your final answer.
Try this if statement as an example
if (gasType.equals(“Regular”) {
//Stuff you want to print
}
I'm trying to make the program so that the dicountPrice would be equal to the corresponding if statement. I'm not too sure if it
s my if statement or if I'm missing something in my class. I've tried doing super.purchases and this.purchases and as of now I'm stumped.
Sample output:
Name: Snow White
Address: 111 Dwarf Lane
Telephone: 555-0000
Customer Number: 200-A010
Customer Type: Preferred
Total Purchases: 2566.0
Total Owed: 2566.0
Total Discount Percent: 0.0
Total Owed Minus Discount: 2566.0
But I need the discount percent to be 10
TIA
public class Customer extends Person {
protected String customerNumber, customerType;
protected double purchases;
Customer(){
super(DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE);
setCustomerType(DEFAULT_VALUE);
setPurchases(0.0);
}
Customer(String name, String address, String phone, String cusNum, String cusType, double purch){
super (name,address, phone);
customerNumber = cusNum;
customerType = cusType;
purchases = purch;
}
public void setCustomerNumber(String custNum){
customerNumber = custNum;
}
public String getCustomerNumber(){
return customerNumber;
}
public void setCustomerType(String cusType){
customerType = cusType;
}
public String getCustomerType(){
return customerType;
}
public void setPurchases(double purch){
purchases = purch;
}
public double getPurchases(){
return purchases;
}
public double getTotalOwed(){
return purchases;
}
public String toString(){
return super.toString() + "\nCustomer Number: " + customerNumber + "\nCustomer Type: " + customerType +
"\nTotal Purchases: " + purchases + "\nTotal Owed: " + purchases;
}
}
public class PreferredCustomer extends Customer {
private double discountPercent=0.0;
PreferredCustomer(){
super(DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE, DEFAULT_VALUE,0.0);
}
PreferredCustomer(String name, String address, String phone, String customerNumber, String customerType, double purchases){
super (name, address, phone, customerNumber, customerType, purchases);
}
public void setDiscountPercent(double dp){
discountPercent = dp;
}
public double getDiscountPercent(){
if (purchases >= 2000){
discountPercent = 100 * .10;
}
else if (purchases >= 1500 && purchases <2000){
discountPercent = 100 * .7;
}
else if (purchases >= 1000 && purchases <1500){
discountPercent = 100 * .6;
}
else if (purchases >=500 && purchases <1000){
discountPercent= 100 * .5;
}
else if(purchases<500){
discountPercent = 0;
}
return discountPercent ;
}
public double getTotalOwed(){
return purchases - discountPercent;
}
public String toString(){
return super.toString() + "\nTotal Discount Percent: " + discountPercent + "\nTotal Owed Minus Discount: " + this.getTotalOwed();
}
}
From what I see in your source, the issue comes from getDiscountPercent; You do calculations and modify members in a getter function which is a bad idea. A getter should only return data and not modify the internal state of the class.
A solution would be to create a private method called calculateDiscountPercent and call it internally whenever you modify members that would affect the discount percent value. Like this:
private void calculateDiscountPercent(){
if (purchases >= 2000){
discountPercent = 100 * .10;
}
else if (purchases >= 1500 && purchases <2000){
discountPercent = 100 * .7;
}
else if (purchases >= 1000 && purchases <1500){
discountPercent = 100 * .6;
}
else if (purchases >=500 && purchases <1000){
discountPercent= 100 * .5;
}
else if(purchases<500){
discountPercent = 0;
}
}
From the source, I see that the discountPercent is affected by the purchases member from its super class. Therefore, override its setter function to calculate the discount every time the purchase changes. Like so:
#Override
public void setPurchases(double purch){
super.setPurchases(purch);
this.calculateDiscountPercent();
}
Complete class would look like:
public class PreferredCustomer extends Customer {
private double discountPercent=0.0;
PreferredCustomer(){
super(DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE,DEFAULT_VALUE, DEFAULT_VALUE,0.0);
this.calculateDiscountPercent();
}
PreferredCustomer(String name, String address, String phone, String customerNumber, String customerType, double purchases){
super (name, address, phone, customerNumber, customerType, purchases);
this.calculateDiscountPercent();
}
public void setDiscountPercent(double dp){
discountPercent = dp;
}
public double getDiscountPercent(){
return discountPercent ;
}
#Override
public void setPurchases(double purch){
super.setPurchases(purch);
this.calculateDiscountPercent();
}
private void calculateDiscountPercent(){
if (purchases >= 2000){
discountPercent = 100 * .10;
}
else if (purchases >= 1500 && purchases <2000){
discountPercent = 100 * .7;
}
else if (purchases >= 1000 && purchases <1500){
discountPercent = 100 * .6;
}
else if (purchases >=500 && purchases <1000){
discountPercent= 100 * .5;
}
else if(purchases<500){
discountPercent = 0;
}
}
public double getTotalOwed(){
return purchases - discountPercent;
}
public String toString(){
return super.toString() + "\nTotal Discount Percent: " + discountPercent + "\nTotal Owed Minus Discount: " + this.getTotalOwed();
}
}
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 tried write a table that displays the taxable income for an incremental taxable income from 50,0000 to 60,000 under 4 different filing categories. Employing a method, the return statement in the code only printed out one of the four filing categories columns. Please how can I get the remaining columns printed out?
public class TaxableIncome {
public static void main(String[] args) {
System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
System.out.println("-----------------------------------------------------------------------------------------------------");
double Tincome;
int profile=1;
for(Tincome=50000; Tincome<=60000; Tincome+=50) {
System.out.println( Tincome +"\t\t" + computetax(profile, Tincome));
}
}
public static double computetax( int status , double income) {
double tax;
double single=0;
double mjoint=0;
double mseperate=0;
double head=0;
for(status=1;status<=4;status++) {
if(status==1) {
tax = 8350*.10 + (33950-8350)*0.15 + (income- 33950)*0.25;
single= tax;
}
if(status==2) {
tax = 16700*0.10 + (income-16700)*0.15;
mjoint = tax;
}
if(status==3 ) {
tax = 8350*0.10 + (33950-8350)*0.15 + (income-33950)*0.25;
mseperate= tax;
}
if(status ==4){
tax = 11950*0.10 + (45500-11950)*0.15 +(income-45500)* 0.25;
head =tax;
}
}
return (single);
}
}
Or use System.out.print() to output each value as they're calculated within computeTax, then do a System.out.println(); to get a carriage return.
You should roll all of the calculation results into their own object and return one of those.
static class TaxDetails {
double single = 0;
double mjoint = 0;
double mseperate = 0;
double head = 0;
}
public static TaxDetails computetax(double income) {
TaxDetails details = new TaxDetails();
details.single = 8350 * .10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
details.mjoint = 16700 * 0.10 + (income - 16700) * 0.15;
details.mseperate = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
details.head = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
return details;
}
public void test() {
System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
System.out.println("-----------------------------------------------------------------------------------------------------");
for (double income = 50000; income <= 60000; income += 50) {
TaxDetails tax = computetax(income);
System.out.println(income + "\t\t" + tax.single + "\t\t" + tax.mjoint + "\t\t" + tax.mseperate + "\t\t" + tax.head);
}
}
You are calculating your value for tax four times, then discarding that and returning just the single value for single.
Move the loop on status out of computetax and into caller.
EDIT: given that you're on a self-study course, here's a version that should work within your constraints.
It still needs a lot of improvement, but will get you further.
public class TaxableIncome {
public static void main(String[] args) {
System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
System.out.println("-----------------------------------------------------------------------------------------------------");
double Tincome;
int profile=1;
for(Tincome=50000; Tincome<=60000; Tincome+=50) {
double single=computetax(1, Tincome);
double joint=computetax(2, Tincome);
double seperate=computetax(3, Tincome);
double head=computetax(4, Tincome);
System.out.println( Tincome +"\t\tsingle:\t" + single + "\tjoint:\t" + joint + "\tseparate:\t" + separate + "\thead:\t" + head);
}
}
public static double computetax( int status , double income) {
double tax;
double single=0;
double mjoint=0;
double mseperate=0;
double head=0;
if(status==1) {
tax = 8350*.10 + (33950-8350)*0.15 + (income- 33950)*0.25;
single= tax;
}
if(status==2) {
tax = 16700*0.10 + (income-16700)*0.15;
mjoint = tax;
}
if(status==3 ) {
tax = 8350*0.10 + (33950-8350)*0.15 + (income-33950)*0.25;
mseperate= tax;
}
if(status ==4){
tax = 11950*0.10 + (45500-11950)*0.15 +(income-45500)* 0.25;
head =tax;
}
}
return (single);
}
}