Uploaded by Md. Shohug Hossain

quiz-4-theroypdf

advertisement
FA20CSE215.10/16
Quiz 4
Handwritten Submission
Instructions:
All of these assignments are handwritten. Write your name and ID on the top of every page in
the sheet and create a PDF named Quiz4_1234567.pdf with the scanned copies. Replace
1234567 with your own 7-digit NSU student ID.
Q1. Definition of Person class and driver class is provided below:
Person.Java
public class Person {
private String NID;
public Person(String NID){
this.NID = NID;
}
public String getNID() {
return NID;
}
}
Driver.java
public class Driver {
public static void main(String... args) throws CloneNotSupportedException {
Person person1 = new Person("123141");
Person person2 = new Person("123341");
if(person1.compareTo(person2) > 0){
System.out.println("Person1 NID is greater than Person2");
}else if(person1.compareTo(person2) < 0){
System.out.println("Person1 NID is less than Person2");
}else{
System.out.println("Person1 and Person2 have same NID");
}
if(person1.equals(person2)){
System.out.println("Person1 and Person2 have same NID");
}else{
System.out.println("Person1 and Person2 dont have same NID");
}
Person person3 = person2.clone();
if(person3.equals(person2)){
System.out.println("Person2 and Person3 have same NID");
}else{
System.out.println("Person2 and Person3 dont have same NID");
}
}
}
FA20CSE215.10/16
Quiz 4
Handwritten Submission
A) Rewrite Person class to support Driver.Java by doing the following:
a. Implement appropriate interface provided by Java Language for cloning [Hint – look
into .clone() method]
b. Implement appropriate interface provided by Java Language for comparing two objects
[Hint – look into .compareTo() method].
c. Override necessary method from Object classs provided by Java Language for checking
if two objects are equal. [Hint – look into .equals() method].
d. Check if replacing compareTo method for objects with comparison Operators (< or >)
works in Java. If not, explain why not. Read the book or search the internet to come up
with an answer.
e. Check if replacing equals method by equalTo operator (==) gives the same result. If not,
explain why not. Read the book or search the internet to come up with an answer.
Instructions:
For Question 1A) a) – c) , submit rewritten Person class (by hand only)
For Questions 1A) d) – e), submit handwritten answers.
[THIS PAGE IS INTENTIONALLY LEFT BLANK. PLEASE TURN OVER FOR NEXT QUESTION]
FA20CSE215.10/16
Quiz 4
Handwritten Submission
Instructions:
All of these assignments are handwritten. Write your name and ID on the top of every
page in the sheet and create a PDF named Quiz4_1234567.pdf with the scanned
copies. Replace 1234567 with your own 7-digit NSU student ID.
Q2. The Person and Driver class are provided below.
Driver.java
import java.io.*;
import java.util.Scanner;
public class Driver {
public static void main(String... args) {
Person person = null;
File inputFile = new File("input.txt");
File outputFile = new File("output.txt");
StringBuffer outputStringBuffer = new StringBuffer();
try(FileInputStream inputFS = new FileInputStream(inputFile);
PrintStream outputFS = new PrintStream(outputFile);
) {
Scanner scanner = new Scanner(inputFS);
while(scanner.hasNext()){
do {
String nid = scanner.next();
outputStringBuffer.append("Enter 11 or 18 digit NID : " + nid +"\n");
try {
person = new Person(nid);
outputStringBuffer.append("Person NID: " + person + "\n");
}catch (NIDHasCharacterException ex){
outputStringBuffer.append(ex);
}catch (NIDLengthMismatchException ex){
outputStringBuffer.append(ex);
}catch (NIDStartsWithZeroException ex) {
outputStringBuffer.append(ex);
}catch (InvalidNIDPatternException ex){
outputStringBuffer.append(ex);
}
System.out.println(outputStringBuffer.toString());
outputFS.println(outputStringBuffer.toString());
outputStringBuffer.delete(0,outputStringBuffer.length());
}while (person == null);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FA20CSE215.10/16
Quiz 4
Handwritten Submission
Person.java
public class Person {
private String NID;
public Person(String NID)
throws NIDHasCharacterException
, NIDLengthMismatchException
, InvalidNIDPatternException
, NIDStartsWithZeroException {
setNID(NID);
}
public String getNID() {
return NID;
}
public void setNID(String NID)
throws NIDHasCharacterException
, NIDLengthMismatchException
, InvalidNIDPatternException
, NIDStartsWithZeroException {
if(!NIDChecker.isDigitOnly(NID))
throw new NIDHasCharacterException(NID);
if(!NIDChecker.is11Digits(NID)){
if(!NIDChecker.is17Digits(NID)){
throw new NIDLengthMismatchException(NID);
}
}
if(NIDChecker.doesNIDStartWithZero(NID))
throw new NIDStartsWithZeroException(NID);
if(!NIDChecker.isValidNIDPattern(NID)){
throw new InvalidNIDPatternException(NID);
}
this.NID = NID;
}
@Override
public String toString() {
return this.getNID();
}
}
The definition of NIDChecker is unimportant, assume that it works as expected.
FA20CSE215.10/16
Quiz 4
Handwritten Submission
Sample output format is given below.
Enter 11 or 18 digit NID : 12345678901
Person NID: 12345678901
Enter 11 or 18 digit NID : 1234567890a
QuestionsOn.Interface.Q2.NIDHasCharacterException: Person NID: 1234567890a contains Alphabetic Characters.
Should only contain digits.
Enter 11 or 18 digit NID : 11111111111
QuestionsOn.Interface.Q2.InvalidNIDPatternException: Person NID: 11111111111 is Invalid.
Check for patterns.
Does this have repeating patterns.
Enter 11 or 18 digit NID : 22222222222
QuestionsOn.Interface.Q2.InvalidNIDPatternException: Person NID: 22222222222 is Invalid.
Check for patterns.
Does this have repeating patterns.
Enter 11 or 18 digit NID : 11111111111111111
QuestionsOn.Interface.Q2.InvalidNIDPatternException: Person NID: 11111111111111111 is Invalid.
Check for patterns.
Does this have repeating patterns.
Enter 11 or 18 digit NID : 2222222222222222
QuestionsOn.Interface.Q2.NIDLengthMismatchException: Person NID: 2222222222222222 has length 16 .
Should be length 11 or 17.
Enter 11 or 18 digit NID : 12345678
QuestionsOn.Interface.Q2.NIDLengthMismatchException: Person NID: 12345678 has length 8 .
Should be length 11 or 17.
Enter 11 or 18 digit NID : 01234567890
QuestionsOn.Interface.Q2.NIDStartsWithZeroException: Person NID: 01234567890 starts with 0 .
Should not start with 0.
Enter 11 or 18 digit NID : 1234567890123456
QuestionsOn.Interface.Q2.NIDLengthMismatchException: Person NID: 1234567890123456 has length 16 .
Should be length 11 or 17.
Enter 11 or 18 digit NID : 12345678901234567
Person NID: 12345678901234567
Enter 11 or 18 digit NID : 1234567890123456a
QuestionsOn.Interface.Q2.NIDHasCharacterException: Person NID: 1234567890123456a contains Alphabetic
Characters.
Should only contain digits.
A) Your goal is to write the following 4 exception class definitions to match the given
output:
InvalidNIDPatternException
NIDHasCharacterException
NIDLengthMismatchException
NIDStartsWithZeroException
Write the class definitions by hand. Do not type your answers. Look at the Exception class
definitions and determine which specific exception class they should extend. You can
create an Exception inheritance hierarchy if you want. Use StringBuffer to concatenate
the strings instead of using String + when generating error messages.
FA20CSE215.10/16
Quiz 4
Handwritten Submission
B) Explain the difference between StringBuilder and StringBuffer and write a few
examples of how Strings can be generated from both. Which is more efficient?
[THIS PAGE IS INTENTIONALLY LEFT BLANK. PLEASE TURN OVER FOR NEXT QUESTION]
FA20CSE215.10/16
Quiz 4
Handwritten Submission
Instructions:
All of these assignments are handwritten. Write your name and ID on the top of every
page in the sheet and create a PDF named Quiz4_1234567.pdf with the scanned
copies. Replace 1234567 with your own 7-digit NSU student ID.
Q3. Rewrite the following method using try with resources.
public static void version1(String fileName)
throws IOException {
File inputFile = new File(fileName);
FileInputStream inputFS = new FileInputStream(inputFile);
Scanner scanner = new Scanner(inputFS);
while (scanner.hasNextLine()){
System.out.println(scanner.nextLine());
}
inputFS.close();
}
A) Rewrite the above method using try with resources. Do not type your answers.
B) Can you initialize multiple resources while using try with resources?
[THIS PAGE IS INTENTIONALLY LEFT BLANK. PLEASE TURN OVER FOR NEXT QUESTION]
Download