Question

In: Computer Science

In java please A boolean must be used for this problem! Directions: * Initialize a Boolean...

In java please

A boolean must be used for this problem!

Directions:

* Initialize a Boolean variable "first" to true.
* Repeat (another value has been read successfully)
*        If first is true
*            Set the minimum to the value.
*            Set first to false.
*        Else if the value is less than the minimum
*            Set the minimum to the value.
* Display the minimum.

So far I have:

import java.util.Scanner;
public class FindMinimumValue {

   public static void main(String[] args) {
       // initialize variables
       Scanner console=new Scanner(System.in);
       boolean first=true;
       int userNum=0, minValue=1, userNum2=0;
      
      
       //prompt
       System.out.println("Please enter two numbers. Enter 'y' when done.");
       userNum=console.nextInt();
       userNum2=console.nextInt();
      
      
       //calculate
       while (console.hasNextInt()) {
       if (first==true) {
           minValue=userNum;
           first=false;
       }
       else if (userNum<minValue){
           minValue=userNum;
       }
       }
      
      
       //display
       System.out.println("The minimum is: "+minValue);

   }

}

Can you please help because the output is just coming out to minValue=1 even though 'first'=true. I need the minimum input from the user and I have to use the boolean value in my program. Thank you

Solutions

Expert Solution

Program :

import java.util.Scanner;
public class FindMinimumValue {

public static void main(String[] args) {


// initialize variables
Scanner console=new Scanner(System.in);
boolean first=true;
int userNum=0, minValue=1,userNum2=0;
//prompt
System.out.println("Please enter two numbers. Enter 'y' when done.");
//userNum=console.nextInt();
//userNum2=console.nextInt();
// output is just coming out to minValue=1
//even though 'first'=true
//This is because, console.hasNextInt() gives false always
//in the previous case
//This program reads 2 integers
//first number is assigned to userNum
//2nd number is assigned to userNum2(previously)
//there is no 3rd number
//therefore we get false when we use console.hasNextInt()
//please check the output of this statement
//by uncommenting above two statements userNum= and userNum2=
//System.out.println(console.hasNextInt());
//calculate
//console.hasNextInt() check whether there is an integer or not
//on the modified case, there is two
//so this while loop execute 2 times
//And we will get correct output
while (console.hasNextInt()) {


//assigns each number entered one by one
userNum=console.nextInt();
if (first==true) {


minValue=userNum;
first=false;


}
else if (userNum<minValue){


minValue=userNum;


}


}
//display
System.out.println("The minimum is: "+minValue);

}

}

Output :


Related Solutions

In JAVA Directions: You must trace through the code and determine the status of the array....
In JAVA Directions: You must trace through the code and determine the status of the array. The code can be found on the second page of this packet. Assume Array A = Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Value 33 12 39 6 -2 30 15 11 55 100 40 39 1 How many elements does A have? ______________ What is the start index? _______________ (assume this value is stored in a...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a...
Problem 1: Recursive anagram finder please used Java please Write a program that will take a word and print out every combination of letters in that word. For example, "sam" would print out sam, sma, asm, ams, mas, msa (the ordering doesn't matter) input: cram output: cram crma carm camr cmra cmar rcam rcma racm ramc rmca rmac acrm acmr arcm armc amcr amrc mcra mcar mrca mrac macr marc Hints: Your recursive function should have no return value and...
Problem 4: Coins Used java please You are given a target value in cents and some...
Problem 4: Coins Used java please You are given a target value in cents and some coins: quarters, dimes, nickels, and pennies (in that order). Your program should tell which coins add up to that target. input: 63 3 2 5 3 output: 63 cents = 2 quarters, 1 dimes, 0 nickels, 3 pennies Hints: Your recursive function can have ten int parameters: targetValue, valueSoFar, quartersLeft, dimesLeft, nickelsLeft, penniesLeft, quartersSpent, dimesSpent, nickelsSpent, penniesSpent. Base cases: 1. you hit the target...
***This problem must be done using R so please provide the R code used to find...
***This problem must be done using R so please provide the R code used to find the solution. I have provided the data in data-wtLoss.txt below the question. I will also give "thumbs-up for correct R code" Thanks in advance.*** The file “data-wtLoss.txt” contains data on weight loss and self esteem evaluation at three time points over a period of three months for 34 individuals who are randomly selected from a residential area. These individuals are randomly assigned to one...
***This problem must be done using R so please provide the R code used to find...
***This problem must be done using R so please provide the R code used to find the solution. I have provided the data in data-wtLoss.txt below the question. I will also give "thumbs-up for correct R code" Thanks in advance.*** The file “data-wtLoss.txt” contains data on weight loss and self esteem evaluation at three time points over a period of three months for 34 individuals who are randomly selected from a residential area. These individuals are randomly assigned to one...
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
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...
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
A JAVA program that will read a boolean matrix corresponding to a relation R and output...
A JAVA program that will read a boolean matrix corresponding to a relation R and output whether R is Reflexive, Symmetric, Anti-Symmetric and/or Transitive. Input to the program will be the size n of an n x n boolean matrix followed by the matrix elements. Document your program nicely. NOTE: The program must output a reason in the case that an input relation fails to have a certain property.
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT