Week 05: Drop-In Lab
Objectives
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.
Warning: These exercises are basic, long and maybe tedious.
Language Drills: if
statements
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 if
statements. For each question, test your answer out by coding up a
simple main method:
-
Predict what the following code will produce for the cases of
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!");
-
Predict what the following code will produce for the cases of
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!");
-
Predict what the following code will produce for the cases of
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!");
-
Predict what the following code will produce for the cases of
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!");
-
Predict what the following code will produce for the cases of
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?"); }
-
Predict what the following code will produce for the cases of
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?"); }
-
Predict what the following code will produce for the cases of 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?"); }
Language Drills: Boolean Expressions
Do as many or as few of the following exercises as you want — until you get sick of them!
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; System.out.println(!finished);
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; System.out.println(!!!finished);
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; System.out.println(!!!!!finished);
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; finished++; System.out.println(finished);
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; System.out.println(finished && again);
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; System.out.println(finished || again);
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; System.out.println(finished || !again);
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; System.out.println(!finished || !again);
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
boolean finished = false; boolean again = false; // My goodness! Look at this one...! System.out.println((finished || !again) && (finished || (again || !again)));
Language Drills: Loops
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.
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
int x = 0; while (x < 100) { System.out.println(x); x = x + 10; }
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
int x = 0; while (x <= 100) { System.out.println(x); x = x + 10; }
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
int x = 0; while (x <= 100) { System.out.println(x); x++; }
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
int x = 100; while (x <= 100) { System.out.println(x); x--; }
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
int x = 0; while (x <= 100) { System.out.println(x); x += 30; }
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
int x = 1; while (x <= 100) { System.out.println(x); x *= 30; }
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
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; }
-
Predict what the following code will do, and what it will print out. Now run the code and see that you were correct.
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; }
Online coding practice
There are some online sites for coding practice. (The first one in particular is a lot of fun!)
- http://codingbat.com/java
- https://www3.ntu.edu.sg/home/ehchua/programming/java/J2a_BasicsExercises.html
- https://www3.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html#ExerciseClass
- http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/objects.html
- https://www.caveofprogramming.com/java/beginners-java-test-your-knowledge-of-classes-and-objects.html
Construct some classes
Imagine that you are running a used car yard and you want to manage an inventory of cars.
-
Write a Car class to store information about a particular car:
-
Fields in this class should be:
serialNumber maker dateOfManufacture dateOfAcquisition acquisitionCost salePrice condition
- Make the field
condition
a String with three possible values:"good"
,"bad"
,"average"
- Store dates using the Java
java.util.Date
class: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
-
- Write constructors and get and set methods for your Car class
- From a
main
method in another class, fill out an inventory array of 6 cars - Print out your inventory. Make it look nice (ie, use
System.out.printf()
method) - Apply a discount of 10% to all of your
"bad"
cars and print out the inventory again. (Note that you might need to add additional methods to your Car class.) - Play with this system and make it more realistic.
Imagine that you are maintaining the stock of a vending machine:
-
Write a VendingProduct class to store information about your machine with the fields:
serialNumber productName positionNumber quantity price
- Write constructors and get and set methods for your VendingProduct class.
- Add a field called
minimumNumber
, and a methodcheckQuantity
which returnsfalse
if the quantity of a given product falls below the minimum number. - From a main program in another class, fill out an inventory of 6 products. Have 10 units of each product.
- Print out your inventory. Make it look nice.
- Play with this system and make it more realistic.