aspose file tools*
The moose likes Java in General and the fly likes interfaces for a lab Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "interfaces for a lab" Watch "interfaces for a lab" New topic
Author

interfaces for a lab

Adam Jones
Greenhorn

Joined: Aug 22, 2002
Posts: 19
ok, i am in a java class right now and i have worked ahead a few labs and i am completely stuck on something right now. the lab requires the use of an interface for a rental car program. i made all of my classes and the interface but when i try to run it and enter the information i get a zero for the two outputs that i should have numbers in.
if at all possible could anyone offer any idea as to what i am doing wrong or what i forgot....also, all constructive criticism will be more than welcome....many thanks for any time that you put into helping me
this is the test class i wrote(sorry, none of the code posted have comments yet)

this is the interface provided with the java course

this is one of the classes i wrote for each car class needed (Economy, Midsize, Luxury) each of the other two classes are exactly alike with the appropriate words substituted for each car category

sorry about the lenght of this post
adam jones
Adam Jones
Greenhorn

Joined: Aug 22, 2002
Posts: 19
oops, i just noticed something i forgot to remove from one of the sections of code
in the last code section the Economy class, the (double x)

is not supposed to be there....i was giving something a try and it did not work....just forgot to take it out
Adam Jones
Greenhorn

Joined: Aug 22, 2002
Posts: 19
i hope no one took the time to work on this using any amount of brainpower....i was working on it a while ago and found my problem. i was trying to grasp the whole interface thing and not mess it up and while i was working with my classes i ended up removing all of the nice little lines that actually called the methods that calculated everything. the code i posted was code i was working on while i was at school and when i got home i opened up the same project that i was working on(i was farther on the code at home but had time to kill at school so i tried to catch up and all) and found that the method calls were in there and everything worked fine with the corrrect code
so if anyone tried to sift through my crap, thank you very much for the attempt and i am sorry if i wasted any of your time
one question though:
the whole interface thing...is it important enough to use often? it seemed to me that i could just as easily put everything in the classes themselves. Of course, this was a tiny project and i am sure a huge project that needs to use the same methods would make use of interfaces. Is that right? Feedback ?
thanks again
adam jones
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
First of all, I'm fairly new to Java and programming myself so don't assume I know what I'm talking about. It seems to me your code has pretty much gone around the interface, which is probably why it doesn't seem necessary to you. Let me try and show you what I mean...

I'll be the first to admit that's a pretty poor exmaple, it's late and JAI is giving me a migraine, but I -think- that sort of conveys the point. I'm not sure why the hell the interface has calculation methods, everytime the mileage gets updated it'll need to be recalculated anyway, seems unnecessary to create a separate method.
As for interfaces, they're VERY important. It's not really a question of 'how often' to use them but simply a matter of using them when they're appropriate. The point (as I understand it) is to keep your code modular and flexible, and (if I understand correctly) is about the closest thing to 'multiple inheritance' there is in Java. Sometimes, as with small examples like this, it's difficult to see the usefulnesss in it, it can almost seem redundant. Here's a simple little example, let's say we have a 'Ford' and we have a 'AutomaticTransmission'. We code them specifically for each other and they interact directly with each other, and they work perfectly, why would we go through the hassle of using interfaces?
But what happens if we want to stick a 'ManualTransmission' on the Ford? Or what if we want to use that AutomaticTransmission on a Dodge instead? We can't, because the Ford and the AutomaticTransmission are tied directly to each other, to get them to work with something else we'd have to go in and modify their code. Instead of constantly modifying the code to make it work with a different car or transmission (and then having to modify EVERY SINGLE ONE if we want to change something) why not use an interface? We can make a 'Car' interface and a 'Transmission' interface. We'll create our 'AutomaticTransmission' and 'ManualTransmission' classes and both will implement the Transmission interface, along with any new transmissions we come up with in the future. Then we'll do the same with our cars, every one will implement the Car interface.
Now... all any 'transmission' has to do is code itself based upon the car's interface... not around 'Dodge' or 'Ford' but around 'Car'. As long the car implements the Car interface the transmission won't have problems. We'll also code our cars around the 'transmission' interface so they'll work with any transmission that implements it. If we ever need to modify something, we don't have to go through and change EVERY version of each transmission type (one for each type of car), just the single transmission class, which makes maintenance a hell of alot easier.
I hope that helped.
[ August 03, 2003: Message edited by: Ken Blair ]
Adam Jones
Greenhorn

Joined: Aug 22, 2002
Posts: 19
wonderful, thank you for all of your help
[ August 15, 2003: Message edited by: Adam Jones ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interfaces for a lab