$30
CSC 3020
Java Programming
Assignment 04
50 points
Assignment Objectives:
• To use the Java library classes Date and Random.
• To create objects for primitive values using the wrapper classes (Byte, Short, Integer, Long, Float, Double, Character, and Boolean).
• To simplify programming using automatic conversion between primitive types and wrapper class types.
• To use the BigInteger and BigDecimal classes for computing very large numbers with arbitrary precisions.
• To use the String class to process immutable strings.
• Use regular expressions to validate String data entered into an application.
• To use the StringBuilder class to process mutable strings.
Solution to this assignment will not be posted on Canvas; however, any question can be discussed in the class upon request of a student.
All assignments must be submitted by the Canvas. No email or hard copy is accepted. You must follow the following format:
a. For non-programming questions, use a word file to type your answers. Don’t use the text box on the Canvas to answer the questions or to write comments, we will not read it.
b. State your answer clearly.
c. For programming questions, include only the source file for each problem.
d. Submit your file to the Canvas. You must submit your assignment on time; otherwise, you will receive zero. In addition, you cannot submit your file more than one time.
e. There will be several folders on the Canvas. You need to upload your file(s) using the correct folder on the Canvas.
f. Name each file: “Assignment Number(Question number(s))”.
g. To upload your file(s):
• In Course Navigation, click the Assignments link.
• Click the title of the assignment.
• Click the Submit Assignment button.
• Add File. ...
• Add Another File. ...
• Submit Assignment. ...
• View Submission.
It is your responsibility to make sure that each file is uploaded correctly. If you uploaded a wrong file, you receive zero; files will not be accepted after due date even if you have a prove that the file is created before the due date.
Make sure you review the Cheating & Plagiarism policy on Canvas.
Answer questions 1 to 5 on a word file; write a program for each of Q.6 - Q.8.
Q01. (11 points - 1 point each)
a. Which packages contain the classes Date, Random, System, and Math?
Date: java.time.*;
Random: java.util.Random;
System: java.lang.System;
Math: java.lang.Math;
b. How do you convert an integer into a string? How do you convert a numeric string into an integer? How do you convert a double number into a string? How do you convert a numeric string into a double value?
i. You can convert an integer to a string by creating a string and setting it equal to Integer.toString(yourInteger).
ii. You can convert a numeric string to an integer with Integer.parseInt(yourString).
iii. You can convert a double to a string by creating a string and setting it equal to Double.toString(yourDouble).
iv. You can convert a numeric string to a double with Double.parseDouble(yourString).
c. What is the output of the following code?
public class Test {
public static void main(String[] args) {
java.math.BigInteger x = new java.math.BigInteger("7");
java.math.BigInteger y = new java.math.BigInteger("4");
java.math.BigInteger z = x.add(y);
System.out.println("x is " + x);
System.out.println("y is " + y);
System.out.println("z is " + z);
}
}
Output:
x is 7
y is 4
z is 11
d. To create the string Welcome to Java, you may use a statement like this:
String s = "Welcome to Java";
or:
String s = new String("Welcome to Java");
Which one is better? Why?
The first one creates a literal String s, while the second creates a String object s. The String literal is faster while the string object creates a new object every time.
e. Does any method in the String class change the contents of the string?
the toUpperCase, and toLowerCase methods change the contents of the string to upper or lowercase resectively. Concat(), as well as many other methods.
f. Suppose string s is created using new String(); what is s.length()?
The length will be 0.
g. What is the difference between StringBuilder and StringBuffer?
StringBuilder is not thread safe because it is not synchronized.
h. How do you create a string builder from a string? How do you return a string from a string builder?
Create a StringBuilder Object and then new StringBuilder(String). To return a string from a string builder set a string = to string builder obj.toString().
i. Write three statements to reverse a string s using the reverse method in the StringBuilder class.
s.reverse()
j. Write three statements to delete a substring from a string s of 20 characters, starting at index 6 and ending with index 10. Use the delete method in the StringBuilder class.
k. Show the output of the following program:
public class Test {
public static void main(String[] args) {
String s = "Java";
StringBuilder builder = new StringBuilder(s);
change(s, builder);
System.out.println(s);
System.out.println(builder);
}
private static void change(String s, StringBuilder builder) {
s = s + " and HTML";
builder.append(" and HTML");
}
}
Output:
Java
Java and HTML
Q02. (4 points)
What are autoboxing and autounboxing?
Autoboxing is automatically converting a primitive value to a wrapper object.
Autounboxing is automatically conveting a wrapper object to a primitive value.
Are the following statements correct?
a. Integer x = 3 + new Integer(5);
a. yes, the code will run but there is unnecesary boxing of 5.
b. Integer x = 3;
a. yes.
c. Double x = 3;
a. no, value must be a double not an int.
d. Double x = 3.0;
a. yes.
e. int x = new Integer(3);
a. yes, due to autounboxing
f. int x = new Integer(3) + new Integer(4);
a. yes, but it has unnecesary boxing
Q03. (5 points)
Suppose that s1, s2, s3, and s4 are four strings, given as follows:
String s1 = "Welcome to Java";
String s2 = s1;
String s3 = new String("Welcome to Java");
String s4 = "Welcome to Java";
What are the results of the following expressions? Assume that the statements are independent.
a. s1 == s2 true
b. s1 == s3 false
c. s1 == s4 true
d. s1.equals(s3) true
e. s1.equals(s4) true
f. "Welcome to Java".replace("Java", "HTML ) Welcome to HTML
g. s1.replace('o', 'V') WelcVme tV Java
h. s1.replaceAll("o", "VT") WelcVTme tVT Java
i. s1.replaceFirst("o", "T") WelcTme to Java
j. s1.toCharArray() Welcome to Java
Q04. (6 points)
Suppose that s1 and s2 are given as follows:
StringBuilder s1 = new StringBuilder("Java");
StringBuilder s2 = new StringBuilder("HTML");
Show the value of s1 after each of the following statements. Assume that the statements are independent.
a. s1.append(" is fun");
b. s1.append(s2);
c. s1.insert(3, "is fun");
d. s1.insert(2, s2);
e. s1.charAt(2);
f. s1.length();
g. s1.deleteCharAt(3);
h. s1.delete(1, 3);
i. s1.reverse();
j. s1.replace(1, 3, "Computer");
k. s1.substring(1, 3);
l. s1.substring(1);
Q05. (4 points)
which pattern match the following regular expressions.
• ab*c
• ab+c
• ab?c
.
• ab{1,3}c
• red|blue
• [0-9]
• [a-zA-Z0-9]
• Which regex pattern should be used to match a two-digit year or a four-digit year?
Programming Questions
Q06. (5 points)
Find the first 10 numbers greater than Long.MAX_VALUE that are divisible by 5 or 6.
Q07. (5 points)
The String class is provided in the Java library.
Provide your own implementation (name the new class MyString2) with one data field, array of characters, and the following methods:
• public MyString2(String s); constructor
• public String getArray(); return the data field as a String.
• public MyString2 substring(int begin); create a new string starts at begin and return it.
• public MyString2 toUpperCase(); convert all characters in a string to uppercase and return it.
• public static MyString2 valueOf(boolean b); convert b value to string and return it.
Do not use any method from String (you can use length), StringBuilder, or StringBuffer
Q08. (10 points)
The StringBuilder class is provided in the Java library. Provide your own implementation (name the new class MyStringBuilder2) with three data fields, array of characters, size, capacity, and the following methods:
• public MyStringBuilder2(String s); constructor (1 points)
• public MyStringBuilder2 append(MyStringBuilder2 s); a method that takes s object and add s array of characters to this array of characters, then returns this. (3 points)
• public MyStringBuilder2 substring(int begin, int end); (3 points)
• Accessors methods that return the size, the capacity, and the array of characters as a string. (3 points)
Do not use any method from String (you can use length), StringBuilder, or StringBuffer