Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134700144
Author: Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 5.5, Problem 5.5.1CP
Explanation of Solution
Given code:
//import statement
import java.util.Scanner;
//Class definition
public class Test
{
//Main method
public static void main(String[] args)
{
//declare the scanner variable
Scanner input= new Scanner(System.in);
//declare the required variable
int number, max ;
//read the input
number = input.nextInt() ;
//set the maximum value
max = number ;
//use while loop
while (number != 0)
{
//read the integer value
number = input...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
public class ReversedDigit { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter a number: "); int number = scan.nextInt(); int reversedNumber = 0;
do { reversedNumber = number%10; number = number/10; } while (number > 0);
}}
Modify the coding example, to count what is the total of all digits of a number. For example, when user enters 301, your output is 4 because 3+0+1=4; when user enters 5116, your output is 13 (5+1+1+6 = 13).
I have this code:
import java.util.Scanner;
public class CalcTax {
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
// Welcome message System.out.println("Welcome to my tax calculation program.");
// Declare the variables int i, pinCode, maxRetries = 3; String choice;
// Get the PIN code for (i = 0; i < maxRetries; i++) { System.out.print("Please enter the PIN code: "); pinCode = sc.nextInt(); if (pinCode == 5678) { break; } System.out.println("Invalid pin code. Please try again."); } if (i == maxRetries) { System.out.println("Invalid pin code. You have reached the maximum number of retries."); System.exit(0); }
// Get the income, interest, deduction, and paid tax amount double income, interest, deduction, paidTax; do { System.out.print("Please…
Please help me fix the code
Make it looks like the expected
import java.util.Scanner;
public class NumberLoops
{
public static void main(String[] args)
{
double num;
double result = 1;
int i;
Scanner sc = new Scanner(System.in); // Create a Scanner object
// Take user input for the Positive Number
System.out.print("Enter a positive integer: ");
num = sc.nextDouble(); // Read user input
// if the input number is not an integer
if(num % 1 != 0)
{
// print the message
System.out.println("Not an Integer: " + num);
System.exit(0);
}
// if the number is not a positive number
if(num
Chapter 5 Solutions
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
Ch. 5.2 - Prob. 5.2.1CPCh. 5.2 - How many times are the following loop bodies...Ch. 5.2 - Prob. 5.2.3CPCh. 5.3 - What is wrong if guess is initialized to 0 in line...Ch. 5.4 - Revise the code using the System. nanoTime () to...Ch. 5.5 - Prob. 5.5.1CPCh. 5.6 - Prob. 5.6.1CPCh. 5.6 - What are the differences between a while loop and...Ch. 5.7 - Do the following two loops result in the same...Ch. 5.7 - What are the three parts of a for loop control?...
Ch. 5.7 - Suppose the input is 2 3 4 5 0. What is the output...Ch. 5.7 - What does the following statement do? for ( ; ; )...Ch. 5.7 - If a variable is declared in a for loop control,...Ch. 5.7 - Convert the following for loop statement to a...Ch. 5.7 - Count the number of iterations in the following...Ch. 5.8 - Can you convert a for loop to a while loop? List...Ch. 5.8 - Can you always convert a while loop into a for...Ch. 5.8 - Identify and fix the errors in the following code:...Ch. 5.8 - Prob. 5.8.4CPCh. 5.9 - How many times is the println statement executed?...Ch. 5.9 - Show the output of the following programs. (Hint:...Ch. 5.11 - Will the program work if n1 and n2 are replaced by...Ch. 5.11 - In Listing 5.11. why is it wrong if you change the...Ch. 5.11 - In Listing 5. 11, how many times the loop body is...Ch. 5.11 - Prob. 5.11.4CPCh. 5.11 - Prob. 5.11.5CPCh. 5.12 - What is the keyword break for? What is the keyword...Ch. 5.12 - The for loop on the left is converted into the...Ch. 5.12 - Rewrite the programs TestBreak and TestContinue in...Ch. 5.12 - After the break statement in (a) is executed in...Ch. 5.13 - What happens to the program if (low high) in line...Ch. 5.14 - Simply the code in lined 27-32 using a conditional...Ch. 5 - (Count positive and negative numbers and compute...Ch. 5 - (Repeat additions) Listing 5.4,...Ch. 5 - (Conversion from kilograms to pounds) Write a...Ch. 5 - (Conversion from miles to kilometers) Write a...Ch. 5 - (Conversion from kilograms to pounds and pounds to...Ch. 5 - Prob. 5.6PECh. 5 - (Financial application: compute future tuition)...Ch. 5 - (Find the highest score) Write a program that...Ch. 5 - (Find the two highest scores) Write a program that...Ch. 5 - (Find numbers divisible by 5 and 6) Write a...Ch. 5 - (Find numbers divisible by 5 or 6, but not both)...Ch. 5 - (Find the smallest n such that n2 12,000) Use a...Ch. 5 - (Find the largest n such that n3 12,000) Use a...Ch. 5 - (Compute the greatest common divisor) Another...Ch. 5 - (Display the ASCII character table) Write a...Ch. 5 - (Find the factors of an integer) Write a program...Ch. 5 - (Display pyramid) Write a program that prompts the...Ch. 5 - (Display four patterns using Loops) Use nested...Ch. 5 - (Display numbers in a pyramid pattern) Write a...Ch. 5 - (Display prime numbers between 2 and 1,000) Modify...Ch. 5 - Prob. 5.21PECh. 5 - For the formula to compute monthly payment, see...Ch. 5 - (Demonstrate cancellation errors) A cancellation...Ch. 5 - Prob. 5.24PECh. 5 - (Compute ) You can approximate by using the...Ch. 5 - (Compute e) You can approximate e using the...Ch. 5 - (Display leap years) Write a program that displays...Ch. 5 - (Display the first days of each month) Write a...Ch. 5 - (Display calendars) Write a program that prompts...Ch. 5 - (Financial application: compound value) Suppose...Ch. 5 - (Financial application: compute CD value) Suppose...Ch. 5 - (Game: lottery) Revise Listing 3.8, Lottery.java,...Ch. 5 - (Perfect number) A positive integer is called a...Ch. 5 - (Game: scissor; rock, paper) Programming Exercise...Ch. 5 - (Summation) Write a program to compute the...Ch. 5 - (Business application: checking ISBN) Use loops to...Ch. 5 - (Decimal to binary) Write a program that prompts...Ch. 5 - (Decimal to octal) Write a program that prompts...Ch. 5 - (Financial application: find the sales amount) You...Ch. 5 - (Simulation: heads or tails) Write a program that...Ch. 5 - (Occurrence of max numbers) Write a program that...Ch. 5 - (Financial application: find the sales amount)...Ch. 5 - (Math: combinations) Write a program that displays...Ch. 5 - (Computer architecture: bit-level operations) A...Ch. 5 - (Statistics: compute mean and standard deviation)...Ch. 5 - (Reverse a string) Write a program that prompts...Ch. 5 - (Business: check ISBN-13) ISBN -13 is a new...Ch. 5 - (Process string) Write a program that prompts the...Ch. 5 - (Count vowels and consonants) Assume that the...Ch. 5 - Prob. 5.50PECh. 5 - (Longest common prefix) Write a program that...
Knowledge Booster
Similar questions
- Please determine errors and correct it. package test; import java.util.Scanner; public class Test { public static void main(String[] args) {String fname,lname; int hrsworked; Double payrate,income,fnfp,paye,fiji,medical=6.50,total_deduction,netpay; Scanner sc = new Scanner(System.in); System.out.print("\nEnter Employee First Name: "); fname = sc.nextLine(); System.out.print("Enter Employee Last Name: "); lname = sc.nextLine(); System.out.println("Enter Employee Hours Worked: "); hrsworked = sc.nextInt(); System.out.print("Enter Employee Pay Rate: "); payrate = sc.nextDouble(); income = hrsworked * payrate; fnfp = income*0.085; paye = income*0.065; fiji = income*0.0285; total_deduction = hrsworked + fnfp + paye + fiji +medical; netpay = income-total_deduction; System.out.printf("\n*-------------* Pay Slip for: %s *-------------*\n",(fname+" "+lname)); System.out.printf("Fortnightly Income: $ %.2f\n",income); System.out.printf("Hours Worked: \n", hrsworked); System.out.printf("FNPF: $…arrow_forwardThe following code segment has an error. Find the error and suggest a correction.arrow_forwardimport java.util.Scanner; public class OutputWithVars { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int userNum; int userA; int userB; System.out.println("Enter integer:"); userNum = scnr.nextInt(); userA = userNum * userNum; userB = userNum * userNum * userNum; System.out.printf("You have entered %d", userNum); System.out.printf("\n%d squared is %d", userNum, userA); System.out.printf("\nAnd %d cubed is %d", userNum, userB); System.out.println("\nEnter another integer:"); int user = scnr.nextInt(); System.out.printf("%d", user) Purpose: I am trying to stop and let the user input another value but my result is Enter integer: You have entered 2 2 squared is 4 And 2 cubed is 8 Enter another integer: meaning my first part worked but... Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at…arrow_forward
- First, you will debug the Paint1 class so that no errors remain and all calculations work correctly by completing the following: o Find and fix the three errors in the given code. • Use the following sample input values to test your program. If you have found and fixed all the errors, your output should exactly match the sample. Input: 30 25 Output: Wall area: 750.0 square feet Paint needed: 2.142857142857143 gallons Tip: When you run your program in Eclipse, it will prompt you to enter input in the Console window. If you cannot find this window, go to Window, then Show View, then Console. Next, you will add loops to validate all user input and handle exceptions so that code passes all test cases by completing the following: • Review the code, looking for the two code blocks where user input is required. • Implement a do-while loop in both blocks of code to ensure that input is valid and any exceptions are handled. • Use the two following sample sets of input to test your program.…arrow_forwardimport java.util.Scanner; public class leapYearLab { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int userYear; boolean LeapYear; System.out.println("Enter the year"); userYear = scnr.nextInt(); isLeapYear(userYear); //if leap year if (isLeapYear){ System.out.println(userYear + " - leap year"); } else{ System.out.println(userYear + " - not a leap year"); } scnr.close(); } public static boolean isLeapYear(int userYear){ boolean LeapYear; /* Type your code here. */ if ( userYear % 4 == 0) { // checking if year is divisible by 100 if ( userYear % 100 == 0) { // checking if year is divisible by 400 // then it is a leap year if ( userYear % 400 == 0) LeapYear = true; else LeapYear = false; } // if the year is not divisible by 100 else…arrow_forwardimport java.util.Scanner; public class DebugSix3 { public static final int TIME_WASTER = 500; public static void main(String[] args) throws InterruptedException { Scanner input = new Scanner(System.in); System.out.print("Enter a number between 1 and 20 >> "); int num = input.nextInt(); if (num < 1 || num > 20) { System.out.println("Number out of range!"); return; } for (int i = num; i > 0; i--) { System.out.print(i + " "); Thread.sleep(TIME_WASTER); } System.out.println("Blastoff!"); } } The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. An example of the program is shown below: Enter a number between 1 and 20 >> 5 5 4 3 2 1 Blastoff! Depending on the value assigned to the…arrow_forward
- import java.util.Scanner; public class CharMatch { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String userString; char charToFind; int strIndex; userString = scnr.nextLine(); charToFind = scnr.next().charAt(0); strIndex = scnr.nextInt(); /* Your code goes here */ }}arrow_forwardExplain these codes import java.text.DecimalFormat;import java.util.Scanner; public class Main{ private static DecimalFormat df2 = new DecimalFormat("#.00"); public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println(); System.out.print("\tEnter Name : "); String emp_name =input.nextLine(); System.out.print("\tPress F for Full Time or P for Part Time : "); char job_criteria =input.next().charAt(0); char select = Character.toUpperCase(job_criteria); System.out.println(); if (select == 'F') { System.out.print("\t------ Full Time Employee ----- "); System.out.println(); System.out.print("\tEnter Basic Pay : "); double basic_pay = input.nextDouble(); FullTimeEmployee emp = new FullTimeEmployee(); emp.setName(emp_name); emp.setMonthlySalary(basic_pay); System.out.println("\n");…arrow_forwardimport java.util.Scanner;/** This program computes the time required to double an investment with an annual contribution.*/public class DoubleInvestment{ public static void main(String[] args) { final double RATE = 5; final double INITIAL_BALANCE = 10000; final double TARGET = 2 * INITIAL_BALANCE; Scanner in = new Scanner(System.in); System.out.print("Annual contribution: "); double contribution = in.nextDouble(); double balance = INITIAL_BALANCE; int year = 0; // TODO: Add annual contribution, but not in year 0 do { year++; double interest = balance*(RATE/100); balance = (balance+contribution+interest); } while(balance< TARGET); System.out.println("Year: " + year); System.out.printf("Balance: %.2f%n", balance); }}arrow_forward
- expression that will cause the following code to print "Equal" if the value of sensorReading is "close enough" to targetValue...arrow_forwardunsure how to do thisarrow_forwardimport java.util.Scanner;public class LabProgram { public static void main(String args[]) { Scanner scnr = new Scanner(System.in); int credits; int seed; GVDie die1, die2; die1 = new GVDie(); die2 = new GVDie(); // Read random seed to support testing (do not alter) seed = scnr.nextInt(); die1.setSeed(seed); // Read starting credits credits = scnr.nextInt(); int rounds = 0; while (credits > 0) { // Step 1: Roll both dice die1.roll(); die2.roll(); int total = die1.getValue() + die2.getValue(); if (total == 7 || total == 11) { // Player wins one credit credits++; // UPDATE - print the dice total here System.out.println("Dice total: " + total); //UPDATE - break the loop and end the round break; } else if (total == 2 ||…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
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