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
I was going through some of the questions online and I came across this code. I couldn't understand the meaning of this particular line of code. Please can anyone explain me the meaning of this line of code.
So, there are two classes Student class and Course class. Each class has its own setters and getters. Now, there is this one output statement which is something like this:
System.out.println("I like " + student2.getCourse().getCourseCode());
**bdw student2 is an object of the Studentclass
The part that i am confused is student2.getCourse().getCourseCode()
It looks like two methods are somehow linked or something. What is the meaning of this line. Any article or suggestions is really appreiciated. Thank you!
student2.getCourse() returns a course. It is possible to call getCourseCode() on a course. This is an example of method chaining.
The equivalent code is:
Course tempVar = student2.getCourse();
System.out.println("I like" + tempVar.getCourseCode());
As you learn more programming, you will find this to be consistent. A variable declared Course is clearly a course, but the result of a method called Course.getCourse also is a course, and you can use it as a course (including calling the methods of course (no pun intended)).
System.out.println("I like " + student2.getCourse().getCourseCode());
I've formatting the line above fro reference. Stundent2.getCourse() returns the course that the student is enrolled in (presumably). getCourseCode() is then called on the course that was returned previously
Related
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.
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 8 years ago.
Improve this question
So, I have an object in a class called "arrays":
static arrays VideoGame = new arrays(arrays.product[9], arrays.price[9]);
and a function:
public static void getPrice(int price){
System.out.println("The cost is: " + price);
}
In my main file I call that function like this:
arrays.getPrice(arrays.price[9]);
Is there anything like
arrays.getPrice(VideoGame.price);
that lets you actually use the VideoGame object with the function?
EDIT: I forgot to say that "arrays" is a class I made with functions that create arrays.
I'm just going to go out on a limb and try to answer as best I can.
Assuming arrays is some custom class you made that just defines two arrays and offers getters/setters for those arrays:
Yes, what you seem to what to do is entirely possible although it seems pointless for all practical application. To do so you would just define a variable named "price" in your arrays class and make it public.
You could then use your exact code
arrays.getPrice(VideoGame.price);
However, by adding price to arrays this variable is then added to every instance of arrays. This can be avoided by adding the "static" keyword modifier to your new price variable which means that it will be instantiated at most once. Additionally, when you add the static keyword, I believe you no longer need to specify your specific object - meaning your code could now look even like:
arrays.getPrice(arrays.price);
By making a variable public, you avoid having to make a getter/setter for it, although this is discouraged.
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 8 years ago.
Improve this question
I have the below piece of code and statements 2(scopeId) and 3(productScopeId) are repetiting in many methods. How can I refactor the below methos in such a way that it can be used in many methods. I don't want to create a class with these two variables and return scope and product scope id. Can you please suggest any other better way. Thanks in advance for your help.
public List<Product> getAllHydroProducts()
throws RepositoryException {
String productTypeId = getProductTypeId(Group.GROUP_TYPE_NAME_HYDRO, getAllProductTypes());
String scopeId = getScopeTypeId(Group.SCOPE_TYPE_NAME_HTX, getAvailableScopes());
String productScopeId = getScopeTypeId(Group.SCOPE_TYPE_NAME_PROJECT, getAvailableScopes());
return getProductsByTypeAndScope(productTypeId , scopeId ,productScopeId );
}
I work under the assumption that you have several constants like Group.GROUP_TYPE_NAME_HYDRO, and all your methods are just like the one you show us, but with different constants (I believe you should add those other methods to your question, since without them there is no visible repeated code).
You have two possible scenarios (again, with more code would be easier):
1) You are only changing the product type constant, keeping the other two all the same in your other methods. If that's the case, you can just extract a more generic method like:
public List<Product> getAllProductsOfType(YourConstantType productType) throws RepositoryException {
return getProductsByTypeAndScope(
getProductTypeId(productType, getAllProductTypes()),
getScopeTypeId(Group.SCOPE_TYPE_NAME_HTX, getAvailableScopes()) ,
getScopeTypeId(Group.SCOPE_TYPE_NAME_PROJECT, getAvailableScopes())
);
}
Note that I don't know what your constants are, since you did't put them on your question, that's why YourConstantType is there.
Then your existing methods would just call this new one with their parameter.
2) Another scenario: You also have different scopes on your methods. In this case, I might go with a builder pattern
Also... is that a checked exception what I see there? I hope not ...
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 8 years ago.
Improve this question
I'm reading Martin Fowler's Refactoring book. In many of his refactoring examples, he is using variables that start with _varname. Why? Is this some old convention that is before my time? In the past year when I started learning Java, I have not seen anyone at work use this. Please advise as to where and why should this be used?
I'm already seeing differences in answers to this question...
Why does martin fowler do this in this code for extract method refactoring?
FROM:
void printOwing(double amount) {
printBanner();
//print details
System.out.println ("name:" + _name);
System.out.println ("amount" + amount);
}
TO:
void printOwing(double amount) {
printBanner();
printDetails(amount);
}
It is a convention to start the names of private fields of an object with an underscore in order to distinguish them from local variables in the code. This convention is not universal. Personally, I think that it is a bit superfluous when you have syntax highlighting that also shows the difference.
An alternative (although you could also use both) is to always reference members through this:
package org.foo.bar;
class Baz {
private String quux;
Baz (String quux) {
this.quux = quux;
}
String getQuux () {
return this.quux;
}
}
The convention is often used for private fields.
You don't have to use it, the most important is to be consistent, so if you are working on an existing code base, continue with that style.
Most likely, to distinguish local variables from class members.
EDIT: Now that you've added sample code, that is indeed the case.
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();
}
?