Question

In: Computer Science

Java Program 1. Write a program that asks the user: “Please enter a number (0 to...

Java Program

1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered.

a. What is considered to be the body of the loop?

b. What is considered the control variable?

c. What is considered to be the test?

d. What is considered to be the update?

e. How can you incorporate the sentinel component into the test?

f. How can you calculate the sum of all previous numbers?

g. Which kind of loop do you think is more convenient to use? And why?

h. Construct the loop based on the information you collected

Solutions

Expert Solution

a.The statements inside the braces followed by the 'do' keyword are body of the loop

b. The user input 'n' is the control variable

c. The user input 'n' is the test

d. When we take the user input, it is the input

e. Sentinal component in the test is during the user input, when the data is updated for`

f. After each input, the value of the sum is updated by adding the value of user input

g. A do while loop is the most convenient to use, since we don't know how many times we need to repeat the process.

h. Code:

import java.util.*;

public class Main {

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        double n;
        do{
            //body of the loop
            System.out.print("Please enter a number (0 to exit) : ");
            n = sc.nextDouble();//update the value
            sum += n;//calculating the sum of the input values
        }while(n!=0);//n is the control variable

        System.out.println("The sum is : "+sum);
    }
}



Output:

Code Screenshot:


Related Solutions

Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
write a program that: 1) asks the user to enter grades, a negative number is the...
write a program that: 1) asks the user to enter grades, a negative number is the signal to stop entering. (while loop) - add each grade to a list (do not add the negative number to the list) after the while loop to enter grades is finished: 2) use a for loop on the list to calculate the average of all numbers in the list 3) use a for loop on the list to find the largest number in the...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
In java, write a program that asks the user to enter a character. Then pass the...
In java, write a program that asks the user to enter a character. Then pass the character to the following methods. isVowel() – returns true if the character is a vowel isConsonant() – returns true if the character is a consonant changeCase() – if the character is lower case then change it to upper case and if the character is in upper case then change it to lower case. (return type: char) Example output is given below: Enter a character...
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
Write a program (polygon.py) that asks the user to enter the number of sides in a...
Write a program (polygon.py) that asks the user to enter the number of sides in a regular polygon. For example, an equilateral triangle is a regular 3-sided polygon, a square is a regular 4-sided polygon, and a pentagon is a regular 5-sided polygon. If a user enters a number of sides between 3 and 25, inclusive, draw the polygon and wait for the user to click on the screen to quit the program. If the user enters a number less...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a program that asks the user to enter a number. Display the following pattern by...
Write a program that asks the user to enter a number. Display the following pattern by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user.For example, if the user enters 5, the output would be: * *   * *   *   * *   *   *   * *   *   *   *   * short codes please
Program: Java (using eclipse) Write a program that asks the user to enter today’s sales for...
Program: Java (using eclipse) Write a program that asks the user to enter today’s sales for five stores. The program should then display a bar chart comparing each store’s sales. Create each bar in the bar chart by displaying a row or asterisks. Each asterisk should represent $100 of sales. You must use a loop to print the bar chart!!!! If the user enters a negative value for the sales amount, the program will keep asking the user to enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT