#program in java I want you to use a hash map to make a decision about whether s
ID: 3707225 • Letter: #
Question
#program in java
I want you to use a hash map to make a decision about whether some zoo animals are mammals.
Using the data set from https://archive.ics.uci.edu/ml/datasets/Zoo read the data file and decide whether each animal is a mammal. your program must use a hash map to hold the data. Further, your program must produce the list of mammals based on the data in the hash map. (In other words, don't decide and print out whether each animal is a mammal as its read then just create the hash map to complete the assignment.) You can add data fields as you read the data in. You can create a 19th boolean to indicate if its a mammal. After all the data has been read go back through all the keys in the hash map and decide if the data associated with each key indicates if that animal is a mammal or not.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
public class Mammels {
public static void main(String[] args) throws IOException {
Map<String, Boolean> mammelsMap = new HashMap<>();
String file = "zoo.data.txt";
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String parts[] = line.split(",");
mammelsMap.put(parts[0], parts[parts.length-1].equals("1"));
}
br.close();
System.out.println("List of mammals: ");
for (Map.Entry<String, Boolean> entry : mammelsMap.entrySet()) {
Boolean value = entry.getValue();
if (value) {
System.out.println(entry.getKey());
}
}
}
}
download file zoo.data.txt and place it in the same place where your generated .class file is while running it.
Sample executiuon:
List of mammals:
reindeer
boar
lynx
hamster
goat
lion
leopard
squirrel
giraffe
pony
polecat
fruitbat
platypus
wolf
mink
puma
calf
oryx
cheetah
cavy
porpoise
bear
dolphin
sealion
mole
girl
aardvark
elephant
deer
mongoose
vole
seal
vampire
hare
wallaby
gorilla
opossum
antelope
pussycat
buffalo
raccoon
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.