PyCharms Iteration Part a Lets write the code to prompt the

PyCharm’s Iteration

Part (a): Let’s write the code to prompt the user for (an integer) blood-sugar reading (BSR) and print a warning message if it is over 500. Also, print the message \">> Invalid input\" if the user enters a non-integer, such as a float or a string.

Note that if you have completed the Worked_Exercise_5.1 above you should use that code as the basis for this problem.

Use this sample interaction to demonstrate your code:

Enter a BSR: 155

Enter a BSR: string input

>> Invalid input

Enter a BSR: 522

** Warning! Blood sugar reading over 500: 522

Enter a BSR: 485

Enter a BSR: 578

** Warning! Blood sugar reading over 500: 578

Enter a BSR: 210

Enter a BSR: 330.6

>> Invalid input

Enter a BSR: 519

** Warning! Blood sugar reading over 500: 519

Part (b)

Create a list of BSRs as follows:

blood_sugar_readings = [155, 190, 522, 485, 578, 210, 130, 519]

Use a for-loop to iterate through the list in order to find the highest and lowest BSR values. Note that finding the maximum and minimum values of a list using a for-loop is shown in Section 5.7.2 of the textbook on p. 62-3. (Note that since we are ‘hard-coding’ the list, we may assume the data is valid, i.e., it has been previously checked.)

HINT: as is done in the textbook, while developing the code, add a print statement as the last line of the for-loop writing out the current BSR value, the smallest, and the largest. Then when your code is running correctly, you can comment out that line (but leave it in the code-base).

After looping through the list, print the highest and lowest BSR values.

Solution

1)

BloodSugarReading.java

import java.util.Scanner;

public class BloodSugarReading {

   public static void main(String[] args) {

       // Declaring avriable
       int num;

       // Scanner class object is used to read the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       // This loop continue to execute until the user enter\'s other than zero
       while (true) {
           try {
               // Getting the entered integer from the user
               System.out.print(\"Enter a BSR: (Zero(0) to Exit) :\");
               num = sc.nextInt();

               // If it is Integer and if it is greater than 500 the display
               // the message
               if (num > 500) {
                   System.out
                           .println(\"** Warning! Blood sugar reading over 500: \"
                                   + num);
               }
               // If the entered integer is zero program will exit
               else if (num == 0)
                   break;
           }
           // If the user entered thing is not an integer then error message
           // will get displayed
           catch (Exception e) {
               // Displaying the error message
               System.out.println(\">> Invalid Input\");
               sc.nextLine();
           }

       }

   }

}

_______________________________________

output:

Enter a BSR: (Zero(0) to Exit) :155
Enter a BSR: (Zero(0) to Exit) :Hello
>> Invalid Input
Enter a BSR: (Zero(0) to Exit) :522
** Warning! Blood sugar reading over 500: 522
Enter a BSR: (Zero(0) to Exit) :485
Enter a BSR: (Zero(0) to Exit) :578
** Warning! Blood sugar reading over 500: 578
Enter a BSR: (Zero(0) to Exit) :210
Enter a BSR: (Zero(0) to Exit) :330.6
>> Invalid Input
Enter a BSR: (Zero(0) to Exit) :519
** Warning! Blood sugar reading over 500: 519
Enter a BSR: (Zero(0) to Exit) :0

______________________________________________

2)

MaxAndMinBSR.java

public class MaxAndMinBSR {

   public static void main(String[] args) {
      
       //Declaring variables
       int min,max;
      
       //Declaring and initializing an integer array
   int    blood_sugar_readings[] = {155, 190, 522, 485, 578, 210, 130, 519};
  
   //Assigning the first value of the array to min variable
   min=blood_sugar_readings[0];
  
   //Assigning the first value of the array to max variable
   max=blood_sugar_readings[0];
  
   //This loop will find the smallest and largest BSR value in the array
   for(int i=0;i<blood_sugar_readings.length;i++)
   {
      
       //Finding the largest BSR value in the array
       if(blood_sugar_readings[i]>max)
           max=blood_sugar_readings[i];
      
       //Finding the Smallest BSR value in the array
       if(blood_sugar_readings[i]<min)
           min=blood_sugar_readings[i];
      
       //System.out.println(\"\ Current BSR value is :\"+blood_sugar_readings[i]);
       //System.out.println(\"The Smallest BSR value :\"+min);
       //System.out.println(\"The Largest BSR value :\"+max);
      
      
   }
  
   //Displaying the Smallest BSR value
   System.out.println(\"The Smallest BSR value :\"+min);
  
   //Displaying the Largest BSR value
   System.out.println(\"The Largest BSR value :\"+max);

   }

}

_________________________________________

Output:


Current value is :155
Minimum BSR value :155
Maximum BSR value :155

Current value is :190
Minimum BSR value :155
Maximum BSR value :190

Current value is :522
Minimum BSR value :155
Maximum BSR value :522

Current value is :485
Minimum BSR value :155
Maximum BSR value :522

Current value is :578
Minimum BSR value :155
Maximum BSR value :578

Current value is :210
Minimum BSR value :155
Maximum BSR value :578

Current value is :130
Minimum BSR value :130
Maximum BSR value :578

Current value is :519
Minimum BSR value :130
Maximum BSR value :578

______________________________Thank You

PyCharm’s Iteration Part (a): Let’s write the code to prompt the user for (an integer) blood-sugar reading (BSR) and print a warning message if it is over 500.
PyCharm’s Iteration Part (a): Let’s write the code to prompt the user for (an integer) blood-sugar reading (BSR) and print a warning message if it is over 500.
PyCharm’s Iteration Part (a): Let’s write the code to prompt the user for (an integer) blood-sugar reading (BSR) and print a warning message if it is over 500.
PyCharm’s Iteration Part (a): Let’s write the code to prompt the user for (an integer) blood-sugar reading (BSR) and print a warning message if it is over 500.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site