Java POST THE RIGHT SOLUTION PLEASE: Information is available in the comments pa
ID: 3889083 • Letter: J
Question
Java
POST THE RIGHT SOLUTION PLEASE:
Information is available in the comments
package algs11;
import java.util.Arrays;
import stdlib.*;
/**
* CSC300Program1 version 1.0
*
* Your Name goes here
* You class section goes here
*
* Does your program have compile errors? Yes / No
* Which TODOs have NOT been completed correctly? Delete the lines below for TODOs that are correct
* TODO 1
* TODO 2
* TODO 3
* TODO 4
* TODO 5
*
* This is a skeleton file for your programming assignment. Edit the sections marked TODO.
*
* Unless specified otherwise, you must not change the declaration of any method.
* This will be true of every skeleton file I give you.
*
* For example, you will get zero points if you change the line
*
* to something like
*
* or
*
*
* Each of the functions below is meant to be SELF CONTAINED. This means that
* you should use no other functions or classes. You should not use any HashSets
* or ArrayLists, or anything else! Exception: You may use Math.abs (look it up)
* In addition, each of your functions should go through the argument array at most once.
*/
public class CSC300Program1 {
/**
* valRange returns the difference between the maximum and minimum values
* in the array; Max-Min. You can assume the array is nonempty. Your solution must go
* through the array exactly once. Here are some examples (using "==" informally):
*
*
*/
public static double valRange (double[] list) {
return StdRandom.uniform (); //TODO 1: fix this
}
/**
* distanceBetweenMinAndMax returns difference between the minPosition and
* the maxPosition in an array of doubles.
*
* You can assume the array is nonempty and all elements are unique.
* Your solution must go through the array exactly once. Here are some examples (using "==" informally):
*
*
*/
public static int distanceBetweenMinAndMax (double[] list) {
return StdRandom.uniform (100); //TODO 2: fix this
}
/**
* posOfElementClosestTo returns the position of the element in the array that is
* closest to the theVal parameter, in the absolute value sense.
* In the event of a tie, return the position of the first value found
* (starting from 0)
*
* You can assume the array is nonempty and all values are unique. Your solution
* must go through the array exactly once. Here are some examples (using "==" informally):
*
*
*/
public static int posOfElementClosestTo( double theVal, double[] list) {
return StdRandom.uniform (100); //TODO 3: fix this
}
/**
* A test program, using private helper functions. See below.
*/
public static void main (String[] args) {
// for ValRange: array must be nonempty
testValRange (0, new double[] {11} );
testValRange (0, new double[] { 11,11,11,11,11} );
testValRange (10, new double[] {11, 1} );
testValRange (10, new double[] {1,11} );
testValRange (32, new double[] {11, 21, 9, 31, 41});
testValRange (32, new double[] {41, 21, 9, 31, 11});
testValRange (32, new double[] {11, 41, 9, 31, 21});
testValRange (32, new double[] {-41, -21, -11, -31, -9});
testValRange (32, new double[] {-9, -21, -11, -31, -41});
testValRange (32, new double[] {-41, -11, -9, -31, -21});
testValRange (32, new double[] {-11, -21, -41, -31, -9});
testValRange (0.7, new double[] { 0.2, -0.5, -0.1});
StdOut.println();
// for distanceBetweenMinAndMax: array must be nonempty with unique elements
testDistanceBetweenMinAndMax (0, new double[] {11});
testDistanceBetweenMinAndMax (0, new double[] {-11});
testDistanceBetweenMinAndMax (4, new double[] {9, 11, 21, 31, 41});
testDistanceBetweenMinAndMax (3, new double[] {11, 9, 21, 31, 41});
testDistanceBetweenMinAndMax (1, new double[] {11, 21, 9, 3, 41});
testDistanceBetweenMinAndMax (1, new double[] {11, 21, 31, 9, 41});
testDistanceBetweenMinAndMax (1, new double[] {11, 21, 31, 41, 9});
testDistanceBetweenMinAndMax (4, new double[] {9, -11, -21, -31, -41});
testDistanceBetweenMinAndMax (3, new double[] {-11, 9, -21, -31, -41});
testDistanceBetweenMinAndMax (2, new double[] {-11, -21, 9, -31, -41});
testDistanceBetweenMinAndMax (1, new double[] {-11, -21, -31, 9, -41});
testDistanceBetweenMinAndMax (1, new double[] {-11, -21, -31, -41, 9});
testDistanceBetweenMinAndMax (3, new double[] {1, -4, -7, 7, 8, 11, 9, -5});
testDistanceBetweenMinAndMax (3, new double[] {0.1, -0.4, -0.7, 0.7, 0.8, 1.1, 0.9, -0.5});
StdOut.println();
// for posOfElementClosestTo: array must be nonempty with unique elements
testPosOfElementClosestTo( 0, 3 , new double[] {4});
testPosOfElementClosestTo( 2, 3 , new double[] {1,2,3,4});
// TODO 4 Add calls for three more test cases for posOfElementClosestTo
// these must be substantively different than two provided.
// Try to imagine what 'could go wrong' in the function being tested and create a test case that
// checks this case.
StdOut.println ("Finished tests");
}
/* Test functions --- lot's of similar code! */
private static void testValRange (double expected, double[] list) {
double actual = valRange (list);
if (expected != actual) {
StdOut.format ("Failed valRange(%s): Expecting (%.1f) Actual (%.1f) ", Arrays.toString(list), expected, actual);
}
}
private static void testDistanceBetweenMinAndMax (int expected, double[] list) {
int actual = distanceBetweenMinAndMax (list);
if (expected != actual) {
StdOut.format ("Failed distanceBetweenMinAndMax(%s): Expecting (%d) Actual (%d) ", Arrays.toString(list), expected, actual);
}
}
private static void testPosOfElementClosestTo (int expected, double theVal, double[] list) {
//TO DO 5 fix this!
StdOut.format ("Failed. posOfElementClosestTo Actually the test was not even attempted! Fix this! ");
}
}
Explanation / Answer
import java.util.Arrays;
import stdlib.*;
/**
* CSC300Program1 version 1.0
*
* Your Name goes here
* You class section goes here
*
* Does your program have compile errors? Yes / No
* Which TODOs have NOT been completed correctly? Delete the lines below for TODOs that are correct
* TODO 1
* TODO 2
* TODO 3
* TODO 4
* TODO 5
*
* This is a skeleton file for your programming assignment. Edit the sections marked TODO.
*
* Unless specified otherwise, you must not change the declaration of any method.
* This will be true of every skeleton file I give you.
*
* For example, you will get zero points if you change the line
*
* to something like
*
* or
*
*
* Each of the functions below is meant to be SELF CONTAINED. This means that
* you should use no other functions or classes. You should not use any HashSets
* or ArrayLists, or anything else! Exception: You may use Math.abs (look it up)
* In addition, each of your functions should go through the argument array at most once.
*/
public class CSC300Program1 {
/**
* valRange returns the difference between the maximum and minimum values
* in the array; Max-Min. You can assume the array is nonempty. Your solution must go
* through the array exactly once. Here are some examples (using "==" informally):
*
*
*/
public static double valRange (double[] list) {
return StdRandom.uniform (); //TODO 1: fix this
}
/**
* distanceBetweenMinAndMax returns difference between the minPosition and
* the maxPosition in an array of doubles.
*
* You can assume the array is nonempty and all elements are unique.
* Your solution must go through the array exactly once. Here are some examples (using "==" informally):
*
*
*/
public static int distanceBetweenMinAndMax (double[] list) {
return StdRandom.uniform (100); //TODO 2: fix this
}
/**
* posOfElementClosestTo returns the position of the element in the array that is
* closest to the theVal parameter, in the absolute value sense.
* In the event of a tie, return the position of the first value found
* (starting from 0)
*
* You can assume the array is nonempty and all values are unique. Your solution
* must go through the array exactly once. Here are some examples (using "==" informally):
*
*
*/
public static int posOfElementClosestTo( double theVal, double[] list) {
return StdRandom.uniform (100); //TODO 3: fix this
}
/**
* A test program, using private helper functions. See below.
*/
public static void main (String[] args) {
// for ValRange: array must be nonempty
testValRange (0, new double[] {11} );
testValRange (0, new double[] { 11,11,11,11,11} );
testValRange (10, new double[] {11, 1} );
testValRange (10, new double[] {1,11} );
testValRange (32, new double[] {11, 21, 9, 31, 41});
testValRange (32, new double[] {41, 21, 9, 31, 11});
testValRange (32, new double[] {11, 41, 9, 31, 21});
testValRange (32, new double[] {-41, -21, -11, -31, -9});
testValRange (32, new double[] {-9, -21, -11, -31, -41});
testValRange (32, new double[] {-41, -11, -9, -31, -21});
testValRange (32, new double[] {-11, -21, -41, -31, -9});
testValRange (0.7, new double[] { 0.2, -0.5, -0.1});
StdOut.println();
// for distanceBetweenMinAndMax: array must be nonempty with unique elements
testDistanceBetweenMinAndMax (0, new double[] {11});
testDistanceBetweenMinAndMax (0, new double[] {-11});
testDistanceBetweenMinAndMax (4, new double[] {9, 11, 21, 31, 41});
testDistanceBetweenMinAndMax (3, new double[] {11, 9, 21, 31, 41});
testDistanceBetweenMinAndMax (1, new double[] {11, 21, 9, 3, 41});
testDistanceBetweenMinAndMax (1, new double[] {11, 21, 31, 9, 41});
testDistanceBetweenMinAndMax (1, new double[] {11, 21, 31, 41, 9});
testDistanceBetweenMinAndMax (4, new double[] {9, -11, -21, -31, -41});
testDistanceBetweenMinAndMax (3, new double[] {-11, 9, -21, -31, -41});
testDistanceBetweenMinAndMax (2, new double[] {-11, -21, 9, -31, -41});
testDistanceBetweenMinAndMax (1, new double[] {-11, -21, -31, 9, -41});
testDistanceBetweenMinAndMax (1, new double[] {-11, -21, -31, -41, 9});
testDistanceBetweenMinAndMax (3, new double[] {1, -4, -7, 7, 8, 11, 9, -5});
testDistanceBetweenMinAndMax (3, new double[] {0.1, -0.4, -0.7, 0.7, 0.8, 1.1, 0.9, -0.5});
StdOut.println();
// for posOfElementClosestTo: array must be nonempty with unique elements
testPosOfElementClosestTo( 0, 3 , new double[] {4});
testPosOfElementClosestTo( 2, 3 , new double[] {1,2,3,4});
// TODO 4 Add calls for three more test cases for posOfElementClosestTo
// these must be substantively different than two provided.
// Try to imagine what 'could go wrong' in the function being tested and create a test case that
// checks this case.
StdOut.println ("Finished tests");
}
/* Test functions --- lot's of similar code! */
private static void testValRange (double expected, double[] list) {
double actual = valRange (list);
if (expected != actual) {
StdOut.format ("Failed valRange(%s): Expecting (%.1f) Actual (%.1f) ", Arrays.toString(list), expected, actual);
}
}
private static void testDistanceBetweenMinAndMax (int expected, double[] list) {
int actual = distanceBetweenMinAndMax (list);
if (expected != actual) {
StdOut.format ("Failed distanceBetweenMinAndMax(%s): Expecting (%d) Actual (%d) ", Arrays.toString(list), expected, actual);
}
}
private static void testPosOfElementClosestTo (int expected, double theVal, double[] list) {
StdOut.format ("Failed. posOfElementClosestTo Actually the test was not even attempted! Fix this! ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.