In: Computer Science
JAVA SHOW YOUR OUTPUT
package exampletodo;
import java.util.Arrays;
import stdlib.*;
/**
* Edit the sections marked TODO
*
* Unless specified otherwise, you must not change the declaration of any
* method.
*/
public class example {
/**
* valRange returns the difference between the maximum and minimum values in the
* array; Max-Min. Precondition: the array is nonempty. Your solution must go
* through the array at most once.
*
* Here are some examples (using "==" informally):
*
* <pre>
* 0 == valRange (new double[] { -7 })
* 10 == valRange (new double[] { 1, 7, 8, 11 })
* 10 == valRange (new double[] { 11, 7, 8, 1 })
* 18 == valRange (new double[] { 1, -4, -7, 7, 8, 11 })
* 24 == valRange (new double[] { -13, -4, -7, 7, 8, 11 })
*
* The code below is a stub version, you should replace the line of code
* labeled TODO with code that achieves the above specification
* </pre>
*/
public static double valRange(double[] list) {
return -1; // TODO 1: fix this code
}
/**
* posOfLargestElementLtOeT returns the position of the largest element in the
* array that is less than or equal to the limit parameter if all values are
* greater than limit, return -1;
*
* Precondition: the array is nonempty and all elements are unique. Your
* solution must go through the array exactly once.
*
* <pre>
* 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0
* 5 == posOfLargestElementLtOeT(3, new double[] { 11, -4, -7, 7, 8, 1 }), // value:1 is in pos 5
* -1 == posOfLargestElementLtOeT(-7, new double[] { 1, -4, -5, 7, 8, 11 }), // all elements are > -7
*
* The code below is a stub version, you should replace the line of code
* labeled TODO with code that achieves the above specification
* </pre>
*/
public static int posOfLargestElementLtOeT(double limit, double[] list) {
return -2; // TODO 2: fix this code
}
/**
* isPerfectNumber determines (true or false) if a given number is a 'Perfect
* Number'
*
* A perfect number is one that is equal to the sum of its proper divisors.
* Example 1: 6; the proper divisors are 1, 2, 3 ; 1+ 2 + 3 is 6, so 6 IS a
* perfect number Example 2: 15; the proper divisors are 1, 3, 5 ; 1 + 3 + 5 is
* 9, so 15 IS NOT a perfect number Example 3: 28; the proper divisors are 1, 2,
* 4, 7, 14; 1 + 2 + 4 + 7 + 14 is 28, so 28 IS a perfect number
*
* Precondition: number is a positive integer
*
* The code below is a stub version, you should replace the line of code labeled
* TODO with code that achieves the above specification
*
* Hint: find the sum of the proper divisors
*/
public static boolean isPerfectNumber(int number) {
return false; // TODO 3: fix this code
}
**CODE START**
import java.util.Arrays;
public class example
{
public static double valRange(double[] list)
{
double smallest =
list[0];
//Find smallest element by
traversing through the list
for(int
i=0;i<list.length;i++){
if(smallest >
list[i])
smallest = list[i];
}
double largest =
list[0];
//Find largest element by
traversing through the list
for(int
i=0;i<list.length;i++){
if(largest <
list[i])
largest = list[i];
}
//return difference of
largest and smallest
return
(largest-smallest);
}
public static int
posOfLargestElementLtOeT(double limit, double[] list) {
//Let the index
of largest element be "index"
//initially no
largest element so index = -1
int index = -1;
for(int
i=0;i<list.length;i++){
//If current element is less than limit, then proceed
if(list[i]
<= limit){
//If there is no largest element till now,
make this one largest
//Otherwise compare it with current
largest element
if(index==-1){
index = i;
}
else if(list[index] < list[i]){
index = i;
}
}
}
return index;
}
public static boolean isPerfectNumber(int
number) {
int sum = 0;
//Taking sum of all its
divisors
for(int
i=1;i<number;i++){
if(number%i==0)
sum+=i;
}
//If sum==number return
true, else return false
if(sum==number)
return
true;
return false;
}
public static void main(String[] args) {
System.out.println(valRange
(new double[] { -7 }));
System.out.println(valRange
(new double[] { 1, 7, 8, 11 }));
System.out.println(valRange
(new double[] { 11, 7, 8, 1 }));
System.out.println(valRange
(new double[] { 1, -4, -7, 7, 8, 11 }));
System.out.println(valRange
(new double[] { -13, -4, -7, 7, 8, 11 }));
System.out.println(posOfLargestElementLtOeT(3, new double[] { -7
}));
System.out.println(posOfLargestElementLtOeT(3, new double[] { 11,
-4, -7, 7, 8, 1 }));
System.out.println(posOfLargestElementLtOeT(-7, new double[] { 1,
-4, -5, 7, 8, 11 }));
System.out.println(isPerfectNumber(6));
System.out.println(isPerfectNumber(15));
System.out.println(isPerfectNumber(28));
}
}
**CODE END**
OUTPUT
**CODE SNIPPET END**