0

I have used the comparable interface before but using it with generic objects and a second object has been causing me some difficulties

Here is my driver program

import java.io.*;
import java.util.*;
public class Prog2 {
public static void main (String[]args){

    //Declare Variables 
        Scanner inFile = null;
        ListArray<Part> partArray = new ListArray<Part>(13);


    //Open the file
        try {
            inFile = new Scanner(new File("parts.txt"));
        }        
     //If the file is not found, end the program   
        catch(FileNotFoundException e){
            System.out.println("Error: File not found");
            System.exit(0);
        }  
      //While the file has new text, read it in  
        while(inFile.hasNext()){
        //Read a line of code in    
            String temp = inFile.nextLine();
        //split the line into an array  
            String[] tempA = temp.split(",[ ]*");
        //place the specific info into variables    
            int pnum = Integer.parseInt(tempA[0]);
            String name = tempA[1];
            double price = Double.parseDouble(tempA[2]);
            String warN = tempA[3];
            int quant = Integer.parseInt(tempA[4]);
        //add the info into an object   
            partArray.add(new Part(pnum, name,price,warN,quant));                   
    }



    }

}

The class meant to be written like an Array list

public class ListArray <E extends Comparable>{

//Declare Variables 
    private E[] list;
    private int size;

//Construct Constructor 
    public ListArray(){
        list = (E[]) new Comparable[10];
    }

    public ListArray(int capacity){
        list = (E[]) new Comparable[capacity];
    }

/*This method will allow users to get the variable stored 
 * at the index they specify
 * @param: int index: the index of the wanted item
 * @return: E: the item at the speicifed index */
    public E get(int index){
        return list[index];
    }

/*This method will allow users to add an element to the 
 * end of the list array 
 * @param: E item: the item being added to the array */
    public void add(E item){
        list[size] = item;
        size++;
    }

/*This mehod will allow the user to find a specified item 
 * inside of the array 
 * @param: E target: the item the user wants to know the index of 
 * @return: int: the index of the item found */
    public int find(E target){

        for(int i = 0; i < size; i++){

            if(target.compareTo(list[i]) == 0){
                return i;
            }
        }
        return -1;
    }

/*This method will allow users to get the size of the array 
 * @return: int: the size of the array */
    public int size(){
        return size;
    }
}

and the Part class that reads in from a csv file.

public class Part <E extends Comparable>{

    //Declare Variables 
        private int pnum;
        private String name;
        private double price;
        private String warh;
        private int quant;

    //Construct Constructor 
        public Part(){
            pnum = 0;
            name = "";
            price = 0.0;
            warh = "";
            quant = 0;
        }

        public Part(int pnum, String name, double price, String warh, int quant){
            this.pnum = pnum;
            this.name = name;
            this.price = price;
            this.warh = warh;
            this.quant = quant;     
        }

    //Getters
        public int getPnum(){
            return pnum;
        }

        public String getName(){
            return name;
        }

        public double getPrice(){
            return price;
        }

        public String getWarh(){
            return warh;
        }

        public int getQuant(){
            return quant;
        }

    //Setters
        public void setPnum(int pnum){
            this.pnum = pnum;
        }

        public void setName(String name){
            this.name = name;
        }

        public void setPrice(double price){
            this.price = price;
        }

        public void setWarh(String warh){
            this.warh = warh;
        }

        public void setQuant(int quant){
            this.quant = quant;
        }

When I run the program, I am given this error inside of the console

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Bound mismatch: The type Part is not a valid substitute for the bounded parameter of the type ListArray Bound mismatch: The type Part is not a valid substitute for the bounded parameter of the type ListArray at Prog2.main(Prog2.java:8)

From the looks of it, this is a problem of how COmparable is implemented in one of my classes, and it not being correctly implemented in the other. I tried looking at other posts on the website and tried implementing them to no avail. Thank you so much!

Chase Geis
  • 5
  • 1
  • 3
  • Possible duplicate of [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – k5_ Sep 16 '18 at 17:43
  • Don't use raw types. So basically do `ListArray >` and `Part >` to avoid them – k5_ Sep 16 '18 at 17:44
  • I'm not experienced with generics but with Comparable they should be used in this way `>` – Maxim Sep 16 '18 at 17:46
  • @k5_ While it's true that the OP shouldn't be using raw types, the raw type isn't causing the error in this case, so it's not an appropriate duplicate. – Radiodef Sep 16 '18 at 18:27

2 Answers2

1

You have specified your ListArray to only be paramtrizable with types that extend Comparable

ListArray <E extends Comparable>

But, you're trying to parametrize it with Part, which does not extend Comparable.

It looks like you've made some mistake in making Part generic. You should have Part implement Comparable i.e. :

public class Part implements Comparable<Part>

And then implement the compareTo method in Part

@Override
public int compareTo(Part other) {
    // ... code here
}
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
0

Your problem here stems from the fact that you declared the Part class of using a generic E which extends the Comparable interface.

Same goes for your ListArray class, in which you define it again as accepting a E which extends the Comparable interface.

When you try to create a new ListArray by doing so:

ListArray<Part> partArray = new ListArray<Part>(13);

it'll effectively expect something that is within bounds, in this case this being something that implements the Comparable interface. Since your Part object does not do so, this is reason why you get this error (also the compiler message is quite informative about this).

I would generally suggest you have a good read on generics if you attempt to use them, as it seems that you're lacking in understanding them.

akortex
  • 5,067
  • 2
  • 25
  • 57