Answered step by step
Verified Expert Solution
Question
1 Approved Answer
DATA STRUCTURES- JAVA PROGRAMING. PLEASE PROVIDE THE CODES FOR SOLUTION. [PLEAE NOTE THAT THE QUESTION IS DESCRIBED IN 2 DIFFERENT LANGUAGES, MALAY AND ENGLISH, PLEASE
DATA STRUCTURES- JAVA PROGRAMING. PLEASE PROVIDE THE CODES FOR SOLUTION. [PLEAE NOTE THAT THE QUESTION IS DESCRIBED IN 2 DIFFERENT LANGUAGES, MALAY AND ENGLISH, PLEASE REFER TO ONE OF THEM]
PetaPagar merupakan struktur yang menyimpan data dalam bentuk pasangan- pasangan kekunci dan nilai. Jadual 4 menunjukkan metod- metod lazim yang digunapakai dalam satu implementasi PetaPagar. HashMap is a structure that stores data in the form of key and value pairs. Table 4 shows the common methods utilized in an implementation of the HashMap Jadual 4: Senarai metod-metod dan spesifikasi untuk PetaPagar Table 4: List of methods and their specifications for HashMap Pembina/Nama Metod Spesifikasi Constructor/Method name Specification ) Constructor for the Konstruktor lalai yang menerima parameter- HashMap class parameter kekunci dan nilai pasangan Default constructor which accepts Key and value pair parameters ii) get Memulangkan entry yang dipetakan kepada kekunci di dalam peta-pagar Return the entry mapped to key in the HashMap ti) put Menambah entry baru sekiranya kekunci belum lagi dipetakan di dalam peta-pagar. Sebaliknya, mengemaskini entry lama sekiranya kekunci telah wujud di dalam peta-pagar. Add new entry if Key is not yet mapped into the HashMap. Otherwise, update entry mapped to Key if the HashMap already contains the key Rajah 4 menunjukkan satu aplikasi PetaPagar yang menetapkan kekunci dengan nama pelajar dengan pasangan nilainya merujuk kepada kursus yang sedang diambil di universiti. Figure 4 shows an application of the HashMap that fixes key with student name and pair its value with the course taken at the university. * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates and open the template in the editor. package exam2020; @author IBM public class MyHashMap { // for better re-sizing is taken as 244 private static final int SIZE = 16; private Entry table[] = new Entry[SIZE]; * To store the Map data in key and value pair. * Used linked list approach to avoid the collisions class Entry final String key; String value; Entry next; // object of class Entry to traverse the map Entry(String k, String v) { key=k; value = v; public String getValue() { retum value; public void setValue(String value) { this.value = value; } public String getKey0 { retum key; public Entry get(String k) { int hash=k.hashCode() % SIZE; Entry e = table[hash]; // Bucket is identified by hashCode and traversed the bucket // till element is not found. while(e != null) { iffe.key.equals(k)) { return e; e=e.next; } return null; * If the map previously contained a mapping for the key, the old * value is replaced public void put(String k, String v) { int hash=k.hashCode() % SIZE; Entry e = table[hash]; ifle != null) // If we will insert duplicate key-value pair, // Old value will be replaced by new one. if(e.key.equals(k)) { e.value = y; } else { // Collision: insert new element at the end of list // in the same bucket while(e.next != null) { e=e.next; } Entry entryInOldBucket = new Entryk, v); e.next = entryInOldBucket; } } else // create new bucket for new element in the map. Entry entryInNewBucket = new Entry(k, v); table[hash] = entryInNewBucket; public static void main(String[] args) { MyHashMap myHashMap = new MyHashMapO; // New entry myHashMap.put("Bruce W", "SE"); myHashMap.put("Dean W", "AI"); myHashMap.put("TonyS", "IS"); myHashMap.put("LaraC", "CST"); System.out.println(""); System.out.println("Retrieving student data: "); Entry el = myHashMap.get("Dean W"); System.out.println("+el.getKey(+"." + el.getValue()); Entry e2 = myHashMap.get("Bruce W"); System.out.println("" +22.getKey(+":" + c2.getValue()); System.out.println(""); // Updating record myHashMap.put("Bruce W", "CST"); myHashMap.put("JeanG", "SE"); System.out.println("Retrieving student data: "); Entry e3 = myHashMap.get("BruceW"); System.out.println("" + c3.getKey0 + ":" + c3.getValue(); Entry c4 = myHashMap.get("JeanG"); System.out.println("" + 04.getKey(+": +4.getValue()); Kemaskini metod-metod dalam aplikasi PetaPagar di dalam Rajah 4 tersebut supaya aplikasi tersebut dapat menerima nombor matriks ataupun alamat emel pelajar sebagai kekunci. Pastikan juga pasangan nilai untuk setiap kekunci boleh menyimpan sebarang data-data pelajar yang lain seperti nombor telefon dan tahun pendaftaran. Untuk markah penuh, aplikasi PetaPagar anda sepatutnya boleh di compile dan run walaupun kekunci yang dicari tidak wujud. Rajah 5 menunjukkan contoh paparan bagi aplikasi yang telah dikemaskini. Update the methods for the HashMap application in Figure 4 so the application can accept the student's matrix number or email address as key. Please make sure the pairing value for each key can store any other student data such as phone number and year of registration. Your Hashiap application should compile and runs sucessfully even though an unknown key is being searched, for a full mark. Figure 5 shows an example output for the updated application. run: Retrieving student data: DeanW : AI, 017-449 2272, 2020 bruce@gmail.com : SE, 011-559 8900, 2018 17110022: CST, 013-4509 722, 2018 Retrieving student data: bruce@gmail.com: CST, 011-559 8900, 2018 JeanG: SE, 014-234 9906, 2020 BUILD SUCCESSFUL (total time: 0 seconds) Rajah 5: Contoh paparan untuk aplikasi yang telah dikemaskini Figure 5: Example output for the updated application Markah tidak akan diberikan bagi apa-apa pelaksanaan menggunakan kelas HashMap atau mana-mana kelas Collection yang sedia ada didapati daripada Java API Library No marks will be given for any implementation using the existing HashMap class or any other Collection classes available from the Java API Library. PetaPagar merupakan struktur yang menyimpan data dalam bentuk pasangan- pasangan kekunci dan nilai. Jadual 4 menunjukkan metod- metod lazim yang digunapakai dalam satu implementasi PetaPagar. HashMap is a structure that stores data in the form of key and value pairs. Table 4 shows the common methods utilized in an implementation of the HashMap Jadual 4: Senarai metod-metod dan spesifikasi untuk PetaPagar Table 4: List of methods and their specifications for HashMap Pembina/Nama Metod Spesifikasi Constructor/Method name Specification ) Constructor for the Konstruktor lalai yang menerima parameter- HashMap class parameter kekunci dan nilai pasangan Default constructor which accepts Key and value pair parameters ii) get Memulangkan entry yang dipetakan kepada kekunci di dalam peta-pagar Return the entry mapped to key in the HashMap ti) put Menambah entry baru sekiranya kekunci belum lagi dipetakan di dalam peta-pagar. Sebaliknya, mengemaskini entry lama sekiranya kekunci telah wujud di dalam peta-pagar. Add new entry if Key is not yet mapped into the HashMap. Otherwise, update entry mapped to Key if the HashMap already contains the key Rajah 4 menunjukkan satu aplikasi PetaPagar yang menetapkan kekunci dengan nama pelajar dengan pasangan nilainya merujuk kepada kursus yang sedang diambil di universiti. Figure 4 shows an application of the HashMap that fixes key with student name and pair its value with the course taken at the university. * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates and open the template in the editor. package exam2020; @author IBM public class MyHashMap { // for better re-sizing is taken as 244 private static final int SIZE = 16; private Entry table[] = new Entry[SIZE]; * To store the Map data in key and value pair. * Used linked list approach to avoid the collisions class Entry final String key; String value; Entry next; // object of class Entry to traverse the map Entry(String k, String v) { key=k; value = v; public String getValue() { retum value; public void setValue(String value) { this.value = value; } public String getKey0 { retum key; public Entry get(String k) { int hash=k.hashCode() % SIZE; Entry e = table[hash]; // Bucket is identified by hashCode and traversed the bucket // till element is not found. while(e != null) { iffe.key.equals(k)) { return e; e=e.next; } return null; * If the map previously contained a mapping for the key, the old * value is replaced public void put(String k, String v) { int hash=k.hashCode() % SIZE; Entry e = table[hash]; ifle != null) // If we will insert duplicate key-value pair, // Old value will be replaced by new one. if(e.key.equals(k)) { e.value = y; } else { // Collision: insert new element at the end of list // in the same bucket while(e.next != null) { e=e.next; } Entry entryInOldBucket = new Entryk, v); e.next = entryInOldBucket; } } else // create new bucket for new element in the map. Entry entryInNewBucket = new Entry(k, v); table[hash] = entryInNewBucket; public static void main(String[] args) { MyHashMap myHashMap = new MyHashMapO; // New entry myHashMap.put("Bruce W", "SE"); myHashMap.put("Dean W", "AI"); myHashMap.put("TonyS", "IS"); myHashMap.put("LaraC", "CST"); System.out.println(""); System.out.println("Retrieving student data: "); Entry el = myHashMap.get("Dean W"); System.out.println("+el.getKey(+"." + el.getValue()); Entry e2 = myHashMap.get("Bruce W"); System.out.println("" +22.getKey(+":" + c2.getValue()); System.out.println(""); // Updating record myHashMap.put("Bruce W", "CST"); myHashMap.put("JeanG", "SE"); System.out.println("Retrieving student data: "); Entry e3 = myHashMap.get("BruceW"); System.out.println("" + c3.getKey0 + ":" + c3.getValue(); Entry c4 = myHashMap.get("JeanG"); System.out.println("" + 04.getKey(+": +4.getValue()); Kemaskini metod-metod dalam aplikasi PetaPagar di dalam Rajah 4 tersebut supaya aplikasi tersebut dapat menerima nombor matriks ataupun alamat emel pelajar sebagai kekunci. Pastikan juga pasangan nilai untuk setiap kekunci boleh menyimpan sebarang data-data pelajar yang lain seperti nombor telefon dan tahun pendaftaran. Untuk markah penuh, aplikasi PetaPagar anda sepatutnya boleh di compile dan run walaupun kekunci yang dicari tidak wujud. Rajah 5 menunjukkan contoh paparan bagi aplikasi yang telah dikemaskini. Update the methods for the HashMap application in Figure 4 so the application can accept the student's matrix number or email address as key. Please make sure the pairing value for each key can store any other student data such as phone number and year of registration. Your Hashiap application should compile and runs sucessfully even though an unknown key is being searched, for a full mark. Figure 5 shows an example output for the updated application. run: Retrieving student data: DeanW : AI, 017-449 2272, 2020 bruce@gmail.com : SE, 011-559 8900, 2018 17110022: CST, 013-4509 722, 2018 Retrieving student data: bruce@gmail.com: CST, 011-559 8900, 2018 JeanG: SE, 014-234 9906, 2020 BUILD SUCCESSFUL (total time: 0 seconds) Rajah 5: Contoh paparan untuk aplikasi yang telah dikemaskini Figure 5: Example output for the updated application Markah tidak akan diberikan bagi apa-apa pelaksanaan menggunakan kelas HashMap atau mana-mana kelas Collection yang sedia ada didapati daripada Java API Library No marks will be given for any implementation using the existing HashMap class or any other Collection classes available from the Java API Library
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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