• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

For varsity...

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dagnabbit Fred, you beat me to it.

Ian,

try reading this(brief) article
 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are discussing the same sort of thing here in the Beginners' forum.
 
Ian Cockcroft
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic