OOP Lab 4 Exercises: Objects and arrays. OOP Lab 4 Exercises: Objects and arrays. Authors: Joshua Ritterman Ewan Klein Version: 1.1 Date: 2009-02-10 Contents Preamble Submission Marking Exercises Exercise 1: The Lamp Sample solution Exercise 2: Counting the oddballs Sample solution Exercise 3: Flipping the Array Sample solution Preamble These exercises do not involve any new material over and above what was covered in Labs 2 and 3. In other words, they only deal with material up to and including Chapter 4 of Head First Java. Submission Please use the DICE submit command to submit your code for this exercise. Here are the steps you need to take. Put your name and matriculation number inside a comment at the start of each Java file that you plan to submit. Zip up all your files together into an archive using the zip command on DICE. Let's call this archive 'week04.zip' (though you can name it something else if you want). For example, if you want to submit the two files Foo.java and Baz.java, then you would zip them up at the DICE command line as follows, where % is the command line prompt: % zip week04 Foo.java Baz.java This would produce the file 'week04.zip'. At the DICE command line, give the following command: % submit inf1 inf1-op cpt week04.zip Note that cpt -- standing for 'class programming test' -- is the code for this assessed exercise. That's it! If you want more information about the submit command, please look at Section 3 of the following instructions for Functional Programming: http://www.inf.ed.ac.uk/teaching/courses/inf1/fp/tutorials/LabWeekExercise.pdf Don't try to use Web-CAT for submitting this exercise. The exercise is due in at 16.00 on Friday 6th February. Marking These exercises will be marked out of 25, then scaled down to 5 overall. The component marks for the individual exercises are indicated below. Exercises Exercise 1: The Lamp In this exercise you will model a lamp as a Java object. Make a Lamp class. This will contain at least one instance variable which will be of type boolean and will hold the state of the lamp: i.e., whether it is on or off. In addition, add methods to do the following things: switch the light on and off, and check its current state, i.e., whether it is on or off. Next, write a launcher class with a main() method to carry out the following tasks: create a lamp object; turn it on and off; print the the lamp's on/off status to the console (i.e., using System.out.println()). Sample solution Lamp.java:
public class Lamp {
boolean lampOn = false;
/**
* Switch the light on and off.
*/
void toggleLamp() {
lampOn = !lampOn;
}
/**
* Check the status of the lamp.
*/
boolean getLampState() {
return lampOn;
}
/**
* Print out the lamp's status to the console -- this is just for
* convenience.
*/
void reportStatus() {
if (getLampState())
System.out.println("The lamp is on.");
else
System.out.println("The lamp is off.");
}
public static void main(String[] args) {
Lamp l = new Lamp();
// turn the lamp on
l.toggleLamp();
l.reportStatus();
// turn it off again
l.toggleLamp();
l.reportStatus();
}
}
[5 marks] Exercise 2: Counting the oddballs In this exercise, you are to create an integer array of length 15 and then define a method called fill() that fills the array with random numbers. Once you have done this, define some code that loops through the array, and for each element, prints to the console whether the number is even or odd. For example if the array is {2,5,2,8, ...}, the output will be:
a[0] = even
a[1] = odd
a[2] = even
a[3] = even
...
To get a random number, first add the following statement at the beginning of your file:
import java.util.*;
Then the following statements create a new Random object rn, declare a variable r of type int, and assign it the result of calling nextInt() on the Random object:
Random rn = new Random();
int r = rn.nextInt();
To find out whether a number n is even, use the modulo operator % to test whether it is divisible by 2 with no remainder:
n % 2 == 0
In order to do this exercise, you should wrap your methods in a class called ArrayWrapper, and then call them from from a main() method in the same class. Here is an example skeleton class to do this, but you are not required to use it; feel free to write your own:
public class ArrayWrapper {
int[] a = new int[15];
public void fill() {
// TODO: fill the array with random integers.
}
public boolean isEven(int index) {
// TODO: test whether the value of array a at
// index is even.
}
public static void main(String[] args) {
ArrayWrapper wrapper = new ArrayWrapper();
wrapper.fill();
// TODO: Print out the even numbers in the array.
}
}
Sample solution See below. [10 marks] Exercise 3: Flipping the Array In this exercise you are to once again make an array of integers and fill it with random numbers. As in the previous exercise, create a wrapper class to hold your methods. But this time, once the array is filled you are to print the array as it is, then call a method reverse() that reverses the array and print it again. For example if the array is {1,4,7,3}, the output will be:
a[0] = 1
a[1] = 4
a[2] = 7
a[3] = 3
reversing...
a[0] = 3
a[1] = 7
a[2] = 4
a[3] = 1
Sample solution ArrayWrapper.java:
import java.util.*;
public class ArrayWrapper {
private int[] a = new int[15];
/**
* Fill the array with random integers.
*/
public void fill() {
Random rn = new Random();
for (int i = 0; i < a.length; i++)
a[i] = rn.nextInt();
}
/**
* Test whether an array holds an even number at a given index.
*
* @param index
* The index to be checked
* @return True if the number is even.
*/
public boolean isEven(int index) {
return ((a[index] % 2) == 0);
}
/**
* @return The length of the array.
*/
public int length() {
return a.length;
}
/**
* Reverse the elements of the array.
*/
public void reverse() {
int temp;
for (int i = 0; i < (a.length / 2); i++) {
// swap elements around
temp = a[i];
a[i] = a[14 - i];
a[14 - i] = temp;
}
}
public static void main(String[] args) {
ArrayWrapper wrapper = new ArrayWrapper();
wrapper.fill();
String evenOrOdd = "";
System.out.println("Odd and Evens:");
for (int i = 0; i < wrapper.length(); i++) {
if (wrapper.isEven(i))
evenOrOdd = "Even";
else
evenOrOdd = "Odd";
System.out.printf("a[%d] = %s%n", i, evenOrOdd);
}
System.out.println("Array:");
for (int i = 0; i < wrapper.length(); i++)
System.out.printf("a[%d] = %d%n", i, wrapper.a[i]);
System.out.println("Reversing...");
wrapper.reverse();
for (int i = 0; i < wrapper.length(); i++)
System.out.printf("a[%d] = %d%n", i, wrapper.a[i]);
}
}
[10 marks] footer This text is a component of the course Informatics 1: Object-Oriented Programming, University of Edinburgh. The HTML was generated with docutils. Date: Wed 11 Feb 2009 15:13:34 GMT OOP Informatics Home