Problem 9
Variables and Names
You can print things out with System.out.println()
and you
can do math. The next step is to learn about variables. In programming,
a variable is nothing more than a container that allows a program to store some data.
It is important to name variables appropriately to help other programmers (and yourself)
read the code more effectively.
If you get stuck with this exercise, remember the tricks you've been taught so far for finding differences and focusing on details:
- Write a comment above each line explaining to yourself what it does in English.
- Read your
.java
file backwards. - Read your
.java
file out loud, saying even the punctuation and symbols. Mention every uppercase/lowercase character.
public class VariablesAndNames {
public static void main(String[] args) {
int cars, drivers, passengers, carsNotDriven, carsDriven;
double spaceInCar, carpoolCapacity, averagePassengersPerCar;
cars = 100;
spaceInCar = 4.0;
drivers = 30;
passengers = 90;
carsNotDriven = cars - drivers;
carsDriven = drivers;
carpoolCapacity = carsDriven * spaceInCar;
averagePassengersPerCar = passengers / carsDriven;
System.out.println("There are " + cars + " cars available.");
System.out.println("There are only " + drivers + " drivers available.");
System.out.println("There will be " + carsNotDriven + " empty cars today.");
System.out.println("We can transport " + carpoolCapacity + " people today.");
System.out.println("We have " + passengers + " to carpool today.");
System.out.println("We need to put about " + averagePassengersPerCar + " in each car.");
}
}
What You Should See
~/.../pbd-solutions/08$ javac VariablesAndNames.java ~/.../pbd-solutions/08$ java VariablesAndNames There are 100 cars available. There are only 30 drivers available. There will be 70 empty cars today. We can transport 120.0 people today. We have 90 to carpool today. We need to put about 3.0 in each car. ~/.../pbd-solutions/08$
What You Should Do on Your Own
Assignments turned in without these things will not receive any points.
- I used 4.0 for
spaceInCar
, but is that necessary? What happens if it’s just 4? - Remember that 4.0 is a “floating point” number. Find out what that means.
- Write comments above each of the variable assignments.
- Make sure you know what
=
is the assignment operator and that assigning values to variables.
Frequently-Asked Questions
What is the difference between =
(single-equal) and ==
(double-equal)?
The =
(single-equal) assigns the value on the right to a variable on the left.
The ==
(double-equal) tests if two things have the same value. You’ll learn more about
comparing things in a later assignment.
What do you mean by “read the file backwards”?
Very simple. Imagine you have a file with 16 lines of code in it. Start at line 16, and compare it to my file at line 16. Then do it again for 15, and so on until you’ve read the whole file backwards.
Why did you use 4.0 for spaceInCar
? Changing it to 4 doesn’t seem to do anything.
That is because spaceInCar
was previously defined as a
double
variable. If it had been defined as an int
variable, putting 4 into it would have made a difference.
Copyright © 2010 Zed A. Shaw. Used by permission.
(The original Python version of this assignment is part of Zed Shaw's excellent Learn Python the Hard Way course and was translated to/reinterpreted for Java by Graham Mitchell. Further editing done by D. Gallo)
◄ 8: Numbers and Math 10: More Variables and Printing ►
Adapted from ProgrammingByDoing.com
©2013 Graham Mitchell
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.