Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Suppose you want to add some more to the SleighTrack class we wrote in class: import java.util.Map ; public class SleighTrack { private Map locationCount;

Suppose you want to add some more to the SleighTrack class we wrote in class:

import java.util.Map; public class SleighTrack { private Map locationCount; public SleighTrack(String directions) { locationCount = new HashMap(); locationCount.put(new Location(0, 0), 1); } } 
public SleighTrack(String directions) { locationCount = new HashMap(); locationCount.put(new Location(0, 0), 1); int x = 0; int y = 0; for (int i = 0; i < directions.length(); i++) { if (directions.charAt(i) == '>') { x += 1; } else if (directions.charAt(i) == '<') { x -= 1; } else if (directions.charAt(i) == '^') { y += 1; } else if (directions.charAt(i) == 'v') { y -= 1; } else { throw new IllegalArgumentException(); } Location loc = new Location(x, y); int t = locationCount.getOrDefault(loc, 0); locationCount.put(loc, t+1); } } 

A. Write a public boolean allVisited(Set locations) method that returns true iff the current SleighTrack instance has visited all of the given locations. (It is possible to do this one without using a for loop.)

B. Write a public int mostPresents() method that returns the highest number of presents delivered to a single location in the current SleighTrack. (It is possible to do this one without a for loop, but youll need to read over the instance method of Map and the static methods of Collections to see how.)

C. Write a public void presentsToCoal(Set locations) method that removes the given locations from the current SleighTrack, if they are in it. (It is possible to do this one without a for loop, but youll need to carefully read over the instance methods of Map to see how.)

Thank you!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago