
Concept explainers
import java.util.Scanner;
import java.util.InputMismatchException;
public class Cents {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int numCents;
boolean valueFound = false;
while (!valueFound) {
try {
numCents = scnr.nextInt();
valueFound = true;
System.out.println(numCents + " cents = " + (numCents / 5) + " nickels");
System.out.println("Processed one valid input value");
}
/******************************************************************************************************* New Java Code can only be added here. No other code can be edited
******************************************************************************************************/
}
}
}
![The while loop reads values from input until an integer is read. Complete the exception handler in the while loop to catch an
InputMismatchException exception and:
• Output "Removed unacceptable input for number of cents". End with a newline.
• Discard the invalid input.
Ex: If the input is 20 60, then the output is:
20 cents = 4 nickels
Processed one valid input value
Ex: If the input is Aya Tia 20 60, then the output is:
Removed unacceptable input for number of cents
Removed unacceptable input for number of cents
20 cents = 4 nickels
Processed one valid input value
Note: Each value read from input is on one line.
1 import java.util.Scanner;
2 import java.util. InputMismatchException;
3
4 public class Cents {
5 public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int numCents;
boolean valueFound
1010 00
6
7
8
9
10
11
12
13
14
15
16
17
18
=
while (!value Found) {
try {
}
false;
numCents scnr.nextInt ();
valueFound = true;
System.out.println(numCents + "cents = + (numCents / 5) + nickels");
System.out.println("Processed one valid input value");](https://content.bartleby.com/qna-images/question/0cc153ae-b205-4fb2-9991-7cf6a21d5016/6b67975f-5fb9-4f65-9f6c-bc0d26cc2d2a/33a8znq_thumbnail.png)

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images

- How do I make: Code 1: import java.util.Scanner; public class ProjectEliza { public static void main(String[] args) { while(true){ Scanner s=new Scanner(System.in); System.out.println("ELIZA: Hello, my name is Eliza. What is your name? "); System.out.print("USER: "); String name=s.next(); System.out.println("ELIZA: Hello, "+name+". Tell me what is on your mind today in 1 sentence."); System.out.print("USER: "); String sentence=s.next(); sentence += s.nextLine(); String[] words = sentence.split("\\s+"); // for (int i = 0, l = words.length; i + 1 < l; i++)// System.out.println(words[i]); //System.out.println("ELIZA: "+question2(words)); System.out.print("USER: "); String exit=s.next(); if(exit.equals("EXIT")){ System.out.println("ELIZA: Do you want to run the session…arrow_forwardthere are 2 errors in this code can you fix it for me? public class Exercise09_10 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a, b, c: "); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); QuadraticEquation equation = new QuadraticEquation(a, b, c); double discriminant = equation.getDiscriminant(); if (discriminant < 0) { System.out.println("The equation has no roots"); } else if (discriminant == 0) { System.out.println("The root is " + equation.getRoot1()); } else // (discriminant >= 0) { System.out.println("The roots are " + equation.getRoot1() + " and " + equation.getRoot2()); } } }arrow_forwardimport java.util.Scanner; public class ParkingFinder {/* Your code goes here */ public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int numVisits; int duration; numVisits = scnr.nextInt(); duration = scnr.nextInt(); System.out.println(findParkingPrice(numVisits, duration)); }}arrow_forward
- Given string inputStr on one line and integers idx1 and idx2 on a second line, output "Match found" if the character at index idx1 of inputStr is equal to the character at index idx2. Otherwise, output "Match not found". End with a newline. Ex: If the input is: eerie 4 1 then the output is: Match found Note: Assume the length of string inputStr is greater than or equal to both idx1 and idx2.arrow_forwardimport java.util.Scanner; public class RomanNumerals { public static void main(String[] args) { Scanner in = new Scanner("I C X D M L"); char romanNumeral = in.next().charAt(0); System.out.println("Value: " + valueOf(romanNumeral) + " Expected: 1") ; romanNumeral = in.next().charAt(0); System.out.println("Value: " + valueOf(romanNumeral) + " Expected: 100") ; romanNumeral = in.next().charAt(0); System.out.println("Value: " + valueOf(romanNumeral) + " Expected: 10") ; romanNumeral = in.next().charAt(0); System.out.println("Value: " + valueOf(romanNumeral) + " Expected: 500") ; romanNumeral = in.next().charAt(0); System.out.println("Value: " + valueOf(romanNumeral) + " Expected: 1000") ; romanNumeral = in.next().charAt(0); System.out.println("Value: " + valueOf(romanNumeral) + " Expected: 50") ; } /** Gives the value…arrow_forwardimport java.util.Scanner; public class CircleAndSphereWhileLoop{ public static final double MAX_RADIUS = 500.0; public static void main(String[] args) { Scanner in = new Scanner(System.in); // Step 2: Read a double value as radius using prompt // "Enter the radius (between 0.0 and 500.0, exclusive): " // Step 3: While the input radius is not in the ragne (0.0, 500.0) // Display a message on one line (ssuming input value -1) // "The input number -1.00 is out of range." // Read a double value as radius using the same promt double circumference = 2 * Math.PI * radius; double area = Math.PI * radius * radius; double surfaceArea = 4 * Math.PI * Math.pow(radius, 2); double volume = (4 / 3.0) * Math.PI * Math.pow(radius, 3); // Step 4: Display the radius, circle circumference, circle area, // sphere surface area, and…arrow_forward
- Using Java.arrow_forwardStringFun.java import java.util.Scanner; // Needed for the Scanner class 2 3 /** Add a class comment and @tags 4 5 */ 6 7 public class StringFun { /** * @param args not used 8 9 10 11 12 public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter your first name: "); 13 14 15 16 17 18 System.out.print("Please enter your last name: "); 19 20 21 //Output the welcome message with name 22 23 24 //Output the length of the name 25 26 27 //Output the username 28 29 30 //Output the initials 31 32 33 //Find and output the first name with switched characters 34 //All Done! } } 35 36 37arrow_forwarddo this code using two classes Emi and main import java.util.Scanner; class Emi { public static void main(String []args) { Scanner a = new Scanner(System.in); double principal, rate, time, emi; System.out.print("Enter principal: "); principal = a.nextFloat(); System.out.print("Enter rate: "); rate = a.nextFloat(); System.out.print("Enter time in year: "); time = a.nextFloat(); rate=rate/(12*100); time=time*12; emi= (principal*rate*Math.pow(1+rate,time))/(Math.pow(1+rate,time)-1); System.out.print("Monthly EMI is= "+emi+"\n"); } }arrow_forward
- import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(" "); String s1 = sc.nextLine(); System.out.print(""); String s2 = sc.nextLine(); int minLen = Math.min(s1.length(), s2.length()); int matchCount = 0; for (int i = 0; i < minLen; i++) { if (s1.charAt(i) == s2.charAt(i)) { matchCount++; } } if (matchCount == 1) { System.out.println("1 character matches"); } else { System.out.println(matchCount + " characters match"); } sc.close(); }}arrow_forward8) Now use the Rectangle class to complete the following tasks: - Create another object of the Rectangle class named box2 with a width of 100 and height of 50. Note that we are not specifying the x and y position for this Rectangle object. Hint: look at the different constructors) Display the properties of box2 (same as step 7 above). - Call the proper method to move box1 to a new location with x of 20, and y of 20. Call the proper method to change box2's dimension to have a width of 50 and a height of 30. Display the properties of box1 and box2. - Call the proper method to find the smallest intersection of box1 and box2 and store it in reference variable box3. - Calculate and display the area of box3. Hint: call proper methods to get the values of width and height of box3 before calculating the area. Display the properties of box3. 9) Sample output of the program is as follow: Output - 1213 Module2 (run) x run: box1: java.awt. Rectangle [x=10, y=10,width=40,height=30] box2: java.awt.…arrow_forwardimport java.util.Scanner; public class AutoBidder { public static void main (String [] args) { Scanner scnr = new Scanner (System.in); char keepGoing; int nextBid; nextBid = 0; keepGoing 'y'; * Your solution goes here */ while { nextBid = nextBid + 3; System.out.println ("I'll bid $" + nextBid + "!") ; System.out.print ("Continue bidding? (y/n) "); keepGoing = scnr.next ().charAt (0); } System.out.println (""); }arrow_forward
- 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





