hey guys, having a problem, i have 2 constructors, one with 5 paramerters and one with 7. they both except 5 of the same incoming values, so my question is, is there anyway i can write one constuctor, to use the other constuctors code so i dont have to re-write the code, here are the constuctors
public Movie(String title, String actor, String director, int year, int duration){
create a public method that takes 7 params, and call that method from both of the constructors, shuffle your logic so that it can adopt to such changes
ray326
04-14-2005, 09:24 PM
Constructors can't use each other but you could factor out a "group setter" for the five parm constructor. That way it both could share the code.
void initvals(String title, String actor, String director, int year, int duration) {...}
So the five parm constructor does a new then an initvals(), the seven parm construct does a new, initvals() then sets the last two.
keko2005
04-14-2005, 09:30 PM
no quite following u, sorry, but can u explain a little more please?
keko2005
04-15-2005, 12:26 AM
ok, i think this is what u guys ment, can u look at it.it works, but i dont know if its the best way to go about it
private String blank = "";
public Movie(String title, String actor, String director, int year, int duration){
prety much, what you need to do is set all of the values(all of 7 of them) in conChoice() function, only you ahve to make sure that if the 6th and 7th param does not have value then you do not want to initialize those values and that will automatically fall back to 5 params..its prety simple...and you have almost got it
ray326
04-16-2005, 02:27 PM
Instead of "blank, blank" use "null, null" in your parameter list and that should work fine.
keko2005
04-16-2005, 10:25 PM
hey guys, lol your gonna laugh, i read my project wrong and i wasnt supposed to do what u guys were helping me. whatever, but i have a few more questions, if you guys dont mind. im having trouble comprehending how to overwrite the equals method from Object and the compareTo method from Comparable interface. im not sure whats the purpose, and what are we gonna be comparing. i will post the text that my prof. gave me when i get home tonight so maybe u can better understand what im saying.
thanks,
keko
ray326
04-16-2005, 11:41 PM
Not "overwrite", "override". You simply declare your own equals() method in your class. The compareTo() thing is the same except equals() returns true or false, compareTo() returns -1, 0 or 1 so you can use it for sorting.
keko2005
04-17-2005, 11:01 AM
sorting? whatd o u mean? heres the question that he gave us:
You must modify the Customer class you submitted last time, so that it overrides the equals method from
the Object class, and the compareTo method from the Comparable interface. Equality should be based on
the Customer’s ID number and name. Comparison should be performed on the basis of last name, then first
name, and then ID number.
ray326
04-17-2005, 07:42 PM
sorting? whatd o u mean?The only reason for implementing Comparable is the objects are going to be in some kind of ordered collection, i.e. they're going to be sorted. As each object is put into the collection the collection will use the compareTo method to put them into the right place, comparing the new object to the objects already in the collection.
keko2005
04-18-2005, 11:32 AM
ok i understand ray. ok, so i built a compareTo() method and an equals one, i sent both of them the same object and then differnt ones, they return the right values, compare returned 1 for true and -1 for false, same as equals. ok now, when i run the sort method that out prof.gave use, i get this out put.
Unsorted list of customers:
1: Anderson, Jon
2: Squire, Chris
3: Howe, Steve
4: Wakeman, Rick
5: Bruford, Bill
Sorted list of 5 customers:
1: Anderson, Jon
2: Squire, Chris
3: Howe, Steve
4: Wakeman, Rick
5: Bruford, Bill
they are still the same, there not supposed to be, i cant figure out whats wrong with the picture, and i dont understand the retarted replies that my prof. is sending me from the questions i asked him.
maybe u guys can help me out on how to go about doin this, cause im not a good logical thinker.
ok, i have a customer class, that contains variables, first, last, and id. also contains a toString which returns id: last, then first. ok the driver creates and array of objects for the customer class
///////////////////////////////////////////////////////////////////////////////
heres the code:
Customer customers[ ] = new Customer[ 5 ];
customers[ 0 ] = new Customer( "Jon", "Anderson" );
customers[ 1 ] = new Customer( "Chris", "Squire" );
customers[ 2 ] = new Customer( "Steve", "Howe" );
customers[ 3 ] = new Customer( "Rick", "Wakeman" );
customers[ 4 ] = new Customer( "Bill", "Bruford" );
System.out.println( "Unsorted list of customers:" );
Tools.displayArray( customers );
System.out.println();
Tools.sort( customers );
System.out.println( "Sorted list of " + Customer.getCount( ) +
" customers:" );
Tools.displayArray( customers );
System.out.println( )
///////////////////////////////////////////////////////////////////////////
and this is the tools class which contains the sort mehtod:
public static void sort( Object array[ ] ) {
int i, j;
Object index;
for( i = 1; i < array.length; i++ ) {
index = array[ i ];
j = i;
ok, so now that thats out of the way, now heres where i lose control, im supposed to write the comparTo method as u guys know, based on the comparison of last, first, then id. and equality on id, last then first.
this is where i get lost, i build the compareTo method in a couple difffernt ways. i get correct results when testing the method, yet the sort method still doesnt work. why, what am i doing wrong, how do i go about writing the comparTo method so the sort method will work.
ray326
04-19-2005, 12:53 AM
compare returned 1 for true and -1 for false, same as equals
No, equals should return a boolean. Compare should return an int; 0 for equal, >0 for greater, <0 for less. If you have a Customer then what you need is a CustomerList, which contains a sorted collection of those Customers. You'll also need a Comparator class, CustomerComparator that empliments Comparator. Here's (http://www.javaworld.com/javaworld/jw-11-1998/jw-11-collections.html) a quick intro to collections that has a few simple examples.
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.