The main goals for this lab are:
This lab will be submitted and autograded on Gradescope.
Notes: in this lab you are not allowed to include any
import
statements other thanimport java.lang.IndexOutOfBoundsException
This lab is a paired programming assignment. What exactly does that mean? You will be working in pairs on the CS lab computers. Each pair will be working on one computer. One person will be the driver and the other person will be the navigator. Here is the rule: the driver controlls the lab computer, but the driver can only type what the navigator tells them to type. For this to work well, each pair should be constantly talking among themselves. After each problem, you will switch roles, the navigator will become the driver and the driver will become the navigator.
You will design all the necessary classes in order to make the following driver program work properly and produce the given expected output (we have broken down the step below).
public static void main(String[] args){
Mammal[] mammals = new Mammal[4];
mammals[0] = new Dolphin();
mammals[1] = new Platypus();
mammals[2] = new Human();
mammals[3] = new CSStudent();
for (int i=0; i< mammals.length; i++){
System.out.print("Generally, a " + mammals[i].getName());
System.out.print(" can be found ");
if(mammals[i].livesInWater() == false){
System.out.print("on land, ");
}
else {
System.out.print("in water, ");
}
System.out.print("it can ");
if(mammals[i].laysEggs() == false) {
System.out.print("not ");
}
System.out.print("lay eggs, and is often overheard saying'");
mammals[i].speak();
System.out.println("'");
}
}
Will produce the following output:
Generally, a Dolphin can be found in water, it can not lay eggs, and is often overheard saying 'ak, ak, ak, ak'
Generally, a Platypus can be found in water, it can lay eggs, and is often overheard saying 'errrr'
Generally, a Human can be found on land, it can not lay eggs, and is often overheard saying 'I'll take a grande latte with a double-shot of espresso'
Generally, a CSStudent can be found on land, it can not lay eggs, and is often overheard saying 'I love programming!'
Make sure that each class is declared public
and stored
in their own files.
Design a Mammal
class with the following:
private String
variables named name
and sound
void
method named speak()
that prints the object’s sound
boolean
method named laysEggs()
boolean
method named livesInWater()
Design a Dolphin
class that inherits from the Mammal
class.
As seen in the Driver code, it should include a constructor that takes no arguments.
Override methods as appropriate
Design a Platypus
class that inherits from the Mammal
class.
As seen in the Driver code, it should include a constructor that takes no arguments.
Design a Human
class that inherits from the Mammal
class.
As seen in the Driver code, it should include a constructor that takes no arguments.
Override methods as appropriate
Design a CSStudent
class that inherits from the Human
class.
Override methods as appropriate
Before moving on in this lab, run the Driver program and make sure the output is correct.
======= As seen in the Driver code, it should include a constructor that takes no arguments. Override methods as appropriate
Before moving on in this lab, submit your code to gradescope to make sure the output is correct.
Working with arrays are great, we can easily access data from an array and insert data into an array. Just give me the index, and accessing and inserting data is easy!
But, what happens we run out of space in our array - Oh no! We need to create a new bigger array, then copy over all the data from the older smaller array to the new bigger array - that doesnt sound like fun at all!
Wouldn’t it be nice if we have a data structure that took care of all this building
and expanding for us. Thats what we are going to build now.
We will create a class called ExpandableArray
that takes care of that.
So, if a programmer wants to just store data in an array,
they dont have to worry about running out of space, your data structure will do that
automatically.
You will be using and adding more functionality to your ExpandableArray
in Homework02.
We have provided a file called
ExpandableArrayTests.java
that you can use
to test your ExpandableArray
.
You can download it by running:
wget https://raw.githubusercontent.com/BMC-CS-151/BMC-CS-151.github.io/main/labs/lab02/ExpandableArrayTests.java
The tests will not compile until you’ve written completed the lab.
Below we list some of the instance methods you will need to implement.
Lets start by creating a file called
ExpandableArray.java
. We can define our class with the following line:
public class ExpandableArray<E>
Question: In our class definition what is E
and what does it denote?
Our ExpandableArray
class will need to keep track of some data.
Question: What data do you think we will need to store?
Now add those instance variables
Create two constructors:
We will create two insert
methods. The first will take in an item and insert
the item at the begining of the ExpandableArray
.
The second will take in an item and a location and insert
the item in the specified location.
When a user inserts an item in the array, make sure to move
shift elements up or down to maintain our rule of always storing an element whose list index is i
at index i
of the array.
For the insert method which takes an index to insert at, ensure that the index is valid.
If the user specifies an invalid index, throw an IndexOutOfBoundsException
error.
An index is invalid if does not correspond to an element in the array. If our underlying array is of size 20, but only 4 of them are filled, any index > 4 is invalid.
Your error should include an informative message. Similar to how the follow code
int[] test = new int[2];
int a = test[3];
would throw the following error with the following message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 2
Both insert
methods should be named insert
and return void
type.
Write a method named get
that will extract
an item from the ExpandableArray
. The user should be able to specify
an index and the ExpandableArray
will return the item at that index.
Again, if the user specifies an index that is greater than the number
of elements in the ExpandableArray
, throw an IndexOutOfBoundsException
.
Write a remove method that enables a user to specify the index and
the ExpandableArray
will remove the item at that index.
Since we do not want any empty spaces in the middle of our ExpandableArray
,
make sure to move over every item that you need to when removing the item
in the index specified by the user. Your remove method should return the removed element.
Write a toString()
method that will print out each item
in the ExpandableArray
. Each item should be seperated by a comma and space.
Write a set
method which takes an element and an index, and sets the value accordingly.
Again, if the user specifies an index that is greater than the number of elements in the ExpandableArray
, throw an IndexOutOfBoundsException
.
Write a size
method which returns the number of elements in the array.
After compiling the test class, make sure to include the ea
flag when running
the program:
java -ea ExpandableArrayTests
This will ensure that all tests actually run. We are using assert
to test the ExpandableArray
.
Once your tests pass, submit your code to Gradescope.
After getting ExpandableArrayTests.java
to pass all the test,
change the data structure in exercise 1 to use your ExpandableArray
.
Make sure the
program works the same.
In todays lab we covered Inheritance and creating our first data structure.
Before leaving, make sure you submit to Gradescope.