Jump to content

Help with compareTo method

- - - - -

  • Please log in to reply
7 replies to this topic

#1
vctrshowalter

vctrshowalter

    Newbie

  • Members
  • Pip
  • 3 posts
The first prompt is this: Design a Java interface called Priority that includes two methods: setPriority and getPriority. The interface should define a way to establish a numeric priority among a set of objects. Design and implement a class called Task that represents a task (such as a to-do list) that implements the Priority interface. Create a driver class to exercise some Task objects.

Which I've done here:

public interface Priority

{

	public void setPriority(int level);

	public int getPriority();

}



public class Task implements Priority

{

	private String msg, priority;

	private int level;

	

	//-----------------------------------------------------

	//	Constructor: Creates a task and a priority level.

	//-----------------------------------------------------

	public Task (String msg, int level)

	{

		this.msg = msg;

		this.level = level;

		

		if (level == 1)

			priority = "Critical";

		if (level == 2)

			priority = "Very Important";

		if (level == 3)

			priority = "Normal";

		if (level == 4)

			priority = "Low";

		if (level == 5)

			priority = "Not Important";

	}

	

	//-----------------------------------------------------

	//	Sets the priority level.

	//-----------------------------------------------------

	public void setPriority (int level)

	{

		this.level = level;


		if (level == 1)

			priority = "Critical";

		if (level == 2)

			priority = "Very Important";

		if (level == 3)

			priority = "Normal";

		if (level == 4)

			priority = "Low";

		if (level == 5)

			priority = "Not Important";

	}

	

	//-----------------------------------------------------

	//	Returns the priority level.

	//-----------------------------------------------------

	public int getPriority()

	{

		return level;

	}

	

	//-----------------------------------------------------

	//	Returns a description of the task object.

	//-----------------------------------------------------

	public String toString()

	{

		return msg + "\t" + "Priority Level: " + level + "\t" + priority;

	}

}


This is no problem. However, now I have to do this: Modify the task class from the previous problem so that it also implements the Comparable interface from the Java standard class library. Implement the interface such that the tasks are ranked by priority. Create a driver class whose main method shows these new feature of task objects.
I understand that compareTo will return either -1, 0 or 1. I just don't understand how to sort them once they've been compared and how to print them ranked by priority. Thanks for any help :)

Edited by vctrshowalter, 08 December 2011 - 09:45 AM.


#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
You should compare based on level:

public int compareTo(Object myTaskObject)
{
      if(this.level > ((Task)myTaskObject).level) return 1;
      else if( this.level == ((Task)myTaskObject).level) return 0;
      return -1;
}

For the sorting: put each task in an arraylist since arraylist implements collection. Then call on the static method in the collection class sort, which sorts the elements in ascending order based on their natural ordering, which means it will use the compareTo method in the Task class for comparisons.

This was an assignment when i did OOP so lucky for you I have it coded. If you get trouble let us know.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Slight improvement -well- it's shorter :D :
.....implements Comparable[COLOR=#FF8C00][B]<Task>[/B][/COLOR]

public int compareTo([COLOR=#FF8C00][B]Task [/B][/COLOR]task){
    return this.level - task.level;
}
You don't have to return exactly 1 and -1. Higher than 0 and lower than 0 will do
Javadoc:

Quote

* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.


#4
foulsquare

foulsquare

    Newbie

  • Members
  • Pip
  • 1 posts
lol

#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

foulsquare said:

lol
This does not help.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

fread said:

This does not help.
Wow his first post was "lol"
That's quite funny to me for some reason.

#7
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

foulsquare said:

lol
I think you should head over to the intro section and introduce yourself. @lethatwire: I don't even know why he laughed. Maybe WimDC and I posted a whole lot of crap (or very hilarious) stuff up there.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

fread said:

I think you should head over to the intro section and introduce yourself. @lethatwire: I don't even know why he laughed. Maybe WimDC and I posted a whole lot of crap (or very hilarious) stuff up there.
I think you've misunderstood the reason for my laughter. No where can I find evidence that this was directed towards Wim or you. I know both of you post reliable, correct and easy to follow information.

I find it funny that somebody goes through the trouble of completing the joining process, just to make 1 post of "lol." It's quite inappropriate of foulsquare.
I don't want this topic to go off subject though. So let's leave it there.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users