Question

In: Computer Science

Write a while loop that will let the user enter a series of integer values and...

  1. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates.

for Loop

  1. Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3.
  1. Write a for loop to display a string entered by the user backwards.

do Loop

  1. Write a do .. loop for the random number dice problem from Chap Three. Add a counter variable that is used to count the number of times this code is executed in the loop and stops after 5 times.

roll = rand.nextInt(6) + 1;

      System.out.println(" Roll = " + roll);

ctr++;

Here is a sample output display:

The rolls are:

Roll = 3

Roll = 1

Roll = 4

Roll = 6

Roll = 4

Add a loop structure using the while loop and then add a for loop. The program should include 3 loops that do the same thing. What is the “best” loop structure to use for this problem and why?

Can you please tell me how to connect one class and where i should write in main or other class please

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Random;

// Defines class loop
class MyLoop
{
   // Scanner class object created to accept data from console
   Scanner sc = new Scanner(System.in);

   // Random class object created
   Random rand = new Random();
  
   // Method to accept data till odd number is entered
   // Counts number of time loop iterated and total of even numbers
   void calculateSum()
   {
       // To store the number
       int no;
       // To store the loop iteration
       int count = 0;
       // To store total
       int total = 0;

       // Note: While loop is used because we don't know after how many times
       // user will enter the odd number

       // Loops till odd number
       while(true)
       {
           // Increase the counter by one
           count++;
           // Accepts a number
           System.out.print("\n Enter a number (An odd number will stop the loop): ");
           no = sc.nextInt();
           // Calculate total
           total += no;
           // Checks if the number is odd then stop the loop
           if(no % 2 == 1)              
               break;
       }
       // Displays the number of times loop iterated
       System.out.print("\n The number of iterations: " + count);
       // Displays the total
       System.out.print("\n The total of the values: " + total);
   }
  
   // Method to display all the numbers ending in 3 from 13 - 93
   void displayEndingBy3()
   {
       // Note: for loop is used because we know how many times to iterate
       // or stopping condition (93) and starting value 13
      
       // Loops from 13 to 93
       for(int c = 13; c <= 93; c++)
           // Checks if unit digit of the number is 3 then number ending in 3
           if(c % 10 == 3)
               // Display the number
               System.out.println(c);
   }

   // Method to display a string entered by the user backwards.
   void showBackward()
   {
       // Accepts a string
       System.out.print("\n Enter a string: ");
       String data = sc.next();
      
      
       System.out.println("\n Backward String: ");
       // Note: for loop is used because we know how many times to iterate
       // or stopping condition (0) and starting value length of the string minus one
      
       // Loops from length of the string -1 (last index position)
       // to 0 (first index position)
       for(int c = data.length()-1; c >= 0; c--)
           // Displays the character at c index position
           System.out.print(data.charAt(c));
   }
  
   // Method to generate random number between 1 and 6 and displays it
   void rollDice()
   {
       // Counter variable
       int ctr = 0;
      
       System.out.println("\n The rolls are: ");
      
       // Note: The best loop structure is is for loop
       // because we know how many times to iterate
      
       // Loop 5 times
       do
       {
           // Generates a random number between 1 and 6
           int roll = rand.nextInt(6) + 1;
           // Displays the number
           System.out.println(" Roll = " + roll);
           // Increase the counter by one
           ctr++;
       }while(ctr < 5); // Checks if ctr value is less than 5 then continue
   }
}// End of class

// Driver class definition
public class LoopDriver
{
   // main method definition
   public static void main(String []s)
   {
       // Creates an object of class MyLoop
       MyLoop loop = new MyLoop();
      
       System.out.println("\n ******** Calculate sum till odd number entered ******** ");
       loop.calculateSum();
      
       System.out.println("\n ******** Display numer ends in 3 from 13 - 93 ******** ");
       loop.displayEndingBy3();
      
       System.out.println("\n ******** Display string backward ******** ");
       loop.showBackward();
      
       System.out.println("\n ******** Display rolled dice value ******** ");
       loop.rollDice();
   }// End of main method
}// End of driver class

Sample Output:


******** Calculate sum till odd number entered ********

Enter a number (An odd number will stop the loop): 12

Enter a number (An odd number will stop the loop): 4

Enter a number (An odd number will stop the loop): 6

Enter a number (An odd number will stop the loop): 5

The number of iterations: 4
The total of the values: 27
******** Display numer ends in 3 from 13 - 93 ********
13
23
33
43
53
63
73
83
93

******** Display string backward ********

Enter a string: This

Backward String:
sihT
******** Display rolled dice value ********

The rolls are:
Roll = 6
Roll = 1
Roll = 2
Roll = 3
Roll = 2


Related Solutions

​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
Use a while(true) loop to ask the user the following 2 values “Enter a rate r...
Use a while(true) loop to ask the user the following 2 values “Enter a rate r =” “Enter a nonnegative integer (enter negative integer to quit):” If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the geometric series, namely 1 + r + rˆ2 + rˆ3 + . . . +rˆn. Use...
Use a while(true) loop to ask the user the following 2 values “Enter a value x...
Use a while(true) loop to ask the user the following 2 values “Enter a value x “ “Enter a nonnegative integer n (enter negative integer to quit): “ If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the Riemann zeta series for the geometric series, namely 1 + 2ˆ-x + 3ˆ-x +...
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT