Okay, now that we’ve gotten that first, hard assignment out of the way, we’ll do another. The nice thing is that in this one, we still have a lot of the setup code (which will be nearly the same every time), but the ratio of set up to “useful” code is much better.
Type the following text into a single file named
sure to match what I have written exactly, including spacing, punctuation,
The little vertical bar (“
|”) that you see on line 4 is called the “pipe”
character, and you can type it using Shift + backslash (“
you are using a normal US keyboard, the pipe/backslash key is located
Notice that the first line is the same as the previous assignment,
except that the name of the class is now
GasolineReceipt instead of
FirstProg. Also note that the name of the file you’re putting things
GasolineReceipt.java instead of
FirstProg.java. This is not
In Java, each file can contain only one public class, and the name of the
public class must match the file name (capitalization and all) except
that the file name ends in
.java and the public class name does not.
So, what does “public class” mean in Java? I’ll tell you when you’re older. Seriously, trying to go into that kind of detail up front is why most “beginner” programming books are bad for actual beginners. So don’t worry about it. Just type it for now. (Unfortunately, there’s going to be a lot of that.)
You will probably notice that the second line of this program is exactly the same as the previous assignment. There are no differences whatsoever.
Then, after the second open curly brace, there are twelve printing statements. They are all identical except for what is between the quotation marks.
Once everything is typed in and saved as
can compile and run it the same way you did the previous assignment. Switch
to your terminal window, change the directory into the one where you
are saving your code and type this to compile it:
If you are extremely good with annoying details and fortunate, you will
have no errors and the
javac command will complete without saying
anything at all. Probably, you will have errors. If so, go back and
compare what you type with what I wrote very carefully. Eventually you
will discover your error(s). Fix them, save the file again, and try
Once it compiles with no errors, you can run it like before:
And you should see output like this:
Two programs done. Nice! What you have accomplished so far is not easy, and anyone that thinks it is has a lot of experience and has forgotten what it is like to try this stuff for the first time. Don’t quit! Work a little every day and this will get easier.
“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell