The following exercises are taken from a book I'm working on, on Java 8. I'll add more as I complete further chapters. The book itself will likely be available one day for Amazon Kindle.
Modify a basic "hello world" program so that it produces the following output. (You might recognise this ditty from the film 2001: A Space Odyssey.)
Daisy, Daisy Give me your answer do I’m half crazy All for the love of you
Create a hello world program. Then delete all the text in your Java class file. Now retype it all yourself. Unless you’ve got an incredible memory, you’ll need to refer back to this book, or to another program you’ve created.
This might seem like a futile exercise. After all, you don’t yet understand the purpose of everything you’re typing, or even of most of it. But in fact, it’s much easier to understand code after you’ve typed it a few times, so typing it out now will help you a lot later on.
You will most likely make some mistakes when you do this, but by carefully comparing what you’ve typed, remembering to get the case of your letters right and put brackets and semi-colons in all the right places, you’ll eventually manage it.
If you have the patience, do this over and over again until you can actually type a "hello world” program from memory.
Don't forget you can always use Google to help you find answers!
Some beginner programers are wary of doing Internet searches, believing that Googling is cheating. It isn’t. One of the best things you can do is get into the habit of Googling like a Google addict (assuming of course, you have an Internet connection available). The only programmers who don’t Google are the ones who write the same kinds of programs day after day, or the ones who have to work without an Internet connection for some reason. The rest of us Google like crazy.
Create a program that produces the following output. Notice there is a tab character after each number.
1. Search for a book 2. Add a new book 3. Quit this program
2. The following is a classic challenge, without which a book on programming would scarcely be complete. In the USA, temperatures are measured in Fahrenheit. But most of the rest of the world uses the unit that scientists prefer, celsius.
To convert Fahrenheit to celsius, you have to subtract 32 and then divide by 1.8. Write a program that converts a temperature in Fahrenheit to a temperature in celsius. Print the result formatted to one decimal place.
Define the following constants: "Hello”, "Raj”, 29, 74.5. Now use these to create a program that produces the following output.
Hello, my name is Raj. I am 29 years old and I weigh 74.5 Kg
If you get stuck, move on to the next exercise and come back to it later. As long as you keep trying to write programs, your coding will improve — even if it sometimes feels like you’re not improving. Some people don’t get on well with made up exercises like these. There’s no shame in that! You can learn Java just as well by making up your own exercises, or by trying to write programs that interest you. The key is to just keep coding and keep trying things out.
If your code produces the right output, it doesn’t matter whether it matches my answer or not. But you need to think about testing your programs carefully, using various values for the variables that control how the program runs. Testing software thoroughly is as important as writing it in the first place. To help you out, I’ll give you an example of how to test your solution to the first question.
1. There are 0.62 miles in one kilometre. Write a program that converts a value specified in miles to a number of kilometres. Display the result to one decimal place. If the number of kilometres is less than two, display the text "Walk” (on a new line). If the number of kilometres is between two and six or equal to two or six, display the text "Bicycle”. If the number of kilometres is greater than six, display the text "Bus”.
When you’ve finished, test this by starting with values for the number of miles that produce each of the three possible outputs. When you’re testing code, it’s important to also test "edge cases”, where values are just on the edge of values that change how your program runs. So try actually setting the value for "kilometres” in your program to values like 2, 6, or just less than or just greater than 2 or 6, temporarily ignoring the value you converted from the number of miles.
In Britain, many people of older generations are used to talking about their height in feet and inches, and their weight in stones and pounds; the old "imperial” units. Here’s how this converts to metric units:
One foot is 30.5 centimetres
One inch is 2.54 centimetres
One stone is 6.35 kilograms
One pound is 0.45 kilograms
Define the following constants (or use your own height and weight in imperial).
Write code that converts these imperial unit measurements to metric units (kilograms and centimetres). Output the centimetres value to the nearest centimetre and the kilogram value to one decimal place.
Your program should produce output similar to the following.
Height: 180 cm Weight: 78.4 kg
If you’re feeling ambitious after this, write a program that converts your weight and height in metric units to imperial units (stones and pounds, and feet and inches).
Start by creating a program the defines the following constants.
Now make the program produce the following output. The output should change automatically as appropriate when you change any of the constants. Also, avoid typing the word "Gender” in your code more than once.
Name: Mike Mikerson - - - - - - - - - Gender: male Age: 21 Height: 1.82 m Weight: 75.0 kg
Now calculate BMI, body mass index, in your program. BMI is calculated by dividing weight in kilograms by height squared; in other words, divide weight by height and then divide by height again. Modify the program so that it produces the following output, which should updated appropriately if you change the values of the constants. Avoid typing "Weight category” more than once in your program.
If the BMI calculates to less than 18.5, output "underweight” for the weight category. If the BMI calculates to greater than 25, output "overweight” for the weight category. Otherwise the weight category is "normal”.
Name: Mike Mikerson - - - - - - - - - Gender: male Age: 21 Height: 1.82 m Weight: 75.0 kg BMI: 22.6 Weight category: normal
Finally, try changing the values of the constants and check that you can get the program to output the three different weight categories. See if you can find values for height and weight that are just big or small enough to change the weight category.
If you are used to using imperial measurement units, you can combine this program with the program you created in the last exercise, so that your height and weight can be specified in imperial units.
For each of the following expressions in boolean logic, say whether the boolean variable will get set to true or false. I won’t give you answers to these. Try to work them out just by thinking about them, then check your answer by coding them in Eclipse and running your program, outputting the boolean values.
If you get any of them wrong, try to work out why you got them wrong. If you want to check your understanding of logical operators in a boolean expression, you can always simplify the expression by gradually replacing conditions by simple hard-coded true or false values.
Some of these expressions can be written in a more readable way; for example, by adding brackets. See if you can rewrite any of them to be more readable. Check that your rewritten version produces the same results as the previous version.
Be careful. For example, the expression !(age > 20) might seem to produce the same results as (age < 20), but in fact it doesn’t produce the same results for all values. What happens when the age is equal to 20? How should we correctly simplify !(age > 20) ?