Questions tagged [java]

Java (not to be confused with JavaScript) is a class-based, object-oriented, strongly typed, reflective language and run-time environment (JRE). Java programs are compiled to bytecode and run in a virtual machine (JVM) enabling a "write once, run anywhere" (WORA) methodology.

Filter by
Sorted by
Tagged with
3
votes
1answer
43 views

Rate Limiter implementation in Java

I have been working on implementing rate limiter in Java. Following is my approach. I have used 'sliding window with counters' technique to implement this rate limiter. What I do is instead of rate ...
-1
votes
0answers
29 views

timing out on one case, USACO 2018 US Open Contest, Silver Problem 3. Multiplayer Moo1

Referring to this question: http://usaco.org/index.php?page=viewproblem2&cpid=836 Hi! So I'm working on this problem, and my approach was to search ever possible ways, but it time out on one case: ...
3
votes
1answer
26 views

Calculating difference between cached period and requested

There is a remote resource containing datasets arranged by time, data is static and doesnt change. Whenever we are going to fetch anything from there we will be saving it in our cache. User might ...
1
vote
1answer
36 views

Logging conditionally repetitively in code

I am using Log4j2 for logging. Multiple operations return success code. If status 0 is received, it means operation is successful, else failed. If operation is ...
1
vote
0answers
39 views

Building a list from the Catresian square of an array

The code generates a list containing all possible combinations of the items of an array. Every triplet of items contains two operands and the third item containing ...
6
votes
3answers
1k views

Setters dependent on other instance variables in Java

I am relatively new to java and I am trying to set up a class with non-trivial setters, but I wonder if setting an instance variable via another one is allowed/good practice? ...
0
votes
1answer
46 views

Mutating input List method argument to add new item [closed]

I really don't like the idea of method (especially public) mutating state of input argument. I been thinking to see what will be the better way of achieving this without violations. I wrote a sample ...
2
votes
0answers
26 views

Message handler

The code bellow is code of "Hello" message handler. It works, but I really don't like as it looks. The "hello" protocol contains three messages: client -> server: diffie-...
-2
votes
0answers
16 views

How to Copy Firebases' Data to another Firebase [closed]

I am a student studying Android Studio. I want to implement the "cart" functionality using Firebase. After searching for an item with the primary key value in the previously saved Firebase, ...
2
votes
0answers
54 views

Codeforces problem Java code optimization

The problem says: You are given an array a of length 2n. Consider a partition of array a into two subsequences p and q of length n each (each element of array a should be in exactly one subsequence: ...
4
votes
0answers
46 views

Just a digital clock in Swing

I've written a little clock implementation in Java Swing. It now seems to work flawlessly after having terrible Swing specific timing problems. Please indicate if this is the way to draw the clock and ...
2
votes
2answers
74 views

Xs and Os game logic in Java [closed]

I am practicing programing in Java. I have started to make a Xs and Os (Tic-Tac-Toe) game and I began with baseline logic of the game. I would appreciate any advice on my programming style, what am I ...
3
votes
1answer
41 views

Seat Reservation 2D Array Applied to Methods

I have completed my lab assignment for class, but I'm still wondering if there's any ways I could better optimize my code. specifically my methods isSeatAvailable and reserveSeat. Here is my problem: ...
2
votes
3answers
98 views

Chess knight move simulator

I was asked to write chess simulation of pieces as a take home interview question and was given this spec. This is what I came up with: ChessGenerator.java ...
-2
votes
0answers
32 views

Using linear programming on lifeguard (USACO 2018 January Contest, Silver Problem 1) [closed]

I'm trying to solve http://www.usaco.org/index.php?page=viewproblem2&cpid=786, however, I only passed 8 out of 10 cases: https://i.stack.imgur.com/KGqhN.png But I think I cover all the cases, so, ...
10
votes
8answers
2k views

OOP implementation of Rock Paper Scissors game logic in Java

What is your opinion on my solution for Rock Paper Scissors game logic in Java? Round outcome enum class ...
0
votes
3answers
84 views

Java recursive sort verification function for a linked list

It should return true, if there is no element that follow or the following elements are greater than their predecessors. ...
1
vote
1answer
68 views

Message encode/decode library

I write protocol messages encoder/decoder for my project. All messages have 2-bytes header (1 byte - message type, 1 byte message sub-type) and body. There are two types of messages: Plain message - ...
2
votes
1answer
61 views

Print the App Name Using Latest Version

I am trying to solve a problem, where we are given an app name, service name, and version number of an api. We need to print the name of app, which is using the latest version for most of the service. ...
6
votes
3answers
80 views

Calculate the amount of savings I will have in n months

I have the following problem and I am new in Java: I have an initial capital of 10 000. I invest this initial capital of 10 000 in the bank for a interest rate per month of 0.04/12. My net income is ...
2
votes
1answer
62 views

Applying 2d Array To Methods Plus Return Statements (Java)

I have an assignment that I have completed, but I feel as though the way I went about solving the problems could be optimized a bit. As well, I don't know if I'm properly utilizing return statements. ...
8
votes
1answer
85 views

Parenthesis-realigning program

This is my code for an assignment. The task is to take a user input in form of a sequence of parenthesis' followed by an E to mark its end and determine whether it is a correct sequence (as an example ...
2
votes
0answers
35 views

Create a distribution from .xlsx file using Java

I have an Excel file with 2 columns, names and numbers. I want to know for each name what is the probability for each number. I prefer to do it in Java so I would be able to combine it with the rest ...
1
vote
0answers
63 views

How can I apply an Optional in which I want to call a method?

I have this piece of code: ...
6
votes
0answers
84 views

Generate unique random strings considering collisions

I'm coding a random strings generator. It's working, but I'm not sure if this is efficient. ...
2
votes
2answers
101 views

LeetCode 3Sum Java Solution

I have this Java solution to LeetCode's 3Sum problem. I would be really grateful if I could get feedback on correctness, scalability, efficiency, coding best practices, and OOP design. I'm relatively ...
1
vote
0answers
22 views

Dijkstra's shortest-path algorithm implemented via Priority Queues and an adjacency matrix represented graph

My adjacency matrix looks like this, where non-zero weights denote edges. [0, 2, 0, 0, 3] [2, 0, 2, 4, 0] [0, 2, 0, 3, 0] [0, 4, 3, 0, 1] [3, 0, 0, 1, 0] Here's my attempt, would appreciate a code ...
3
votes
2answers
66 views

Multiple filters and maps java stream

I need to find the odd occurring numbers as a list. For example the below list should give 1 and 2 as output. Is there a way that I can do it in a better way? ...
0
votes
1answer
45 views

Spring Boot should we always use interfaces even with one implementation [closed]

quick question. I'm using Spring Boot and I created e.g. this class ...
2
votes
1answer
54 views

Java CSV to XML converter

I'm relatively new to programming and I was trying to work a project. I would like recommendations of how to annotate or comment my project and any improvement I could make to it, any suggestion is ...
5
votes
2answers
53 views

Setter method for an object using a special logic

I have a method that received a list of CoordinateBE : setExits(..) I want to update the existing coordinates list with a new coordinate only if there is no coordinate in the current list that has the ...
2
votes
0answers
30 views

Which of my string indexing functions in Kotlin is semantically closer to my Java one?

I have a working string indexing method in Java and did convert in Kotlin. All methods shown below produce the same result but as I am really new to the Kotlin language I wanted to ask which of my two ...
5
votes
3answers
884 views

Money change exam

I was asked to create a method that would: Return a Change object or null if there was no possible change The "machine" has unlimited bills of: 2, 5 and 10 The Change object must return the ...
2
votes
1answer
31 views

Prim's algorithm via Priority Queues to print the minimum spanning tree of an adjacency matrix undirected graph

My adjacency matrix looks like this, where non-zero weights denote edges. [0, 2, 0, 0, 3] [2, 0, 2, 4, 0] [0, 2, 0, 3, 0] [0, 4, 3, 0, 1] [3, 0, 0, 1, 0] Here's my attempt: ...
8
votes
3answers
807 views

A* (shortest path) with the ability to remove up to one wall

Problem You are given an HxW matrix. Each element is either 0 (passable space) or 1 (wall). Given that you can remove one wall, find the shortest path from [0,0] (start) to [width-1, height-1] (end). ...
6
votes
2answers
649 views

OOP concepts In a console application

I'm currently trying to improve my skills in Java. I have created a simple console-based bank system with four classes. A user interface, account holder, abstract bank account class which account ...
3
votes
2answers
50 views

Prim's algorithm via Priority Queues to print the minimum spanning tree of an undirected graph

Here's my attempt, I believe the runtime is O(V*(ElogE + log E)) since the while loop runs V times and it takes ElogE + log E to do the priority queue operations inside the loop. ...
6
votes
4answers
275 views

Extending class vector in java

I've seen in Java a class vector implemented, it's very useful because you can declare vectors without say the size of the vector explicitly, and it has a lot of functions operating with its elements. ...
3
votes
1answer
33 views

Detecting a cycle in a directed graph via an adjacency list implementation and a recursive DFS

Here's my attempt. I iterate through each vertex in the graph and do a DFS to see if I reach back on a vertex I already visited in this vertex's iteration. It seems to work but I am not satisfied with ...
2
votes
3answers
76 views

Copy file using read and write streams

I have this piece of code in my program, where I assign 10000 as the size of the buffer. Now How can I check if the file that I am copying is less than 10000, for example 5000, than how to free the ...
6
votes
1answer
48 views

MessageHandler handles different message subtypes differently (can visitor pattern be applied?)

Introduction I'm working with Java 1.8. There is a lot of repetition in the MessageHandler class that I would like to remove but I'm having trouble implementing a ...
3
votes
1answer
44 views

Match users to their preferences by ID

I am using Java, but coming from a JavaScript background. I have two objects: a list of users an Iterable of users preferences I am joining them into a new users object that I call ...
3
votes
1answer
41 views

Alternative class to imitate AsyncTask class (deprecated) with Thread and Handler classes

As the title says, I have created an alternative class to imitate AsyncTask class (deprecated) with Thread and Handler classes but i feel uncomfortable to use it.(the app works fine with my class) Can ...
6
votes
2answers
114 views

RPG item, character, and display

Question description I am currently developing an RPG to gain programming experience. An RPG consists of many individual components such as the combat system, quest system and the item system. I ...
2
votes
2answers
168 views

API call return type and error handling [closed]

I wrote the following method which would just call an API which would then update a user's subscription to something. ...
0
votes
1answer
42 views

Sonar: Move constants to a class or enum [closed]

I found one project on Internet and after I downloaded it, I got this message from SonarQube: Move constants to a class or enum. But, when I downloaded ...
2
votes
2answers
74 views

How to Avoid Conditional Check Duplication [closed]

In the code, I am checking altPhoneExist and altEmailExist conditions multiple times. How can I avoid this and improve the ...
3
votes
0answers
86 views

Migrate customer and transaction data from CSV to MongoDB

I wrote a Java Spring Boot application with MongoDB that on every application start loads customer, account type and transaction details from CSV files to MongoDB. It has one end point that returns ...
0
votes
0answers
61 views

Great-circle distance in Java

Today's assignment states that I need to write a complete java program which will calculate the relative orthodromic distance between two sets of geographic coordinates declared by the user. When the ...
6
votes
1answer
68 views

Expiring key/value repository. Java

The following code is in-memory repository that stores key/value pairs. It will be used on our server to keep information about active clients. Clients send keepalive messages to indicate that them ...

1
2 3 4 5
207