Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This question is a repeat from Lab Exercise 1. Here, along with the header and footer methods, you need to add a static method.
This question is a repeat from Lab Exercise 1. Here, along with the header and footer methods, you need to add a static method. The Question: Create a data structure that will keep a record of key and values. Here the key will be the year of studies of a group of university students, and the values will be their first names. These students are the leaders in the University Student Council. Your task would be to find out how many student leaders belong to a specific year and get a leader- representative from each year and print the names of those representatives. (a) Define a generic class called Pair with the following specifications (see the class diagram below): [Hint: this code is given in the class handout] Two private data fields: key Y and value N. Constructor with both Y and N parameters. Getter and setter methods for both the data fields. Pair - key: Y - value: N + Pair(key: Y, value: N) + setKey (key: Y): void + setvalue (value: N): void +getKey():Y +getValue(): N (b) Create a public static void header method (with void parameter list) with the following information (see the sample output): Names: firstNamel and firstName2. (Hardcode it; meaning put the data manually) Student Numbers: studentNumber] and studentNumber2 (Hardcode it) Goal of this project: Brief description of the project. (Hardcode it) (c) Create a public static void footer method (with void parameter list) with the following Message (see the sample output): This is timeOfDay on theDate. Completion of Lab Assignment 1 is successful! Good bye! yourFullName. (d) Write a static method public static Pair[] getRep yourFirstName(Pair[] pa) that will return a pair containing student representative along with the corresponding year, from each year from the Pair list. Hint: You can create two ArrayList lists with appropriate tags and populate those arrayLists based on the ArrayList method called contains. (example: if the name of the arrayList is myArray, then if myArray.contains(get the year from Pair object reference variable) is false, populate myArray) (e) Define the driver method and do the following (Check the class handouts): i) ii) iii) iv) Call the header method. Declare an ArrayList type reference variable with Integer-tag and fill out the list with integer values 2, 3, 4, 3, 2, 2 with the aid of Array.asList(valuel, value2..) method as shown below. These numbers will represent the year of studies of the student leaders, Note that both ArrayList and Arrays classes are available in java.util.* package. ArrayList anyValidName = new ArrayList(Arrays.asList(2,3,....)); Declare a second ArrayList type reference variable with String-tag and fill out the list with String values Harry, Lavender, Ron, Hermione, Luna and Vincent with the aid of Array.asList(..) method. These string values represent the names of the student leaders. (FYI: Based on both the Lists, Harry is in 2nd year, Lavender is in 3rd year and so on). Create an array of size any ValidName.size() of Pair type reference variables. v) vi) vii) viii) ix) x) xi) Populate this Pair-array by the key and the corresponding value pairs using the two ArrayList reference variables with the help of the getter methods (Slide 16, Unit 1- P2) from the ArrayList class. Now ask the user the following question: "From which academic year would you like to list the names of the leaders: " Validate that the user enters either 2 or 3 or 4 (see the sample output). Now based on the user's choice print the names of the leader(s) from that specific year. Keep asking the user till she/he does not want to continue. When the user press 'n', your program will call the getRep_yourFirstName() method from main, and then based on the returned values, print the list of the student's rep from each year (see the sample output). Call the footer method. Sample output: ***** Names: firstName1 and firstName2. Student Numbers: studentNumber1 and studentNumber2 Goal of this project: Brief description of the project. ***** From which year you are looking for the names of the student leaders: 1 Invalid entry! Enter a positive integer between 2 and 4: dsfsdfas Invalid entry! Enter a positive integer between 2 and 4:5 Invalid entry! Enter a positive integer between 2 and 4: f Invalid entry! Enter a positive integer between 2 and 4: 2 We found 3 students from year 2 and they are: Harry Luna Vincent Do you want to continue? [y or n]: y From which year you are looking for the names of the student leaders: 5 Invalid entry! Enter a positive integer between 2 and 4: r Invalid entry! Enter a positive integer between 2 and 4:3 We found 2 students from year 3 and they are: Lavender Hermione Do you want to continue? [y or n]: n Here is the list of the class-reps..... 2- Harry 3- Lavender 4- Ron ******* This is timeOfDay on theDate. Completion of Lab Assignment 1 is successful! Good bye! yourFullName. *****
Step by Step Solution
★★★★★
3.29 Rating (158 Votes )
There are 3 Steps involved in it
Step: 1
To solve this lab exercise youll first define a generic class called Pair to store keyvalue pairs Then youll create header and footer methods as well as a static method to get student representatives ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started