GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Apr-2008, 22:36
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Help....


Given
JAVA Code:
public class Tree {
	private String species;
	private double height;
	private int age;

	public Tree (String aTree, double newHeight, int newAge) {
		species = aTree;
		height = newHeight;
		age = newAge;
	}

	public String getSpecies() {
		return species;
	}

	public double getHeight() {
		return height;
	}

	public void getAge() {
		return age;
	}

	public void increaseAge(int numYears, double rateOfGrowth) {

		// to be completed as specified in the problem above
	}

	public void addHeighte(double amount) 
{
			height = height + amount;
		}

		public int compareTo(Tree other) 
		{
			// to be completed as specified in the problem above
}

public boolean equals(Tree other)
{

	return getSpecies().equals(other.getSpecies()) &&
			Math.abs(height - other.height) <= 12;
}

	public String toString() 
{
		return “Species: ” + species + 
				“ Height: ” + height;
	}
 
public class TreeList {
	private Tree[] list;
	private int numTrees;
	
	/**
	 	constructs an empty list of size 1
	 */
	public TreeList() {
	
		list = new Tree[1];
		numTrees = 0;
	}
	
	/**
		adds the specified Tree to the end of the list, after 
resizing if necessary
	*/
	public void addToEnd(Tree nextTree) {
		
		if (numTrees == list.length)
			resize();
		
		list[numTrees] = nextTree;
		numTrees++;
	}
	
	
/**
		returns the number of Trees in the list
	*/
	public int size() {
	
		return numTrees;
	}

	/**
	 	helper method to allow dynamic growth of the list, by doubling 
the capacity of the list and maintaining the current order
	 */
	private void resize() {
	
		Tree[] temp = new Tree[list.length * 2];
		
		for (int i = 0; i < size(); i++) {
			temp[i] = list[i];
		}
		
		list = temp;
	}
}

Complete this method for the TreeList class:


/*
InsertAtIndex: Adds nTree to the collection at
the specified index position.

It does this, by
checking to see if there is enough room in the
collection, and resizing if necessary.

Moving the Tree element that is currently at
the specified index position after sliding all the
trees currently in the collection to the next
index position in the age.

Make sure the size() method of this class returns
the correct number of trees in the collection after
the insertion.
*/
public void insertAtIndex(Tree aTree, int index) {


Complete this method for the TreeList class:


/*
treesOfSameAge: returns a new TreeList of all Trees whose age is equal to the age of parameter other.
*/
public TreeList treesOfSameAge(Tree other) {



Write the following header and method for the Tree class:

compareTo(Tree other) – takes one Tree parameter. If the species of this tree and the other tree are the same, returns a value less than 0 if the height of this tree is less than the height of the other by more than 12 inches, returns a value greater than 0 if the height of this tree is greater than the height of the other by more than 12 inches, returns 0 if the heights of the two trees are within 12 inches of each other. If the species of the two trees are different, than this method returns a value less than 0 if the species of this tree comes before the species of the other tree alphabetically, or else returns a value greater than 0 if the species of this tree comes after the species of the other tree alphabetically.


increaseAge(int numYears, double rateOfGrowthPerYear) – increases the age of this tree by the number of years, and the height of this tree by the rate of growth for this tree, per year.

told a girl i could help her with her java hw but its far more advanced
than what i Know... can anyone help me out here please? it would be greatly apprreciated
Last edited by LuciWiz : 30-Apr-2008 at 11:55. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 28-Apr-2008, 11:41
JustinFox JustinFox is offline
Junior Member
 
Join Date: Mar 2008
Posts: 59
JustinFox will become famous soon enough

Re: Help....


for the increaseSize method, just
add the newYears or whatever to the age variable of the class.
and for the rateOfChange, just do this

JAVA Code:

height = height + (Integer.doubleValue(newYears) * rateOfChange);


for the Compare, all this means is that when you type something like:

JAVA Code:

tree1.compareTo(tree2);


it will run all the tests provided in the instructions and return a certain value depending on which it finds.

so....

JAVA Code:

private int compareTo(Tree tree)
{

  if(this.species.equalsIgnoreCase(tree.species)) // where 'this' refers to tree1 // in my previous example 'tree1.compareTo(tree2);
  return -1;

  // etc...

}


Hope this gets you in the right direction...

Justin Fox
  #3  
Old 29-Apr-2008, 09:06
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: Help....


Hey Justin im still confused on the compare To method I understand what you wrote before that. thank you!
  #4  
Old 30-Apr-2008, 12:49
JustinFox JustinFox is offline
Junior Member
 
Join Date: Mar 2008
Posts: 59
JustinFox will become famous soon enough

Re: Help....


No problem , glad I could help.

for the compareTo() method, if you look at the previous post
in the line of code:

JAVA Code:


tree1.compareTo(tree2);



And then look at the actual structure of the compareTo method:

JAVA Code:

int compareTo(Tree tree) // here your return type is integer, and you are
{                                 // passing a Tree object as your parameter.

if(this.getSpecies().equalsIgnoreCase(tree.getSpecies()))
return -1;

//in this if statement, notice the keyword 'this'.  if you look at the actual
//call of the function 'tree1.compareTo(tree2);' then you can clearly see
//that 'this' is refering to 'tree1'.

// the equalsIgnoreCase() method is a String method that see's if two
// string are alike, regardless the case.  For example: 'HEllO' and 'hello'.

// if this is too confusing, you can alway construct your compareTo() method
// like so:

// int compareTo(Tree tree1, Tree tree2)
// {
    
         // if(tree1.getSpecies().equalsIgnoreCase(tree2.getSpecies()))
         // return -1;

         // etc, etc... 

// }

}



I hope this clarifies everything about the compareTo method.

Justin Fox
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.