Question: please do not change any of the starter code Once parameterized, the type T can now be used in the body of the class, which

 please do not change any of the starter code Once parameterized,

the type T can now be used in the body of the

class, which will take on whatever type instances of Foo are instantiated

please do not change any of the starter code

with. Once a parameterized class has been defined, the following syntax can

be used to instantiate an instance of Foo parameterized with an integer

type: Parameters can be bounded using the extends keyword. For example will

match any type T, but T must be a subclass of Bar.

When you use a variable to represent your type, this is known

Once parameterized, the type T can now be used in the body of the class, which will take on whatever type instances of Foo are instantiated with. Once a parameterized class has been defined, the following syntax can be used to instantiate an instance of Foo parameterized with an integer type: Parameters can be bounded using the extends keyword. For example will match any type T, but T must be a subclass of Bar. When you use a variable to represent your type, this is known as a named parameter and is used when you need to refer to the type (such as when you declare a variable, method parameter, or method return type that needs to match T ). However, if you don't need to refer to the type, it is better to use the wildcard, ? instead of a specific type. The wildcard matches any type and can still be used in conjunction with the a subtype of Bar " (using the syntax It is also possible to use generics of generics. For example, the following creates an that is parameterized to only hold objects of type Foo that have themselves been parameterized with a Double : List Foo Double >baz= new ArrayList Foo Double >(); Several classes have been provided in the project to model Undergraduate students, students (both of which are subclasses of Student), and Droid (which are not students). Also included are Course s, which may have Section (s). These classes are intended to model an enrollment system based on the following rules: - Each Section should only hold student types and each Section should only consist of one type of Grads and undergrads should not be able to enroll in the same section. - Each should be able to hold any number of sections, but those sections should conform to the previous rules. Follow the instructions below to parameterize your classes to conform to these rules and to understand the 3.3.1. Parameterizing a class 1. Open the Course, and Section class files. Two instances of each entity ( Undergraduate , Droid) have been created for you (feel free to modify or add if you wish). 2. In the class create a section and enroll all 6 instances to this section. Problem: Observe that since the Section class is not parameterized (it uses raw types) we are able to add any of these three types into the same section, violating rule 1 above. Solution: Parameterize the Section class so that each section can only hold one type of object. Update the rest of the class to use this type as needed. Observe that in the code you wrote should now be giving you warnings: the class is now parameterized so you should parameterize instances when you create them. An example: Section> ugradSection = new Section>("001 "); Modify your code in the to create three separate sections (one for and Droid) and add the entities to the appropriate section instead. 3.3.2. Parameterizing with a Bound Problem: The Course class still allows us to add sections consisting of non-student objects Solution A: we could parameterize the Course class just like we did with the Section class to only hold Section (s) of a particular type. Try this solution. New Problem: You will find that you are not able to add sections holding different types. For example, you will not be able to add a section of Graduates and only one or the other. Explain (to yourself or partner) why this is the case. Solution B: We want the Course class to be able to hold Section (s) of any type of student. Remove the parameterizations you did in Solution A and instead of parameterizing the class, parameterize the list so that it can only hold sections containing objects which must be a subtype of student (hint: use the wildcard). Parameterize the addsection method so that it only accepts sections of one type of student (and so that no warnings or compiler errors are issued). If your changes were correct, then adding the section of Droid s should no longer be allowed. 3.3.3. Parameterizing a class with a bound Problem: though we can no longer add a section to a course with non-student objects, we are still able to instantiate a section with non-student objects Solution: parameterize the class so that it can only hold one type of object and that type of object must be a subtype of Course.java: Here are all of the additional code if needed, Droid.java: Graduate.java: Undergraduate.java

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!