This lab gives an example of the programming expertise expected for this course. If you have a lot of trouble with this lab, you may need to do a bit of review.
The pre-task
The labs of for this course are targeted for the CSCI workstation lab. While it is possible for you to complete the labs on your personal computer you must you install the appropriate software before lab.
The real task
You can use either NetBeans
or Processing for this lab.
In either case,
you will be given driver code that calls
an exceeds
method.
The exceeds
method takes two arguments:
The first is an array of integers, and the second is an integer.
The exceeds
method returns the number of elements
of the array that are greater than the
second argument.
For example, all of the following calls should return 1.
exceeds(new int[]{-100000, 2016}, 150)
exceeds(new int[]{150, 2016}, 150)
exceeds(new int[]{2016}, 150)
Either Or
with NetBeans
Start up NetBeans and create a project for a Java Application. Use the project and class names of your choice.
Delete the default main
method
and replace it with the following method:
public static void main(String[] args) { java.util.Random g = new java.util.Random() ; int X[] = new int[1000 + g.nextInt(10)] ; for (int i = 0; i<X.length; ++i) { X[i] = g.nextInt(10) ; } System.out.println(exceeds(X, 3)) ; System.out.println(exceeds(X, 7)) ; System.out.println(exceeds(new int[]{}, 2015)) ; }
Be sure to prepend the
public static
modifier
to your exceeds
method before compiling your program.
with Processing
Start up Processing with the following program.
void setup() { noLoop() ; } void draw() { java.util.Random g = new java.util.Random() ; int X[] = new int[1000 + g.nextInt(10)] ; for (int i = 0; i<X.length; ++i) { X[i] = g.nextInt(10) ; } println(exceeds(X, 3)) ; println(exceeds(X, 7)) ; println(exceeds(new int[]{}, 2015)) ; }
The
Processing IDE
prepends the public static
modifier
to your exceeds
method before passing it to the
Java compiler.
Writing and testing the missing method
Whatever your choice,
implement the missing exceeds
method.
You may consult the internet, but
write this method on your own.
(Looking at the code of the person next to you is not writing your own.)
When appropriate the lab instructor will provide hints to the
entire class to make sure we finish in a hour or so.
When run, your problem should print three numbers. The first should be close to 600, the second should be close to 200, and the third should be 0.
If it takes a little while
You should work alone on this lab for at least 20 minutes. If you haven’t finished by 20 minutes, find someone who has already finished to help you out!