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.
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String variable called myString1 and initialize it to "Programming". C) Declare a String variable called myString2 and initialize it to "Language". D) Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".". E) Print the sum of the lengths of myString1 and myString2 (must call the length() method). F) Print the 2nd, 4th, and 7th character...
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...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer nn, a non-negative integer where n≤20n≤20 Output: integer xyxy, the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0≤x,y0≤x,y....
Java Program Please Read all directions carefully Write a method named smallToLarge that asks the user...
Java Program Please Read all directions carefully Write a method named smallToLarge that asks the user to enter numbers, then prints the smallest and largest of all the numbers typed in by the user and the average (rounded to 2 decimal places). You may assume the user enters a valid integer number for the number of numbers to read. Here is an example dialogue: /* initialize smallest and largest variables with the 1st user input for Number */ How many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT