Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Chapter 17 Generics PC2 PC 2 Assignment explained at the bottom. PC 1 I man

ID: 3661490 • Letter: J

Question

Java Chapter 17 Generics PC2

PC 2 Assignment explained at the bottom.

PC 1 I managed to put together which is as follows:

Write a generic class named MyList, with a type paramter T. The type paramter T should be contrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which accepts a pramter of type T. When an argument is passed to the method, it is added to the ArrayList. Write two other methods, largest and smallest, which return the largest and smallest values in the ArrayList.

This is my code for PC1

/*
* Write a generic class named Mylist, with a type prameter T.
* The type prameter T should be constrained to an upper bound; the number class
* The clsas should have as a field an ArrayList of T. Write a public method named add,
* which accepts a pramter of type T. When an argument is passed to the method, it is
* added to the ArrayList. Write two other methods, largetst and smallest, which return
* the largest and smallest values in the ArrayList.
*/

package mylist;

/**
*
* @author charles.parker
*/

import java.util.ArrayList;

public class MyList<T extends Number> {
  
// Create a field of ArrayList T
ArrayList<T> myList = new ArrayList<T>();
  
// Add method accepts param of type T and adds it to myList
public void add(T addme) {
myList.add(addme);
}
  
// largest() loops through the list and finds the largest number, returns value
// where value is the largest number
public T largest() {
T value = myList.get(0);
for(int i = 1; i < myList.size(); i++) {
if (myList.get(i).doubleValue() > value.doubleValue()) {
value = myList.get(i);
}
}
return value;
}
  
//smallest() loops through the list and finds the smallest number, returns value
// where value is the smallest number
public T smallest() {
T value = myList.get(0);
for(int i = 1; i < myList.size(); i++) {
if (myList.get(i).doubleValue() < value.doubleValue()) {
value = myList.get(i);
}
}
return value;
}

public static void main(String[] args) {
  
MyList<Integer> myListDriver = new MyList<Integer>();

//add stuff to the list
myListDriver.add(4);
myListDriver.add(44);
myListDriver.add(900);
myListDriver.add(12);
myListDriver.add(82);

//output largest
System.out.println("Largest Number is:" + myListDriver.largest());
//output smallest
System.out.println("Smallest number is:" + myListDriver.smallest());
}
  
}

Assuming this is correct i need help with PC-2.

Modify the Mylist class that you wrote for programming Challenge 1 so the type parameter T should accept any type that implements the Comparable interface. Test the class in a program that creates on instance of MyList to store Integers, and another instance to store Strings.

Explanation / Answer

To approach this, I have referred to the section of my book which covers, "constraining a type parameter to a type that implements an interface," which seems like what I need to do here. Unfortunately, I can't seem to figure out how to do that =/

Would it be

public class MyList<T extends Comparable

Yes, that means you want it to implement Comparable. But Comparable isn’t Comparable; it is Comparable<T>. You you often want <T extends Comparable<? super T>>

public class Test {

       public <T extends Comparable> T findLarger(T x, T y) {

                if(x.compareTo(y) > 0) {

                      return x;

                 } else {

                      return y;

                         }

               }

  public static void main(String args[])

          {

              Test t = new Test();

             //insert code here

               }

      }



why does Object x = t.findLarger(123, “456”); work??

I mean 123 and "456" are of different types? How can these be compared in the line x.compareTo(y)??

It works because both Integer and String implement Comparable. So it can compile that line, interpreting T = Comparable. The thing is, Comparable is a generic class as well, and for complete use of generics (which would prevent you comparing different types like that) you need to do something like this:

?

3

4

5

6

7

public <T extends Comparable<? super T>> T findLarger(T x, T y) {

if(x.compareTo(y) > 0) {

        return x;

    } else {

        return y;

    }

}

Then t.findLarger(123, "456") won't compile because Integer implements Comparable<Integer>, and String implements Comparable<String>, so it can't find a single value of T to match them both.

reference:

I'll try. In the first case:

?

1

public <T extends Comparable> T findLarger(T x, T y)

?

1

public <T extends Comparable<T>> T findLarger(T x, T y)

?

1

public <T extends Comparable<? super T>> T findLarger(T x, T y)

is actually a more flexible version that does the same thing. Since Integer implements Comparable<Integer>, and String implements Comparable<String> you don't need this more flexible version. But an example where you would is the class java.sql.Date (used with databases). This extends java.util.Date, and implements Comparable<java.util.Date>. So a java.sql.Date argument could never be used with a constraint like T extends Comparable<T>, but it could be used with T extends Comparable<? super T>. This last bit is a bit complicated, though, so don't worry too much if it doesn't make sense at the moment. Just try and understand the difference between the first two examples.

public class MyList<T extends Comparable

Yes, that means you want it to implement Comparable. But Comparable isn’t Comparable; it is Comparable<T>. You you often want <T extends Comparable<? super T>>

public class Test {

       public <T extends Comparable> T findLarger(T x, T y) {

                if(x.compareTo(y) > 0) {

                      return x;

                 } else {

                      return y;

                         }

               }

  public static void main(String args[])

          {

              Test t = new Test();

             //insert code here

               }

      }



why does Object x = t.findLarger(123, “456”); work??

I mean 123 and "456" are of different types? How can these be compared in the line x.compareTo(y)??

It works because both Integer and String implement Comparable. So it can compile that line, interpreting T = Comparable. The thing is, Comparable is a generic class as well, and for complete use of generics (which would prevent you comparing different types like that) you need to do something like this:

?

3

4

5

6

7

public <T extends Comparable<? super T>> T findLarger(T x, T y) {

if(x.compareTo(y) > 0) {

        return x;

    } else {

        return y;

    }

}

Then t.findLarger(123, "456") won't compile because Integer implements Comparable<Integer>, and String implements Comparable<String>, so it can't find a single value of T to match them both.

reference:

I'll try. In the first case:

?

1

public <T extends Comparable> T findLarger(T x, T y)

You can call this with any arguments where you can pick a T that fits them all.

Now, if you call t.findLarger(123, "456"), what can T be? The first argument is an int, which will be boxed to an Integer. So T can be Integer, or any supertype of Integer. Which means Number, Object, Serializable and Comparable (implemented interfaces are possible supertypes as well). But there's a constraint: T extends Comparable. That rules out Number, Object and Serializable. T must be either Integer or Comparable.

Now consider the second argument. It's a String. That would match T being String, Object, Serializable, Comparable or CharSequence. Again, the constraint rules out some of them, so T can be String or Comparable.

The line will compile if we can pick a T that matches both arguments. We can: T = Comparable.

As you've already realised, that lets you do things that don't make sense, like comparing an integer and a string. But, Comparable is actually a generic interface, and if we give it a generic type we can make the compile-time checking stronger. So we can try:

?

1

public <T extends Comparable<T>> T findLarger(T x, T y)

Now if we try and call t.findLarger(123, "456"), what can T be?

Looking at the first argument, and considering the constraint, T can be Integer, or Comparable<Integer> (which is implemented by Integer). Looking at the second argument, T can be String or Comparable<String>. Which means that there's no choice of T that matches both arguments. So the compiler will disallow it.

The version I gave before:

?

1

public <T extends Comparable<? super T>> T findLarger(T x, T y)

is actually a more flexible version that does the same thing. Since Integer implements Comparable<Integer>, and String implements Comparable<String> you don't need this more flexible version. But an example where you would is the class java.sql.Date (used with databases). This extends java.util.Date, and implements Comparable<java.util.Date>. So a java.sql.Date argument could never be used with a constraint like T extends Comparable<T>, but it could be used with T extends Comparable<? super T>. This last bit is a bit complicated, though, so don't worry too much if it doesn't make sense at the moment. Just try and understand the difference between the first two examples.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote