Looping in java - java

I know this is weird, I want do this way and I couldn't find a solution which is why asking you guys! I've methods like which I'm calling inside a class as listed below -
//
#Test
Key.chooseNew(0);
Key.navigate_toNew(1);
Key.send_keys_loginNew(2);
Key.logoutNew(3);
How can I run it in a loop in java replacing those numbers or such things doesn't exist?!

You are calling a different method on each line so there is no loop.
The only pattern I can see is:
int i = 0;
Key.chooseNew(i++);
Key.navigate_toNew(i++);
Key.send_keys_loginNew(i++);
Key.logoutNew(i++);

Have a look at Reflection in java. You could save the names of the methods to be called in a list in the order you want them called and then inside the loop you can call the method with the method name using obj.getClass().getMethod() as shown in this post.
Hope this is what you were looking for.

Related

get return type without execution of method

i have tried to make some web research, but i haven't find any answer, i would like to get immediately the return statement without the execution of the method that give it. (it run before)
public static int totalIncome (String [][]field) {
int sum = 0;
sum += chairPrice (field); // here get the number without run the method
return sum;
}
i can't post all the code of the project i am working, i am stuck to get all the ticket sold and i was thinking this was a good way, but isn't working because it run the method again.
any idea how to do this is welcome.
i remove some method beuse it did't let me post it all, i can't even post the method chairPrice.
StackOverflow say there is too much code in my post, i tried to share all the code by codeshare,
https://codeshare.io/kmRQWV
There is no way, in Java, to say "I already called this method, give me the value that was returned last time." You must explicitly store a variable somewhere to hold the value.
That said, from your code, it doesn't look like you can't call chairPrice because it's already been called before, but because some other state has changed. You will need to figure out the problem in your logic.

How can I use the return value of a method as a parameter in another method?

I have this command Out.print(getWin(dice, bet, bid)); but Java doesn't seem to know the 3 values dice, bet and bid as they are return values of other methods and therefore not known in the Main Method.
Now I know that there is the possibility to call up the whole functions to give the return values as parameters Out.print(getWin(rollTheDice(), givenBet(), givenBid())); but the big problem is that the exercise I am working on, requires me to include lines like System.Out.Print("Amount of Bid: "); into the functions itself which means that when I call up the functions as parameters it starts printing out code and asking the user to enter data again and I am trapped in an endless loop instead of getting the return value of the function getWin which is the thing I actually want.
Is there any way I can pass on the parameters as variables like suggested in the 1st row of the question? Maybe by initializing them outside the function (although I already tried that as well and it didn't work either)? Otherwise I am starting to think this exercise isn't really doable the way I am supposed to do it.
I was able to fix it:
The clue is to assign the functions to variables as the commenter suggested and at the same time executing the function:
int dice = rollTheDice();
int bet = givenBet();
int bid = givenBid();
getWin(dice, bet, bid);

Method call as a parameter of another method in Java

so i am required to do an assignment in which i follow JUnit test file to create all the code that i need(almost like a design doc), basicly its coding a vending machine, inside the test file there is this code right here
assertThat(snackMachine.chewingGums().quantity()).isEqualTo(DEFAULT_QUANTITY - 1);
assertThat(snackMachine.chips().quantity()).isEqualTo(DEFAULT_QUANTITY - 1);
assertThat(snackMachine.chocolates().quantity()).isEqualTo(DEFAULT_QUANTITY - 1);
and i was scratching my head, looking at this, how can a method inside a class have a method inside it aswell, so chewingGums() has quantity() inside of it????, is this possible in java?, because i have looked all over, and i havent seen a way to implement it, like it shows here.
chewingGums returns an object that has a quantity method.
chewingGums().quantity()
Would be the same as
Gum gum = chewingGums();
gum.quantity();
Where Gum is the type that chewingGums returns.
It's similar to this line:
new Scanner().nextInt();
new Scanner() evaluates to a Scanner object, then nextInt is called on that object. This isn't a great example since Scanner is a constructor, but it's the simplest method chaining example I could think of.

A function providing single object to recursive method

The code is posted for review on review board. My intention is not asking to review the code.
[Maze] : https://codereview.stackexchange.com/questions/33155/maze-code-review
In the above code the function solve does nothing but provide a stack object (reference to object of type stack) which would be used by a code executing recursively.
Since there are so many patterns is there a name for such a function which only assists / or does setup for recursive calls ?
If so any do's / dont's / alternatives ?
I think you did just fine. Every recursive algorithm needs some initial values for it's first step. It's common practice to encapsulate this initial call in another method so the caller doesn't have to bother with those values.
If your initial values would be more complicated to set up, you could encapsulate that in additional methods too. Say your stack needs to have some content instead of being empty. You could do something like this:
public List<Coordinate> solve() {
return getMazePath(0, 0, getInitialStack());
}
This way the solve method stays clear and easy as the entry point of your recursion.

Java weird method

I am currently trying to learn how to make games in 3D, so I watched a few YouTube-Tutorials. In one Tutorial I found this method:
int floorTexture = glGenTextures();
{
// ...
}
Source (Line 215)
I have never seen this type of method (I think it's a method) before, so I now have two questions:
Can I add parameters to this method? This code doesn't work
int texture (String texturename) = glGenTextures();
What does the =glGenTextures() do?
(I want to load different textures in one method.)
That's not a method declaration - it's a method call followed by a block.
The block itself is unnecessary, and basically just confusing. Heck, the fact that the main method is nearly 500 lines long is a good indication that this isn't code you should be taking hints from - at least in terms of structure...
That's not a method definition. It's a method call. The { after the call introduces a new block/scope. If you look immediately above that call, you'll see another block that (because it's by itself) doesn't look like a method definition.
This is a method call followed by a block of code. The block of code has nothing to do with the preceding method call. Its only use is to define a new block scope (allowing to define local variables that are visible only in this block).
Have a look at the indentation. It's just a definition of an int variable floorTexture which is initialized to the return value of glGenTextures(). The code that follows is just a block within main to ensure variables go out of scope after the block is left. So there is no method, and no way to add parameters.

Categories