Question

In: Computer Science

This is an intro to Java Question. My current solution is giving me bad outputs. Please...

This is an intro to Java Question. My current solution is giving me bad outputs. Please show me your way of solving this.

Problem 4: Min/Max Search by Value Develop a program that, given a sequence S of integers as input, produces as two output values, the first is the minimum value that appears in the sequence and the second is the maximum value that appears in the sequence.

Facts

● Scanner has a method that returns a boolean indicating whether a next integer exists in its input stream ( hasNextInt() )

● Scanner objects can be initialized to to scan String data as input.

Input

The input will begin with a single line containing T , the number of test cases to follow. The remaining lines contain the T sequences, one line per sequence. Each of these lines contains the values in the sequence. Each such value is separated from the next by at least one space.

Output For each sequence given as input, there should be four lines of output. The first line echos the given sequence. The second line indicates the minimum value that occurs. The third line indicates the maximum value that occurs. The fourth line is blank.

Sample Input

3

3 6 -1 4 6 5 3

0 0 0 0 -4 45 2 0 3 5 11 -7 854 25 3 -7 4 -3

Sample Output 3 6 -1 4 6 5 3

-1

6

0 0 0 0

0

0

-4 45 2 0 3 5 11 -7 854 25 3 -7 4 -3

-7

854

So Far This is what I have:

import java.util.Scanner;

public class MinMaxSearchByValue {

   public static void main(String [] args) {

       Scanner input = new Scanner(System.in);

       int cases = input.nextInt();

       input.nextLine();

       for (int i = 0; i < cases; i++) {

           String givenListOfNums = input.nextLine();

           Scanner stringScanner = new Scanner (givenListOfNums);

           int min = stringScanner.nextint();

           int max = min;

           while (stringScanner.hasNextInt()) {


               int nextValue = stringScanner.nextInt();

               if(nextValue < min) {

                   min = nextValue;}

                   else if (nextValue > max) {
                      
                       max = nextValue;
                   }
           }
       }

System.out.printf("%s%n%d%n%d%n%n", givenListOfNums, min, max);

   }


}

Solutions

Expert Solution

import java.util.Scanner;

public class MinMaxSearchByValue {

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);
       System.out.println("Enter number of commas: ");

       //reading the number of cases
       int cases = input.nextInt();

       input.nextLine();

       for (int i = 0; i < cases; i++) {
           //reading elements from user
           System.out.println("Enter elements separeted by comma: ");
           String givenListOfNums = input.nextLine();
           //splitting the array with , as delimeter
           String arr[]=givenListOfNums.split(",");

           //assuming 1st element as min and max
           int min = Integer.parseInt(arr[0]);

           int max = min;

           for(int j=1;j<arr.length;j++){

               //comparing min value with current value
               int nextValue = Integer.parseInt(arr[j]);

               // if current value is small than making current value as min
               if (nextValue < min) {

                   min = nextValue;
               }
               // if current value is max than making current value as max
               else if (nextValue > max) {

                   max = nextValue;
               }
           }
           System.out.printf("Elements :%s%nMin element:%d%nMax element : %d%n%n", givenListOfNums, min, max);
       }

   }

}

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA Private Key (10 points) (Cyber Security) In the RSA algorithm, encrypting and decrypting a message is done using a pair of numbers that are multiplicative inverses with respect to a carefully selected modulus. In this task, you must calculate the modular multiplicative inverse for given set of values. What is the Modular Multiplicative Inverse? In mathematics, each operation typically has an inverse. For example,...
this is my assignment for intro to java tha i could not figure out, my system...
this is my assignment for intro to java tha i could not figure out, my system just crashed wit a lot of errors. thank for your help Create a new Java class inside your project folder. The name of the class should be: TempConverter Note that this means you should have the following line at the top of your program: public class TempConverter Write a program that allows the user to convert a temperature given in degrees Celsius or Fahrenheit...
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently....
Intro to Java Question. Please Provide code and pseudocode for understanding. Not receiving correct results currently. Problem 5: Player Move Dungeon (10 points) (Game Development) You're the lead programmer at a AAA studio making a sequel to the big hit game, Zeldar 2. You've been challenged to implement player movement in dungeons. The game is top-down, with dungeons modeled as a 2d grid with walls at the edges. The player's location is tracked by x,y values correlating to its row...
Renewable Energy question: (PLEASE provide me with TYPED solution) ********* PLEASE TYPE YOUR SOLUTION ************ I...
Renewable Energy question: (PLEASE provide me with TYPED solution) ********* PLEASE TYPE YOUR SOLUTION ************ I lost one question because I could not read the hand writing of the solution that somone provided, so please type yours. Thank you. #. Solar heat harvesting makes use of what three modes of heat distribution? How does each work?
RENEWABLE ENERGY QUESTION : *************(PLEASE provide me with typed solution)*************** PLEASE TYPE YOUR SOLUTION. Do Not...
RENEWABLE ENERGY QUESTION : *************(PLEASE provide me with typed solution)*************** PLEASE TYPE YOUR SOLUTION. Do Not Provide Hand-Written solution Please. Q) Which state in the United States would be the best one to harvest wind power? Why? (Don’t forget to include the state’s shoreline in this decision).
please give me a summary about the company Stryker. Please tell me the good and bad...
please give me a summary about the company Stryker. Please tell me the good and bad about Stryker and why they are important.
This is a Entry to Java question. Please show me the PseudoCode or Commits first so...
This is a Entry to Java question. Please show me the PseudoCode or Commits first so I can learn to analyze everything a a little bit better. Simple Calculator (10 points) (General) Calculators represent the most basic, general-purpose of computing machines. Your task is to reduce your highly capable computer down into a simple calculator. You will have to parse a given mathematical expression, and display its result. Your calculator must support addition (+), subtraction (-), multiplication (*), division (/),...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT