
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
![Intro to Java
Instuctions:
Design and implement a program that reads a series of 10 integers from the user and prints their average
For each of the 10 numbers input from the user:
one at a time, prompt the user to enter a number. Then read input from the user as a string.
Attempt to convert it to an integer using the "Integer.parseInt" method.
If the process throws a "NumberFormatExeption", print an appropriate error message and prompt the user for the number again.
Continue prompting and reading in number until 10 valid integers have been read
Print "The average is" and then print the average of the 10 numbers
Code:
import java.util.Scanner;
public class Lab1MainClass {
public static final int VALUES_TO_READ = 10;
public static void main(String[] args) {
// local variables
int[] values = new int [VALUES_TO_READ];
double average;
Scanner scan = new Scanner(System.in);
int values Read = 0; // how many numbers the user has given us, a count of where we are
String userInput;
// read inputs from user
System.out.println("Please enter " + VALUES_TO_READ + "values one at a time.");
while(valuesRead<VALUES_TO_READ) {
System.out.print("Enter value #" + valuesRead + ";");
userInput = scan.nextLine(); // read in the whole line up through the user's enter key
try {
int intValue = Integer.parseInt(userInput);
// parsing was successful
values[valuesRead] = intValue;
valuesRead++;
} catch (NumberFormatException e) {
// parsing was unsuccessful
System.out.println("That's not a number, please try again.");
}
}
// calculate their average
// print out the output average
System.out.println("TODO average and output");
}](https://content.bartleby.com/qna-images/question/00ad0db1-c9d3-4dc7-9356-8892c2066d7c/ef5e3cff-df8d-4a91-bb22-6b46a6746a6b/bo9tat_thumbnail.jpeg)
Transcribed Image Text:Intro to Java
Instuctions:
Design and implement a program that reads a series of 10 integers from the user and prints their average
For each of the 10 numbers input from the user:
one at a time, prompt the user to enter a number. Then read input from the user as a string.
Attempt to convert it to an integer using the "Integer.parseInt" method.
If the process throws a "NumberFormatExeption", print an appropriate error message and prompt the user for the number again.
Continue prompting and reading in number until 10 valid integers have been read
Print "The average is" and then print the average of the 10 numbers
Code:
import java.util.Scanner;
public class Lab1MainClass {
public static final int VALUES_TO_READ = 10;
public static void main(String[] args) {
// local variables
int[] values = new int [VALUES_TO_READ];
double average;
Scanner scan = new Scanner(System.in);
int values Read = 0; // how many numbers the user has given us, a count of where we are
String userInput;
// read inputs from user
System.out.println("Please enter " + VALUES_TO_READ + "values one at a time.");
while(valuesRead<VALUES_TO_READ) {
System.out.print("Enter value #" + valuesRead + ";");
userInput = scan.nextLine(); // read in the whole line up through the user's enter key
try {
int intValue = Integer.parseInt(userInput);
// parsing was successful
values[valuesRead] = intValue;
valuesRead++;
} catch (NumberFormatException e) {
// parsing was unsuccessful
System.out.println("That's not a number, please try again.");
}
}
// calculate their average
// print out the output average
System.out.println("TODO average and output");
}
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 4 steps with 1 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- In java please help with the following: Given a string that contains space separated words, write an application that displays the words in ascending order (If two words are the same, display only one). You need to use the right data structure for storing the words. For example, for input:String input="apple banana apple orange blueberry"; The program will print out: apple banana blueberry orangearrow_forwardValidating User Input Summary In this lab, you will make additions to a Java program that is provided. The program is a guessing game. A random number between 1 and 10 is generated in the program. The user enters a number between 1 and 10, trying to guess the correct number. If the user guesses correctly, the program congratulates the user, and then the loop that controls guessing numbers exits; otherwise, the program asks the user if he or she wants to guess again. If the user enters a "Y", he or she can guess again. If the user enters "N", the loop exits. You can see that the "Y" or an "N" is the sentinel value that controls the loop. Note that the entire program has been written for you. You need to add code that validates correct input, which is "Y" or "N", when the user is asked if he or she wants to guess a number, and a number in the range of 1 through 10 when the user is asked to guess a number. Instructions Ensure the file named GuessNumber.java is open. Write loops…arrow_forwardin Java Tasks Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet.) 2. The user enters his or her choice of "rock", "paper", or "scissors" at the keyboard. (You can use a menu if you prefer.) 3. The computer's choice is displayed. Tasks 4. A winner is selected according to the following rules: a. If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.) b. If one player chooses scissors and the other player chooses paper, then scissors wins. (Scissors cuts paper.) C. If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps…arrow_forward
- In javaarrow_forwardCount of Guess Create a JAVA program to generate a random number in the range [1,10] and then prompt the user to guess a number until the user guesses the correct random number generated. The program should print the number of attempts in which the user enters the correct number.arrow_forward14. Create a JAVA program to read a multi-word string from the user and then find the length of the largest word in the string. Testcase: {"This a Java Programming Problem."} Output: {11}arrow_forward
- 16arrow_forwardStudent name: CIS 232 Introduction to Programming Homework Assignment 10 Due Date: 11/23/2020 Instructor: Dr. Lomako Problem Statement: Write a Java program with the main method and a multiplication method (must be created, not the library method). Create “HW10_lastname.java” program that: Prints a program title Creates two n x n square matrices A and B and initializes them with random values Displays the matrices A and B Calls the multiplication method that multiplies two square matrices and returns a matrix AB that is the result of the multiplication. Prints AB matrixarrow_forwardPrimeAA.java Write a program that will tell a user if their number is prime or not. Your code will need to run in a loop (possibly many loops) so that the user can continue to check numbers. A prime is a number that is only divisible by itself and the number 1. This means your code should loop through each value between 1 and the number entered to see if it’s a divisor. If you only check for a small handful of numbers (such as 2, 3, and 5), you will lose most of the credit for this project. Include a try/catch to catch input mismatches and include a custom exception to catch negative values. If the user enters 0, the program should end. Not only will you tell the user if their number is prime or not, you must also print the divisors to the screen (if they exist) on the same line as shown below AND give a count of how many divisors there are. See examples below. Your program should run the test case exactly as it appears below, and should work on any other case in general. Output…arrow_forward
- Java Programming: Question 12 Not sure how to do this question. Help of any input and output would be appreciated.arrow_forwardJava Calculating the User's Sum Write a complete program that reads in numbers and calculates the sum of the numbers in that range. Code Specifications In the main method, read input from the user. Read in two integers from the user: a lower end of the range and an upper end of the range. Check if the numbers are valid: the lower number cannot be greater than the upper number. If the numbers are invalid, use a loop to ask for new numbers. Continue looping until you get two valid values. Write a method called calculateTheSum. The method takes in a lower and upper end of the range. The method calculates the sum of all values from lower (inclusive) to upper (inclusive). Invoke the method from main the output the result. Test Cases I recommend testing your code using the test cases below. I've listed the user inputs along with a sample of the result. User Inputs Result lower = 10, upper = 1 the program should ask for new input lower = 5, upper = -5 the program should ask…arrow_forwardIn Java: Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or 1995! the output is: no Hint: Use a loop and the Character.isDigit() function.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education