Reputation:
I'm posting after 2 hours of trying to solve it but I'd like some help here. The task is the following:
Create a method getHeaviest which takes no parameters and returns a string. When called, the method should return the name of the heaviest dinosaur in the database. How you achieve this is up to you. If no dinosaurs are in the database, return an empty string instead. Don't worry about dinosaurs having the same weight.
import java.util.Map;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;
public class randomdamdam {
private Map<String, Integer> dinos;
public randomdamdam () {
dinos = new HashMap<>();
}
public int size(){
return dinos.size();
}
public void addDino(String newDino, int weight){
if (!dinos.containsKey(newDino)) {
dinos.put(newDino, weight);
System.out.println(newDino + " added. Weight: " + newDino + "kg");
} else {
System.out.println(newDino + " cannot be added. It is already in the database!");
}
}
public void updateDino (String updatedDino, int newWeight){
if (dinos.containsKey(updatedDino)){
System.out.println(updatedDino + "updated. Weight: " +newWeight+ "kg");
} else {
String line = updatedDino + "cannot be updated. It is not in the database!";
System.out.println(line);
}
}
public void removeDino (String removedDino) {
if (dinos.containsKey(removedDino)){
System.out.println(removedDino + "removed");
} else {
String line2= removedDino +"cannot be removed. It is not in the database!";
System.out.println(line2);
}
}
public int getWeight (String existingDinosaur) {
if (dinos.containsKey(existingDinosaur)){
return dinos.get(existingDinosaur);
} else {
String ofweight = existingDinosaur + "cannot be found in the database!";
System.out.println(ofweight);
return 0;
}
}
public Set<String> getDinoNames(){
Set<String> names = dinos.keySet();
return names;
}
public String getHeaviest () {
int max = Collections.max(dinos.values());
for (Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() == max ) {
String heavy = heaviestBoi.toString();
return heaviestBoi;
}}
}
So the thing is that I'd like to get the heaviest dino out of every dino and I've tried multiple times to do that but I actually can't.
Upvotes: 0
Views: 1367
Reputation: 1491
Please refactor the getHeaviest() method like below.Complete source code is given below for your better understandings.
Extra: Please follow the Java naming conventions when you are naming classes .Ex:Starting letter of a class name should be always Capital.
public class randomdamdam {
private Map<String, Integer> dinos;
public randomdamdam () {
dinos = new HashMap<>();
}
public int size(){
return dinos.size();
}
public void addDino(String newDino, int weight){
if (!dinos.containsKey(newDino)) {
dinos.put(newDino, weight);
System.out.println(newDino + " added. Weight: " + newDino + "kg");
} else {
System.out.println(newDino + " cannot be added. It is already in the database!");
}
}
public void updateDino (String updatedDino, int newWeight){
if (dinos.containsKey(updatedDino)){
System.out.println(updatedDino + "updated. Weight: " +newWeight+ "kg");
} else {
String line = updatedDino + "cannot be updated. It is not in the database!";
System.out.println(line);
}
}
public void removeDino (String removedDino) {
if (dinos.containsKey(removedDino)){
System.out.println(removedDino + "removed");
} else {
String line2= removedDino +"cannot be removed. It is not in the database!";
System.out.println(line2);
}
}
public int getWeight (String existingDinosaur) {
if (dinos.containsKey(existingDinosaur)){
return dinos.get(existingDinosaur);
} else {
String ofweight = existingDinosaur + "cannot be found in the database!";
System.out.println(ofweight);
return 0;
}
}
public Set<String> getDinoNames(){
Set<String> names = dinos.keySet();
return names;
}
public String getHeaviest () {//Here is your method which returns the heaviest dino's name
int max = -1;
String name= null;
for (Map.Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() >max ) {
max = heaviestBoi.getValue();
name = heaviestBoi.getKey();
}}
return name;
}
public static void main(String[] args) {
randomdamdam m1 = new randomdamdam();
m1.addDino("Dino1", 450);
m1.addDino("Dino2",455);
m1.addDino("Dino3",700);
System.out.println("Heaviest Dino: "+ m1.getHeaviest() );//Calling the method
}
}
Output:
Dino1 added. Weight: Dino1kg
Dino2 added. Weight: Dino2kg
Dino3 added. Weight: Dino3kg
Heaviest Dino: Dino3 //Heaviest one's name returned
Upvotes: 1
Reputation: 339
I think you were already close. Looking at your getHeaviest
method, within the if statement, you have essentially to get the key element of the "Entry" object (which is the name of the dino). You cannot simply return the whole heaviestBoi
object as it is of type Entry
.
Solution
public String getHeaviest () {
int max = Collections.max(dinos.values());
for (Entry<String, Integer> heaviestBoi : dinos.entrySet())
{
if (heaviestBoi.getValue() == max ) {
return heaviestBoi.getKey();
}
}
}
Additional Comment
Please note that you wrote the following within your if statement:
String heavy = heaviestBoi.toString();
return heaviestBoi;
Hence, the first line has actually no effect in respect of the returned object.
Upvotes: 1
Reputation: 1023
Key of the map must be the name of the dinosaur and value must be its key. So instead of casting amp to String (which is failing) you must return the key which is name of the dinosaur
Replace
String heavy = heaviestBoi.toString();
with
String heavy = heaviestBoi.getKey();
and return that string instead of map object
Upvotes: 1