How should I deal with this bad operator error?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 797 Views - Last Post: 30 November 2017 - 10:03 AM Rate Topic: -----

#16 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12218
  • View blog
  • Posts: 45,291
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 24 November 2017 - 11:43 AM

I think you need to read up on the Comparable interface and try things for yourself, rather than asking for a step-by-step guide for what to do. You have a direction upon which to work- now give it a try!
Was This Post Helpful? 1
  • +
  • -

#17 mike455  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 28 November 2017 - 07:31 PM

Okay, I went back and made a lot of changes. Try to bear with me. But I don't think I can use the compareTo method within the fetch method of this line like I can in the insert method. For example, if I tried to do something like this in the insert method would it okay?

if(data[i].getKey().compareTo(newListing.getKey()));


But I could not use the compareTo method for this line below, right and the reason is because the compareTo only accepts Listing objects:

if(data[i].getKey().compareTo(targetKey))

If that is the case, how could I replace the > and use the compareTo method within that specific line?

This post has been edited by mike455: 28 November 2017 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

#18 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12218
  • View blog
  • Posts: 45,291
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 28 November 2017 - 07:37 PM

Quote

For example, if I tried to do something like this in the insert method would it okay?


Try it and see.

Quote

But I could not use the compareTo method for this line below, right and the reason is because the compareTo only accepts Listing objects:

if(data[i].getKey().compareTo(targetKey))


If you implemented the Comparable interface correctly, then the compareTo() method should accept Listing objects. Again, try this and see.
Was This Post Helpful? 0
  • +
  • -

#19 mike455  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 28 November 2017 - 09:40 PM

Okay, I went back and got everything to compile fine, but I am not sure if it's really a binary search tree array. Can please take a look at the program and tell me suggestions if at any you might have for me. Here is the complete program.
public class Listing implements Comparable<Listing>
{ private String name;  // key field
  private int ID;
   private double GPA;

  public Listing(String n, int id, double gpa)
  {  name = n;
      ID = id;
     GPA = gpa;
  }
   public String toString()
   {   return("Name is " + " "  + name +
                    "\nID is" + " " + ID +
                    "\nGPA is" + " "  + GPA + "\n");
   }
   public Listing deepCopy()
   {  Listing clone = new Listing(name, ID, GPA);
      return clone;
   }
   public int compareTo(Listing other)
   {  
       return this.name.compareTo(other.getKey());
   }
   public int compareTo(String targetKey)
   { 
       return (name.compareTo(targetKey));
   } 
   public String getKey()
   {  
       return name;
   }
 }// end of class Listing




public class BinaryTreeArray
{ 
   private Listing[] data; 
   private int size; 
   private int i = 0; 

  public BinaryTreeArray()
  { 
      size = 100; 
      data = new Listing[size]; 
  }
  public boolean insert(Listing newListing)
  { 
     while(i < size && data[i]!= null)
     { 
       if(data[i].getKey().compareTo(newListing.getKey()) > 0)
         i = 2 * i + 1; 
        else 
         i = 2 * i + 2; 
     } 
     if(i >= size) 
       return false; 
     else 
     { 
         data[i] = newListing.deepCopy();
          return true;
      } 
  }
  public Listing fetch(String targetKey)
  {  
    while(i< size && data[i]!= null && data[i].getKey()!=targetKey)
    {
        if(data[i].getKey().compareTo(targetKey) > 0)
          i = 2 * i + 1;
       else
          i = 2 * i + 2;   
    }
    if(i >= size || data[i] == null)
      return null; 
    else 
      return data[i].deepCopy();
  }

} 



import java.util.Scanner;


public class MainBinaryTreeArray
{
  public static void main(String[] args)
  {  

     int choice; 
     Scanner scan= new Scanner(System.in);
     BinaryTreeArray data = new BinaryTreeArray(); 
     Listing l1  = new Listing("Carol",    4354,  3.2);
     Listing l2  = new Listing("Timothy",  2353,  4.0);
     Listing l3  = new Listing("Dean",     4544,  2.4);
     Listing l4  = new Listing("Sue",      3445,  3.0);
  

   do
   { 

       // Choose which operation by entering a number 

      System.out.println("*****************(Menu Operations:)******************"); 
      System.out.println(" (Press 1). Insert."); 
      System.out.println(" (Press 2). Fetch."); 
      System.out.println(" (Press 5). Output."); 
      System.out.println(" (Press 6). Exit the program."); 
      System.out.println("Enter your choice: "); 
      choice = scan.nextInt(); 
      switch(choice)
      { 
        case 1: 
             System.out.println("Is a student's info inserted: " + data.insert(l1));  
           break; 
        case 2:
         System.out.println("Is a student's info fetched: "  + data.fetch("Timothy"));
           break; 
        case 5: 
            System.out.println("The student's info that is output: " + l4.toString()); 
      }
  
  
  }while(choice!=6);

 }

}

Was This Post Helpful? 0
  • +
  • -

#20 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12218
  • View blog
  • Posts: 45,291
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 28 November 2017 - 09:52 PM

I would delete the compareTo(String) method in your Listings class. It is unnecessary.

Beyond that, what specific problems or errors are you encountering? Does your implementation work as desired?
Was This Post Helpful? 0
  • +
  • -

#21 mike455  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 29 November 2017 - 05:12 PM

Yeah, actually I can't get my showAll() method to print out all the student's records and also my insert and fetch method aren't printing out the specific student's when I use input the correct parameters. Can you help me please? Here is an updated version of the program.

public class Listing implements Comparable<Listing>
{ private String name;  // key field
  private int ID;
   private double GPA;

  public Listing(String n, int id, double gpa)
  {  name = n;
      ID = id;
     GPA = gpa;
  }

  public String toString()
  {   return("Name is " + " "  + name +
                    "\nID is" + " " + ID +
                    "\nGPA is" + " "  + GPA + "\n");
   }
   public Listing deepCopy()
   {  Listing clone = new Listing(name, ID, GPA);
      return clone;
   }
   public int compareTo(Listing other)
   {  
       return this.name.compareTo(other.getKey());
   }
   public String getKey()
   {  
       return name;
   }
 }// end of class Listing




public class BinaryTreeArray
{ 
   private Listing[] data; 
   private int size; 
   private int i = 0; 

  public BinaryTreeArray()
  { 
      size = 100; 
      data = new Listing[size]; 
  }
  public void showAll(){
    
    for(; i<size; i++)
      System.out.print(data[i] + " ");

   } 
  public boolean insert(Listing newListing)
  { 
     while(i < size && data[i]!= null)
     { 
       if(data[i].getKey().compareTo(newListing.getKey()) > 0)
         i = 2 * i + 1; 
        else 
         i = 2 * i + 2; 
     } 
     if(i >= size) 
       return false; 
     else 
     { 
         data[i] = newListing.deepCopy();
          return true;
      } 
  }
  public Listing fetch(String targetKey)
  {  
    while(i< size && data[i]!= null && data[i].getKey()!=targetKey)
    {
        if(data[i].getKey().compareTo(targetKey) > 0)
          i = 2 * i + 1;
       else
          i = 2 * i + 2;   
    }
    if(i >= size || data[i] == null)
      return null; 
    else 
      return data[i].deepCopy();
  }

} 




import java.util.Scanner;


public class MainBinaryTreeArray
{
  public static void main(String[] args)
  {  

     int choice; 
     Scanner scan= new Scanner(System.in);
     BinaryTreeArray data = new BinaryTreeArray(); 

     Listing l1  = new Listing("Carol",    4354,  3.2);
     Listing l2  = new Listing("Timothy",  2353,  4.0);
     Listing l3  = new Listing("Dean",     4544,  2.4);
     Listing l4  = new Listing("Sue",      3445,  3.0);
  

   do
   { 

       // Choose which operation by entering a number 

      System.out.println("*****************(Menu Operations:)******************"); 
      System.out.println(" (Press 1). Insert."); 
      System.out.println(" (Press 2). Fetch."); 
      System.out.println(" (Press 5). Output all student's records."); 
      System.out.println(" (Press 6). Exit the program."); 
      System.out.println("Enter your choice: "); 
      choice = scan.nextInt(); 
      switch(choice)
      { 
        case 1: 
        System.out.println("The student's info that's inserted: " + data.insert(l1)); 
        break; 
        case 2:
        System.out.println("The student's info that's fetched: " + data.fetch("Timothy"));
         break; 
        case 5: 
        System.out.print("All students' info that is output: " ); 
        data.showAll();
      }
  
  
  }while(choice!=6);

 }

}

Was This Post Helpful? 0
  • +
  • -

#22 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 659
  • View blog
  • Posts: 2,736
  • Joined: 13-June 14

Re: How should I deal with this bad operator error?

Posted 29 November 2017 - 11:36 PM

In BinaryTreeArray, why are you sharing i between all the methods?
Was This Post Helpful? 0
  • +
  • -

#23 mike455  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 12-November 17

Re: How should I deal with this bad operator error?

Posted 30 November 2017 - 06:30 AM

I changed that! So now it's inside each insert and fetch method. The i is initialized to 0 in both insert and fetch methods. But do you actually think you could help me out with the problem at hand. If I wanted to return a student's record inside of the fetch method, would I need to say something like this: return data[i].deepCopy instead of return true (which actually doesn't print out the student's information based on the string parameter I put when calling the fetch method)inside of this method on this line:

Listing student; 
return student= data[i].deepCopy();



Also, I am having a terribly time trying to figure out why when I call the showAll() method inside of the do-while, it only outputs the first student record but not the all of the rest of the student records. The rest of the student records just return like this null null null null, etc. Do you think you could help please?

This post has been edited by mike455: 30 November 2017 - 06:30 AM

Was This Post Helpful? 0
  • +
  • -

#24 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12218
  • View blog
  • Posts: 45,291
  • Joined: 27-December 08

Re: How should I deal with this bad operator error?

Posted 30 November 2017 - 10:03 AM

Quote

If I wanted to return a student's record inside of the fetch method, would I need to say something like this: return data[i].deepCopy instead of return true (which actually doesn't print out the student's information based on the string parameter I put when calling the fetch method)inside of this method on this line:


The fetch() method is specified to return a Listing object. You cannot return a boolean value in it. It looks like you are already returning the Listing object in the fetch() method, so I'm not sure what your question is. Does your fetch() method not work as expected?


Quote

Also, I am having a terribly time trying to figure out why when I call the showAll() method inside of the do-while, it only outputs the first student record but not the all of the rest of the student records. The rest of the student records just return like this null null null null, etc. Do you think you could help please?


I would suggest debugging your insert() method. Try inserting a few Listing objects. After each insertion, print out the contents of the array. If the elements are not getting inserted, then you know that your insert() method is not working correctly.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2