I am taking intro to java programming this semester. i understand what i am supposed to do on this homework assignment but i cant figure out what the minus symbol is supposed to mean
Quote:
This homework will be counted as much as the full score for a single quiz. Be serious and work carefully.Consider the following code about while loop:
int n=19;
while (n>2)
{
System.out.print(n+10);
n-=3;
}
and the objective of the assignment is
Quote:
Now do the following:
For each iteration (or loop) list the beginning value for the variable n, the (Boolean) value of the comparison (n>=2) and the output of the iteration, if any. Indicates when the loop terminates and why it does.
i just dont understand what the minus sign in n-=3; it is no where in my notes except as a armithic operator.
More to the point, this also applies to *, /, and %.
Ericcumbee, please pay more attention to spelling. While I can still uderstand you, the Java compiler is not as smart.
BTW, if you need any help, I can help you. I've already passed the first year of Java and I'm currently learning the second year.
__________________ To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
TCPA would take your freedom! Say NO! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
HDTV the way it should be: To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Originally Posted by A friend of mine who has a Linux kernel named after his girlfriend.
If I was VirtualBox, I could load my virtualization module into Hannah and boot up another kernel in the same address space.
*puts on java instructor hat* You should check with your instructor to make sure outside help like this is appropriate *takes hat off*
Now, as was said before n-=3 is an instance of what's called an "assignment operator"
expressions of the form:
variable = variable operator expression;
can be rewritten in a shortcut form as:
variable operator= expression;
One thing to remember is that precedence rules may seem non-intuitive. The expression is done before the variable operator part. An example:
a = a * 4 - b;
written in this way, a * 4 is done first, then b is subtracted. You may be tempted to rewrite that as:
a *= 4-b;
however this is not the same. This is actually equivalent to:
a = a * (4 - b);
whose value may be considerably different.
the System.out.print(n+10) is doing addition, but not modifying the value of n. The only time a variable is modified is when it is the left-hand side of an assignment (to the left of the =) or when ++ or -- is used on it (if you've covered pre- and post-increment/decrement yet.)
You're not taking into account the addition when doing your output, so that column is entirely incorrect. Also you have an ordering problem, the n-=3 is done after the print, so make sure you have the correct value at the correct place. Remember the program is done in sequential order unless there is a control structure, usually indicated by the curly brace.
Also you need to be careful, in the code you posted the condition on the loop is strictly greater than, but in your spreadsheet you say greater than or equal to, which is it?
To double check your answers, why not just make a program on your own and see what happens. It will be good practice.
i would write out my code but i am having a problem executing it,
i do the javac filename.java
and it compiles without any errors
then i do java filename
and it gives me
Exception in thread "main" java.lang.NoClassDefFoundError: Add
and then i run the program on a lab computer and it runs fine.
this isnt due until thursday so i think i am gonna work on some other stuff tonight and comeback to it in the morning.
You must not have named the class the same as your filename. Remember case matters. Regardless of what you named the file, you can run it with the command:
java CLASSNAME
where CLASSNAME is the identifier you used after class in your program. Another way to know is to see what the name of the .class file that javac created for you is named. That (without the .class extension) is what you want to use.
ok i omited the equal sign somehow when i copied the code snippet from onenote to my first post it should be
Quote:
nt n = 19;
while (n>=2)
{
System.out.print(n+10);
n -= 3;
}
Now do the following:
For each iteration (or loop) list the beginning value for the variable n, the (Boolean) value of the comparison (n>=2) and the output of the iteration, if any. Indicates when the loop terminates and why it does.
thank you so very much for your help. it is deeply appreciated. those little details are what kills me.
one more thing would the last one count as a iteration, am i correct in saying that the loop terminates after 7 interations
or should i say
the loop terminated when Boolean Comparison was false in the 7th iteration.