Question

In: Computer Science

JAVA SHOW YOUR OUTPUT package exampletodo; import java.util.Arrays; import stdlib.*; /** * Edit the sections marked...

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

       }

Solutions

Expert Solution

**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**


Related Solutions

Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import...
Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import java.util.List; /** * An abstraction over the idea of a search. * * @author liberato * * @param <T> : generic type. */ public abstract class Searcher<T> { protected final SearchProblem<T> searchProblem; protected final List<T> visited; protected List<T> solution; /**    * Instantiates a searcher.    *    * @param searchProblem    *            the search problem for which this searcher will find and   ...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
package week_2; import java.util.Random; import static input.InputUtils.*; /** * Your program should generate a random number...
package week_2; import java.util.Random; import static input.InputUtils.*; /** * Your program should generate a random number between 0 and 9, and challenge the user to guess the number. Write a loop that asks the user to guess a number that the computer is thinking of. Print a success message if they guess correctly. If the user does not guess correctly, tell the user that they need to guess higher, or lower, and ask the user to try again. The user...
fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void...
Using the following in Java- package intersectionprinter; import java.awt.Rectangle; public class IntersectionPrinter { public static void main(String[] args) { Rectangle r1 = new Rectangle(0,0,100,150); System.out.println(r1);    Rectangle r2 = new Rectangle(50,75,100,150); System.out.println(r2);    Rectangle r3 = r1.intersection(r2);    } } Write a program that takes both Rectangle objects, and uses the intersection method to determine if they overlap. If they do overlap, then print it's coordinates along with its width and height. If there is no intersection, then have the...
Download labSerialization.zip and unzip it: ListVsSetDemo.java: package labSerialization; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List;...
Download labSerialization.zip and unzip it: ListVsSetDemo.java: package labSerialization; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; /** * Demonstrates the different behavior of lists and sets. * Author(s): Starter Code */ public class ListVsSetDemo { private final List<ColoredSquare> list; private final Set<ColoredSquare> set; /** * Initializes the fields list and set with the elements provided. * * @param elements */ public ListVsSetDemo(ColoredSquare... elements) { list = new ArrayList<>(Arrays.asList(elements)); set = new HashSet<>(Arrays.asList(elements)); } /** * Creates a string...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java,...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java, BubbleSortTestCaseMaker.java, and Statistician.java. PART 1: Implementing BubbleSorter Implement a very simple BubbleSorter class that records how many array visits and how many swaps are performed. Look at the starter file before reading on. This class has an instance variable called “a”. Its type is int[]. This is the array that will be bubble-sorted in place. Usually a single letter is a bad variable name,...
/* * Assignment: #3 * Topic: Identifying Triangles * Author: <YOUR NAME> */ package edu.depaul.triangle; import...
/* * Assignment: #3 * Topic: Identifying Triangles * Author: <YOUR NAME> */ package edu.depaul.triangle; import static edu.depaul.triangle.TriangleType.EQUILATERAL; import static edu.depaul.triangle.TriangleType.ISOSCELES; import static edu.depaul.triangle.TriangleType.SCALENE; import java.util.Scanner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * A class to classify a set of side lengths as one of the 3 types * of triangle: equilateral, isosceles, or scalene. * * You should not be able to create an invalid Triangle. The * constructor throws an IllegalArgumentException if the input * cannot be interpreted as...
Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args)...
Determine the Output: Package questions; import java.util.*; public class Quiz1 {       public static void main(String[] args) {             // TODO Auto-generated method stub             System.out.println("*** 1 ***");             ArrayList<Integer>list1=new ArrayList<Integer>();             for(int i=0;i<30;i++)             {                   list1.add(new Integer(i));             }             System.out.print("[");             for(int i=0;i<list1.size();i++)             {                   System.out.print(list1.get(i)+" ");             }             System.out.println("]");                           System.out.println("*** 2 ***");             ArrayList<Integer>list2=new ArrayList<Integer>();             for(int i=30;i<60;i++)             {                   list2.add(i); //Auto Boxing an Integer not need to use new Integer             }             System.out.println(list2); //toString for an ArrayList --created by the Java Programmers                           System.out.println("*** 3 ***");             ArrayList<Integer>list3=new ArrayList<Integer>();             list3.add(list1.remove(22)); //when...
In the following Java program replace conditions in while loops to produce Christmas tree output. import...
In the following Java program replace conditions in while loops to produce Christmas tree output. import java.util.Scanner; public class Tree {     public static void main(String[] args)     {         int size;         Scanner scan = new Scanner(System.in);         System.out.print("Enter the size: ");         size = scan.nextInt();         int count = 0;         while (__________) //??? condition         {             int len = 0;             // print blanks             while (____________)//??? condition             {                 System.out.print(' ');                 len++;            ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT