Part 1. A. Take some notes on JavaDoc, and include them in your submission. B. T
ID: 3793964 • Letter: P
Question
Part 1.
A. Take some notes on JavaDoc, and include them in your submission.
B. This is a separate exercise – go search out the description of these concepts.
Describe each of the following:
Wrapper classes
Immutable objects
Tokenizing strings
The differences between String and StringBuffer class
String objects and == operator vs. the equals() method - what is the difference?
Part 2.
I will include a text file with this assignment. It is a text version of this assignment.
Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number.
Part 3. Programming with String obejcts.
Create a class to deal with telephone numbers that your customers enter.
User’s aren’t very disciplined, so even if you indicate that you want the number entered like this:
407 582 2213
They sometimes enter the number like any of these:
(407)582-2213
1 (407) 582-2213
407.582.2213
4075822213
1 407 582-2213
Or other combinations that I can’t think of, but they will.
The constructor for your class takes a String as its only parameter.
If the number they pass to your constructor does not have 10 digits, (or 11 digits if it starts with a 1), you should throw an Exception from the constructor.
InvalidTelephoneException. It’s the Telephone constructor method that will throw this exception.
A test of your class can be though of as: try to make a phone number out of a String.
Telephone phone;
String testString = “407.582.2213”;
try {
phone = new Telephone(testString);
}catch(InvalidTelephoneException ite) {
Send some information to the console about why that string is not a phone number
}
Your class must have a getAreaCode, getExchange, getLocalNumber methods. For the example above, the area code is 407, the exchange is 582, and the local number is 2213.
Follow good practices for OO programming.
A toString method will present the number in a canonical form: (407) 582-2213
Create a test class that demonstrates that your code will work correctly for each of the documented cases above.
Include all of the code, and the output from running your test class.
Use JavaDoc comments to document the class. Include examples IN THE JAVADOC of what format of phone numbers will be acceptable by the class (service provider) that you have created.
Include the JavaDoc that is generated by running the JavaDoc tool on your code. Paste in the web page that is created. It might be messy - don’t try to format it in Word.
There is no end to this one…. The ‘service’ that this class provides will vary in its capabilities. This is a pretty common real-world problem. As soon as you think you are done someone will want it to handle something that you didn’t think of. You are ‘done’ when your code meets the requirements that are documented, and it is demonstrated.
Text doc for part 2
Explanation / Answer
Hi, I have answered all parts of question 1.
Please repost others in separate post.
Wrapper classes
Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.
Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
Here is the simplest example of autoboxing:
Character ch = 'a';
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
Passed as a parameter to a method that expects a value of the corresponding primitive type.
Assigned to a variable of the corresponding primitive type.
Immutable objects
An object is considered immutable if its state cannot change after it is constructed. Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code.
Immutable objects are particularly useful in concurrent applications. Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state.
Ex: String class is immutable
Tokenizing strings
The java.util.StringTokenizer class allows you to break a string into tokens. It is simple way to break string.
The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.
The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.
An instance of StringTokenizer behaves in one of two ways, depending on whether it was created with the returnDelims flag having the value true or false:
If the flag is false, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters.
If the flag is true, delimiter characters are themselves considered to be tokens. A token is thus either one delimiter character, or a maximal sequence of consecutive characters that are not delimiters.
A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.
A token is returned by taking a substring of the string that was used to create the StringTokenizer object.
The following is one example of the use of the tokenizer. The code:
StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
prints the following output:
this
is
a
test
The differences between String and StringBuffer class
String object is immutable whereas StringBuffer objects are mutable.
String is slow and consumes more memory when you concat too many strings because every time it creates new instance.
StringBuffer is fast and consumes less memory when you cancat strings.
All the methods of StringBuffer are synchronized(thread safe) where as String methods are not synchronized
String objects and == operator vs. the equals() method - what is the difference?
== => compares the reference
equals() => compare the content of string