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.
Related
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.
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.
I am trying to understand a sample code in Webots (robot simulation program).
I have faced this code :
Servo rightShoulderPitch = getServo("RShoulderPitch");
rightShoulderPitch.setPosition(1.5);
I do not understand what is meat by the first line. It look like that "rightShoulderPitch" is an object of Servo class but it is not created as usual and how 'getServo' (i think it is a method) comes here .
This class's header is, if it helps:
public class FieldPlayer extends Robot {
Also it has description by the company in the reference manual, but I could not understand what they mean. It can be found here search for getservo.
--- RShoulderPitch: is the name of the shoulder of the robot
I will appriceite help very much.
Thanks
This line:
Servo rightShoulderPitch = getServo("RShoulderPitch");
... calls the getServo method, passing in the string value "RShoulderPitch". The return value is used as the initial value of the rightShoulderPitch variable, which is of type Servo. (Note that rightShoulderPitch isn't an object - it's a variable. It has a value, which would either be null or a reference to an object.)
We can't tell what the return type of getServo is, but it's got to be something which is implicitly convertible to Servo - so either Servo itself, or some subclass.
getServo could:
Create a new object, and return a reference to it
Return a reference to an existing object (e.g. from a cache)
Return null
Throw an exception
If none of that helps, please clarify exactly what you don't understand. It sounds like you may be new to Java - in which case, learning "just Java" without the Webots API would probably be a good approach; only learn Webots when you're confident in the language itself.
To complement Jon's excellent answer, I'll try to explain you in much more general terms.
When you want a sandwich, you have two solutions:
prepare the sandwich yourself. This would be the equivalent of the code Sandwich s = new Sandwich()
go to a snack bar and ask them a sandwich. This would be the equivalent of the code Sandwich s = snackBar.getSandwich("Ham & Cheese").
In the latter case, it's the snackBar object's getSandwich() method which will use the name of the sandwich you want ("Ham & Cheese") to prepare a sandwich and return it for you. This method will thus probably, internally, call new Sandwich(). But it could also delegate to another object and call, for example: cook.prepareSandwich("Ham & Cheese"). And in this case, it's the cook object which will call new Sandwich(). Or the snackBar object could also just get a sandwich that has been prepared in advance and stored in some cache: fridge.getSandwich("Ham & Cheese").
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.
I see the following code syntax. Calling
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("x11")
.setOAuthConsumerSecret("x33")
.setOAuthAccessToken("x55")
.setOAuthAccessTokenSecret("x66");
All the methods after each other without using the object instance.
How does this work in programming my own class when i want to use this kind of calling methods?
make each of those methods return the same object which they are called on:
class MyClass{
public MyClass f(){
//do stuff
return this;
}
}
It's a pretty common pattern. Have you ever seen this in C++?
int number=654;
cout<<"this string and a number: "<<number<<endl;
each call of the operator << returns the same ostream that is passed as its argument, so in this case cout, and since this operation is done left to right, it correctly prints the number after the string.
That style of writing code is called 'fluent' and it is equivalent to this:
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey("x11");
cb.setOAuthConsumerSecret("x33");
cb.setOAuthAccessToken("x55");
cb.setOAuthAccessTokenSecret("x66");
Each method returns 'this' in order to accommodate this style.
If you use this style, be prepared to encounter some inconvenience while debugging your code, because if you want to single-step into only one of those methods instead of each and every single one of them, the debugger might not necessarily give you the option to do that.
This is simmilar to a Builder design pattern.
Here you can find a excellent example of it inspired by Josh Bloch's code from Effective Java 2nd ed.