Starting Out with Java: Early Objects (6th Edition)
6th Edition
ISBN: 9780134462011
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 10, Problem 1AW
Explanation of Solution
Given code:
//Definition of class "ExceptionTest"
public class ExceptionTest
{
//Definition of main
public static void main(String[] args)
{
//Declare the variable "number"
int number;
//Declare the variable "str"
String str;
//Try block
try
{
//Assign "xyz" to "str"
str = "xyz";
//Parse the string and store it in //number
number = Integer.parseInt(str);
//Print "A"
System.out.println("A");
}
//Catch block
catch (NumberFormatException e)
{
//Print the value "B"
System.out.println("B");
}
//Catch block
catch (IllegalArgumentException e)
{
//Print the value "C"
System.out.println("C");
}
//Print the value "D"
System...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
//Assignment 06 */public static void main[](String[] args) { String pass= "ICS 111"; System.out.printIn(valPassword(pass));}
/*
public static boolean valPassword(String password){ if(password.length() > 6) { if(checkPass(password) { return true; } else { return false; } }else System.out.print("Too small"); return false;}
public static boolean checkPass (String password){ boolean hasNum=false; boolean hasCap = false; boolean hasLow = false; char c; for(int i = 0; i < password.length(); i++) { c = password.charAt(1); if(Character.isDigit(c)); { hasNum = true; } else if(Character.isUpperCase(c)) { hasCap = true; } else if(Character.isLowerCase(c)) { hasLow = true; } } return true; { return false; }
}
Analyze the following code:
public class Test{
public static void main(String[] args) {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
}
}
catch (Exception ex) {
System.out.println("NumberFormatException");
int i = 0;
int y = 2/i;
}
catch (RuntimeException ex) {
}
}
System.out.println("RuntimeException");
The program displays NumberFormatException.
The program displays RuntimeException.
The program displays NumberFormatException followed by RuntimeException.
The program has a compile error.
using System;
namespace ErrorHandlingApplication{class DivNumbers{int result;
DivNumbers(){result = 0;}public void division(int num1, int num2){try{result = num1 / num2;}catch (DivideByZeroException e){Console.WriteLine("Exception caught: {0}", e);}finally{Console.WriteLine("Result: {0}", result);}}static void Main(string[] args){DivNumbers d = new DivNumbers();d.division(25, 0);Console.ReadKey();}}}
Modify this c# program on which enables users to enter value desired.
Chapter 10 Solutions
Starting Out with Java: Early Objects (6th Edition)
Ch. 10.1 - Prob. 10.1CPCh. 10.1 - Prob. 10.2CPCh. 10.1 - Prob. 10.3CPCh. 10.1 - Prob. 10.4CPCh. 10.1 - Prob. 10.5CPCh. 10.1 - Prob. 10.6CPCh. 10.1 - Prob. 10.7CPCh. 10.1 - Prob. 10.8CPCh. 10.1 - Prob. 10.9CPCh. 10.1 - When does the code in a finally block execute?
Ch. 10.1 - What is the call stack? What is a stack trace?Ch. 10.1 - Prob. 10.12CPCh. 10.1 - Prob. 10.13CPCh. 10.1 - Prob. 10.14CPCh. 10.2 - What does the throw statement do?Ch. 10.2 - Prob. 10.16CPCh. 10.2 - Prob. 10.17CPCh. 10.2 - Prob. 10.18CPCh. 10.2 - Prob. 10.19CPCh. 10.3 - What is the difference between a text file and a...Ch. 10.3 - What classes do you use to write output to a...Ch. 10.3 - Prob. 10.22CPCh. 10.3 - What class do you use to work with random access...Ch. 10.3 - What are the two modes that a random access file...Ch. 10.3 - Prob. 10.25CPCh. 10 - Prob. 1MCCh. 10 - Prob. 2MCCh. 10 - Prob. 3MCCh. 10 - Prob. 4MCCh. 10 - FileNotFoundException inherits from __________. a....Ch. 10 - Prob. 6MCCh. 10 - Prob. 7MCCh. 10 - Prob. 8MCCh. 10 - Prob. 9MCCh. 10 - Prob. 10MCCh. 10 - Prob. 11MCCh. 10 - Prob. 12MCCh. 10 - Prob. 13MCCh. 10 - Prob. 14MCCh. 10 - Prob. 15MCCh. 10 - This is the process of converting an object to a...Ch. 10 - Prob. 17TFCh. 10 - Prob. 18TFCh. 10 - Prob. 19TFCh. 10 - True or False: You cannot have more than one catch...Ch. 10 - Prob. 21TFCh. 10 - Prob. 22TFCh. 10 - Prob. 23TFCh. 10 - Prob. 24TFCh. 10 - Find the error in each of the following code...Ch. 10 - // Assume inputFile references a Scanner object,...Ch. 10 - Prob. 3FTECh. 10 - Prob. 1AWCh. 10 - Prob. 2AWCh. 10 - Prob. 3AWCh. 10 - Prob. 4AWCh. 10 - Prob. 5AWCh. 10 - Prob. 6AWCh. 10 - The method getValueFromFile is public and returns...Ch. 10 - Prob. 8AWCh. 10 - Write a statement that creates an object that can...Ch. 10 - Assume that the reference variable r refers to a...Ch. 10 - Prob. 1SACh. 10 - Prob. 2SACh. 10 - Prob. 3SACh. 10 - Prob. 4SACh. 10 - Prob. 5SACh. 10 - Prob. 6SACh. 10 - What types of objects can be thrown?Ch. 10 - Prob. 8SACh. 10 - Prob. 9SACh. 10 - Prob. 10SACh. 10 - What is the difference between a text file and a...Ch. 10 - What is the difference between a sequential access...Ch. 10 - What happens when you serialize an object? What...Ch. 10 - TestScores Class Write a class named TestScores....Ch. 10 - Prob. 2PCCh. 10 - Prob. 3PCCh. 10 - Prob. 4PCCh. 10 - Prob. 5PCCh. 10 - FileArray Class Design a class that has a static...Ch. 10 - File Encryption Filter File encryption is the...Ch. 10 - File Decryption Filter Write a program that...Ch. 10 - TestScores Modification for Serialization Modify...Ch. 10 - Prob. 11PC
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
- using System; class Program { publicstaticvoid Main(string[] args) { int number,max=-9999999,i=0; while (i<6) { Console.WriteLine("Enter a number: "); number=Convert.ToInt32(Console.ReadLine()); if(number==0) { break; } else { if(number>max) { max=number; } } i++; } Console.WriteLine("Largest number is "+max); } } Hello! This code is written in C# to get six numbers from the user and choose the largest of those numbers. How can it be changed to get six numbers from the user and then add the smallest number with the largest number? if possible Thank you!arrow_forwardPlease Solve it in javaarrow_forwardclass Output{public static void main(String args[]){Integer i = new Integer(557);float x = i.floatValue();System.out.print(x);}} What will the given code prints on the output screen.arrow_forward
- Java Questions - (Has 2 Parts). Based on each code, which answer out of the choices "A, B, C, D, E" is correct. Each question has one correct answer. Thank you. Part 1 - The following code will cause a(n) ___ exception. class Sample{public static void main(String[] args) { int x = 0; int y = 10; int z = y/x; } } A. ArithmeticExceptionB. ArrayIndexOutOfBoundsExceptionC. StringIndexOutOfBoundsExceptionD. NullPointerExceptionE. ClassNotFoundException Part 2 - A programmer carelessly assigned a negative value as an index of an array. Which type of exception will be raised during execution? A. An IndexOutOfBoundsException is thrown.B. A NumberFormatException is thrown.C. The first slot of the array is used.D. This is an Error, so the program immediately terminates no matter what.E. The last slot of the array is used.arrow_forward1.public class Test{ public static void main(String args[]){ Person p = null; try{ } p.talk(); } catch (NullPointerException e) { System.out.print("There is a NullPointerException."); } catch(Exception e) { System.out.print("There is an Exception."); } System.out.print("Everything went fine."); } what will be the result if you run this program? O There is a NullPointerException. Everything went fine. O There is a NullPointerException. There is a NullPointerException. There is an Exception. This code will not compile, because in Java there are no pointers.arrow_forwardusing System; class main { publicstaticvoid Main(string[] args) { Random r = new Random(); int randNo= r.Next(1,11); Console.Write("Hi, my name is Stevie. I am thinking of a number from 1 to 10.\nGuess which number I am thinking of? "); int guess = Convert.ToInt32(Console.ReadLine()); if (guess == randNo) { Console.WriteLine("You guessed correctly!"); } else { Console.WriteLine("I was thinking of the number "+randNo+"\nSorry! Play again."); } } } this is a guessing game program for a random number between 1-10 in C#. how can code be added to tell you if your guess is too high or too low from the chosen random number?arrow_forward
- using System; class Program { publicstaticvoid Main(string[] args) { int number,max=-9999999; while (true) { Console.WriteLine("Enter a number: "); number=Convert.ToInt32(Console.ReadLine()); if(number==0) { break; } else { if(number>max) { max=number; } } } Console.WriteLine("Largest number is "+max); } } Hello! Hope you are well! This C# program gets numbers from the user and prints the largest one. How can it be fixed to only ask for 6 numbers from the user and then choose the largest one? Thank you!arrow_forwardJava output caffeine levelsarrow_forwardusing System; class main { publicstaticvoid Main(string[] args) { Random r = new Random(); int randNo= r.Next(1,11); Console.Write("Hi, my name is Stevie. I am thinking of a number from 1 to 10.\nGuess which number I am thinking of? "); int guess = Convert.ToInt32(Console.ReadLine()); do { if(guess<randNo) { Console.WriteLine("Your guess is too low.please enter another number"); guess = Convert.ToInt32(Console.ReadLine()); } if(guess>randNo) { Console.WriteLine("Your guess is too high.please enter another number"); guess = Convert.ToInt32(Console.ReadLine()); } if(guess == randNo) { Console.WriteLine("You guessed correctly!"); } }while(guess!=randNo); } } Hello! This program is in C# and it's a guessing game. The user must choose a number between 1-10 and if the guess is wrong it will say if the guess is too high or too low. How can it be modified to guess a color on the rainbow (ROYGBIV)?arrow_forward
- public class Test { } public static void main(String[] args) { String str = "Salom"; System.out.println(str.indexOf('t)); }arrow_forwardpublic class Test { } public static void main(String[] args){ int a = 10; System.out.println(a*a--); }arrow_forward// ArtShow.java - This program determines if an art show attendee gets a 5% discount // for preregistering. // Input: Interactive. // Output: A statement telling the user if they get a discount or no discount. import java.util.Scanner; public class ArtShow { public static void main(String args[]) { Scanner s = new Scanner(System.in); String registerString; System.out.println("Did you preregister? Enter Y or N: "); registerString = s.nextLine(); // Test input here. If Y, call discount(), else call noDiscount(). System.exit(0); } // End of main() method. // Write discount method here. // Write noDiscount method here. } // End of ArtShow class.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