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;
}
}
Swap Min and Max
Page 1 of 19 Replies - 146 Views - Last Post: 04 February 2012 - 09:59 PM
Topic Sponsor:
#1
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
Replies To: Swap Min and Max
#2
Re: Swap Min and Max
Posted 04 February 2012 - 07:46 PM
Doh. Reading fail. Ignore that...
This post has been edited by jon.kiparsky: 04 February 2012 - 07:48 PM
#3
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
#4
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;
}
}
#5
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
*/
#6
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].
#7
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.
#9
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]");
}
}
#10
Re: Swap Min and Max
Posted 04 February 2012 - 09:59 PM
Ok, I got it. Thank you very much.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|