Results 1 to 7 of 7
  1. #1
    paulmmj is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default Having a real hard time with Comparable interface and comparing String length

    Hello Java Forums,

    I missed the day in class that my AP CompSci teacher taught interfaces, but I was given the assignment nonetheless, and I'm trying my best to do it. I've gotten PRETTY close, but I'm still just a few lines of code off from victory, and I am simply out of original ideas and book/internet resources. I have enclosed the typed up question as well as my code thus far, and an comments I may have.

    1. Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parameters and return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part of the Comparable interface, so largest can use the compareTo method of its parameters to compare them. See Section 5.5 of the text for a description of the compareTo method.
    2. Write a class Comparisons whose main method tests your largest method above.
    First prompt the user for and read in three strings, use your largest method to find the largest of the three strings, and print it out. (It's easiest to put the call to largest directly in the call to println.) Note that since largest is a static method, you will call it through its class name, e.g., Compare3.largest(val1, val2, val3).
    Add code to also prompt the user for three integers. Try to use your largest method to find the largest of the three integers. What error message do you get if you pass the integers in directly? This is a situation where the Integer wrapper class is useful. Create a new Integer object to hold each of the integers and pass the objects to largest. Again, it's easiest if you put the call directly in the call to println.

    interface Comparable
    {
    int compareTo (String o);
    }

    public abstract class Compare3 implements Comparable
    {
    public static String largest (String one, String two, String three)
    {
    if (one.compareTo(two) >= 0)
    {
    if (one.compareTo(three) >= 0)
    return one;
    else
    return three;
    }

    else
    {
    if (one.compareTo(three) >= 0)
    return two;
    else
    return three;
    }

    }
    }
    I think the error lies in my if statements with the greater than and less thats, and I think there may be another one creeping in the interface part at the top, as I'm just doing that out of the authority of my book, I don't really understand it. I just need some pointing in the right direction, I've become a rather competent coder since my first few posts here at Java Forums.

    Any ideas or resources?

    Best regards,

    paulmmj
    Last edited by paulmmj; 01-18-2011 at 12:54 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Where in your instructions does is say to make Compare3 abstract?
    Where in your instructions does is say to make compare3 implement Comparable.
    Where in your instructions does is say to make the parameters and return type of largest String? It especially won't work when later your instructions say to use it to compare Integers.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may wish to carefully re-read the instructions given to you. No where does it say that the Compare3 class should itself implement Comparable, nor that it should be abstract (which in fact it shouldn't be, especially if all it has is a static method). Also, you should not create a Comparable interface since this already exists as part of standard Java (look here for the API).

    Also, do the instructions tell you that the largest method should take three Strings for parameters? Again, you must re-read the instructions, do it slowly and carefully, and ask if you have any questions about the instructions. Most of your mistakes are from carelessness not misunderstanding.

    Best of luck.

  4. #4
    paulmmj is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default Apologies

    I'm sorry, I was in error with those simple mistakes, being a bit of a slave to my IDE trying to fix errors as the pop up with the "quick fixes." I've taken all those suggestions into consideration, but I'm still having on persistant difficulty that I'll explore after I paste my new code. But before I do, thank you for responding so fast!

    Java Code:
    public class Compare3
    {
    	public static Comparable largest (Comparable one, Comparable two, Comparable three)
    	{	
    		if (one.compareTo(two) >= 0)
    		{
    			if (one.compareTo(three) >= 0)
    				return one;
    			else
    				return three;
    	    }
    		
    		else
    		{
    			if (one.compareTo(three) >= 0)
    				return two;
    			else
    				return three;
    		}	
    
    	}	
    }
    And here are my errors:

    Java Code:
    Comparable is a raw type. References to generic type Comparable<T> should be parameterized	Compare3.java	/MP2/src	line 3	Java Problem
    Comparable is a raw type. References to generic type Comparable<T> should be parameterized	Compare3.java	/MP2/src	line 3	Java Problem
    Comparable is a raw type. References to generic type Comparable<T> should be parameterized	Compare3.java	/MP2/src	line 3	Java Problem
    Comparable is a raw type. References to generic type Comparable<T> should be parameterized	Compare3.java	/MP2/src	line 3	Java Problem
    Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized	
    Compare3.java	/MP2/src	line 5	Java Problem
    Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized	
    Compare3.java	/MP2/src	line 7	Java Problem
    Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized	
    Compare3.java	/MP2/src	line 15	Java Problem
    These errors were why I defined Comparable and compareTo at the top of my code in the interface portion, they made this go away, but I couldn't input my Strings.

    When I did input my Strings, it always gave the "three" as opposed to the longest one, although this was with my own compareTo.

    I'm currently tinkering to see what might fix this.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Those are not errors, but warnings. If you're not supposed to be using generic expressions, you can safely ignore these warnings.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by paulmmj View Post
    When I did input my Strings, it always gave the "three" as opposed to the longest one, although this was with my own compareTo.
    The compareTo method of the String class compares Strings alphabetically and not their length. It seems you need clarification as to what your teacher means by largest.

  7. #7
    paulmmj is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    I think there's something wrong with my comparison logic... I noticed this minor thing here, where my code SHOULD read:

    else
    {
    if (two.compareTo(three) >= 0)
    return two;
    else
    return three;
    }
    I've amended it now, but if I test it with Strings, it ALWAYS returns two, no matter the length. Any suspicions as to why?

    But okay, if I can live with errors, I can live with errors. I can add a @SuprressWarning clause to get them out of sight.

Similar Threads

  1. Real Time?
    By RaustBD in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 02:14 AM
  2. Static (having a hard time)
    By maknib in forum New To Java
    Replies: 4
    Last Post: 11-02-2010, 07:05 AM
  3. Comparable Interface
    By Yelrubk in forum New To Java
    Replies: 3
    Last Post: 04-28-2010, 02:46 PM
  4. interface Comparable<T> problem
    By Lennon-Guru in forum New To Java
    Replies: 3
    Last Post: 03-05-2008, 12:17 AM
  5. Extreme nooby having hard time with this app
    By asterix350z in forum New To Java
    Replies: 2
    Last Post: 12-05-2007, 07:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •