CSE1115_Final_163
CSE1115_Final_163
1. Find out if the following JAVA programs have any error. List the errors if any. Fix the code and rewrite
after the list. You cannot delete any line of code. However, you are allowed to edit or add any code as per
requirement. [2+3+3]
String c;
while ((c = in.readLine()) != null) {
out.writeLine(c);
}
} finally {
in.close();
out.close();
}
}
}
2. Write a JAVA program which will read from three (3) files: firstnames.txt, middlenames.txt, last-
names.txt. All these files have equal number of lines, each line in these files represent the first name, middle
name and the last name of a person. Now, your code should create a new file named fullnames.txt and write
the full names of the persons in the following format: first name<space>initials of middle name<space>last
name. Three input files may look like as shown below: [8]
Your code should generate the output file in the following manner:
3. Write a JAVA program (Application.java) which takes four (4) inputs from users (keyboard): name, age, cell
phone number and email address; writes these information in one file (output.txt). However, your program
should throw the following custom made exceptions if respective conditions fail: EmptyNameException if
name is empty, UnderAgeException if age is less than 18, InvalidCellException if cell phone number is
less that 11 digits and does not start with a 0 (zero), and InvalidEmailException if email address does not
maintain the format: <string>@<string>.<string>.
Note: You need to write these four exception classes as well and throw exceptions properly from Application
class. However, you don’t need to catch these exceptions anywhere. [8]
4. (a) Write a simple counter application which user interface is given bellow.
In this application, counter value will appear in the text box. When a user clicks the up button the
counter value will be increased, and the value will be decreased when the user will press the down button.
If the user press reset button, then the counter value in the text box will be set to zero. Importantly,
you have to give some necessary conditions so that neither the counter will have a negative value nor a
value greater than 100. If this terminal condition occurs, you have to give some warning to the user via
showing a message in a label or using a dialog. [5]
(b) Consider the following program, which was written to sort a list of book according to their price(from
higher to lower price). There is a problem in the following code, which you need to resolve. However,
you are only allowed to edit the Book class to solve the problem. [3]
Collections.sort(books);
for(Book book : books){
System.out.println(book.name+", Price"+book.price);
}
}
}
class Book{
public String name;
public double price;
5. Consider a server program, which is running on a computer and can read a list of student name with their
CGPA whenever a client connects to the server. After that server send the name and CGPA of a student to
that particular client who has the highest CGPA in that student list. Please note that this server program
can handle multiple clients; however, it is not necessary that server has to handle these clients parallelly.
Unfortunately, a group of hackers, named BABU007, attack the server and erase the whole server program.
Fortunately, we recover the client program which is given bellow. Now, as our lead developer is on vacation,
we are seeking for your help to write down the server program by carefully observing the client program. If
your server program can handle multiple clients properly then your teacher will give eight marks; however, if
you can handle single client only, then you will get five marks as rewards. [8]
for(int i=0;i<studentName.length;i++){
out.println(studentName[i]);
out.println(studentCgpa[i]);
}
out.println("end");
System.out.println(highestCgpa);
System.out.println(highestCgpaStudentName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
6. (a) Consider the following program, which is initialized a Student type GenQueue object.
while(studentQueue.hasItems()){
Student st = studentQueue.dequeue();
System.out.println(st.name+" "+st.cgpa);
}
}
}
class Student{
public String name;
public double cgpa;
In this generalized queue we can enqueue and dequeue object which type is determined during initial-
ization. Now, unfortunately, we lost the GenQueue class due to the server hacks. Now you have to write
a GenQueue class which must have three methods enqueue, dequeue and hasItems. Carefully observe
the above code to determine the return type, and the parameter of these methods. Please note that the
GenQueue must be a generic class so that it can handle any type of object. [5]
(b) Please complete the following incomplete program. [3]
7. (a) Write a program in JAVA where two different threads will execute in parallel and print even and odd
numbers respectively ranging from 1 to 100. Note that synchronization between these two threads are
not necessary. Your program may generate scrambled output like: 1 2 4 6 3 5 7 9 11 13 8 15 ... [3]
(b) Now, rewrite the same program of 7(a) to synchronize the two threads so that they print the numbers
in correct order: 1 2 3 4 5 6 ... 99 100. [3]
(c) Rewrite the program of 7(b) such that the thread which prints even numbers will print two even numbers
followed by one odd number. Thus output of your program should be: 1 2 4 3 6 8 5 10 12 7 ... 97 99. [2]