This is Java assignment. Please follow the directions and add
screenshot of execution.
Part 2 [Note: Save your successful implementation of Part 1. We are about to extend it!] So the feedback you get from the President is “This is fantastic! But hey, can you folks produce 2 other sorts that list Students first by Gpa within projected graduation year (classOf) and a second one that produces a list by Major within projected graduation year? So your skillful implementation has produced a complement along with some more work for you [This is the real world, folks!] So now you have to strategize on how to do this. The solution that is thought to be best involves creating a field within Student that indicates how it should be ordered. This has a slight drawback but if we want to keep compareTo() compatible with all sort routines that might use it, we have to make a few sacrifices. We can't add a parameter to compareTo( so we embed the info in the Student class itself. In Student.java near the top of the class, add the following public constants: public final static int order_Gpa_Within_Major = 1; public final static int order_Gpa_Within_ClassOf = 2; public final static int order_Major_Within_Class Of = 3; Add a member called ordering which is an integer. Add a getter and setter for it. Also, add another type of getter that examines ordering and returns a string instead of just the integer value. Use the following strings: order_Gpa_Within_Major (1) – return "Gpa Within Major" order_Gpa_Within_ClassOf (2) - return "Gpa Within ClassOf" order_Major_Within_Class Of (3) - return "Major Within ClassOf" The prototype for this function is: public String orderingString(); So now, compareTo() must examine ordering to discover which of 3 orderings to use. Based on that, it performs the test for the selected criteria and returns a negative value, 0 or a positive value. Note: You will probably want to add a Constructor which includes a parameter to set the ordering member. I recommend you keep the original constructor! The new constructor includes the new parameters. Modify the original Constructor to call the new Constructor and use a default value for ordering (i.e. add order_Gpa_Within_Major to the parameter list and call the new Constructor. Move all the assignments that set the other members to the new Constructor. This way, all of the member setting occurs in primarily 1 Constructor and the Constructors rely on each other so you do not duplicate a lot of code! The operation of the class now works as follows: 1. Make sure all Student records in an array have been set to use the same ordering with 2. Call the selection sort. 3. Iterate through and print the students. One added complication. As with the first sort, you want to sequence the printed fields so the sort criteria occurs up front. So toString() should be modified to look at the ordering and return a different string depending on the ordering member.
Iterative Development! This is a great opportunity to work ‘iteratively'. That is, develop a bit and test it. Then develop more and test the new code. Then ‘integrate' the various tested parts into a whole. So I recommend the following: Copy your original Student.java Replace the compareTo() with new code that implements one of the new sorts Replace toString() to generate a string with the correct fields in the front of the text based on the new sort order. Test that code with the original UseStudent.java (basic) Once the second sort and toString() work, Repeat the above writing a 3rd compareTo( [comenting out the old compareTo() for now] to implement the third sort method. Correct the toString0 code as well. Verify the 3rd sort works. Finally, you will now integrate your code into a single Student class. Add the constants given at the start of part 2. Add the member and its getters and setters. Modify your compareTo() code to base its comparisons on the setting in the ordering member. Assume that these will always be consistent between Student objects. Make toString() examine ordering and base its returned string on that value. Test your latest code with the UseStudent2.java file to test your implementation. This will assume interfaces exist the change ordering.
This is Java assignment. Please follow the directions and add screenshot of execution.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am