Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 18, Problem 11SA
Program Plan Intro
“hashCode” method:
- The hash code is an integer value which is the characteristic of an object.
- The hashing
algorithm is used to evaluate a hash code for an object. - Different objects in the code contain same data and a hashing algorithm should give the same hash code for both.
- For example, if the user wants to evaluate the hash code for a string, he would use the characters making up the string in the calculation. Possibly the user would use each character’s Unicode value in a mathematical formula which would give us an integer value.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In this problem you are given a Set of data and an element. You will need to look to see if the set contains the element. If found return the element otherwise return "?". "Note sometimes the names of classes/variables/objects can be misleading!!!!"
import java.util.HashSet;public class SetContainsLetter{public static Object solution(HashSet<Integer> nums, int elm){// ↓↓↓↓ your code goes here ↓↓↓↓return null;}}
Below is the code for a Time class that has both an equals method and a hashCode method; however, the body of the hashCode method is missing. In the text box, write the code you would put inside the body of the hashCode method. Remember that a hashCode with no collisions is better than a hashCode that has collisions and a hashCode with no gaps in the values is better than a hashCode that has gaps.
public class Time {
private int hour; // A number between 1 and 12
private int min; // A number between 0 and 59
private boolean pm; // True if the time is pm.
public Time(int hour, int min, boolean pm) {
this.hour = hour;
this .min = min;
this.pm = pm;
}
public int getHour() { return hour; }
public int getMin() { return min; }
public boolean isPM() { return pm == true; }
public boolean isAM() { return pm == false; }
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj ==…
Below is the code for a Time class that has both an equals method and a hashCode method; however, the body of the hashCode method is missing. In the text box, write the code you would put inside the body of the hashCode method. Remember that a hashCode with no collisions is better than a hashCode that has collisions and a hashCode with no gaps in the values is better than a hashCode that has gaps.
public class Time {
private int hour; // A number between 1 and 12
private int min; // A number between 0 and 59
private boolean pm; // True if the time is pm.
public Time(int hour, int min, boolean pm) {
this.hour = hour;
this .min = min;
this.pm = pm;
}
public int getHour() { return hour; }
public int getMin() { return min; }
public boolean isPM() { return pm == true; }
public boolean isAM() { return pm == false; }
public boolean equals(Object clock) {
if (this == clockj)
return true;
if…
Chapter 18 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Ch. 18.1 - Prob. 18.1CPCh. 18.1 - What are the three general types of collections?Ch. 18.1 - Prob. 18.3CPCh. 18.1 - Prob. 18.4CPCh. 18.1 - Prob. 18.5CPCh. 18.1 - Prob. 18.6CPCh. 18.1 - Prob. 18.7CPCh. 18.2 - Prob. 18.8CPCh. 18.2 - Prob. 18.9CPCh. 18.2 - Prob. 18.10CP
Ch. 18.2 - Prob. 18.11CPCh. 18.2 - Prob. 18.12CPCh. 18.2 - Prob. 18.13CPCh. 18.2 - Prob. 18.14CPCh. 18.2 - Prob. 18.16CPCh. 18.2 - Prob. 18.17CPCh. 18.2 - Prob. 18.18CPCh. 18.2 - Prob. 18.20CPCh. 18.3 - Prob. 18.21CPCh. 18.3 - Prob. 18.22CPCh. 18.3 - Prob. 18.23CPCh. 18.3 - Prob. 18.24CPCh. 18.3 - Any time you override the Object classs equals...Ch. 18.3 - Prob. 18.26CPCh. 18.3 - Prob. 18.27CPCh. 18.3 - Prob. 18.28CPCh. 18.4 - Prob. 18.29CPCh. 18.4 - Prob. 18.31CPCh. 18.4 - Prob. 18.32CPCh. 18.6 - How do you define a stream of elements?Ch. 18.6 - How does a stream intermediate operation differ...Ch. 18.6 - Prob. 18.35CPCh. 18.6 - Prob. 18.36CPCh. 18.6 - Prob. 18.37CPCh. 18.6 - Prob. 18.38CPCh. 18.6 - Prob. 18.39CPCh. 18 - Prob. 1MCCh. 18 - Prob. 2MCCh. 18 - This type of collection is optimized for...Ch. 18 - Prob. 4MCCh. 18 - A terminal operation in a stream pipeline is also...Ch. 18 - Prob. 6MCCh. 18 - Prob. 7MCCh. 18 - This List Iterator method replaces an existing...Ch. 18 - Prob. 9MCCh. 18 - Prob. 10MCCh. 18 - This is an object that can compare two other...Ch. 18 - This class provides numerous static methods that...Ch. 18 - Prob. 13MCCh. 18 - Prob. 14MCCh. 18 - Prob. 15TFCh. 18 - Prob. 16TFCh. 18 - Prob. 17TFCh. 18 - Prob. 18TFCh. 18 - Prob. 19TFCh. 18 - Prob. 20TFCh. 18 - Prob. 21TFCh. 18 - Prob. 22TFCh. 18 - Prob. 1FTECh. 18 - Prob. 2FTECh. 18 - Prob. 3FTECh. 18 - Prob. 4FTECh. 18 - Write a statement that declares a List reference...Ch. 18 - Prob. 2AWCh. 18 - Assume that it references a newly created iterator...Ch. 18 - Prob. 4AWCh. 18 - Prob. 2SACh. 18 - Prob. 4SACh. 18 - Prob. 5SACh. 18 - Prob. 6SACh. 18 - How does the Java compiler process an enhanced for...Ch. 18 - Prob. 8SACh. 18 - Prob. 9SACh. 18 - Prob. 10SACh. 18 - Prob. 11SACh. 18 - Prob. 12SACh. 18 - Prob. 13SACh. 18 - Prob. 14SACh. 18 - Word Set Write an application that reads a line of...Ch. 18 - Prob. 3PCCh. 18 - Prob. 5PCCh. 18 - Prob. 8PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- for (String name : likedBy) { String likedUser= name.trim(); Set<String> likes = likesMap.getOrDefault(likedUser, new HashSet<>()); Here you create a new Set for likes. This is in the iteration over the likers though so it results in many missing entries. [ How do i Fix this ] import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class FacebookLikeManager { private Map<String, Set<String>> likesMap; public FacebookLikeManager() { likesMap = new HashMap<>(); } public void buildMap(String filePath) { try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line = reader.readLine(); while (line != null) { String[]…arrow_forwardpublic class Final HashMap { 1 private static final 2 Map map = new HashMap(); 2 public static void main(String[] args) { 3 map.put("param1", "value1"); 4 map.put("param2","value2"); 5 map=new HashMap(); }} Answers a) Yes b)No, Compilation Error at line 3 c)No, Runtime Error d) No, Compilation Error at line 5arrow_forward. Write a code using the following hints and apply HashMap<K, V> and its methods get and put for public class CourseInformation (i) Create a HashMap object called Course to add a key and its value. //Key is your course name and value is your course code. (ii) Use put method to map a course name with the course code. // add any FOUR COURSE NAMES WITH ITS COURSE CODE which are represented as a String (iii) Use get method to find the course code for any one of the course.arrow_forward
- package comp1110.exam; /** * COMP1110 Exam, Question 5 */ public class Q5StringHash { /** * Return a hash of the given string as an integer in the range 0 ... buckets - 1. * * @param value The string to be hashed * @param buckets The number of buckets into which the hash should be made (defining its range) * @return An integer hash of value in the range 0 ... buckets - 1. */ public static int hash(String value, int buckets) { // FIXME complete this method, without using Java's built-in hashCode() method return -1; } } ///////////// DO NOT USE ANY INBUILT HASHING FUNCTIONS IN JAVA! //////////////////////arrow_forwardPLESE NO COPY PASTE Answer the following : (3 Marks) Create an ArrayList , HashMap and HashSet Add 2 elements/entries to ArrayList , HashMap and HashSet . ArrayList HashMap HashSetarrow_forwardIn this problem you will have to take the HashMap provided and print out all the keys. import java.util.HashMap;public class MapPrintKeys{public static void solution(HashMap<String,Integer> map){// ↓↓↓↓ your code goes here ↓↓↓↓}}arrow_forward
- The compareTo method. First you compare this.ticker which is a String, then if they are the same then you compare by the this.purchasePrice which is a double hashCode public int hashCode() The hashCode method using the Code-> Generate equals and hashCode with no changes Overrides: hashCode in class Object Returns: int Representing the hashCode compareTo public int compareTo(Stock another) The compareTo method not auto generated Compares first by ticker and if the tickers are the same secondarily compares by price Specified by: compareTo" in interface Comparable<stock> Parameters: another - the object to be compared. Returns: int Representing order Throws: IllegalArgumentException"- if another is null.arrow_forwardGetting more errors please free. import java.util.HashMap;import java.util.Set;public class MapUse{ //main driver code public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<>(); //insert map.put("abc", 1); map.put("def", 2); map.put("abc1", 10); map.put("def1", 20); map.put("abc", 5); //updates abc from 1 to 5, every key is unique, no dupplicate keys //check size System.out.println("size "+ map.size()); //check Presence if(map.containsKey("abc")){ System.out.println("has abc"); } if(map.containsKey("abc1")){ System.out.println("has abc1"); } //get Value int v = 0; if(map.containsKey("abc1")){ //checks whether the key is actually present v = map.get("abc1"); //to protect from null pointer exception } System.out.println(v); //remove int s = map.remove("abc"); //removes the…arrow_forward[JAVA] Please provide comments to my code import java.util.HashMap;public class Items {private final HashMap<String, Integer> allWeapons = setHash("weapon");private final HashMap<String, Integer> allArmor = setHash("armor");private final HashMap<String, Integer> allConsumables = setHash("consumable");public String type;public String name;public int modifier;public Items(String type) {randomItem(type);}public Items(String type, String name) {switch (type) {case "weapon":if (allWeapons.containsKey(name)) {this.type = "weapon";this.name = name;this.modifier = allWeapons.get(name);} else {randomItem(type);}break;case "armor":if (allArmor.containsKey(name)) {this.type = "armor";this.name = name;this.modifier = allArmor.get(name);} else {randomItem(type);}break;case "consumable":if (allConsumables.containsKey(name)) {this.type = "consumable";this.name = name;this.modifier = allConsumables.get(name);} else {randomItem(type);}break;default:randomItem(type);break;}}private…arrow_forward
- Java Programming: There are lots of errors in this code. Please help me fix them. Attached is images of what the code must have and circled the errors in the code. Interpreter.java public class Interpreter { private HashMap<String, Object> variables; public void interpretFunction(FunctionNode fn) { // Create a hash map for local variables variables = new HashMap<String, Object>(); for(Node constantNode : fn.getConstants()) { Object constant = constantNode.getValue(); variables.put(constantNode.getName(), constant); } for(Node localVarNode : fn.getVariables()) { Object localVar = null; variables.put(localVarNode.getName(), localVar); } interpretBlock(fn.getStatements(), variables); } public void interpretBlock(List<StatementNode> statementNodes, HashMap<String, Object> variables) { for(StatementNode statement : statementNodes) {…arrow_forwardWhich of the following methods is a method of wrapper Integer for obtaining hash code for the invoking object? a) int hash() b) int hashcode() c) int hashCode() d) Integer hashcode()arrow_forward#include<iostream> #include<cstdlib> #include<string> #include<cstdio> using namespace std; const int TABLE_SIZE = 128; /* * HashEntry Class Declaration */ class HashEntry { public: int key; int value; HashEntry(int key, int value) { this->key = key; this->value = value; } }; /* * HashMap Class Declaration */ class HashMap { private: HashEntry **table; public: HashMap() { table = new HashEntry * [TABLE_SIZE]; for (int i = 0; i< TABLE_SIZE; i++) { table[i] = NULL; } } /* * Hash Function */ int HashFunc(int key) { return key % TABLE_SIZE; } /* * Insert Element at a key */ void Insert(int key, int value) { int hash = HashFunc(key); while (table[hash] != NULL && table[hash]->key != key)…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education