Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSIS 110 Lecture 36: String vs StringBuffer and Exception Handling - Prof. Brian F. Hanks, Study notes of Javascript programming

The differences between string and stringbuffer classes, including their immutability and methods. It also discusses exception handling in java, focusing on runtime errors and logic errors. Examples of common exceptions and their solutions.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-xrq-1
koofers-user-xrq-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 36
Announcements: Quiz Friday – Review next time.
Last Time: String and StringBuffer
What are some major differences between the String and StringBuffer classes?
- Strings are immutable (value cannot be changed)
- StringBuffer class contains methods that change the value of a StringBuffer object
- String has special syntax for initialization and concatenation
The StringBuffer class is similar to String, except that StringBuffer objects can be
modified. Let's look at some methods provided by the StringBuffer class:
append – add some characters to the end of the StringBuffer
insert – insert characters in the middle of the StringBuffer
setCharAt – change the character at position p to a new value
deleteCharAt – delete the character at position p
toString – return a String representation of the StringBuffer
Also has methods like the String class: length, charAt.
Constructors:
StringBuffer()
StringBuffer( String s )
Exceptions
Let's look at the problems project. The Example class includes three public methods.
Don't execute these methods yet!
First, look at the array definition – this is slightly different syntax than we have seen
before. This is a simple way to initialize an array.
Give example of how this makes some array initialization much easier. Use
daysPerMonth as an example.
Let's look at the loop1 method. What is wrong with it? What will happen when we run it?
How about the listExample method? What's going to happen when we run it?
What about the getUserInput method? Is it correct?
Java throws an Exception when something goes wrong that it cannot recover from.
pf3
pf4

Partial preview of the text

Download CSIS 110 Lecture 36: String vs StringBuffer and Exception Handling - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 36

Announcements: Quiz Friday – Review next time. Last Time: String and StringBuffer What are some major differences between the String and StringBuffer classes?

  • Strings are immutable (value cannot be changed)
  • StringBuffer class contains methods that change the value of a StringBuffer object
  • String has special syntax for initialization and concatenation The StringBuffer class is similar to String, except that StringBuffer objects can be modified. Let's look at some methods provided by the StringBuffer class: append – add some characters to the end of the StringBuffer insert – insert characters in the middle of the StringBuffer setCharAt – change the character at position p to a new value deleteCharAt – delete the character at position p toString – return a String representation of the StringBuffer Also has methods like the String class: length, charAt. Constructors: StringBuffer() StringBuffer( String s ) Exceptions Let's look at the problems project. The Example class includes three public methods. Don't execute these methods yet! First, look at the array definition – this is slightly different syntax than we have seen before. This is a simple way to initialize an array. Give example of how this makes some array initialization much easier. Use daysPerMonth as an example. Let's look at the loop1 method. What is wrong with it? What will happen when we run it? How about the listExample method? What's going to happen when we run it? What about the getUserInput method? Is it correct? Java throws an Exception when something goes wrong that it cannot recover from.

A couple of weeks ago, we said that there were two types of errors that we could make when we wrote a program: syntax errors and logic errors. Let's look at the logic error category in more detail. What kinds of errors did we just see? These are called runtime errors. The program terminates because something goes wrong. There is another type of logic error – the program doesn't throw an exception, but it produces the wrong result. What distinctions can you see between the runtime errors we just saw?

  • Some are due to programmer error: IndexOutOfBoundsException, NullPointerException.
  • Others are due to user errors: InputMismatchException. In this case, the program is correct, but the user made an error. What can we do to correct these problems?
  • Fix the program errors (for the first type of problem)
  • Catch the Exception so that the program does not terminate Can you think of any other situations where something can go wrong that is not because of programmer error?
  • File Not Found
  • Reading past the end of a file Exception Syntax Normally, a program terminates when an exception is thrown. You can override this behaviour by enclosing statements that might cause an exception in a try-catch block. try { statements that might throw an exception } catch ( ExceptionClass exceptionArg ) { statements to handle exception } You can have multiple catch blocks after a try - one for each exception class that you wish to handle. An exception matches a catch block if the type of the exception is the same as the argument type, or is a subclass of the argument type. You don't need to worry about what subclass means – all that matters for now is that Exceptions are organized in a hierarchy, and some of them are more specialized than others. For example

Lab Create a new project. Create a new class named GetMonth. This class has one public method: public void getMonth() This method should call other (private) methods to:

  1. Ask the user to enter an int with a value between 1 and 365. The value represents a day in a year
  2. Determine what month that day falls in
  3. Print out a message similar to: Day number 200 is in July Of course, the day number and the month should be values based on the number the user entered. Your user input method must:
  • validate the user input to make sure that the value is between 1 and 365.
  • Make sure that the user enters an integer (use try/catch) Hint The {} form of array initialization will be very helpful here. You will probably need two arrays: one for the number of days per month, and one for the names of the months.