public class Homework1 { /** * minValue returns the minimum value in an array of
ID: 3780938 • Letter: P
Question
public class Homework1 {
/**
* minValue returns the minimum value in an array of doubles. You can assume
* the array is nonempty and has no duplicates. Your solution must go
* through the array exactly once. Your solution must not call any other
* functions. Here are some examples (using "==" informally):
*
* <pre>
* -7 == minValue (new double[] { 1, -4, -7, 7, 8, 11 })
* </pre>
*/
public static double minValue (double[] list) {
return 0; //TODO: fix this with your code
}
/**
* minPosition returns the position of the minimum value in an array of
* doubles. The first position in an array is 0 and the last is the
* array.length-1.
*
* You can assume the array is nonempty and has no duplicates. Your solution
* must go through the array exactly once. Your solution must not call any
* other functions. Here are some examples (using "==" informally):
*
* <pre>
* 0 == minPosition(new double[] { -13, -4, -7, 7, 8, 11 })
* </pre>
*/
public static int minPosition (double[] list) {
return 0; //TODO: fix this with your code
}
/**
* distanceBetweenMinAndMax returns difference between the minPosition and
* the maxPosition in an array of doubles.
*
* You can assume the array is nonempty and has no duplicates. Your solution
* must go through the array exactly once. Your solution must not call any
* other functions. Here are some examples (using "==" informally):
*
* <pre>
* 1 == distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11, -9 }) // -9,11
* </pre>
*/
public static int distanceBetweenMinAndMax (double[] list) {
return 0; //TODO: fix this with your code
}
/**
* The following tests below should pass if your methods above are correct.
* It is required for you to write 5 more tests for each method to ensure your
* methods above are written correctly.
*/
public static void main(String[] args) {
// minValue Test sample
double minValue = minValue (new double[] { 1, -4, -7, 7, 8, 11 });
if (minValue == -7) {
System.out.println("The minValue test was successful.");
} else {
System.out.println("The minValue test was not successful.");
}
// minPosition Test sample
double minPosition = minPosition(new double[] { -13, -4, -7, 7, 8, 11 });
if (minPosition == 0) {
System.out.println("The minPosition test was successful.");
} else {
System.out.println("The minPosition test was not successful.");
}
// distanceBetweenMinAndMax Test sample
double distance = distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11, -9 });
if (distance == 1) {
System.out.println("The distanceBetweenMinAndMax test was successful.");
} else {
System.out.println("The distanceBetweenMinAndMax test was not successful.");
}
}
}
Explanation / Answer
package snippet;
public class Homework1 {
/**
* minValue returns the minimum value in an array of doubles. You can assume
* the array is nonempty and has no duplicates. Your solution must go
* through the array exactly once. Your solution must not call any other
* functions. Here are some examples (using "==" informally):
*
* <pre>
* -7 == minValue (new double[] { 1, -4, -7, 7, 8, 11 })
* </pre>
*/
public static double minValue (double[] list) {
double min=9999; //set minimum value
for(int i=0;i<list.length;i++)
{
if(list[i]<min) //if current element is less than minimum
min=list[i];
}
return min; //return minimum value
}
/**
* minPosition returns the position of the minimum value in an array of
* doubles. The first position in an array is 0 and the last is the
* array.length-1.
*
* You can assume the array is nonempty and has no duplicates. Your solution
* must go through the array exactly once. Your solution must not call any
* other functions. Here are some examples (using "==" informally):
*
* <pre>
* 0 == minPosition(new double[] { -13, -4, -7, 7, 8, 11 })
* </pre>
*/
public static int minPosition (double[] list) {
double min=9999;//set minimum value
int pos=0;//set minimum value index
for(int i=0;i<list.length;i++)
{
if(list[i]<min)//if current element is less than minimum
{
min=list[i]; //set minimum value
pos=i;//set index
}
}
return pos;
}
/**
* distanceBetweenMinAndMax returns difference between the minPosition and
* the maxPosition in an array of doubles.
*
* You can assume the array is nonempty and has no duplicates. Your solution
* must go through the array exactly once. Your solution must not call any
* other functions. Here are some examples (using "==" informally):
*
* <pre>
* 1 == distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11, -9 }) // -9,11
* </pre>
*/
public static int distanceBetweenMinAndMax (double[] list)
{
double min=9999;//set minimum value
int pos=0;//set minimum value index
for(int i=0;i<list.length;i++)
{
if(list[i]<min)//if current element is less than minimum
{
min=list[i];//set minimum value
pos=i;//set index
}
}
double max=-9999;//set max value
int pos1=0;//set index
for(int i=0;i<list.length;i++)
{
if(list[i]>max)//check if current is greater
{
max=list[i];
pos1=i;
}
}
return Math.abs(pos1-pos);//return difference of two positions
}
/**
* The following tests below should pass if your methods above are correct.
* It is required for you to write 5 more tests for each method to ensure your
* methods above are written correctly.
*/
public static void main(String[] args) {
// minValue Test sample
double minValue = minValue (new double[] { 1, -4, -7, 7, 8, 11 });
if (minValue == -7) {
System.out.println("The minValue test was successful.");
} else {
System.out.println("The minValue test was not successful.");
}
// minPosition Test sample
double minPosition = minPosition(new double[] { -13, -4, -7, 7, 8, 11 });
if (minPosition == 0) {
System.out.println("The minPosition test was successful.");
} else {
System.out.println("The minPosition test was not successful.");
}
// distanceBetweenMinAndMax Test sample
double distance = distanceBetweenMinAndMax(new double[] { 1, -4, -7, 7, 8, 11, -9 });
if (distance == 1) {
System.out.println("The distanceBetweenMinAndMax test was successful.");
} else {
System.out.println("The distanceBetweenMinAndMax test was not successful.");
}
}
}
=============================================================
Output:
The minValue test was successful.
The minPosition test was successful.
The distanceBetweenMinAndMax test was successful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.