170 Answered Questions for the topic Java

05/10/19

How to verify that a specific method was not called using Mockito?

How to verify that a method is **not** called on an object's dependency? For example: public interface Dependency { void someMethod(); } public class Foo { public bar(final Dependency d)... more

05/10/19

Finding if Path2D self-intersects?

I need to find if Path2D intersects itself. For now, I do it by simply extracting an array of lines from path, and finding if any of these intersect. But it has O(n^2) complexity, and so it is very... more

05/08/19

Computer Science

You have a secret integer in a certain range in your mind. Write an efficient guess procedure from computer to get the target. For example, if the target is in [0, 100], then your... more

05/08/19

Calculating the angle between two lines without having to calculate the slope? (Java)?

I have two Lines: L1 and L2. I want to calculate the angle between the two lines. L1 has points: `{(x1, y1), (x2, y2)}` and L2 has points: `{(x3, y3), (x4, y4)}`. How can I calculate the angle... more

05/08/19

Computer Science

Write a program that takes website names as keyboard input until the user types the word "stop" (without quotes) and counts how many of the website names are commercial website names (i.e., end... more

05/07/19

How can I get the SQL of a PreparedStatement?

I have a general Java method with the following method signature: private static ResultSet runSQLResultSet(String sql, Object... queryParams) It opens a connection, builds a `PreparedStatement`... more

05/06/19

Why doesn't Java allow overriding of static methods?

Why is it not possible to override static methods?If possible, please use an example.

05/05/19

How to update a value, given a key in a java hashmap?

Suppose we have a `HashMap<String, Integer>` in Java. How do I update (increment) the integer value of the string key for each existence of the string I find? One could remove and reenter the... more

05/05/19

Library for polynomial calculus in Java?

Are there any (preferably open source) library for Java that allows one to do calculus with polynomial e.g. addition, multiplying, dividing by constans etc. ? Also if it would be capable to... more

05/02/19

3D Game Geometry?

I have a simple game that uses a 3D grid representation, something like: Blocks grid[10][10][10]; The person in the game is represented by a point and a sight vector: double x,y,z,... more

05/02/19

Is a point inside regular hexagon?

I'm looking for advice on the best way to proceed. I'm trying to find whether a given point A:(a, b) is inside a regular hexagon, defined with center O:(x, y) and diameter of circumscribing circle.... more

05/02/19

How to fill color on triangle?

I draw a triangle using line. How can I fill color on it? So far I can only color the line but not fill the color. public void paintComponent(Graphics g){ super.paintComponents(g); ... more

04/30/19

What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?

What's the difference between `getPath()`, `getAbsolutePath()`, and `getCanonicalPath()` in Java? And when do I use each one?

04/30/19

How do I split a string with any whitespace chars as delimiters?

What regex pattern would need I to pass to the `java.lang.String.split()` method to split a String into an Array of substrings using all whitespace characters (' ', '\ ', '\ ', etc.) as delimiters?

04/29/19

Java: Date from unix timestamp?

I need to convert a unix timestamp to a date object. I tried this: java.util.Date time = new java.util.Date(timeStamp); Timestamp value is: `1280512800` The Date should be "2010/07/30 -... more

04/29/19

How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?

How can I tell if the JVM my application runs in is 32 bit or 64-bit? Specifically, what function or preference do I access to detect this within the program?

04/29/19

Type List vs type ArrayList in Java?

(1) List<?> myList = new ArrayList<?>(); (2) ArrayList<?> myList = new ArrayList<?>(); I understand that with (1), implementations of the List interface can be swapped. It... more

04/29/19

How do I find the inverse tangent of a line?

I've got a line (x1,y1) and (x2,y2). I'd like to use tan inverse to find the angle of that line, how would I do so in java? I'd like to see what angle the line makes in relation to x1,y1

04/27/19

ArithmeticException: "Non-terminating decimal expansion; no exact representable decimal result"?

Why does the following code raise the exception shown below? BigDecimal a = new BigDecimal("1.6"); BigDecimal b = new BigDecimal("9.2"); a.divide(b) // results in the following exception:... more

04/27/19

Convert list to array in Java?

How can I convert a `List` to an `Array` in Java? Check the code below: ArrayList<Tienda> tiendas; List<Tienda> tiendasList; tiendas = new ArrayList<Tienda>(); Resources res =... more

04/26/19

Any way to declare an array in-line?

Let's say I have a method m() that takes an array of Strings as an argument. Is there a way I can just declare this array in-line when I make the call? I.e. instead of: String[] strs = {"blah",... more

04/25/19

Where does the @Transactional annotation belong?

Should you place the `@Transactional` in the `DAO` classes and/or their methods or is it better to annotate the Service classes which are calling using the DAO objects? Or does it make sense to... more

04/24/19

represent a positive integer x as the sum of numbers each to the power k. For example, 25 = 3^2 + 4^2.

I need to write a function in Java which would represent a positive integer x as the sum of numbers each to the power k. For example, 25 = 3^2 + 4^2. So, once I feed 25 and 2 as arguments in it, I... more

04/24/19

Incrementor logic?

I'm trying to get deeper with post and pre incrementors but am a bit stuck with the following expression : public static void main(String[] args) { int i = 0; i = i+=(++i + (i+=2 + --i) -... more

04/22/19

IntelliJ show JavaDocs tooltip on mouse over?

In Eclipse, when hovering over a method, variable, etc. a tooltip is displayed with the corresponding JavaDocs. Is there such a feature in IntelliJ?

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.