Results 1 to 7 of 7
- 01-18-2011, 12:44 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 14
- Rep Power
- 0
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.
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.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;
}
}
}
Any ideas or resources?
Best regards,
paulmmjLast edited by paulmmj; 01-18-2011 at 12:54 AM.
- 01-18-2011, 01:38 AM #2
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.
-
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.
- 01-18-2011, 02:11 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 14
- Rep Power
- 0
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!
And here are my errors: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; } } }
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.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
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.
-
Those are not errors, but warnings. If you're not supposed to be using generic expressions, you can safely ignore these warnings.
- 01-18-2011, 02:31 AM #6
- 01-18-2011, 02:31 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 14
- Rep Power
- 0
I think there's something wrong with my comparison logic... I noticed this minor thing here, where my code SHOULD read:
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?else
{
if (two.compareTo(three) >= 0)
return two;
else
return three;
}
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
-
Real Time?
By RaustBD in forum New To JavaReplies: 1Last Post: 01-01-2011, 02:14 AM -
Static (having a hard time)
By maknib in forum New To JavaReplies: 4Last Post: 11-02-2010, 07:05 AM -
Comparable Interface
By Yelrubk in forum New To JavaReplies: 3Last Post: 04-28-2010, 02:46 PM -
interface Comparable<T> problem
By Lennon-Guru in forum New To JavaReplies: 3Last Post: 03-05-2008, 12:17 AM -
Extreme nooby having hard time with this app
By asterix350z in forum New To JavaReplies: 2Last Post: 12-05-2007, 07:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks