Computer Science Courses
Related sites
To drill some aspects of Java programming. These exercises are all optional.There are also many other good exercises available in your Java text book and online (some of these are pointed to below.) You are welcome to attempt any of these other exercises during supervised lab sessions and seek help and feedback on them.
if
statementsDo as many or as few of the following exercises as you want — until you get sick of them!
The following drills relate to code snippets which contain if
statements. For each question, test your answer out by coding up a simple main method:
x
initialised to be 49, 50 and 51. Now run the code and see that you were correct.int x = 49; if (x <= 50) { System.out.println("I am here!"); } if (x >= 50) { System.out.println("Am I here?"); } System.out.println("Here I am!");
x
initialised to be -49, -50 and -51. Now run the code and see that you were correct.int x = -51; if (Math.abs(x) <= 50) { System.out.println("I am here!"); } if (Math.abs(x) >= 50) { System.out.println("Am I here?"); } System.out.println("Here I am!");
x
initialised to be 49, 50 and 51. Now run the code and see that you were correct.int x = 49; if (x == 50) { System.out.println("I am here!"); } if (Math.abs(x) >= 50) { System.out.println("Am I here?"); } System.out.println("Here I am!");
x
initialised to be 49, 50 and 51. Now run the code and see that you were correct.int x = 51; if (x == 50) { System.out.println("I am here!"); } if (x >= 50) { System.out.println("Am I here?"); } System.out.println("Here I am!");
x
initialised to be 49, 50 and 51. Now run the code and see that you were correct.int x = 49; if (x < 52) { System.out.println("I am here!"); } else if (x < 51) { System.out.println("Am I here?"); } else if (x < 50) { System.out.println("Here I am?"); }
x
initialised to be 49, 50 and 51. Now run the code and see that you were correct.int x = 49; if (x > 52) { System.out.println("I am here!"); } else if (x > 51) { System.out.println("Am I here?"); } else if (x > 50) { System.out.println("Here I am?"); }
x
initialised to be 49, 50 and 51. Now run the code and see that you were correct.int x = 49; if (x < 52) { System.out.println("I am here!"); } else if (x > 51) { System.out.println("Am I here?"); } else if (x >= 50) { System.out.println("Here I am?"); }
Do as many or as few of the following exercises as you want — until you get sick of them!
boolean finished = false; boolean again = false; System.out.println(!finished);
boolean finished = false; boolean again = false; System.out.println(!!!finished);
boolean finished = false; boolean again = false; System.out.println(!!!!!finished);
boolean finished = false; boolean again = false; finished++; System.out.println(finished);
boolean finished = false; boolean again = false; System.out.println(finished && again);
boolean finished = false; boolean again = false; System.out.println(finished || again);
boolean finished = false; boolean again = false; System.out.println(finished || !again);
boolean finished = false; boolean again = false; System.out.println(!finished || !again);
boolean finished = false; boolean again = false; // My goodness! Look at this one...! System.out.println((finished || !again) && (finished || (again || !again)));
Do as many or as few of the following exercises as you want —until you get sick of them!
The following drills relate to code snippets which contain while
loops. For each question, test your answer out by coding up a simple main method. You could also try modifying some of the loops to be for
loops or do-while
loops.
int x = 0; while (x < 100) { System.out.println(x); x = x + 10; }
int x = 0; while (x <= 100) { System.out.println(x); x = x + 10; }
int x = 0; while (x <= 100) { System.out.println(x); x++; }
int x = 100; while (x <= 100) { System.out.println(x); x--; }
int x = 0; while (x <= 100) { System.out.println(x); x += 30; }
int x = 1; while (x <= 100) { System.out.println(x); x *= 30; }
boolean isFinished = false; int x = 0; int counter = 0; int maxCounter = 13; while (!isFinished) { System.out.println(x); x = (x + 30) % 100; counter++; if (counter > maxCounter) isFinished = true; }
boolean isFinished = false; int x = 0; int y = 0; int counter = 0; int maxCounter = 13; while (!isFinished) { System.out.println(x); x = (x + 30) % 100; y = (y + 30) % 150; counter++; if (counter > maxCounter) isFinished = true; }
There are some online sites for coding practice. (The first one in particular is a lot of fun!)
Imagine that you are running a used car yard and you want to manage aninventory of cars.
serialNumber maker dateOfManufacture dateOfAcquisition acquisitionCost salePrice condition
condition
a String with three possible values: "good"
, "bad"
, "average"
java.util.Date
class: main
method in another class, fill out an inventory array of 6 carsSystem.out.printf()
method)"bad"
cars and print out the inventory again. (Note that you might need to add additional methods to your Car class.)Imagine that you are maintaining the stock of a vending machine:
serialNumber productName positionNumber quantity price
minimumNumber
, and a method checkQuantity
which returns false
if the quantity ofa given product falls below the minimum number.
Updated: Sun 12 Jun 2016 17:27:37 AEST • Responsible Officer: JavaScript must be enabled to display this email address. • Page Contact:
+61 2 6125 5111
The Australian National University, Canberra
CRICOS Provider : 00120C ABN : 52 234 063 906