Question

Summary In this lab, you add a loop and the statements that make up the loop...

Summary

In this lab, you add a loop and the statements that make up the loop body to a Java program that is provided. When completed, the program should calculate two totals: the number of left-handed people and the number of right-handed people in your class. Your loop should execute until the user enters the character X instead of L for left-handed or R for right-handed.

The inputs for this program are as follows: R, R, R, L, L, L, R, L, R, R, L, X

Variables have been declared for you, and the input and output statements have been written.

Instructions

  1. Ensure the file named LeftOrRight.java is open.

  2. Write a loop and a loop body that allows you to calculate a total of left-handed and right-handed people in your class.

  3. Execute the program by clicking Run and using the data listed above and verify that the output is correct.

  4. // 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 java.util.Scanner;

    public class LeftOrRight
    {
       public static void main(String args[])
       {
           Scanner s = new Scanner(System.in);
           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
           System.out.println("Enter L if you are left-handed, R if you are right-handed or X to quit.");
           leftOrRight = s.nextLine();

           // 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class LeftOrRight

{
  public static void main(String args[])

  {

    Scanner s = new Scanner(System.in);

    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

    System.out.println("Enter L if you are left-handed, R if you are right-handed or X to quit.");

    leftOrRight = s.nextLine();

// This is the work done in the detailLoop() method

// Write your loop here.
    for(String word: leftOrRight.split(", ")){
      if(word.toUpperCase().equals("R")){
        rightTotal += 1;
      }
      else if(word.toUpperCase().equals("L")){
        leftTotal += 1;
      }
    }

// 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.

Add a comment
Know the answer?
Add Answer to:
Summary In this lab, you add a loop and the statements that make up the loop...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • Newmultiply.Java // NewMultiply.java - This program prints the numbers 0 through 10 along // with these...

    Newmultiply.Java // NewMultiply.java - This program prints the numbers 0 through 10 along // with these values multiplied by 2 and by 10. // Input: None. // Output: Prints the numbers 0 through 10 along with their values multiplied by 2 and by 10. public class NewMultiply {    public static void main(String args[])    {               String head1 = "Number: ";        String head2 = "Multiplied by 2: ";        String head3 = "Multiplied...

  • Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that...

    Java Help, Will definitely give a thumbs up if it works. Declare a class ComboLock that works like the combination lock in a gym locker (Consult the API provided to look for the methods needed). The locker is constructed with a combination - three numbers between 0 and 39. The reset method resets the dial so that it points to 0. The turnLeft and turnRight methods turn the dial by a given number of ticks to the left or right....

  • Open a new file in your text editor, and start a class that will demonstrate a...

    Open a new file in your text editor, and start a class that will demonstrate a working two-dimensional array: import java.util.Scanner; class TwoDimensionalArrayDemo { public static void main(String[] args) { 2. Declare a three-by-three array of integers. By default, the elements will all be initialized to 0. int[][] count = new int[3][3]; 3. Declare a Scanner object for input, variables to hold a row and column, and a constant that can be used to indicate when the user wants to...

  • Parallel Arrays Summary In this lab, you use what you have learned about parallel arrays to...

    Parallel Arrays Summary In this lab, you use what you have learned about parallel arrays to complete a partially completed Java program. The program should either print the name and price for a coffee add-in from the Jumpin’ Jive coffee shop or it should print the message: "Sorry, we do not carry that.". Read the problem description carefully before you begin. The data file provided for this lab includes the necessary variable declarations and input statements. You need to write...

  • Please help, In this lab, you complete a prewritten C++ program for a carpenter who creates...

    Please help, In this lab, you complete a prewritten C++ program for a carpenter who creates personalized house signs. The program is supposed to compute the price of any sign a customer orders, based on the following facts: The charge for all signs is a minimum of $35.00. The first five letters or numbers are included in the minimum charge; there is a $4 charge for each additional character. If the sign is made of oak, add $20.00. No charge...

  • This is what I have so far. I'm getting an error on the ... case 3:...

    This is what I have so far. I'm getting an error on the ... case 3: System.out.println("Enter the rank of the card you want removed"); cards.remove(); System.out.println(); break; -------------------------------------- package PlayingCards; import java.util.Scanner; public class Driver { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean done = false; int menuInput; DeckOfCards cards = new DeckOfCards(); do { System.out.println("Enter the number of one of the choices below:"); System.out.println("1: Shuffle The Deck."); System.out.println("2: Print The Cards Remaining In...

  • The program is described in Chapter 9, Exercise 11, in Programming Logic and Design. In this...

    The program is described in Chapter 9, Exercise 11, in Programming Logic and Design. In this program, you should include two overloaded methods named computeRate(). One version accepts a number of days and calculates the rate at $99.99 per day. The other accepts a number of days and a code for a meal plan. If the code is A, three meals per day are included, and the price is $169.00 per day. If the code is C, breakfast is included,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT