Question

Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters...

Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters in it (see ExtraneousStringException.java. This java file does not need to be modified). Copy paste the contents of ExtraneousStringException.java into a new .java file, uncomment the commented out code, and write your code inside the main method. In the driver class, keep reading strings from the user until the user enters “DONE.” You may read your input from a file, or use the console. public class LastName_HW01Q1 { public static void main(String[] args) throws ExtraneousStringException { // Write your code here. } */ static class ExtraneousStringException extends Exception { public ExtraneousStringException() { super("String too long"); } } }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution for the above problem

I have written code in two files

1) ExtraneousStringException.java

      2) driver class ie LastName_HW01Q1.java

1)ExtraneousStringException.java

class ExtraneousStringException extends Exception {

public ExtraneousStringException() {
super("String too long");
}
}

2)Driver class
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LastName_HW01Q1
{
public static void main(String[] args) throws ExtraneousStringException,IOException
{
// Write your code here.
//buffered reader for taking console input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String text = "";
//for accepting user inputs untill user type DONE
while(true)
{
System.out.println("Please enter a string: ");
text = br.readLine();
if (!text.equals("DONE"))
{
int len=text.length(); //find string length
//checking user given string contains above 30 characters or not
if(len>30){
//if not throw the exception

System.out.println("Length of the given string"+len);
throw new ExtraneousStringException();
}
//It will show user input if only more than 30 characters
System.out.println(text);
}
else
break;// if user type DONE loop will terminates
}
}
  
}

/////another way is to write code .. both classes in one class as follows

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LastName_HW01Q1
{
public static void main(String[] args) throws ExtraneousStringException,IOException
{
// Write your code here.
//buffered reader for taking console input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String text = "";
//for accepting user inputs untill user type DONE
while(true)
{
System.out.println("Please enter a string: ");
text = br.readLine();
if (!text.equals("DONE"))
{
int len=text.length(); //find string length
//checking user given string contains above 30 characters or not
if(len>30){
//if not throw the exception
System.out.println("Length of the given string"+len);
throw new ExtraneousStringException();
}
//It will show user input if only more than 30 characters
System.out.println(text);
}
else
break;// if user type DONE loop will terminates
}
}

static class ExtraneousStringException extends Exception {

public ExtraneousStringException() {
super("String too long");
}
}
}

Add a comment
Know the answer?
Add Answer to:
Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when...

    (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when a string is discoveredthat has too many characters in it. In the main driver of the program, read strings from the user until the user enters “DONE”. If a string is entered that has too many characters (say 20), throw, catch, and handle the exception. The exception is handled by printing an appropriate message, and the programwill continueprocessing more strings.

  • What is wrong with this code? Please bold the changes that were made so this code...

    What is wrong with this code? Please bold the changes that were made so this code runs properly. The Problem is: 11.16 (Catching Exceptions with SuperClasses) Use inheritance to create an exception superclass (called Exception) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherites from ExceptionA and ExceptionC inherits from ExeptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC. I got this far but I'm still getting a lot...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • given the following two classes Within a file named Rational.java, define a public class named Rational...

    given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...

  • Java Method Resolution : Consider the following example: What is printed on the console if Main...

    Java Method Resolution : Consider the following example: What is printed on the console if Main is executed, and why? How the codes could be improved? public class Main {     public static void main(String[] args) throws Exception {         A obj = null;         obj.foo();     } } public class A {     public void foo() {         System.out.println(“foo in A”);     } }

  • Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

  • ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button...

    ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button to a frame in the FXBookQuote2 program. When the user clicks the button, display the title of the book that contains the opening sentence or two from your favorite book in the label. FXBookQuote2.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all...

    Homework Assignment on Mimir: Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. Code: import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; /** * * @author David */ public class ReadingFiles {     /**      * @param args the command line arguments      */     public static void main(String[] args) throws FileNotFoundException {         // TODO code application logic here...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT