Is it wise to "exploit" java call-by-value [closed] - java

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 6 years ago.
Improve this question
Consider the following siutation. A user can enter something like
30D, 90D, 180D, 360D, 1M, 3M, 6M, 12M, 1Y
(D=day, M=month, Y=year).
I want to compute the number of months with the following two methods.
private int getLengthOfPeriodInMonths(Integer lengthOfPeriod, String unitOfPeriod) {
int periodInMonths = lengthOfPeriod;
if ("D".equals(unitOfPeriod)) {
periodInMonths = lengthOfPeriod / 30;
} else if ("Y".equals(unitOfPeriod)) {
periodInMonths = lengthOfPeriod * 12;
}
return periodInMonths;
}
private int getLengthOfPeriodInMonths(Integer lengthOfPeriod, String unitOfPeriod) {
if ("D".equals(unitOfPeriod)) {
lengthOfPeriod = lengthOfPeriod / 30;
} else if ("Y".equals(unitOfPeriod)) {
lengthOfPeriod = lengthOfPeriod * 12;
}
return lengthOfPeriod;
}
Because java works with call-by-value with a reference passed as a value lengthOfPeriod won't change outside of the method. I am not sure what is more appropriate to use.
I know that this method could be refactored by using an enum Periods or something like that. But let's not discuss this here.

Assigning to method parameters is not a good idea (and they should've been made final by default IMHO). If you really want to avoid the extra variable (not that it really makes a difference), you can put returns in your if clauses.
But don't use parameters as "automatic local variables". It can cause hard to see bugs and doesn't make your code any more performant.

The only difference is that you use an own variable int periodInMonths = lengthOfPeriod; in the first method. But this isn't necessary!
You can use the second one.. It does what it should!
int lengthOfPeriodInMonths = getLengthOfPeriodInMonths(lengthOfPeriod, unitOfPeriod);
and you have the calculated int outside of the method stored in lengthOfPeriodInMonths
P.S.: This call
lengthOfPeriod = lengthOfPeriod / 30;
is equivalent to
lengthOfPeriod = new Integer(lengthOfPeriod / 30);
(look "auto-boxing" in Java).
So, as you said, Java uses call-bay-value. The variable lengthOfPeriod gets assigned a new refernece in this call. So this calculation would be lost outside the method! That's why you must return the newly computed value!

First advice: never ever modify the method parameters. They should reflect the value passed by the user of the method at all times.
Second advice: if someone advises you to never do something, especially if he or she uses the phrase "never ever", take his or her advice with a grain of salt. There are no rules without exceptions (never ever ;-). Such rules are great rules of thumb, but always use your own judgement. Choose the solution which you think is clearer. I know of some situations where I found modifying method parameters was more readable than the alternative.
For example, sometimes you might want to allow the user of the method to pass null to the method, but then replace it with some default value. I find the following method
public void doSomething(String s) {
if (s == null) s = "";
System.out.println(s);
}
much clearer than introducing a local variable just to include the default value:
public void doSomething(final String s) {
String sOrEmpty = s == null ? "" : s;
System.out.println(sOrEmpty);
}

Related

Single equal in different situation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have this code...
class Test {
public static void main(String[] args) {
Boolean mySuperBoolean = Boolean.FALSE;
System.out.print("a");
if (mySuperBoolean = Boolean.TRUE) {
System.out.print("b");
}
System.out.print("c");
}
}
I am new to Java, but I knew single equal (=) is used to assign. And double equals (==) is used to check if object is referred to the same location in memory. However, in this case I do not understand how the 'b' is being printed with a single equals, but I understand changing it to a double equals sign will not print it out
if (mySuperBoolean = Boolean.TRUE) will assign Boolean.TRUE to your mySuperBoolean variable and the condition will evaluate to true, hence whatever is inside your if it will always execute
The result of the assignment operator = will be the assigned value. So if (mySuperBoolean = Boolean.TRUE) will always evaluate to true.
Assignment is an expression which resolves to whatever was assigned, in this case(mySuperBoolean = Boolean.TRUE) is an expression which resolves to Boolean.TRUE.
This is really only useful in a few specific situations. One such case is the following idiom:
String line;
while ((line = readLine()) != null) {
//...
}
Or even
i = j = k = 0; // equal to: i = (j = (k = 0))
It's a controversial feature because it allows probable bugs such as yours to compile successfully. To mitigate this, some people will invert the operands (a "yoda condition"):
if (Boolean.TRUE == mySuperBoolean)
This works because if I forget the second equals then the compiler will throw an error because Boolean.TRUE is final and cannot be assigned to.
In essence, what happens here boils down to:
if (Boolean.TRUE) {
System.out.print("b");
}
That assignment puts TRUE into the variable, the variable is boolean, and checked for its current value, end of story.

Elegant way to return a value inside an if [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 6 years ago.
Improve this question
Imagine you want to write a simple method, which take a String argument named "foo". The method should return the int "1" if foo equals "toto" and "2" in the others cases. What is the best way to write this method without using a ternary expression ?
I have several solutions but can't decide by myself which one is the best in Java.
Solution 1 :
public int method1(String foo){
if("toto".equals(foo)){
return 1;
}
return 2;
}
Solution 2 :
public int method2(String foo){
if("toto".equals(foo)){
return 1;
} else {
return 2;
}
}
Solution 3 :
public int method3(String foo){
int result = 2;
if("toto".equals(foo)){
result = 1;
}
return result;
}
Solution 4 :
public int method4(String foo){
int result = 0;
if("toto".equals(foo)){
result = 1;
} else {
result = 2;
}
return result;
}
Any other ideas?
Remember, I search a solution without ternary condition.
Thanks,
EDIT : I know that all the previous methods give the expected result, so all of them can be used. What I wanted to know and what wasn't clear in my initial question is : Is there some kind of standard in the Java community about this situation ?
For example, I know there is a standard about the position of the brackets : you can put them a line after the if-statement but most of the people won't do it in Java.
I know that the ternary expression is probably the best here. But, as you can imagine, my "real" method is more complicated than that and can't used ternary. The problem I presented here is just a simplification, not the entire, real problem.
I think, the best is the most readable
public int method(String foo){
return "toto".equals(foo)?1:2;
}

How to fix a java.lang.StackOverflowError [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 6 years ago.
Improve this question
My teacher makes us do these error fixing assignments, and I just can't fix this one. That's why I came here. I want to ask for one favor and a few questions
Questions: What does the error message mean in detail(I just want to know for my own curiosity), Why does it occur specifically using my code, How can I fix it
Favor: Can u please fix my code, Sorry if this seems like a way to get an answer but I'm a bit lost.
Here's the code
public class Recursion
{
public static void indifferent( int x, int y )
{
System.out.print( x );
indifferent( x - 1, y + 2 );
System.out.print( y );
}
}
public class RecursionDriver//-the driver class
{
public static void main( String [] args )
{
Recursion.indifferent( 7, -1 );
}
}
If you examine the code it should be obvious that it can never terminate. Every call to indifferent makes another call to indifferent. Unconditionally.
The StackOverflowError happens because the nested calls have gotten too deep. Java method calls use a stack to hold local variables (and the return address, and other stuff). The stack has a fixed size. If you have too many nested calls, the stack overflows.
How to fix it? Rewrite the code to do what it is currently doing, but without using recursion.
But first you need to understand what it is doing. How do you do that? Well ... basically ... you read the code.
Well I'm not completely sure what indifferent means because this is what my teacher told us to put as the name for the class(she said its meaningless and told us to ignore it)
Yup. Ignore the name, and read what the code actually does. If you can't figure it out ... then "hand execute" it using a pencil and paper.
In recursion you try to divide your problem in sub problems and try to compute the result. Hence, in recursion you always need to have a base case. Base case in the final depth of your recursion. Once you reach the base case you start coming out of the recursion and get you final answer.
In your code:
public static void indifferent( int x, int y )
{
System.out.print( x );
indifferent( x - 1, y + 2 );
System.out.print( y );
}
You do not have a base case, do your code just keeps going deep in recursion.
public static void indifferent( int x, int y )
{
if(x == 1) //example of a base case
return;
System.out.print( x );
indifferent( x - 1, y + 2 );
System.out.print( y );
}

Beginner Java Scope Issue [duplicate]

This question already has an answer here:
Beginner Java: Variable Scope Issue
(1 answer)
Closed 7 years ago.
I'm new to programming and seem to be running into issues with when a variable, class, etc can and can't be referenced. Below is an example, hoping one of you can fix the specific issue but also help me understand it more broadly so I don't run into it again and again.
to try and avoid posting a bunch of code please note that a Question class is defined as well as a setText, setAnswer, checkAnswer, and display method are all defined elsewhere (all public).
The relevant code is below and I have two questions:
Why is the variable first not recognized in the method presentQuestion()?
At the very end there, why can't I just call the method checkAnswer() on first, i.e. why can't I just do first.checkAnswer(response);? Why do I have to define it in a new variable: boolean outcome = first.checkAnswer(response);?
Code:
/**
* This program shows a simple quiz with two questions.
*/
public class QuestionDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Question first = new Question();
first.setText("Who was the inventor of Java?");
first.setAnswer("James Gosling");
Question second = new Question();
second.setText("Who was the founder of Udacity?");
second.setAnswer("Sebastian Thrun");
int score = 0;
score = score + presentQuestion(first, in);
// Present the second question
score = score + presentQuestion(second, in);
System.out.println("Your score: " + score);
}
/**
* Presents a question to the user and obtains a response.
* #param q the question to present
* #param in the scanner from which to read the user input
* #return the score (1 if correct, 0 if incorrect);
*/
public static int presentQuestion(Question q, Scanner in) {
// Display the first question
first.display();
System.out.println("Your answer:");
String response = in.nextLine();
// Check whether the response was correct
// If so, print "true" and return 1
// Otherwise, print "false" and return 0
boolean outcome = first.checkAnswer(response);
System.out.println(outcome);
if (outcome) {
return 1;
}
else {
return 0;
}
}
}
The reason you can't use the variable first inside presentQuestion is because it's defined in main, and therefore not visible outside of main. Isn't this precisely why you gave presentQuestion its Question q parameter, however?
It seems to me that this is what you want to do:
public static int presentQuestion(Question q, Scanner in)
{
// Display the first question
q.display();
System.out.println("Your answer:");
String response = in.nextLine();
// Check whether the response was correct
// If so, print "true" and return 1
// Otherwise, print "false" and return 0
boolean outcome = q.checkAnswer(response);
System.out.println(outcome);
if (outcome) {
return 1;
} else {
return 0;
}
}
Note that references to first have been replaced by references to q.
To try and clear up what I imagine your confusion may consist in, imagine if presentQuestion were called from another method than main, in which case no first variable would be declared at all. What would then happen to the references to first inside of presentQuestion, now not referring to anything at all? This is why you need to explicitly pass the data you want as parameters. Different methods are independent blocks of code, and you can't intermingle variable references between them even if they happen to call each other.
As for question 2, there should indeed be no problem with checking if(q.checkAnswer(response)) directly, without using the outcome variable. I'm guessing you were simply confused by the error emitted by the compiler when first wasn't recognized again.
first is a local variable, that means it can only be accessed inside the method in which it is defined.
You don't have to put the result of checkAnswer() into a boolean before using it. Actually, if (checkAnswer(response)) { ... } is valid.
presentQuestion takes a question as a parameter. In main, you're calling it on the first question, and then on the second question; it looks like the intent is that you use presentQuestion on the first question, and then on the second question. So far, so good.
The problem is that in presentQuestion, you're referring to the question (which could be the first or the second question) as q in the parameter list. All you need to do is use q instead of first in the rest of the method.
When I was new to programming, I had this problem as well! Then I found out it is very simple.
For your first question, first is declared in the main method and you want to use it in presentQuestion method. But presentQuestion and main are different methods! So you can't get to first in presentQuestion. As you can see, there is a Question-typed parameter in the presentQuestion method. This is like you telling first, "Come here, man! And then change your name to q." When you do pass the argument first to presentQuestion,
presentQuestion (first, in);
first comes to the pressentQuestion method with its name being as q. So you should use q instead of first in the presentQuestion method.
Now the second question, using a variable in this context is not needed. But to increase efficiency, use a boolean variable to store the result of checkAnswer. Let's imagine what happens if you don't use a boolean variable.
System.out.println(q.checkAnswer(response));
if (q.checkAnswer(response)) {
return 1;
} else {
return 0;
}
See? you called q.checkAnswer twice! This would slow down your program so you should use a boolean variable.

Java Recursion - Did I do this right? [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 8 years ago.
Improve this question
My job is to write a recursive version to this method. From what I understand Recursion is starting with a base call (if something then return) followed by an else which unwinds back to the original base. Like starting with a deck, adding on to the deck then removing cards from the deck until you are back to the original deck.
With that in mind here it is.
public static long fact(int n)
{
long result = 1;
while(n > 0)
{
result = result * n;
n = n - 1;
}
return result;
}
//my recursive version:
public static void recFact(int n)
{
if(n==0)
{
return n; // ir 0 it really doesn't matter right?
}
else
{
return recFact(n-1);
}
}
This is just an example test problem for an exam I have coming up, just want to make sure I have a handle on recursion. Did I do this right? If not what am I missing? please no answers in questions, just tell me what I did wrong and maybe some advice on better ways to understand it.
Thanks.
No, this recursive solution is not correct.
For every positive n, you're just return rectFact(n-1), which will recourse until you reach 0, at which point it will return. In other words, your function will always return 0. You're missing the part where you multiply the current n with rectFact(n-1). Additionally, note that 0! is 1, not 0:
public static int recFact(int n)
{
if(n==0)
{
return 1;
}
else
{
return n * recFact(n-1);
}
}
And finally, since the if clause returns, the else is somewhat redundant. This doesn't affect the method's correctness, of course, but IMHO the code looks cleaner without it:
public static int recFact(int n)
{
if(n==0)
{
return 1;
}
return n * recFact(n-1);
}
Your recursive version does no multiplication, and it will return zero for any input. So no, you didn't do it right.
But, the recursive version DOES recurse, so you have that going for you! To understand what's going wrong, walk through a very simple case.
Client calls recFact(3)
This will return to client recFact(2)
Which will return to above recFact(1)
Which will return to above recFact(0)
Which will return to above 0.
There are two major things going wrong:
Your base case is wrong (zero is too low)
You're not doing any multiplication
Good attitude about not wanting the solution handed to you! Hopefully these pointers wil help you figure it out.
EDIT: Apparently I misunderstood your grammar and you did want the solution.
Any recursive function needs three things:
The terminating condition: This tells the function when to stop calling itself. This is very important to avoid infinite recursion and avoid stack overflow exceptions.
The actual processing: You need to run the actual processing within each function. In your non recursive case, this was result = result * n. This is missing from your recursive version!
A collector/agggregator variable: You need some way to store the partial result of the recursive calls below you. So you need some way to return the result of recFact so that you can include it in processing higher up in the call chain. Note that you say return recFact(n - 1) but in the definition recFact returns void. That should probably be an int.
Based from your example you are missing the return type of your recFact which is int
Also recFact will always return 0 because you are not multiplying n each time to the recursion call of the method.
There are two ways to write recursive routines. One is the "standard" way that we all are taught. This is one entry point that must first check to see if the recursive chain is at an end (the escape clause). If so, it returns the "end of chain" value and ends the recursion. If not at the end, it performs whatever calculation it needs to get a partial value according to the level and then calls itself passing a value the next increment closer to the end of the chain.
private final int InitialValue = 15;
System.out.println( "Fact(" + InitialValue + ") = " + recFact( InitialValue ) );
public int recFact( int val ){
if( val < 2 ){
return 1;
}
else{
return recFact( val - 1 ) * val; // recursive call
}
}
//Output: "Fact(15) = 2004310016"
In regular recursion, a partial answer is maintained at each level which is used to supplement the answer from the next level. In the code above, the partial answer is val. When first called, this value is 15. It takes this value and multiplies it by the answer from Fact(14) to supply the complete answer to Fact(15). Fact(14) got its answer by multiplying 14 by the answer it got from Fact(13) and so on.
There is another type of recursion called tail recursion. This differs in that partial answers are passed to the next level instead of maintained at each level. This sounds complicated but in actuality, make the recursion process much simpler. Another difference is that there are two routines, one is non recursive and sets up the recursive routine. This is to maintain the standard API to users who only want to see (and should only have to see)
answer = routine( parameter );
The non-recursive routines provides this. It is also a convenient place to put one-time code such as error checking. Notice in the standard routine above, if the user passed in -15 instead of 15, the routine could bomb out. That means that in production code, such a test must be made. But this test will be performed every time the routine is entered which means the test will be made needlessly for all but the very first time. Also, as this must return an integer value, it cannot handle an initial value greater than 19 as that will result in a value that will overflow the 32-bit integer container.
public static final int MaxFactorialSeq = 20;
private final int InitialValue = 15;
System.out.println( "Fact(" + InitialValue + ") = " + recFact( InitialValue ) );
public int recFact( int value ){
if( value < 0 || value > MaxFactorialSeq ){
throw new IllegalArgumentException(
"Factorial sequence value " + value + " is out of range." );
}
return recFact( value, 1 ); // initial invocation
}
private int recFact( int val, int acc ){
if( val < 2 ){
return acc;
}
else{
return recFact( val - 1, acc * val ); // recursive call
}
}
//Output: "Fact(15) = 2004310016"
Notice the public entry point contains range checking code. This is executed only once and the recursive routine does not have to make this check. It then calls the recursive version with an initial "seed" of 1.
The recursive routine, as before, checks to see if it is at the end of the chain. If so, it returns, not 1 as before, but the accumulator which at this point has the complete answer. The call chain then just rewinds back to the initial entry point in the non-recursive routine. There are no further calculations to be made as the answer is calculated on the way down rather than on the way up.
If you walk though it, the answer with standard recursion was reached by the sequence 15*14*13*...*2*1. With tail recursion, the answer was reached by the sequence 1*15*14*...*3*2. The final answer is, of course, the same. However, in my test with an initial value of 15, the standard recursion method took an average of 0.044 msecs and the tail recursion method took an average of 0.030 msecs. However, almost all that time difference is accounted for by the fact that I have the bounds checking in my standard recursion routine. Without it, the timing is much closer (0.036 to 0.030) but, of course, then you don't have error checking.
Not all recursive routines can use tail recursion. But then, not all recursive routines should be. It is a truism that any recursive function can be written using a loop. And generally should be. But a Factorial function like the ones above can never exceed 19 levels so they can be added to the lucky few.
The problem with recursion is that to understand recursion you must first understand recursion.
A recursive function is a function which calls itself, or calls a function which ultimately calls the first function again.
You have the recursion part right, since your function calls itself, and you have an "escape" clause so you don't get infinite recursion (a reason for the function not to call itself).
What you are lacking from your example though is the actual operation you are performing.
Also, instead of passing a counter, you need to pass your counter and the value you are multiplying, and then you need to return said multiplied value.
public static long recFact(int n, long val)
{
if(n==1)
{
return val;
}
else
{
return recFact(n-1, val) * n;
}
}

Categories

Resources