0% found this document useful (0 votes)
10 views9 pages

G 76 Rfgyb

Uploaded by

aniket23087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

G 76 Rfgyb

Uploaded by

aniket23087
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CSE 201 Advanced Programming

Mid-Sem Exam
Total marks=106

October 08 2024, 3:00 – 4:00 p.m.

Instructions:
o Any form of notes or electronic device not allowed.
o Your responses need to be specific, in terms justification.
o Make necessary assumptions. Ignore syntactic errors unless otherwise mentioned.
o For all questions, the minimum points is zero.
o Once you’re done you need to tag/attach/staple the answer (and supplementary) sheets
with the question paper and return them.
Time allowed: 60 minutes. Total: 100 points
1. [31] Writing a basic class
Marking scheme :Explanation of access modifiers , its scope and also
dataType-> full marks , else half
package ChessOlympiad;
public class ChessOlympiadTeam {

/** Question 1 (4 pts): Add fields for the following properties, which cannot be accessed
outside of the class. name of the team number of wins number of losses *
-2 if any field is not private * -2 if type of any field is wrong **/

private String name;


private int wins;
private int losses;

public class chessOlympiadteam


{
private String name;
private int wins;
private int losses;
}
Explanation of access modifiers -> 2marks
Explanation of scope and dataType-> 2 marks
/** Question 2 (4 pts): Write a constructor that accepts the name of the team, the number of
wins, and the number of losses as arguments and sets the class properties to those values. This
constructor should be accessible from any package*
-1 missing public keyword * -1 wrong parameter types or number * -2 for incorrect body **/

public ChessOlympiadTeam (String name, int wins, int losses) {


this.name = name;
this.wins = wins;
this.losses = losses; }
public ChessOlympiadTeam(String name, int wins, int losses)
{
this.name = name;
this.wins = wins;
this.losses = losses;
}

/** Question 3 (5 pts): Write a second constructor that takes only the name of the team as an
argument. This constructor should set the name of the team to the argument and the set the
number of wins and losses to 0. This constructor should be accessible from any package* -1
missing public keyword *
-1 wrong parameter types or number * -2 for incorrect body **/

public ChessOlympiadTeam (String name) {


this(name, 0, 0);
}

public ChessOlympiadTeam(String name)


{
this(name, 0, 0);
}

/** Question 4 (4 pts): Write methods that return the name of the team, the number of the wins,
and the number of losses. These methods should be accessible from any package. *
-2 if any method has wrong signature * -1 if any method missing * -1 for wrong body(s) **/

public String getName() { return name name; }


public int getWins() { return wins; }
public int getLosses() { return losses; }

public String getName()


{
return name;
}
public int getWins()
{
return wins;
}
public int getLosses()
{
return losses;
}
}

/** Question 5 (4 pts): Next write a method to increase the numbers of wins by 1 and another
method to increase the number of losses by one. These methods should only be accessible from
inside the ChessOlympiad package *
-2 if any method has wrong signature * -2 for wrong body(s) **/

void win() { wins++; }


void lose() { losses++; }

void win()
{
wins++;
}
void lose()
{
losses++;
}

/** Question 6 (4 pts): Write a method that returns true when a team has a “good record,”
meaning the team has more wins than losses. This method should be accessible from any
package. *
-2 for wrong signature * -2 for wrong body **/

public boolean hasGoodRecord() {


return (wins > losses);
}

public boolean hasGoodRecord()


{
return (wins > losses);

/** Question 7 (6 pts): Finally, add a main method to the ChessOlympiadTeam class. In the
main method, construct a ChessOlympiadTeam named "Chakra" with 3 wins and 5 losses. Call
the method that returns true when the team has a good record and print out the result. Now
make three calls to the method that increases the number of wins by 1. Lastly, call the "good
record" method again and print out the result.
* -2 for wrong signature * -4 maximum for failure to follow specifications **/
public static void main(String[] args) {
ChessOlympiadTeam chakra = new ChessOlympiadTeam("Chakra", 3, 5);
System.out.println(chakra.hasGoodRecord());
for (int i = 0; i < 3; i++)
chakra.win();
System.out.println(chakra.hasGoodRecord()); }
}

public static void main(String args[])


{
ChessOlympiadTeam chakra= new ChessOlympiadTeam(“chakra”, 3,5);
system.out.print(chakra.hasGoodRecord());
for(int i=0;i<3;i++)
chakra.win();
system.out.print(chakra.hasGoodRecord());
}
}

2. [25 pts] Interfaces


[i][20 pts]Write a class that implements the Queue interface, as shown below. A queue is a data
structure that accepts data and then returns it in the order in which it was received (first-in,
first-out order). Items are added to the tail of the queue and removed from the head.

public interface Queue


{
public int size(); //Returns number of objects in queue
public boolean isEmpty(); //Returns true if queue is empty

//Adds an item to the tail of the queue


public void enqueue (Object o);

//Removes and returns the item from the head of the queue
public Object dequeue();
}

Ans(2) For mere implementation/Attempt—-------------->6 marks


If only 1 method is implemented(10 marks)
If 2 methods are implemented (13 marks)
If 3 methods are implemented (16 marks)
If all 4 methods are implemented (20 marks)
2[ii][5 marks]Your queue implementation must be accessible and usable from any package.
However, any attempt to extend your class should produce a compile-time error. Your methods
must not throw any exceptions.

A queue may be used as follows:


Sample main method
public static void main(String[] args) {
Queue line = new chakraQueue();
line.enqueue(“Chess”);
line.enqueue(“Olympiad”);
System.out.println(line.dequeue());
System.out.println(line.dequeue());
}

(1)Declare the class as final so that it cannot be extended.


(2)Make the class and its methods public so that they can be accessed from any package.
If both criteria are satisfied then give 5 marks.

3. [20] Exception Handling

public class Differential {


public static void divide (String a, String b) {
String answer = “”;
try {
int dx = Integer.parseInt(a);
int dy = Integer.parseInt(b);
answer = String.valueOf(dx/dy);
} catch (NumberFormatException nfe) {
answer = “bad input”;
} catch (NullPointerException npe) {
answer = “null input”;
} finally {
System.out.print(“answer: “);
}
System.out.println(answer);
}
public static void main(String[] args) {
divide(args[0], args[1]); }
}

(10 pts) Write the output of the following commands or, if the program terminates abruptly, write
the name of the exception that is printed to the console:
Java Differential 18 9 answer: 2 [2.5 pts]
Java Differential 5 null answer: bad input [2.5 pts]
Java Differential 10 answer: division by zero
[2.5 pts]
Java Differential 4 answer: bad input 1 [2.5 pts]

(10 pts) Circle True or False


[True / False] RuntimeException is a subclass of Exception. ->True [2 pts]
[True / False] A method cannot throw both checked and unchecked exceptions->False[2 pts]
[True / False] Exception is a subclass of Error. ->False[2 pts]
[True / False] An error-free class and all of its clients will continue to compile if the clause
“throws RuntimeException” is added to the signature of its constructor. ->True[ 2 pts]
[True / False] An error-free class and all of its clients will continue to compile if the clause
“throws Exception” is added to the signature of its constructor. ->False[2 pts]

4. [30 points] Collections Framework


Write a program that implements a binary search tree using collections like TreeSet or TreeMap.
This should be a binary search tree sorting Student records which could include their names, roll
numbers and CGPAs. You could implement such a class, i.e. Student. The students must be
arranged based on their roll numbers. The program could have methods to create this in-memory
database first. Besides that, you must implement methods to add search, add and delete student
records from the in-memory database.

Points distribution:
1. Student class with appropriate fields: [5pts]
2. Methods to initialize the in-memory data database (using collections): [10pts]
3. Methods to add new records (must use collections): [7.5pts]
4. Methods to search student records (must use collections): [7.5pts]

For attempting the question, give half the marks

1. Student class with appropriate fields: [5pts]

Student class with fields (name, roll number, cgpa) //if any of the field is missing then award 0
class Student
{
String name;
int rollNumber;
double cgpa;
}

2. Methods to initialize the in-memory data database (using collections): [10pts]

private TreeSet<Student> studentSet;


public StudentDatabase()
{
studentSet = new TreeSet<>();
}

OR

private TreeMap<Integer, Student> studentTree;


public StudentDatabase()
{
studentTree = new TreeMap<>();
}

3. Methods to add new records (must use collections): [7.5pts]

//TREE MAP //It must be implemented using put()


public void addStudent(Student student)
{
studentTree.put(student.getRollNumber(), student);
System.out.println("Added: " + student);
}

OR

//TREESET //It must be implemented using add()

public void addStudent(Student student)


{
if (studentSet.add(student))
{
System.out.println("Added: " + student);
}
}

4. Methods to search student records (must use collections): [7.5pts]

//WITH TREEMAP[It must be implemented using get() or foreach()]

public Student searchStudentWithGet(int rollNumber) {


Student student = studentTree.get(rollNumber);
if (student != null) {
System.out.println("Found using get(): " + student);
} else {
System.out.println("Student with roll number " + rollNumber + " not found using get().");
}
return student;
}

OR

public Student searchStudentWithForEach(int rollNumber) {


final Student[] foundStudent = {null}; // To hold the found student
studentTree.forEach((key, student) -> {
if (key == rollNumber) {
foundStudent[0] = student;
}
});

if (foundStudent[0] != null) {
System.out.println("Found using forEach(): " + foundStudent[0]);
} else {
System.out.println("Student with roll number " + rollNumber + " not found using
forEach().");
}
return foundStudent[0];

OR
//TREESET [It must be implemented using contains()]
public Student searchStudent(Student student) {

if (studentSet.contains(student)) {

You might also like