Assignment 4 - Data Structures Due: May 2, 23:59 PT Overview This assignment includes 3 parts, covering Lesson 2 - Lesso
Posted: Thu May 19, 2022 2:27 pm
Assignment 4 - Data Structures
Due: May 2, 23:59 PT
Overview
This assignment includes 3 parts, covering Lesson 2 - Lesson
22.
Part 1 & 2 require you to submit your code on Gradescope for
auto-grading. Therefore, you need to pay
close attention to the specifications for the autograder to grade
your code. And similar to previous
assignments, I suggest you put both parts under ONE project so that
it’s easier to submit.
For Part 3, you don’t need to submit your code, so you can organize
the project anyway you want. Instead,
you need to do live demo during the code meeting and explain your
code.
Part 1. Use Collection Framework (3 pts)
This part includes 3 subparts (1 pt each). Each subpart includes
one method that involves one data
structure. Your job also includes choosing an appropriate data
structure and method(s) to use, according
to lesson 20 & 21. You would get 0 on the subpart if you used
the wrong data structure or the wrong
method(s). Note that the test cases may not catch this kind of
error (especially for subpart 3), so the
actual points will be based on the result of code meeting.
Specifications
• Package name: edu.sjsu.assignment4
• Class name: MyCollections
• Method names (ordered by subparts):
o printFiles
o downsize
o isBalanced
1.1 Simulate a printer to print files
Implement a public static method to print a collection of files.
The method prints the files from the
first to the last. For each file, print its content, then delete it
from the collection, so that the next file
become the first file. Use 3 consecutive *s to indicate the end of
the file content. You can have another
method for printing the content for clearer code. (Hint: use a
while loop - while the collection is not empty)
The test cases won't test if the file is not found, so you can
print any kind of message in "catch".
The parameter should be Collection<File>, where Collection
should be a specific type of data structure
based on the requirement. And you should use the methods that’s
most effective.
See example on next page.
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 2 of 6
For example, if files is a collection of files, and it contains
[file1, file2] where file1’s content is:
Hello, welcome to class!
And file2’s content is:
Assignment 4 is posted.
Please submit it before due date.
Then after calling printFiles(files), files will become empty and
the console will display the following:
Hello, welcome to class!
***
Assignment 4 is posted.
Please submit it before due date.
***
1.2 Downsize an employee collection
Implement a public static method to remove every nth element of a
collection of employee names.
The method returns nothing. Please use a list iterator to go over
the collection and remove
elements. Hint: you can have an int to keep track the original
indexes.
The parameter should be Collection<String> and an int, where
Collection should be a specific type of data
structure that is efficient to remove/add elements in the middle
but not to access elements arbitrarily.
For example, suppose employees is a collection of String, and it
contains:
["Tom", "Bob", "Cathy", "Alice", "Trudy", "Harry", "Denny"]
Then calling downsize(employees, 3) will remove every 3rd element
and employees becomes:
["Tom", "Bob", "Alice", "Trudy", "Denny"]
1.3 Check if the parentheses are balanced
Implement a public static method to check if a String has balanced
parentheses. For simplicity,
suppose we only have ( ) and [ ]s, not { }s. You don't need to
evaluate if the expression is valid.
Use the approach demoed in class (lesson 21). You should only use
the methods that are in the
required data structure class.
Examples (there can be other symbols between parentheses)
• isBalanced("System.out.println(list.get(0))") should return
true;
• isBalanced("[](3*5)^2") should return true;
• isBalanced("[(2+3]*5)^2") should return false (wrong close
parenthesis);
• isBalanced("System.out.println(list.get(0)") should return false
(missing close parenthesis);
• isBalanced("[(3*5)]^2]") should return false (missing open
parathesis);
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 3 of 6
Part 2. Gradebook (4.5 pts)
This part has two subparts for two classes related to each
other.
Specifications
• Package name: edu.sjsu.assignment4
• Class names (ordered by subparts):
o Student
o Gradebook
2.1 Set up Student class (0.5 pt)
Set up the Student class as following. You will need to implement
more methods to complete 2.2.
• (private) Instance variables
o int id
o String name
• 2 Constructors (Alt + Enter and pick constructor to
generate)
o Only takes the id. Set the name as "Unnamed".
o Takes the id and name.
• 2 Getters (Alt + Enter and pick Getter generate)
o public int getId()
o public String getName()
2.2 Gradebook class (4 pts)
The Gradebook class manages a collection of students. The students
are stored in a map, where the keys
are the student objects, and the values are the student's grade as
a Character. The map you use should
be efficient for lookups but don't care about the order. Using the
wrong map will result in 0 in this
subpart even if you passed all other test cases.
Once you decide which map to use, update the Student class so that
the map will treat the students
with the same ID as the same student. Then, you can either use the
map as an underlying structure of
your Gradebook object or extends that map class. Finally, implement
the following:
• Constructor: takes nothing. Just initialize an empty map. If your
Gradebook extends the map class,
you don't need to explicitly write the constructor. Wrong
constructer may result in 0 in this question
since a gradebook object must be constructed for the further
tests.
• Methods
o public boolean addStudent(Student student, char grade) (0.75
pt)
Add the student and grade to the map and return true if added
successfully. If the student already
exists, do NOT put it to the map and return false. If the grade is
not valid (not A, B, C, D, F, or N),
add the student but set the grade to 'N' and still return
true.
o public boolean addStudent(Student student) (0.25 pt)
Add the student to the map. The grade should be 'N'. Other rules
are the same as
addStudent(Student student, char grade). Do you need to implement
the logic again?
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 4 of 6
o public boolean deleteStudent(int id) (0.75 pt)
Delete the corresponding student with the id from the map. Return
true if removed successfully;
return false if the student is not found.
o public boolean updateStudent(int id, String newName) (0.75
pt)
Update the name of the corresponding student with the id from the
map to newName. The grade
does not change. Return true if updated successfully. If the
student is not found, return false.
o public boolean updateGrade(int id, char newGrade) (0.75 pt)
Update the grade of the corresponding student with the id from the
map to newGrade. The name of
the student does not change. Return true if the grade is updated
successfully. If the student is not
found, return false; if the new grade is not valid (not A, B, C, D,
F or N), do NOT change the grade
and return false.
o public void printGrades(Comparator<Student> comparator)
(0.75 pt)
Print the students with the grade (Hint: you may implement toString
in Student class), order based
on the comparator. The format will be "id.name: grade" for each
student as a line. Note that there's
a space between : and grade. Example (suppose comparator sorts the
students by ID):
1.Bob: B
2.Alice: A
Note that, except for printGrades, you should NOT go through the
whole map to find the student.
Part 3. Appointment Program (2.5 pts)
This part has 2 subparts related to each other. It uses the classes
you implemented in Assignment 3.
3.1 Implement an AppointmentManager class
Implement an AppointmentManager class to manage a collection of
appointments. Pick an appropriate
data structure base on the methods' requirements below. Once you
picked a data structure, you may need
to update the Appointment class so that the data structure will
treat the appointments with the same
description as the same appointment. Then, you can either use the
data structure you picked as an
underlying structure of your AppointmentManager object or extends
that data structure class. As long as it
supplies the following 3 methods:
• A method that can add an appointment to the collection.
It should not accept any duplicates (i.e., no appointment with the
same description can be added).
• A method that can delete an appointment.
Since the description of the appointments are identical, you can
use it as a key to find the appointment
to delete, instead of pass in an appointment object. But the design
is up to you.
• A method that can print all appointments in their natural
order.
The format is up to you, as long as the output is sorted by
appointment's natural order.
Hint: You may want to implement the toString method in Appointment
class.
You can define the return type, method name and parameter. You can
add other methods to help you
implement the 3 core methods. During the code meeting, please
explain why you chose this data structure
(or why you didn't choose another), why you choose this parameter,
etc.
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 5 of 6
3.2 Appointment Program
Write a program that give user options to add an appointment,
delete an appointment or display all
appointments. Remember to take care of invalid user input, such as
wrong format of date, no such
appointment, etc. Your program should not crash because of invalid
user input.
Here is an example (User input is italic and in color). Again, you
can have different (even simpler) design.
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: i
Sorry, wrong option!
Please enter again: d
Sorry, no appointment to delete, please add an appointment
first!
Please enter again: a
Please enter the type (onetime, daily or monthly): monthly
Please enter a description: code meeting
Please enter the starting date (yyyy-mm-dd): 2021-06-01
Please enter the starting date (yyyy-mm-dd): 2021-08-01
Appointment added!
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: v
Code meeting, from 2021-06-01 to 2021-08-01, monthly
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: d
Please enter the description of the appointment you want delete:
code
Sorry, appointment not found! please enter again!
Please enter the description of the appointment you want delete:
code meeting
Appointment deleted!
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: v
No any appointment now.
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: q
Thank you for using the program. Have a nice day!
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 6 of 6
Grading (for part 3)
• 3.5 pts (1 pt extra) if your program/code exceed my
expectations
• 2.5 pts (full points) if your program works and won’t crash
because of incorrect user input
• 1.25 pt (half) as long as you tried
Extra Credit (2 pts)
You can either include in your submission for below on Gradescope,
or show the instructor during the
code meeting. For example, you can leave the URL of your Javadoc as
a comment in the code.
• 0.5 pt Provide documentation comments for all 3 classes
(MyCollections, Student & Gradebook).
• 0.5 pt Generate Javadoc API documentation and post it on a
website (not just locally!
Must be accessible to other people, can use GitHub pages as shown
in lesson 16).
• 0.5 pt Test methods in MyCollections using Junit test.
• 0.5 pt Test methods in Gradebook using Junit test.
Due: May 2, 23:59 PT
Overview
This assignment includes 3 parts, covering Lesson 2 - Lesson
22.
Part 1 & 2 require you to submit your code on Gradescope for
auto-grading. Therefore, you need to pay
close attention to the specifications for the autograder to grade
your code. And similar to previous
assignments, I suggest you put both parts under ONE project so that
it’s easier to submit.
For Part 3, you don’t need to submit your code, so you can organize
the project anyway you want. Instead,
you need to do live demo during the code meeting and explain your
code.
Part 1. Use Collection Framework (3 pts)
This part includes 3 subparts (1 pt each). Each subpart includes
one method that involves one data
structure. Your job also includes choosing an appropriate data
structure and method(s) to use, according
to lesson 20 & 21. You would get 0 on the subpart if you used
the wrong data structure or the wrong
method(s). Note that the test cases may not catch this kind of
error (especially for subpart 3), so the
actual points will be based on the result of code meeting.
Specifications
• Package name: edu.sjsu.assignment4
• Class name: MyCollections
• Method names (ordered by subparts):
o printFiles
o downsize
o isBalanced
1.1 Simulate a printer to print files
Implement a public static method to print a collection of files.
The method prints the files from the
first to the last. For each file, print its content, then delete it
from the collection, so that the next file
become the first file. Use 3 consecutive *s to indicate the end of
the file content. You can have another
method for printing the content for clearer code. (Hint: use a
while loop - while the collection is not empty)
The test cases won't test if the file is not found, so you can
print any kind of message in "catch".
The parameter should be Collection<File>, where Collection
should be a specific type of data structure
based on the requirement. And you should use the methods that’s
most effective.
See example on next page.
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 2 of 6
For example, if files is a collection of files, and it contains
[file1, file2] where file1’s content is:
Hello, welcome to class!
And file2’s content is:
Assignment 4 is posted.
Please submit it before due date.
Then after calling printFiles(files), files will become empty and
the console will display the following:
Hello, welcome to class!
***
Assignment 4 is posted.
Please submit it before due date.
***
1.2 Downsize an employee collection
Implement a public static method to remove every nth element of a
collection of employee names.
The method returns nothing. Please use a list iterator to go over
the collection and remove
elements. Hint: you can have an int to keep track the original
indexes.
The parameter should be Collection<String> and an int, where
Collection should be a specific type of data
structure that is efficient to remove/add elements in the middle
but not to access elements arbitrarily.
For example, suppose employees is a collection of String, and it
contains:
["Tom", "Bob", "Cathy", "Alice", "Trudy", "Harry", "Denny"]
Then calling downsize(employees, 3) will remove every 3rd element
and employees becomes:
["Tom", "Bob", "Alice", "Trudy", "Denny"]
1.3 Check if the parentheses are balanced
Implement a public static method to check if a String has balanced
parentheses. For simplicity,
suppose we only have ( ) and [ ]s, not { }s. You don't need to
evaluate if the expression is valid.
Use the approach demoed in class (lesson 21). You should only use
the methods that are in the
required data structure class.
Examples (there can be other symbols between parentheses)
• isBalanced("System.out.println(list.get(0))") should return
true;
• isBalanced("[](3*5)^2") should return true;
• isBalanced("[(2+3]*5)^2") should return false (wrong close
parenthesis);
• isBalanced("System.out.println(list.get(0)") should return false
(missing close parenthesis);
• isBalanced("[(3*5)]^2]") should return false (missing open
parathesis);
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 3 of 6
Part 2. Gradebook (4.5 pts)
This part has two subparts for two classes related to each
other.
Specifications
• Package name: edu.sjsu.assignment4
• Class names (ordered by subparts):
o Student
o Gradebook
2.1 Set up Student class (0.5 pt)
Set up the Student class as following. You will need to implement
more methods to complete 2.2.
• (private) Instance variables
o int id
o String name
• 2 Constructors (Alt + Enter and pick constructor to
generate)
o Only takes the id. Set the name as "Unnamed".
o Takes the id and name.
• 2 Getters (Alt + Enter and pick Getter generate)
o public int getId()
o public String getName()
2.2 Gradebook class (4 pts)
The Gradebook class manages a collection of students. The students
are stored in a map, where the keys
are the student objects, and the values are the student's grade as
a Character. The map you use should
be efficient for lookups but don't care about the order. Using the
wrong map will result in 0 in this
subpart even if you passed all other test cases.
Once you decide which map to use, update the Student class so that
the map will treat the students
with the same ID as the same student. Then, you can either use the
map as an underlying structure of
your Gradebook object or extends that map class. Finally, implement
the following:
• Constructor: takes nothing. Just initialize an empty map. If your
Gradebook extends the map class,
you don't need to explicitly write the constructor. Wrong
constructer may result in 0 in this question
since a gradebook object must be constructed for the further
tests.
• Methods
o public boolean addStudent(Student student, char grade) (0.75
pt)
Add the student and grade to the map and return true if added
successfully. If the student already
exists, do NOT put it to the map and return false. If the grade is
not valid (not A, B, C, D, F, or N),
add the student but set the grade to 'N' and still return
true.
o public boolean addStudent(Student student) (0.25 pt)
Add the student to the map. The grade should be 'N'. Other rules
are the same as
addStudent(Student student, char grade). Do you need to implement
the logic again?
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 4 of 6
o public boolean deleteStudent(int id) (0.75 pt)
Delete the corresponding student with the id from the map. Return
true if removed successfully;
return false if the student is not found.
o public boolean updateStudent(int id, String newName) (0.75
pt)
Update the name of the corresponding student with the id from the
map to newName. The grade
does not change. Return true if updated successfully. If the
student is not found, return false.
o public boolean updateGrade(int id, char newGrade) (0.75 pt)
Update the grade of the corresponding student with the id from the
map to newGrade. The name of
the student does not change. Return true if the grade is updated
successfully. If the student is not
found, return false; if the new grade is not valid (not A, B, C, D,
F or N), do NOT change the grade
and return false.
o public void printGrades(Comparator<Student> comparator)
(0.75 pt)
Print the students with the grade (Hint: you may implement toString
in Student class), order based
on the comparator. The format will be "id.name: grade" for each
student as a line. Note that there's
a space between : and grade. Example (suppose comparator sorts the
students by ID):
1.Bob: B
2.Alice: A
Note that, except for printGrades, you should NOT go through the
whole map to find the student.
Part 3. Appointment Program (2.5 pts)
This part has 2 subparts related to each other. It uses the classes
you implemented in Assignment 3.
3.1 Implement an AppointmentManager class
Implement an AppointmentManager class to manage a collection of
appointments. Pick an appropriate
data structure base on the methods' requirements below. Once you
picked a data structure, you may need
to update the Appointment class so that the data structure will
treat the appointments with the same
description as the same appointment. Then, you can either use the
data structure you picked as an
underlying structure of your AppointmentManager object or extends
that data structure class. As long as it
supplies the following 3 methods:
• A method that can add an appointment to the collection.
It should not accept any duplicates (i.e., no appointment with the
same description can be added).
• A method that can delete an appointment.
Since the description of the appointments are identical, you can
use it as a key to find the appointment
to delete, instead of pass in an appointment object. But the design
is up to you.
• A method that can print all appointments in their natural
order.
The format is up to you, as long as the output is sorted by
appointment's natural order.
Hint: You may want to implement the toString method in Appointment
class.
You can define the return type, method name and parameter. You can
add other methods to help you
implement the 3 core methods. During the code meeting, please
explain why you chose this data structure
(or why you didn't choose another), why you choose this parameter,
etc.
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 5 of 6
3.2 Appointment Program
Write a program that give user options to add an appointment,
delete an appointment or display all
appointments. Remember to take care of invalid user input, such as
wrong format of date, no such
appointment, etc. Your program should not crash because of invalid
user input.
Here is an example (User input is italic and in color). Again, you
can have different (even simpler) design.
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: i
Sorry, wrong option!
Please enter again: d
Sorry, no appointment to delete, please add an appointment
first!
Please enter again: a
Please enter the type (onetime, daily or monthly): monthly
Please enter a description: code meeting
Please enter the starting date (yyyy-mm-dd): 2021-06-01
Please enter the starting date (yyyy-mm-dd): 2021-08-01
Appointment added!
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: v
Code meeting, from 2021-06-01 to 2021-08-01, monthly
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: d
Please enter the description of the appointment you want delete:
code
Sorry, appointment not found! please enter again!
Please enter the description of the appointment you want delete:
code meeting
Appointment deleted!
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: v
No any appointment now.
(a) for adding an appointment;
(d) for deleting an appointment;
(v) for viewing all appointments;
(q) for quitting the program
Please choose an option: q
Thank you for using the program. Have a nice day!
Yan Chen CS 49J Summer 2021 Assignment 4
SJSU, Department of Computer Science Page 6 of 6
Grading (for part 3)
• 3.5 pts (1 pt extra) if your program/code exceed my
expectations
• 2.5 pts (full points) if your program works and won’t crash
because of incorrect user input
• 1.25 pt (half) as long as you tried
Extra Credit (2 pts)
You can either include in your submission for below on Gradescope,
or show the instructor during the
code meeting. For example, you can leave the URL of your Javadoc as
a comment in the code.
• 0.5 pt Provide documentation comments for all 3 classes
(MyCollections, Student & Gradebook).
• 0.5 pt Generate Javadoc API documentation and post it on a
website (not just locally!
Must be accessible to other people, can use GitHub pages as shown
in lesson 16).
• 0.5 pt Test methods in MyCollections using Junit test.
• 0.5 pt Test methods in Gradebook using Junit test.