HashMap as an ArrayList of Strings - java

first off this is an assignment so I'm more looking for help then coded answers (don't want to cheat!). My assignment is to create a program that processes a train/railway network of stations. The section i'm stuck on adds the stations, their connections, and returns these connections as an Array List of Strings. I've included below the code I have so far, and also an extract from the assignment (related to the section I'm on now). I've been struggling with this bit all weekend, so any help would be hugely appreciated.
It's only the implementation of the interface I need to edit, the "MyNetwork" class. I just feel I've been going in circles, and may not have even gotten off on the right foot?
From the assignment;
Create a class MyNetwork that implements the Network interface.
The getConnections method of this class should return an array containing only those stations directly connected to the fromStation argument.
Hint 1: you can do this using a HashMap, with the keys being Strings (representing stations) and the values being ArrayLists of Strings (representing the stations to which there is a direct connection).
Hint 2: Although the getConnections method returns an array of Strings, it would be better for the values in the HashMap to be ArrayLists of Strings
The Interface;
public interface Network {
/**
* Add a station to the network.
* #param station The station to be added.
*/
public void addStation(String station);
/**
* Add a direct connection from one station to another.
* #pre both fromStation and toStation have already been added by the method
* addStation.
* #param fromStation The station from which the connection begins.
* #param toStation The station at which the connection ends.
*/
public void addConnection(String fromStation, String toStation);
/**
* Get a list of all stations directly connected to a given station.
* #pre fromStation has been added to the network by the method addStation.
* #param fromStation
* #return A list of all the stations to which there is a direct connection
* from fromStation.
*/
public String[] getConnections(String fromStation);
/**
* Search for a station in the network.
* #param station Station to be searched for,
* #return true if the Station exists in the network, false otherwise.
*/
public boolean hasStation(String station);
/**
* Get all stations in the network.
* #return An array containing all the stations in the network, with no
* duplicates.
*/
public String[] getStations();
The Implementation:
public class MyNetwork implements Network {
#Override
public void addStation(String station) {
ArrayList<String> Stations = new ArrayList<>();
Stations.add(station);
}
#Override
public void addConnection(String fromStation, String toStation) {
Map<String,String> Connections = new HashMap<>();
Connections.put(fromStation, toStation);
}
#Override
public String[] getConnections(String fromStation) {
return null; // dummy value!
}
#Override
public boolean hasStation(String station) {
return false; // dummy value!
}
#Override
public String[] getStations() {
return null; // dummy value!
}
}

Your network needs to have a state, using one or several instance field(s).
As is, it doesn't have any state. Each method creates a local variable of type List or Map, adds something to this List or Map, and returns. So the List or Map directly goes out of scope and is garbage collected.
private Map<String, List<String>> stations = new HashMap<>();
// now all your methods should use the above map.
See http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Related

Given a log file with API names, start and end timestamps, write a program that prints the average latency for each API call

Q: Given a log file with API names, start and end timestamps, write a program that prints the average latency for each API call.
It was asked in google interview, my solution got rejected. So, want to know how can this be solved in optimized way.
$ cat log
get_foo start 2222222100
get_foo end 2222222150
get_bar start 2222222200
get_foo start 2222222220
get_bar end 2222222230
get_foo end 2222222250
solution :
$ cat log | myprog
get_foo: average = 40
get_bar: average = 30
I suggest you to breakdown the process to simplify the complexity of your problem.
Here could be a good starting point about how to define the process :
Done in Java, it could lead to this :
public class Program {
// The HashMap which contains api keys and object wrappers associated to it.
private Map<String, ApiCall> hashMap = new HashMap<>();
/**
* getHashMap
* Setter of the hashMap which contains api keys and object wrappers
* associated to it.
* #return
*/
public Map<String, ApiCall> getHashMap() {
return hashMap;
}
/**
* setHashMap
* Getter of the hashMap which contains api keys and object wrappers
* associated to it.
* #param hashMap
*/
public void setHashMap(Map<String, ApiCall> hashMap) {
this.hashMap = hashMap;
}
/**
* getNewApiCall
* #return a new ApiCall wrapper object
*/
public ApiCall getNewApiCall() {
return new ApiCall();
}
/**
* main
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
Program app = new Program();
// Arbitrary path to test with my environment
String path = args.length > 1 ? args[0] : "/home/user/test";
// Read all lines from your log file
List<Object> lines = Arrays.asList(
Files.lines(
Paths.get(new File(path).getPath())
).toArray()
);
for (Object o:lines) {
String s = o.toString().trim();
// If it's not and empty line
if (!s.isEmpty()) {
/*
* Split the lines into array with
* [0] = api_name, [1] start or end, [2] timestamp
*/
String[] values = s.split(" ");
// Add the API call to a hashmap without any value
app.getHashMap().put(values[0], null);
// Here you have all name of calls of your API
/*
* Now for each api call, wrap it into an object that will
* handle the average computation, the array of start
* timestamps and the array of end timestamps
*/
ApiCall apiCall = app.getHashMap().get(values[0]);
// Create and object for wrapping starting and ending timestamps
if (apiCall == null) {
apiCall = app.getNewApiCall();
}
/*
* If the line is a start then parse the last value to a long
* and add it to the object wrapper to further compute for the
* average
*/
if (values[1].equals("start")) {
apiCall.addStart(Long.parseLong(values[2]));
}
/*
* Else it is a end timestamp then parse the last value of the
* line to a long and add it to the object wrapper to further
* compute for the average
*/
else {
apiCall.addEnd(Long.parseLong(values[2]));
}
// Add the new incremented object wrapper to the API key
app.getHashMap().put(values[0], apiCall);
}
}
/*
* Stream hashmap entries (API keys) and print the key and the average
* value for each
*/
app.getHashMap().entrySet().forEach(
ks -> System.out.println(
ks.getKey()+" average="+ks.getValue().getAverage()
)
);
}
}
The class leading to object to wrap starting and ending timestamps for a specific API call for further computation.
/**
* ApiCall
* Class allowing to collect starting timestamps and ending timestamp for
* an API call. Compute the average on each addition.
* #author user
* #since 12 sept. 2019
*/
public class ApiCall {
private List<Long> start;
private List<Long> end;
private double average;
public ApiCall() {
start = new ArrayList<>();
end = new ArrayList<>();
}
public void addStart(Long l) {
start.add(l);
setAverage(computeAverage());
}
public void addEnd(Long l) {
end.add(l);
setAverage(computeAverage());
}
public double getAverage() {
return this.average;
}
private void setAverage(Double average) {
this.average = average;
}
private double computeAverage() {
return
(
end.stream().mapToLong(Long::longValue).average().orElse(0.0)
- start.stream().mapToLong(Long::longValue).average().orElse(0.0)
);
}
}
I just did this to give you a starting point about how to do. Don't copy/paste because the code is badly written and there is surely errors about the average computation as you can see in the output :
get_bar average=2.22222223E9
get_foo average=2.22222225E9
Hope it helps.

How to implement code before super without creating another method?

So I want to implement a code that would scramble the words!!
It is a homework assignment question.
Although we are not given the liberty of creating another Method in the class, nor are we allowed to create another field in the class. Everything that we want has to be enclosed within the Constructor parameters.
and then send the word as an argument for super(arg);
Although would it not be illegal and an error if I put any code before super???
Note: I also cannot create any variables outside the constructor.
Note2: ScrambledWordPuzzle is a contructor for class ScrambledWordPuzzle that extends another abstract class
Edit 2: Extra Info
Class to make changes:
public class ScrambledWordPuzzle extends AbstractWordPuzzle {
/**
* The solution to the puzzle
*/
private String solution;
/**
* Creates a scrambled word puzzle given the solution word.
*
* #param solutionWord
* the puzzle word
*/
public ScrambledWordPuzzle(String solutionWord) {
// COMPLETE THIS
// Hint: You need to scramble the letters of the solution word
// to generate the puzzle word and then set the puzzle word.
// The easiest way to scramble the letters is to put them
// into a list, use Collections.shuffle, and then convert the
// the shuffled list of letters back into a string.
super();
this.solution = solutionWord;
}
/**
* Get the solution for this reverse word puzzle.
*
* #return the solution for this reverse word puzzle
*/
#Override
public String getSolution() {
// COMPLETE THIS
return this.solution;
}
}
Abstract Class:
public abstract class AbstractWordPuzzle {
/**
* The puzzle word.
*/
private String puzzle;
/**
* Initializes this puzzle to the empty string.
*/
public AbstractWordPuzzle() {
// COMPLETE THIS
this.puzzle="";
}
/**
* Initializes this puzzle to the specified puzzle word.
*
* #param puzzleWord
* the puzzle word
*/
public AbstractWordPuzzle(String puzzleWord) {
// COMPLETE THIS
this.puzzle=puzzleWord;
}
/**
* Get the solution word. For word puzzles with more than one solution this
* method returns the solution that comes first in dictionary order.
*
* #return the solution word that comes first in dictionary order
*/
public abstract String getSolution();
/**
* Get the puzzle word
*
* #return the puzzle word
*/
public final String getPuzzleWord() {
// ALREADY IMPLEMENTED; DO NOT MODIFY
return this.puzzle;
}
/**
* Set the puzzle word for this puzzle.
*
* #param puzzleWord
* the puzzle word
*/
public final void setPuzzleWord(String puzzleWord) {
// COMPLETE THIS
this.puzzle=puzzleWord;
}
}
I need the code before super() because, if it is after the super code, I will not be able to call the variable or whatever it would be in the super(arg).
Ok, so no methods allowed. Then everything should be inline.
public ScrambledWordPuzzle(String solutionWord) {
super(new MyCollection(solutionWord.split("")).shuffle().toString());
}

Returning Collections

I am trying to return a collection containing connections in a network that starts or ends at a specific station. I am having trouble figuring out how to return it and take the station parameter. Also is having the creation of the hashMap in the method the right way to do it, or should it be created outside of it?
It is giving me the error incompatible types: Connection cannot be converted to Collection<Connection> for the return statement
CODE:
/**
* Return a Collection containing all the Connections in the network that
* start or end at a specified station
*
* #param station Station to/from which the Connection should run
*
* #return a Collection containing all the connections that start or end at
* the specified station
*/
#Override
public Collection<Connection> getConnectionsFrom(Station station) {
Map<Station, Connection> stationConnectionFrom = new HashMap<Station, Connection>();
return stationConnectionFrom.get(station);
}
Only one Connection is being returned. You can change your return type to:
public Connection getConnectionFrom(Station station) {
Map<Station, Connection> stationConnectionFrom = new HashMap<>();
return stationConnectionFrom.get(station);
}
In your case, the map being empty, this will always return null.
If you can't change method signature, you can do:
#Override
public Collection<Connection> getConnectionsFrom(Station station) {
Map<Station, Connection> stationConnectionFrom = new HashMap<Station, Connection>();
return Collections.singletonList((stationConnectionFrom.get(station));
}

Retrieve data from an ArrayList item

Apologies in advance, I am new to Java and I am using someone else's code for the most part so please bear with me. I have looked around but couldn't find anything to help my problem
I've retrieved an ArrayList from a method and then I've attempted to write a foreach loop to retrieve a specific piece of data from what is an 'Observation' below.
For whatever reason it won't allow me to retrieve any of the data stored inside an observation when accessing through the ArrayList.
ArrayList<Observation>[] npcPositions = stateObs.getNPCPositions();
Vector2d playerPosition = stateObs.getAvatarPosition();
int npcCount = npcPositions.length;
for (int i = 0; i <= npcCount; i++)
{
if (playerPosition.x == npcPositions[i].position)
{
}
}
position being a value within the Observation but I get the error that it cannot be resolved or is not a field. Part of the observation class is below and I can not access any of these variables doing what I'm currently doing.
public class Observation implements Comparable<Observation>
{
/**
* Category of this observation (static, resource, npc, etc.).
*/
public int category;
/**
* Type of sprite of this observation.
*/
public int itype;
/**
* unique ID for this observation
*/
public int obsID;
/**
* Position of the observation.
*/
public Vector2d position;
/**
* Reference to the position used for comparing this
* observation with others.
*/
public Vector2d reference;
So what do I need to use to access those variables. I noticed that I have to use [] when I want to store data from stateObs.getNPCPositions and that seems to be the reason why other examples weren't working for me but I am unsure on how to fix it.
UPDATE
The original issue seems to be fixed, however when attempting to retrieve the length of the ArrayList, I get nullpointerexception. How can I get the number of items to be able to run through them in the loop each time.
UPDATE #2
/**
* Returns a list of observations of NPC in the game. As there can be
* NPCs of different type, each entry in the array corresponds to a sprite type.
* Every ArrayList contains a list of objects of type Observation.
* Each Observation holds the position, unique id and
* sprite id of that particular sprite.
*
* #return Observations of NPCs in the game.
*/
public ArrayList<Observation>[] getNPCPositions()
{
return model.getNPCPositions(null);
}
/**
* Returns a list of observations of NPC in the game. As there can be
* NPCs of different type, each entry in the array corresponds to a sprite type.
* Every ArrayList contains a list of objects of type Observation, ordered asc. by
* distance to the reference passed. Each Observation holds the position, sprite type id and
* sprite id of that particular sprite.
*
* #param reference Reference position to use when sorting this array,
* by ascending distance to this point.
* #return Observations of NPCs in the game.
*/
public ArrayList<Observation>[] getNPCPositions(Vector2d reference)
{
return model.getNPCPositions(reference);
}
This:
ArrayList<Observation>[] npcPositions = stateObs.getNPCPositions();
is getting an array of ArrayList. You can get a single ArrayList from index i of the array using:
ArrayList<Observation> list = npcPositions[i];
You can get the Observation at index j of your list using:
Observation obs = list.get(j);
Or you can use them in combination:
Observation obs = npcPositions[i].get(j);
In line:
npcPositions[i].position
Is an array of ArrayList which does not have any property position. Possibly you would try:
npcPositions[i].get(0).position
Edited:
As you said that this line gives NPE:
int npcCount = npcPositions.length;// possibly npcPositions is null
Below line is executed to get the array list:
public ArrayList<Observation>[] getNPCPositions()
{
return model.getNPCPositions(null);//<-- note this, possibly model is also null
}
I am not sure what you are doing in the first two lines of your code, but assumming that what you are doing is correct then the problem lies with your if statement. You are trying to test if a Vector2D.x is equal to a Vector2D which can never happen. try doing this
for(int i = 0; i < npcCount; < i++)
{
if(playerPosition == npcPositions.get(i).position)
{
//do something here
}
}
or you can try this
for(int i = 0; i < npcCount; < i++)
{
if(playerPosition.x == npcPositions.get(i).position.x)
{
//do something here
}
}

JasperReport: How to use subreport return values as input for Main Report Variable Calculation

Scenario:
I've two reports: Main Report (let's call it, A) and sub-report (let's call it, B).
Report A contains sub-report B at the detail band, so sub-report B is displayed for each element at the Report A datasource. Sub-report B also returns a variable to the Main report A.
What I want is to sum those return values from sub-report B and totalize them at the Main report summary.
To do that, I have tried to create a new report variable that sum those returns values... Something like this:
However, I've found that such variables expression are always evaluated before the band detail is rendered, so I always miss the first sub-report return value...
Sadly, the evaluation time (as this link says) cannot be changed on those kind of variables, so I'm stuck...
After been struggling with this for some hours... and searching the internet for a solution... I came with a Workaround (the enlightening forums were these ones: one and two).
First, you need to define a java Class Helper that allows you calculate some arithmetic operation, in my case a Sum operation. I defined these classes:
package reports.utils;
import java.util.Map;
/**
* Utility that allows you to sum Integer values.
*/
public class SumCalculator {
/**
* Stores a map of {#code SumCalculator} instances (A Map instance per thread).
*/
private static final ThreadLocalMap<String, SumCalculator> calculatorsIndex = new ThreadLocalMap<>();
/**
* The sum total.
*/
private int total = 0;
/**
* No arguments class constructor.
*/
private SumCalculator() {
super();
}
/**
* Instance a new {#code SumCalculator} with the given ID.
*
* #param id {#code SumCalculator}'s ID
* #return the new {#code SumCalculator} instance
*/
public static SumCalculator get(String id) {
Map<String, SumCalculator> map = calculatorsIndex.get();
SumCalculator calculator = map.get(id);
if (calculator == null) {
calculator = new SumCalculator();
map.put(id, calculator);
}
return calculator;
}
/**
* Destroy the {#code SumCalculator} associated to the given ID.
*
* #param id {#code SumCalculator}'s ID
* #return {#code null}
*/
public static String destroy(String id) {
Map<String, SumCalculator> map;
map = calculatorsIndex.get();
map.remove(id);
if (map.isEmpty()) {
calculatorsIndex.remove();
}
return null;
}
/**
* Resets the {#code SumCalculator} total.
*
* #return {#code null}
*/
public String reset() {
total = 0;
return null;
}
/**
* Adds the given integer value to the accumulated total.
*
* #param i an integer value (can be null)
* #return {#code null}
*/
public String add(Integer i) {
this.total += (i != null) ? i.intValue() : 0;
return null;
}
/**
* Return the accumulated total.
*
* #return an Integer value (won't be null, never!)
*/
public Integer getTotal() {
return this.total;
}
}
package reports.utils;
import java.util.HashMap;
import java.util.Map;
/**
* Thread Local variable that holds a {#code java.util.Map}.
*/
class ThreadLocalMap<K, V> extends ThreadLocal<Map<K, V>> {
/**
* Class Constructor.
*/
public ThreadLocalMap() {
super();
}
/* (non-Javadoc)
* #see java.lang.ThreadLocal#initialValue()
*/
#Override
protected Map<K, V> initialValue() {
return new HashMap<>();
}
}
Second, at your jasper report, you need to define four text fields:
1) A text field that iniatializes your calculator; it should be (ideally) at the title section of the report and should have an expression like this: SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").reset(). This text field should have the evaluation time: NOW.
2) A text field that calls the increment function (i.e. SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").add($V{SUB_REPORT_RETURN_VALUE}). This text field will reside at your detail band, after the subreport element; and it should have the evaluation time: BAND (this is very important!!)
3) A text field that prints the calculator total. This text field will reside at your summary band, it will evaluate to NOW. Its expression will be: SumCalculator.get("$V{SUB_REPORT_RETURN_VALUE}").getTotal()
4) A text field that destroy the calculator. This text field will also reside at your summary band and must appear after the text field 3. The text field should have an expression like: SumCalculator.destroy("$V{SUB_REPORT_RETURN_VALUE}"). This text field should have the evaluation time: NOW.
Also, the text fields: 1, 2, and 4, should have the attribute "Blank when Null", so they will never be printed (that's why those java operations always return null).
And That's it. Then, your report can look something like this:
if i understand the problem, you can not summarize the amount returned by the sub report in the main report, i had the same problem and i solved in this way.
1.- Create a class which extends from net.sf.jasperreports.engine.JRDefaultScriptlet. and override the method beforeReportInit()
this is the code from this class.
package com.mem.utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
public class SumarizacionSubtotales extends JRDefaultScriptlet {
private final Log log = LogFactory.getLog(getClass());
private Double total;
public Double getTotal() {
return total;
}
public Double add(Double cantidad) {
if(log.isDebugEnabled())log.debug("AGREGANDO LA CANTIDAD : " + cantidad);
this.total += cantidad;
return cantidad;
}
#Override
public void beforeReportInit() throws JRScriptletException {
if(log.isDebugEnabled())log.debug("beforeReportInit");
total = 0.0D;
}
}
2.- add your project's jar in your ireport's classpath.
3.- Replace the class of the REPORT scriptlet.
in the properties with your class.
3.- add in the group footer where you want to print the value returned by the sub-report a textfield with the following expression.
$P{REPORT_SCRIPTLET}.add( $V{sum_detalles} )
In this case $V{sum_detalles} is a variable in the main report which contains the value returned by the sub-report.
4.- Add in the Last page footer another textfield with the following expression.
$P{REPORT_SCRIPTLET}.getTotal()

Categories