+1 (315) 557-6473 

Count of Vehicles using Java Assignment Solution

Count the numbers and types of cars Using HashMaps

The overview for Project 5:

You are to write a java assignment on OOP implementation (at least one other relevant/important class other than the Driver) to track the count of Vehicles found in a dataset using HashMap data structures.

The Vehicle dataset file:

Each line contains a type, a make, and a model of a vehicle. There are 3 types (car, truck, SUV).

There are 10 makes (Toyota, Ford, etc.)

Each makes will have between 2 to 6 models (E.G. Toyota would have Tundra, Highlander, Prius, etc.).

The HashMap’s creation:

These are look-up tables (dictionary) with key/value pairs to be able to look up information about the vehicles.

Read through the data to create the HashMaps. There can/will be many duplicates of each vehicle.

Processing and Requirements

  • The Driver should not contain the HashMaps.
  • Read through the file to create the HashMap.
  • You will need a Hash Map to keep a count of the number of each type of vehicle (car, truck, or SUV). This HashMap is by itself, it doesn’t have to be linked or associated with any other data structure.
  • You will need another HashMap to keep track of the makes.

o for each make you will need to store each model of the vehicle and the number of times that model is found in the dataset.

  • You will need to make Junit test class/es (Junit 5 or 4 are acceptable) and method tests for all Object classes’ methods (excluding any print or toString method/s and the Driver Class).



import java.io.File; import java.util.Scanner; public class Driver { // Entry point of the program public static void main(String[] args) throws Exception { // Load the vehicles file into the vehicle stats object VehicleStats stats = new VehicleStats(); Scanner inFile = new Scanner(new File("Vehicles.txt")); while (inFile.hasNextLine()) { Scanner scanner = new Scanner(inFile.nextLine()); // Get the type, make, and model and put it into the stats String type = scanner.next(); String make = scanner.next(); String model = scanner.next(); stats.add(type, make, model); } inFile.close(); // Display some stats System.out.println("Most frequent type of vehicle: " + stats.mostFrequentType()); System.out.println("All types of vehicles and count of each:"); for (String type : stats.getTypes()) { System.out.println(" " + type + ": " + stats.getTypeFrequency(type)); } System.out.println("Make with most data set: " + stats.mostFrequentMake()); System.out.println("Make with least data set: " + stats.leastFrequentMake()); System.out.println("All makes, models, and frequency: "); for (String make : stats.getMakes()) { System.out.println(" " + make + ":"); for (String model : stats.getMakeModels(make)) { System.out.println(" " + model + ": " + stats.getMakeModelFrequency(make, model)); } } } }


import java.util.HashMap; import java.util.HashSet; import java.util.Set; public class VehicleStats { private HashMap typesFrequency; private HashMap> makes; // Create the stats counter object public VehicleStats() { typesFrequency = new HashMap<>(); makes = new HashMap<>(); } // Update the statistics based on the given new vehicle information public void add(String type, String make, String model) { // Update the types if (!typesFrequency.containsKey(type)) { typesFrequency.put(type, 0); } typesFrequency.put(type, typesFrequency.get(type) + 1); // Update the makes and model counts if (!makes.containsKey(make)) { makes.put(make, new HashMap<>()); } if (!makes.get(make).containsKey(model)) { makes.get(make).put(model, 0); } makes.get(make).put(model, makes.get(make).get(model) + 1); } // Find the vehicle that is mostly frequents public String mostFrequentType() { String result = ""; int resultFrequency = 0; for (String type : typesFrequency.keySet()) { if (typesFrequency.get(type) > resultFrequency) { result = type; resultFrequency = typesFrequency.get(type); } } return result; } // Return all types of vehicle public Set getTypes() { return typesFrequency.keySet(); } // Return how frequent a type of a vehicle is public int getTypeFrequency(String type) { if (typesFrequency.containsKey(type)) { return typesFrequency.get(type); } return -1; } // Find the make of the vehicle that is most frequent public String mostFrequentMake() { String result = ""; int resultFrequency = 0; for (String make : makes.keySet()) { int frequency = makeFrequency(make); if (frequency > resultFrequency) { result = make; resultFrequency = frequency; } } return result; } // Find the make of the vehicle that is least frequent public String leastFrequentMake() { String result = ""; Integer resultFrequency = null; for (String make : makes.keySet()) { int frequency = makeFrequency(make); if (resultFrequency == null || frequency < resultFrequency) { result = make; resultFrequency = frequency; } } return result; } // Return how many vehicles has the given make public int makeFrequency(String make) { if (!makes.containsKey(make)) { return 0; } int count = 0; for (String model : makes.get(make).keySet()) { count += makes.get(make).get(model); } return count; } // Return all makes public Set getMakes() { return makes.keySet(); } // Get the models of a make public Set getMakeModels(String make) { if (!makes.containsKey(make)) { return new HashSet<>(); } return makes.get(make).keySet(); } // Get the frequency of a model within a make public int getMakeModelFrequency(String make, String model) { if (!makes.containsKey(make)) { return 0; } if (!makes.get(make).containsKey(model)) { return 0; } return makes.get(make).get(model); } }

Vehicle Test:

import java.util.Set; import org.junit.Test; import static org.junit.Assert.*; public class VehicleStatsTest { /** * Add vehicles and we should be able to retrieve what we added, with the * right data. */ @Test public void testAddAndGet() { VehicleStats stats = new VehicleStats(); stats.add("suv", "Nissan", "Kicks"); stats.add("car", "Toyota", "NX"); stats.add("truck", "Chevrolet", "Silverado"); // Check types exists assertEquals(3, stats.getTypes().size()); assertTrue(stats.getTypes().contains("suv")); assertTrue(stats.getTypes().contains("car")); assertTrue(stats.getTypes().contains("truck")); assertFalse(stats.getTypes().contains("?")); // Check makes exists assertEquals(3, stats.getMakes().size()); assertTrue(stats.getMakes().contains("Nissan")); assertTrue(stats.getMakes().contains("Toyota")); assertTrue(stats.getMakes().contains("Chevrolet")); assertFalse(stats.getMakes().contains("?")); // Check model of makes exists assertEquals(1, stats.getMakeModels("Nissan").size()); assertTrue(stats.getMakeModels("Nissan").contains("Kicks")); assertEquals(1, stats.getMakeModels("Toyota").size()); assertTrue(stats.getMakeModels("Toyota").contains("NX")); assertEquals(1, stats.getMakeModels("Chevrolet").size()); assertTrue(stats.getMakeModels("Chevrolet").contains("Silverado")); } /** * Test most frequent type */ @Test public void testMostFrequentType() { VehicleStats stats = new VehicleStats(); stats.add("suv", "Nissan", "Kicks"); stats.add("car", "Toyota", "NX"); stats.add("truck", "Chevrolet", "Silverado"); stats.add("car", "Honda", "Accord"); assertEquals("car", stats.mostFrequentType()); } /** * Test of mostFrequentMake method, of class VehicleStats. */ @Test public void testMostFrequentMake() { VehicleStats stats = new VehicleStats(); stats.add("suv", "Nissan", "Kicks"); stats.add("car", "Toyota", "NX"); stats.add("truck", "Chevrolet", "Silverado"); stats.add("car", "Honda", "Accord"); stats.add("car", "Toyota", "Highlander"); assertEquals("Toyota", stats.mostFrequentMake()); } /** * Test of leastFrequentMake method, of class VehicleStats. */ @Test public void testLeastFrequentMake() { VehicleStats stats = new VehicleStats(); stats.add("suv", "Nissan", "Kicks"); stats.add("suv", "Nissan", "Rogue"); stats.add("car", "Toyota", "NX"); stats.add("truck", "Chevrolet", "Silverado"); stats.add("car", "Honda", "Accord"); stats.add("truck", "Honda", "Ridgeline"); stats.add("car", "Toyota", "Highlander"); assertEquals("Chevrolet", stats.leastFrequentMake()); } }