This week’s lab is a chance for us to practice the topics for this week: generic types and a commonly useful interface.
Knowing how to write generic methods and classes can save you a lot of repetitive work for some projects. This is another aspect of Java that is very flexible. One of the best examples of generics being used is in the ArrayList class and other built-in collection types in Java. Speaking of which, there are several interfaces that are compatible with many of the built-in types in Java, which we’ll also be taking the opportunity to practice using here.
Construct the lab file by following the instructions below. The check-in for the lab this Friday will be for all parts of the lab. Once you have completed the program, you will submit it for this assignment.
Part 1: Generic Classes and Methods
Generics allow you to create classes and methods with no specific type. This can save you a lot of effort when programming if you were otherwise planning on creating a lot of classes or methods that were exactly the same except for using different types.
Watch the video below for some examples on creating classes and methods with generic types. This is the same video that was available earlier in the module. Your assignment for this part of the lab will be based on the second example shown in the video, the Addition class.
Create a class called Exponent which functions similarly to the Addition class shown in the video, except for the mathematical operation of exponents. You should use Math.pow() for your arithmetic in the number value methods. Your class must use two generic types for its two operands. Part 2: The Comparable Interface
There are a handful of interfaces built into Java that have commonly used applications. The advantage of learning these interfaces (rather than remaking the functionality yourself) is that a wide variety of Java’s other built-in types were designed with these interfaces in mind and will automatically be compatible with objects that implement these interfaces. For this part of the lab, we will practice implementing one of the most prominent of these interfaces: the Comparable interface
The Comparable interface is used to make objects of a class easily comparable to one another. By implementing Comparable, you are specifying that objects of this class have some kind of order to them, and they can be directly compared to determine which one “comes first”. This is a particularly useful interface when working with objects that need to be sorted, or that otherwise have prominent numeric values that imply some kind of precedence.
The Comparable interface has one abstract method that you must define: compareTo. This method has one parameter, but the type of the parameter changes depending on the class you are comparing it to. This is because the Comparable interface uses a generic type, which becomes the type of objects that this class can be compared to. When you implement the Comparable interface, you must specify the type of objects that this class can be compared to.
public class Foo implements Comparable {
; Objects of this class can be compared to other Foo objects, or any inheriting class
public int compareTo(Foo o) {
; The interface method has a parameter of type Foo to match the type in the class header
}
}
public class Bar implements Comparable {
; Objects of this class can be compared to Numbers or any inheriting class
public int compareTo(Number o) {
; The method has a parameter of type Number to match the type in the class header
}
}The compareTo method compares an object of the class you’re defining with the parameter object and returns an integer. The returned integer signals how the two objects compare to one another:
If the returned value is positive, then the object calling the method comes “after” the parameter object.
If the returned value is negative, then the object calling the method comes “before” the parameter object.
If the returned value is 0, then the two objects are considered equal.
To practice using Comparable, create a new class called Jar that represents jars full of liquids. The Jar class needs the following members:
Variables:int volume – the maximum volume of the jar in ounces
double fillPercent – the percent of the jar that is currently filled with liquid (0 means empty, 0.5 means half full, 1 means completely full).
A constructor with one parameter to set the max volume of the jar. The fillPercent should start at 0.
An accessor method for the max volume of the jar
An accessor method for the amount of ounces of liquid currently in the jar. This is calculated by multiplying the current fill percent by the max volume.
A mutator method that changes the fill percent of the jar (i.e. fills the jar)
You may also add another mutator method to empty the jar to assist with testing
Have the Jar class implement the Comparable interface for the type Jar (implements Comparable). Define the compareTo method according to the following rules:
One Jar is considered to be “after” another if it is filled with more liquid. Don’t just compare the percents! Use the accessor method you wrote to compare the actual ounces in each Jar.
If the two Jar objects are filled with the same amount of liquid, then the one with the larger maximum volume comes “after”. To rephrase the rules in other words, Jars are sorted in order of least filled to most filled, with ties being broken by the total size of the Jars. One of the advantages of using the Comparable interface is that ArrayLists were designed with that interface in mind. If an ArrayList contains objects that are Comparable to one another, then you can use the sort method to automatically sort the list. (The sort method can be passed a Comparator object to be used for the comparison, but if your array contents are already comparable to one another you can just use null as the parameter.)
Create a short main method in your Jar class that tests your Jar class by doing the following:
Create an ArrayList of Jars.
Add a new Jar to the ArrayList with a maximum volume of 18 ounces that is 50% full (9 ounces).
Add another new Jar to the ArrayList with a maximum volume of 10 ounces that is 10% full (1 ounce).
Add another new Jar to the ArrayList with a maximum volume of 10 ounces that is 90% full (9 ounces).
Print the contents of the ArrayList in the order that you added them (print the size and fill amount of each jar so you know which is which).
Sort the ArrayList using sort(null).
Print the contents of the ArrayList again. If the Jars were sorted into ascending order of fill amount and size respectively, then you’ve implemented the Comparable class correctly.
This is the minimum amount of progress that is required for this week’s check-in. Please make your check-in post with all files you’ve created for the lab. Please remember to make the check-in post before the deadline. If no revisions are necessary with your check-in post, then that means you are done with this lab and ready to submit.
Submission
For this lab you will be submitting the following files:
The Exponent file you created for part 1
The Jar file you created for part 2
Submit your files by clicking “Start Assignment” at the top of the page and then selecting your files for submission. You can submit all files by selecting “Add Another File” in the submission box. Please submit the files individually; this assignment will NOT accept a zip file.
This week’s lab is a chance for us to practice the topics for this week: generic
By admin