9 Replies - 146 Views - Last Post: 04 February 2012 - 09:59 PM Rate Topic: -----

Topic Sponsor:

#1 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Swap Min and Max

Posted 04 February 2012 - 07:41 PM

I have been working in this program and I thought it was working but for some reason I cannot find the problem. I am trying to find the max and min and then swap them. Any help is appreciated. Thank you



class SwapMinMax
{

   public static void swapMinAndMax(Comparable[] values)
   {
	   int index1=0;
   	   int index2=0;
           Comparable Min = values[0];
	   Comparable Max = values[0];

	   for(int i=1; i<values.length; i++){
		   if(values[i].compareTo(Min)<0){
	   		Min = values[i];
	   		index1=i;
	    }

	   if(values[i].compareTo(Max)>0){
		Max = values[i];
		index2=i;
           }
	   }
	int temp = index2;
          index2=index1;
          index1=temp;
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Swap Min and Max

#2 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1786
  • View blog
  • Posts: 3,359
  • Joined: 19-March 11

Re: Swap Min and Max

Posted 04 February 2012 - 07:46 PM

Your arrays are length 0, which means that values[i] will always throw an ArrayIndexOutOfBoundsException.

Doh. Reading fail. Ignore that... :)

This post has been edited by jon.kiparsky: 04 February 2012 - 07:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Swap Min and Max

Posted 04 February 2012 - 07:47 PM

Here, the ints just store values. You must physically swap values[index1] and values[index2] in the values array.
int temp = index2;
index2=index1;
index1=temp


Was This Post Helpful? 1
  • +
  • -

#4 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: Swap Min and Max

Posted 04 February 2012 - 07:56 PM

Ok Mr. Do you mean something like this? By the way thank you for your time.

class Homework1A
{
   /**
      Swaps the smallest and largest value.
      @param values an array of values of a class that implements the
      Comparable interface.
   */

   public static void swapMinAndMax(Comparable[] values)
   {
	   int index1=0;
   	   int index2=0;
		   Comparable Min = values[0];
		   Comparable Max = values[0];

		   for(int i=1; i<values.length; i++){
			   if(values[i].compareTo(Min)<0){
			   		Min = values[i];
			   		index1=i;
				}
			   if(values[i].compareTo(Max)>0){
					Max = values[i];
					index2=i;
				}
		}
			int temp = values[index2];
						values[index2]=values[index2];
						values[index1]=temp;
   }
}



Was This Post Helpful? 0
  • +
  • -

#5 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: Swap Min and Max

Posted 04 February 2012 - 08:08 PM

Ok Mr. I believe this is it:


class Homework1A
{
   /**
      Swaps the smallest and largest value.
      @param values an array of values of a class that implements the
      Comparable interface.
   */

   public static void swapMinAndMax(Comparable[] values)
   {
	   int index1=0;
   	   int index2=0;
		   Comparable Min = values[0];
		   Comparable Max = values[0];

		   for(int i=1; i<values.length; i++){
			   if(values[i].compareTo(Min)<0){
			   		Min = values[i];
			   		index1=i;
				}
			   if(values[i].compareTo(Max)>0){
					Max = values[i];
					index2=i;
				}
		}
			Comparable temp = values[index2];
						values[index2]=values[index2];
						values[index1]=temp;
   }
}

/*Comparable temp = Max;
					Max = Min;
					Min = temp;

					index2=Min
					index1=Max
				*/


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Swap Min and Max

Posted 04 February 2012 - 08:12 PM

Not exactly. This line values[index2]=values[index2]; does nothing. You want to assign it values[index1], not values[index2].
Was This Post Helpful? 1
  • +
  • -

#7 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: Swap Min and Max

Posted 04 February 2012 - 08:16 PM

True. I fixed it Thank you again Mr. Macosxnerd101 and that works now.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7523
  • View blog
  • Posts: 28,896
  • Joined: 27-December 08

Re: Swap Min and Max

Posted 04 February 2012 - 08:20 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

#9 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: Swap Min and Max

Posted 04 February 2012 - 09:00 PM

Excuse me Mr. I have a problem testing the program above. I am not sure if I should open another thread with this question, if so please let me know. Thank you. This is not working now. I know swapMinAndMax is void so it cannot return any values but I am not sure how to fix it then. Any help is appreciated. Thank you

import java.util.Comparator;
import java.util.*;

class Homework1ATester{
	public static void main(String[] args){

		String[] names={"Fred","Wilma","Barney"};
		Comparable first = Homework1A.swapMinAndMax(names);

		System.out.println("The Array" + Arrays.toString(first));
		System.out.println("Expected: " + "[Fred, Barney, Wilma]");
	}
}


Was This Post Helpful? 0
  • +
  • -

#10 bartsimpsong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 11

Re: Swap Min and Max

Posted 04 February 2012 - 09:59 PM

Ok, I got it. Thank you very much.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1