Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 5.6 Accumulating Totals in a Loop After I have completed this program, i

ID: 3665569 • Letter: P

Question

Problem 5.6 Accumulating Totals in a Loop

After I have completed this program, it should calculate two totals: the number of left-handed people and the number of right-handed people in your class. The loop should execute until the user enters the character ‘X’ instead of a ‘L’ for left-handed or ‘R’ for right-handed.

Left or Right-Handed

L

R

R

R

L

L

R

X

I need to write a loop and a loop body that allows you to calculate a total of left-handed and a total of right-handed people in our class.

// LeftOrRight.java - This program calculates the total number of left-handed and right-handed
//                    students in a class.  
// Input: L for left-handed; R for right handed; X to quit.
// Output: Prints the number of left-handed students and the number of right-handed students.

import javax.swing.JOptionPane;

public class LeftOrRight
{
   public static void main(String args[])
   {
     
      String leftOrRight = ""; // L or R for one student.
      int rightTotal = 0;   // Number of right-handed students.
      int leftTotal = 0;    // Number of left-handed students.
     
      // This is the work done in the housekeeping() method
      leftOrRight = JOptionPane.showInputDialog("Enter L if you are left-handed, R if you are right-handed or X to quit.");

      // This is the work done in the detailLoop() method
      // Write your loop here.
     


      // This is the work done in the endOfJob() method
      // Output number of left or right-handed students.
      System.out.println("Number of left-handed students: " + leftTotal);
      System.out.println("Number of right-handed students: " + rightTotal);
      System.exit(0);
   } // End of main() method.

} // End of LeftOrRight class.

Left or Right-Handed

L

R

R

R

L

L

R

X

Explanation / Answer

Simple loop :

while (leftOrRight = 'L' || leftOrRight = 'R')
{

if (leftOrRight = "R")
{
Rsum += rightTotal;
rightTotal++;
}
else
{
   Lsum += leftTotal;
leftTotal++;
}
}

while (leftOrRight = 'X')
{
System.exit(0);
}

System.out.println("The sum is of Right handed is " + Rsum);
System.out.println("The sum is of Left handed is " + Rsum);