File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes For varsity... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For varsity..." Watch "For varsity..." New topic
Author

For varsity...

Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
Guys, I really need some help. This project needs tto be in tomorrow. I have done about 70% and just have this piece left. Just running out of time.Can anyone help?

Design and implemet a class CAR that describes the key characteristics of a car , such as model, year, make, manufacturer, colour, horsepower, number of cylinders ets.
Car class should implement the Comparable interface. The natural order of the car objects is based on the ascending orders of the following attyributes of the cars in descending significance.
manufacturer
model
year
write a program SortCars that instantiates a numnber of instances of the Car class, inserts them into a list, sorts cars in the list and prints out the cars in their natural order.

The question...
Extend the Car class in the previous exercise by defining a comparator for each attribute of a car.write a pprogram SortCars2 similar to the program SortCars in the previous exercise, except that it can sort cars based on different attributes of the cars in eithe ascending or descending order using different comparators. the program takes a command-line argument indicating the attribute to be sorted on and whether in ascending or descending order.
eg java SortCArs2 model ascend

Thanks a mil
Ian
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42273
    
  64
What do the 70% you have consist of? Where are you stuck? Post specific questions, and we'll help you get going.

The way you phrase the question sounds like you have very little, and are looking for a solution, but that's not what we do here: DoYourOwnHomework


Ping & DNS - my free Android networking tools app
Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
Yes, I am asking you guys to do it. The 70% is the other questions. there are 8 all together.

I am going to ask for an extension and as I go, post what I have and,hopefully, get feed back. Afterall, you guys wont be in the exam...

my apolgise
Ian
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, well, get the extension, try to do it on your own, come back if you can't, post what you have, and we'll help.


[Jess in Action][AskingGoodQuestions]
Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
I have copied this from the text book o get the jist of what needs to happen, but getting an error.......



the error
C:\JavaCode\Chapter 7\Car.java:3: Car is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
public final class Car implements Comparable
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You error message is about a class named "Car" that incorrectly implements Comparable; whereas the code you show up is for a class named "Alpha" that correctly does so. Not sure why you've showed us this class.

To make the error go away, Car needs to have a compareTo() method like the one in this "Alpha" class.
Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
No, I still get the error even though I over ride compareTo

C:\JavaCode\Chapter 7\Alpha.java:3: Alpha is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
public final class Alpha implements Comparable
^
1 error

Tool completed with exit code 1

Thanks for everything
i.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Your compareTo() has to have a single parameter of type "Object", be public, and return "int"; otherwise you'll get one error or another. In this case, if the method is defined, the error is probably the parameter type.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Originally posted by Ernest Friedman-Hill:
Your compareTo() has to have a single parameter of type "Object"...
Notice that the code you posted doesn't satisfy that. Your compareTo() method has a single parameter, but its type is Alpha and not Object.
Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
something like this??


I still get an error.

C:\JavaCode\Chapter 7\Alpha.java:40: cannot resolve symbol
symbol : variable letter
location: class java.lang.Object
int i = letter.compareTo(a.letter);
^
1 error

Tool completed with exit code 1

So I removed letter..


It compiles but gives a run time error
[ August 03, 2006: Message edited by: Ian Cockcroft ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK, now we're getting somewhere.

Now, read the error message. What does it say? Is that right? (hint: yes.)

Two things you need to know to fix it?

1) How do you test whether a variable of one type is actually referring to an instance of some specific other type?

2) How do you tell the compiler to treat a variable of one type as if it referred to an instance of some other specified type?
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
dagnabbit Fred, you beat me to it.

Ian,

try reading this(brief) article


Java Regular Expressions
Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
It compiles, but, error I am getting...

Exception in thread "main" java.lang.ClassCastException
at java.lang.String.compareTo(String.java:777)
at Alpha.compareTo(Alpha.java:40)
at java.util.Arrays.mergeSort(Arrays.java:1152)
at java.util.Arrays.mergeSort(Arrays.java:1163)
at java.util.Arrays.sort(Arrays.java:1079)
at java.util.Collections.sort(Collections.java:113
at Alpha.main(Alpha.java:18)
Press any key to continue . . .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Every class generally expects that its compareTo() method will be passed an instance of its own class; i.e., you'll write Car.compareTo() expecting that the argument can be cast to a Car. String.compareTo() is written the same way; what are you passing to it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Are you using Java 5?
If so, then the interface you are thinking of is no longer called Comparable; it is now Comparable<T>
So if you set up your Car class to implement Comparable<Car>, you can have a methodNo casts or anything needed. I have tried it out. You need a quick look at the Generics page in the Java Tutorial.
But it won't work if you are using J1.4.2.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
They are discussing the same sort of thing here in the Beginners' forum.
Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
OK, my car class so far




but getting an error........
C:\JavaCode\Chapter 7\Car.java:7: cannot resolve symbol
symbol : constructor Car (java.lang.String,java.lang.String,int)
location: class Car
Car[] carArry = {new Car("Toyota","Collola", 1999)
^
1 error

Tool completed with exit code 1


I have been through it and trhrough it and it works if year is a String. what am i missing?

regards
Ian
[ August 04, 2006: Message edited by: Ian Cockcroft ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You've declared the constructor parameter to be an Integer, but you're passing an int.
Although this works in Java 5, it won't in JDK 1.4 or earlier.
Ian Cockcroft
Greenhorn

Joined: Feb 17, 2002
Posts: 29
OK, i'm just going to go with string, it works...sortof

if i declare the array as


it compiles and runs, giving me the content

but if I add elements to the array, i get an error.



this is so frustrating. should i go back to the beginners forum?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If there is only one Car, then your compareTo() will never be called when you call sort(), right? As there are no comparisons to be done.

As soon as you have more than one Car, then sort() will start comparing them.

Your compareTo() is still doing something wrong that I explained a few posts back. You're calling compareTo() on manufacturer, which is a String; you're passing in car as an argument which is a... ? What does String.compareTo() want to see as an argument?

In compare to you have to, at a minimum, cast the argument to the correct type, and then compare the members of "this" to the members of the incoming argument. You're skipping a few steps, and comparing the member variables of "this" to the argument itself, and that's why you're getting an error.

Should this be in the beginner forum? Yeah, I think so. Let's move it there. But I suspect your problem is really not lack of knowledge so much as failure to breathe. You need to calm down a little and think about each line of code some more.

One more tip: good programmers never write a whole class before even trying to compile it. Write the absolute minimum amount of code needed to compile, then compile it and test it. Then write a little more, and test that. Write an extra test driver class, or write some statements in a main() method to test the methods you write. Keep the tests around, so that when you change the class, you can easily test it again. Add to it a little at a time: code, compile, test, code, compile, test. This helps you understand your code better and have more confidence in yourself and your knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For varsity...