Java calculations w/ conditional statements

Click For Summary

Discussion Overview

The discussion revolves around a Java programming task involving the calculation of rental costs for different car types (Ford, Cadillac, Toyota) using conditional statements. Participants are addressing issues related to the implementation of the code, including the logic for calculating costs based on daily rates and mileage.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the requirements for calculating rental costs based on car type, days rented, and miles driven, highlighting the need for conditional statements.
  • Another participant notes that the code is difficult to read and suggests that the logic could be simplified by consolidating the methods for each car type, as the calculation formula is the same.
  • A participant points out a potential error in the logic for calculating costs, specifically regarding the condition for miles driven over 100, suggesting it should be greater than 100 instead of less than or equal to 100.
  • One participant provides a revised code snippet that aims to correct the identified issues, although the full context of the changes is not provided.

Areas of Agreement / Disagreement

Participants express differing views on the structure and readability of the code, with some agreeing on the need for simplification while others focus on specific logical errors. The discussion remains unresolved regarding the best approach to implement the calculations.

Contextual Notes

There are unresolved issues related to the use of string comparison in Java, as participants have not explicitly addressed the correct method for comparing strings. Additionally, the handling of mileage calculations and the overall structure of the code are points of contention.

clook
Messages
32
Reaction score
0
I'm supposed to calculate the cost of renting a Ford, Cadillac or Toyota, and use conditional statements to calculate the different costs of each vehicle.

users are supposed to enter “F” for Ford, “T” for Toyota, or “C” for Cadillac

users enter “F” for Ford, “T” for Toyota, or “C” for Cadillac

The type of car can only be a Ford, a Cadillac, or a Toyota. Fords rent for $26 per day and .15 per mile. Cadillacs rent for $65 per day and .25 per mile. Toyotas rent for $40 per day and .18 per mile. The total charge will include the number of days rented * the daily rental charge + the number of miles driven * the per mile rate. The first 100 miles are free, so there will be no mileage charge for miles under 100. For miles over 100, only those miles over 100 are to be charged. Therefore, if a car is driven 145 miles, the chargeable miles will be 45.

I've attempted to code this with if and else if statements, but for whatever reason the calculation does not go through and just shows up as "$0.00"

here is the code from my computation class:
Code:
public class Calculate
{
	private double milesDrivenDouble, summaryMilesDrivenDouble;
	private static double totalCostDouble, mileageCostDouble, dailyCostDouble,
	summaryCostDouble, fordGrandTotalDouble, cadillacGrandTotalDouble, toyotaGrandTotalDouble;
	private static int daysRentedInteger, summaryCarsDouble, carCounterInteger, fordCounterInteger,
	cadillacCounterInteger, toyotaCounterInteger;
	private static String carType;
	private final double FORD_DAILY_RATE = 26;
	private final double FORD_PER_MILE_RATE = 0.15;
	private final double CADILLAC_DAILY_RATE = 65;
	private final double CADILLAC_PER_MILE_RATE = 0.25;
	private final double TOYOTA_DAILY_RATE = 40;
	private final double TOYOTA_PER_MILE_RATE = 0.18;
	private double MILES_COUNTED = milesDrivenDouble - 100;
	
	public Calculate()
	{}
	
	public Calculate(double milesDrivenDouble, int daysRentedInteger)
	{
		setMiles(milesDrivenDouble);
		setDaysRented(daysRentedInteger );
		calculateFord();
		calculateCadillac();
		calculateToyota();
	}
	
	private void setMiles(double milesDrivenNewDouble)
	{
		//assign public variable to private
		milesDrivenDouble = milesDrivenNewDouble;
	}
	private void setDaysRented(int daysRentedNewInteger)
	{
		//assign public variable to private
		daysRentedInteger = daysRentedNewInteger;
	}
	
	private void setCarType(String carTypeNew)
	{
		//assign public variable to private
		carType = carTypeNew;
	}
	
	private void calculateFord()
	{
			if(carType == "F" && milesDrivenDouble <= 100)
	{
			mileageCostDouble = FORD_PER_MILE_RATE * MILES_COUNTED;
			dailyCostDouble = FORD_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
	}
			else 
			{
            totalCostDouble = 0 + dailyCostDouble;
			
	}
			fordCounterInteger++;
			summaryCostDouble += totalCostDouble;
			fordGrandTotalDouble += totalCostDouble;
				
	}
		
	private void calculateCadillac()
	{
			if(carType == "C" && milesDrivenDouble <= 100)
	{
			mileageCostDouble = CADILLAC_PER_MILE_RATE * MILES_COUNTED;
			dailyCostDouble = CADILLAC_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
	}
			else if(carType =="C" && milesDrivenDouble <= 100)
			{
			mileageCostDouble = 0;
			dailyCostDouble = CADILLAC_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
			
	}
			cadillacCounterInteger++;
			summaryCostDouble += totalCostDouble;
			cadillacGrandTotalDouble += totalCostDouble;
				
	}
	
	private void calculateToyota()
	{
			if(carType == "T" && milesDrivenDouble <= 100)
	{
			mileageCostDouble = TOYOTA_PER_MILE_RATE * MILES_COUNTED;
			dailyCostDouble = TOYOTA_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
	}
			else if(carType =="T" && milesDrivenDouble <= 100)
			{
			mileageCostDouble = 0;
			dailyCostDouble = TOYOTA_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
			
	}
			toyotaCounterInteger++;
			summaryCostDouble += totalCostDouble;
			toyotaGrandTotalDouble += totalCostDouble;
				
	}
	
		
	public double getTotalCost()
	{
		//returning total cost
		return totalCostDouble;
	}
	public double getSummaryCost()
	{
		   // return shipping cost
		return summaryCostDouble;
	}
	
	public int getCarCounter()
	{
		//return tax 
		return carCounterInteger;
	}
	
}

what did i do wrong?
 
Last edited:
Physics news on Phys.org
There's two things i'd note with your code;

It's very hard to read, by convention a method should normally be written like:

Code:
private void calculateToyota() {
	if(carType == "T" && milesDrivenDouble <= 100) {
		mileageCostDouble = TOYOTA_PER_MILE_RATE * MILES_COUNTED;
		dailyCostDouble = TOYOTA_DAILY_RATE * daysRentedInteger;
		totalCostDouble = mileageCostDouble + dailyCostDouble;
        }
}

Tabbing each block enables easier reading. I don't mean this in a condescending manner, I just think it'll make debugging easier in the long term for you!

Also, your code seems very long for the design spec! Why have you got three separate methods? The formula to work out the expense is the same for each car! Careful use of variables could shorten your code drastically and make it easier to spot potential errors..

Consider this:

We have three "input variables" that we need to get from the user to perform our calculation;

1. Car type.
2. Number of days rented.
3. Number of miles covered.

Then run three conditional statements;

Code:
double dailyRate;
double mileRate;

if (carType == "C") { 
        dailyRate = 65;
        mileRate = 0.25;
} else if (carType =="F") {...
}
.
.
.
if ((numberOfMiles- 100) <= 0) { numberOfMiles= 0; }

double amountOwed = ((dailyrate * numberOfDaysRented) + (numberOfMiles * mileRate));

System.out.println("The amount owed is: " + amountOwed);

Et voila?
 
Last edited:
N.B - I glanced at your code, I think your problem is here:

Code:
	private void calculateToyota()
	{
			if([B]carType == "T" && milesDrivenDouble <= 100[/B])
	{
			mileageCostDouble = TOYOTA_PER_MILE_RATE * MILES_COUNTED;
			dailyCostDouble = TOYOTA_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
	}
			else if(carType =="T" && milesDrivenDouble <= 100)
			{
			mileageCostDouble = 0;
			dailyCostDouble = TOYOTA_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
			
	}

The part in bold should be "> 100" (greater than 100) not "<=" (less than or equal to 100)!
 
I forgot to say, I fixed those operator errors and here's my fixed code:

Code:
/*
 
public class Calculate
{
	private double milesDrivenDouble, summaryMilesDrivenDouble;
	private static double totalCostDouble, mileageCostDouble, dailyCostDouble,
	summaryCostDouble, fordGrandTotalDouble, cadillacGrandTotalDouble, toyotaGrandTotalDouble;
	private static int daysRentedInteger, summaryCarsDouble, carCounterInteger, fordCounterInteger,
	cadillacCounterInteger, toyotaCounterInteger;
	private static String carType;
	private final double FORD_DAILY_RATE = 26;
	private final double FORD_PER_MILE_RATE = 0.15;
	private final double CADILLAC_DAILY_RATE = 65;
	private final double CADILLAC_PER_MILE_RATE = 0.25;
	private final double TOYOTA_DAILY_RATE = 40;
	private final double TOYOTA_PER_MILE_RATE = 0.18;
	private double MILES_COUNTED = milesDrivenDouble - 100;
	
	public Calculate()
	{}
	
	public Calculate(double milesDrivenDouble, int daysRentedInteger)
	{
		setMiles(milesDrivenDouble);
		setDaysRented(daysRentedInteger );
        setCarType(carType);
		calculateFord();
		calculateCadillac();
		calculateToyota();
	}
	
	private void setMiles(double milesDrivenNewDouble)
	{
		//assign public variable to private
		milesDrivenDouble = milesDrivenNewDouble;
	}
	private void setDaysRented(int daysRentedNewInteger)
	{
		//assign public variable to private
		daysRentedInteger = daysRentedNewInteger;
	}
	
	private void setCarType(String carTypeNew)
	{
		//assign public variable to private
		carType = carTypeNew;
	}
	
	private void calculateFord()
	{
			if(carType == "F" && milesDrivenDouble <= 100)
	{
			mileageCostDouble = FORD_PER_MILE_RATE * MILES_COUNTED;
			dailyCostDouble = FORD_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
	}
			else if(carType == "F" && milesDrivenDouble > 100)
			{
		    dailyCostDouble = FORD_DAILY_RATE * daysRentedInteger;
            totalCostDouble = dailyCostDouble;
			
	}
			fordCounterInteger++;
			summaryCostDouble += totalCostDouble;
			fordGrandTotalDouble += totalCostDouble;
				
	}
		
	private void calculateCadillac()
	{
			if(carType == "C" && milesDrivenDouble <= 100)
	{
			mileageCostDouble = CADILLAC_PER_MILE_RATE * MILES_COUNTED;
			dailyCostDouble = CADILLAC_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
	}
			else if(carType =="C" && milesDrivenDouble > 100)
			{
			dailyCostDouble = CADILLAC_DAILY_RATE * daysRentedInteger;
			totalCostDouble = dailyCostDouble;
			
	}
			cadillacCounterInteger++;
			summaryCostDouble += totalCostDouble;
			cadillacGrandTotalDouble += totalCostDouble;
				
	}
	
	private void calculateToyota()
	{
			if(carType == "T" && milesDrivenDouble <= 100)
	{
			mileageCostDouble = TOYOTA_PER_MILE_RATE * MILES_COUNTED;
			dailyCostDouble = TOYOTA_DAILY_RATE * daysRentedInteger;
			totalCostDouble = mileageCostDouble + dailyCostDouble;
	}
			else if(carType =="T" && milesDrivenDouble > 100)
			{
			dailyCostDouble = TOYOTA_DAILY_RATE * daysRentedInteger;
			totalCostDouble = dailyCostDouble;
			
	}
			toyotaCounterInteger++;
			summaryCostDouble += totalCostDouble;
			toyotaGrandTotalDouble += totalCostDouble;
				
	}
	
		
	public double getTotalCost()
	{
		//returning total cost
		return totalCostDouble;
	}
	public double getSummaryCost()
	{
		   // return shipping cost
		return summaryCostDouble;
	}
	
	public int getCarCounter()
	{
		//return tax 
		return carCounterInteger;
	}
	
}
 
Last edited:
please check ww.youngcoders.com/showthread.php?p=142756#post142756 if you don't mind?
 
all of your answers are there, I think you just want other people to do your work for you...
 
Bad clook!
 
Thanks for the heads up.

It's a bit of a pain to be helping you with a problem that you've asked on another forum (and may already be solved for all I know!)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
12K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K