Need help with compareTo method

Joined
Mar 25, 2009
Messages
1
Reaction score
0
Hi,
I need help with my comapreTo method. I have a program that is to sort through data of the SalariedEmployee and sort them based on either there salary, last name, or first name, but I can't figure this insertionSort() method. Can somebody please help me. Here is my program.


// classDataArray.java
// data items as class objects
// to run this program: C>java ClassDataApp
////////////////////////////////////////////////////////////////
public abstract class PersonA
{
protected String lastName;
protected String firstName;
protected int age;
//--------------------------------------------------------------
public PersonA(String last, String first, int a)
{ // constructor
lastName = last;
firstName = first;
age = a;
}

public abstract double getPay( );// abstract method
//--------------------------------------------------------------
public void displayPersonA()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Age: " + age);
}
//--------------------------------------------------------------
public String getLast() // get last name
{ return lastName; }

} // end class Person
////////////////////////////////////////////////////////////////

class ClassDataArrayA
{
private SalariedEmployee[] a; // reference to array
private int nElems; // number of data items
private int salary;

public ClassDataArrayA(int max) // constructor
{
a = new SalariedEmployee[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public SalariedEmployee find(String searchTitle)
{ // find specified value
int j;
for(j=0; j<nElems; j++) // for each element,
if( a[j].getTitle().equals(searchTitle) ) // found item?
break; // exit loop before end
if(j == nElems) // gone to end?
return null; // yes, can't find it
else
return a[j]; // no, found it
} // end find()
//-------------------------------------------------------------- // put person into array
public void insert(String theLast, String theFirst, int theAge, String theTitle, double salary, double theHours)
{
a[nElems] = new SalariedEmployee(theLast, theFirst, theAge, theTitle, salary, theHours);
nElems++; // increment size
}
// ------------------------------------------------------------
public int compareTo(SalariedEmployee salary)
{

}

public void insertionSort()
{
int in, out;


for(out=1; out<nElems; out++) // out is dividing line
{
SalariedEmployee temp = a[out]; // remove marked item
in = out; // start shifts at out
while(in>0 && a[].compareTo(salary)>= temp) // until one is smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()

//--------------------------------------------------------------
public boolean delete(String searchTitle)
{ // delete person from array
int j;
for(j=0; j<nElems; j++) // look for it
if( a[j].getTitle().equals(searchTitle) )
break;
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // shift down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()

//--------------------------------------------------------------
public void displayA() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPersonA(); // display it
}
//--------------------------------------------------------------
} // end class ClassDataArray
////////////////////////////////////////////////////////////////
class ClassDataAppA
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ClassDataArrayA arr; // reference to array
arr = new ClassDataArrayA(maxSize); // create the array
// insert 10 items
arr.insert("Adams", "John", 33,"Director", 50,000);
arr.insert("Smith", "Jay", 33,"Programmer", 75,000);
arr.insert("SOng", "Lee", 44, "Teacher", 25,000);
arr.insert("Parker", "Jeff", 55, "Worker", 30,000);
arr.insert("Bok", "Sam", 66, "President", 90,000);
arr.insert("Cruz", "Tom", 66, "Postman", 31,000);


arr.displayA(); // display items

String searchKey = "Worker"; // search for item
SalariedEmployee found;
found=arr.find(searchKey);
if(found != null)
{
System.out.print("Found ");
found.displayPersonA();
}
else
System.out.println("Can't find " + searchKey);

System.out.println("Deleting Smith, Yee, and Creswell");
arr.delete("Director"); // delete item


arr.displayA(); // display items again

} // end main()
} // end class ClassDataApp


public class SalariedEmployee extends PersonA
{
private String title;
private double salary;
private double weeks;

public static void delete() {
System.out.println("The class method in PersonA.");
}

public SalariedEmployee (String theLast, String theFirst, int theAge, String theTitle, double theSalary, double theWeeks)
{
super(theLast, theFirst, theAge);// call to superclass constructor
if ((theSalary >= 0) && (theWeeks >= 0))
{
title = theTitle;
salary = theSalary;
weeks = theWeeks;
}
else
{
System.out.println(
"Fatal Error: creating an illegal hourly employee.");
System.exit(0);
}
}

/**
*Returns the pay for the month.
*/
public double getPay()
{
return salary/12;
}

public boolean equals(Object otherObject)
{
if(otherObject == null)
return false;
else if (getClass() != otherObject.getClass())
return false;
else
{
SalariedEmployee otherSalariedEmployee =
(SalariedEmployee)otherObject;
return (super.equals(otherSalariedEmployee)
&& (salary == otherSalariedEmployee.salary));
}
}

public String toString()
{
return (getLast() + " " + "\n$" + salary + " per year");
}


public void displayPersonA() //this method overrides display in superclass

{
System.out.print(" Title: " + title);
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Age: " + age);
}

public String getTitle() // get last name
{ return title; }

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top