Let’s do some "real world" simple calculations with Java.
Word problems in math class are often a source of dread for students. But they are a great way to learn how to break a problem down into small pieces, and then write code to do each piece.
Here are three word problems that we can solve with Java.
Let’s create a simple problem to solve with Java.
Our car’s gas tank can hold 12.0 gallons of gas. It gets 22.0 miles per gallon (mpg) when driving at 55.0 miles per hour (mph). If we start with the tank full and carefully drive at 55.0 mph, how many miles can we drive (total miles driven) using the whole tank of gas?
BONUS:
How long will it take us to drive all those miles?
What do we need figure out? We need a variable for our
result: totalMilesDriven.
So we start our program this way…
double totalMilesDriven = 0;
// print result of computation
System.out.println(totalMilesDriven);It’s often good to start with a very simple template. If we run that, we will see 0 (zero) as the result, right? Which is correct, because we haven’t done any computation yet. (but we have a working program!)
Next step, let’s add the variables we know.
double totalMilesDriven = 0;
double totalHoursTaken = 0;
double totalGasGallons = 12.0;
double milesPerGallon = 22.0;
double milesPerHour = 55.0;
// print result of computation
System.out.println(totalMilesDriven);Okay, good. We’ve added all the stuff we know about the computation. Well, except the part of the actual computation.
You probably know that if you multiply the milesPerGallon by the totalGasGallons, that will give you totalMilesDriven. And if you divide the totalMilesDriven by the milesPerHour, you will get the totalHoursTaken.
So let’s add those as Java statements.
double totalMilesDriven = 0;
double totalHoursTaken = 0;
double totalGasGallons = 12.0;
double milesPerGallon = 22.0;
double milesPerHour = 55.0;
totalMilesDriven = milesPerGallon * totalGasGallons;
totalHoursTaken = totalMilesDriven / milesPerHour;
// print result of computation
System.out.println("Results: " + totalMilesDriven + " miles " + totalHoursTaken + " hours");We get as a result 264 miles driven in 4.8 hours. And that’s how a simple Java program can get written. We broke the process into small steps, and then wrote code to do each step.
Let’s do another.
A friend of ours is offering you a "free cat". You’re not allergic to cats but before you say yes, you want to know how much it’ll cost to feed the cat for a year (and then, approximately how much much each month).
We find out that cat food costs $2 for 3 cans. Each can will feed the cat for 1 day. (Half the can in the morning, the rest in the evening.) We know there are 365 days in a year. We also know that there are 12 months in the year. So how much will it cost to feed the cat for a year?
Looking at it, this may be quite simple. If we know each can feeds the cat for a day, we then know that we need 365 cans of food. So we can describe that as
double totalCost = 0;
double cansNeeded = 365;
double costPerCan = 2.0 / 3.0;
totalCost = cansNeeded * costPerCan; // right?
double monthsPerYear = 12;
double costPerMonth = totalCost / monthsPerYear;
// print result of computation
System.out.println("Tabby will cost " + totalCost + " a year " + costPerMonth + " per month");What’s going to be the answer? [1] Run it in jshell to work it all out.
And let’s do one more.
A cell phone company charges a monthly rate of $12.95 and $1.00 a gigabyte of data. The bill for this month is $21.20. How many gigabytes of data did we use? Again, let’s use a simple template to get started.
double dataUsed = 0.0;
// print result of computation
System.out.println("total data used (GB)" + dataUsed);Let’s add what we know: that the monthly base charge (for calls, and so on) is $12.95 and that data costs 1 dollar per gigabyte. We also know the monthly bill is $21.20. Let’s get all that written down.
double dataUsed = 0.0;
double costPerGB = 1.0;
double monthlyRate = 12.95;
double thisBill = 21.20;
// print result of computation
System.out.println("total data used (GB)" + dataUsed);Now we’re ready to do the computation. If we subtract the monthlyRate from thisBill, we get the total cost of data. Then, if we divide the total cost of data by the cost per gigabyte, we will get the dataUsed.
double dataUsed = 0.0;
double costPerGB = 1.0;
double monthlyRate = 12.95;
double thisBill = 21.20;
double totalDataCost = thisBill - monthlyRate;
dataUsed = totalDataCost / costPerGB;
// print result of computation
System.out.println();
System.out.println("total data used (GB)" + dataUsed);How many GBs of data did we use? Turns out to be 8.25 gigabytes.
Now if the bill was $24.00? How many GBs then? (go ahead, I’ll wait…) [2]