test a java method in netbeans [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Let's say I found this piece of code and I want to test it in Netbeans before I incorporate it:
ArrayList list;
for(String s: list)
{
Integer c = stringsCount.get(s);
if(c == null) c = new Integer(0);
c++;
stringsCount.put(s,c);
}
Is there a way to test the above code in Netbeans without having to create a temp class with a main? I believe there is a way in Eclipse but I am looking for a way in Netbeans. Thank you.

Is there a way to test the above code in Netbeans without having to create a temp class with a main?
As far as I know, you can't do that. Your program needs an entry point. The main method is supposed to be your program's entry point.
If you are reluctant to always create a new class. You can always have a class for testing. Just replace the old codes with new testing codes whenever you need it.
P.S: I don't think it can be done in Eclipse as well.

Related

intellij println declaration [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to use ''sout'' keyword in intellij for calling system.out.println() quickly but it seems like doesnt work.
Looks like, I have some problems in settings sections.
Does anyone know the way the quick way problem
It Looks like this:
I solved the problem it was all about defining println() inside a method.
I solved it. The solution is that declaring the println() method within class or
inside another method as well.
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
int fun()
{
return 20;
}
}
Yeah just use it inside in any method, like this:
According to Intellij support question
Make sure you are inside the method where this completion makes sense, not just inside the class.

Best Programming Practice for using packages(Java) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Which one is better to do...
importing package or using them directly inside a method
for eg:-
import java.util.Arrays;
int arr[] = new int[10];
Arrays.sort(arr);
Is the above one better?
-----(OR)-------
int[] arr = new int[10];
java.util.Arrays.sort(arr);
According to me both perform same thing...
But I wanted to know which of these is the best programming practice...
Right now there is no difference but you'll start seeing it when you have your own Arrays class.
If it is unique class name go with first way. That's the best way to organise your imports. If you have duplicated class names (although not suggestable), you'll be forced to have the second way.
It's all by developer but I prefer import classes because code it's more readable.
Second is old best practice but as I say it is from the developer to the developer

For loop Testing in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
public void numberOfChoices() {
for (int i = 0; i < 10; i++) {
int guess = scan.nextInt();
}
}
how can i test this for loop in junit. my method should be void.
i think i couldn't make myself clear. my question is that how can i test the loop that actually it goes ten time? i hope you understand now. but i don't know some people just try to be oversmart and give negative point to my question i think if you couldn't understand ask for more explanation. and this is question is not that simple to solve too so don't understand why i got negative....
Writing code easily to test is the first step for writing test.
Every single method should have their meaning and only do one thing.
If you face problem in writing test, you might think about it.
If your method must be void and you don't work with database I would advise to create getters for the variables that you want to verify. For the loop you might want to have some Array or List, for example, and then in your test do something like this:
assertEquals(expectedList, yourClass.getList);
However, it is not the ideal scenario because you should not change the production code to fit your testing needs. If you work with database then create a separate query in the test to fetch the needed data.

new Obj, always or sometimes? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Should I always make new objects out of everything in java even if i know i will only use one?
obj = new obj();
obj.method();
or just
classOfobject.method();
Why should I bother with the creation of a new obj in a situation like this?
classOfObject.method() only works if method is static.
Having said that, if your class truly represents a "thing", be that a user, a car, whatever, then you should make the method non-static and use new to make an object and invoke information on it.
If the class is a container for methods that don't have anything to do with objects themselves, e.g. a Utils class like StringUtils, then you can use static methods, e.g. StringUtils.toUpperCase(), and be happy with it.

java interface and return types [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have been trying for a while, but now I'm wondering if I can. Is it possible for a method in an interface to return a variable or an array without needing two interfaces or methods(if possible)?
Every method has to specify a return type and stick with it. There are no "union types" like "returns a String or an int[]".
If you really have a method that can return two different things, you can
make a wrapper type (StringOrIntArray) that can hold both
or
have the method return Object which can be pretty much anything, including arrays, and use instanceof at the caller to see what you got.
Both options are not particularly attractive. Maybe take a step back and rethink the bigger picture.
What is wrong with
interface Foo{
int foo1();
//or....
int[] foo2();
}
?

Categories