Notices

Water Fountain General Chit/Chat

Reply
 
LinkBack Thread Tools
Old 09-20-05, 07:35 PM   #1 (permalink)
ericcumbee
Guest
 
Posts: n/a
Java help

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.

any one have any idea?
  Reply With Quote
Sponsor Ads
Old 09-20-05, 08:01 PM   #2 (permalink)
Duwenbasden
Guest
 
Posts: n/a
Coffee?

n-=3 is n=n-3, like in +=
  Reply With Quote
Old 09-20-05, 09:10 PM   #3 (permalink)
Aximsite Veteran
 
star882's Avatar
Uber Member
 
Join Date: Oct 2004
Posts: 1,858
Thanked 3 Times in 3 Posts

Awards Showcase
Aximsite Active Bronze Member 
Total Awards: 1

Quote:
n-=3 is n=n-3, like in +=
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.
star882 is offline   Reply With Quote
Old 09-20-05, 09:28 PM   #4 (permalink)
ericcumbee
Guest
 
Posts: n/a
that is copied right from the word document of my homework assignment
  Reply With Quote
Old 09-20-05, 09:39 PM   #5 (permalink)
ericcumbee
Guest
 
Posts: n/a
in the System.out.print(n+10)
is that performing a calculation? and changing the n?

i think all that is doing is in the output adding 10 to n. am i right on that?

someone what to check me on this?

Last edited by ericcumbee; 09-20-05 at 09:55 PM.
  Reply With Quote
Old 09-20-05, 10:14 PM   #6 (permalink)
JonMisurda
Guest
 
Posts: n/a
*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.

Jon

Last edited by JonMisurda; 09-20-05 at 10:17 PM.
  Reply With Quote
Old 09-20-05, 11:33 PM   #7 (permalink)
ericcumbee
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 09-21-05, 12:10 AM   #8 (permalink)
JonMisurda
Guest
 
Posts: n/a
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.

Jon
  Reply With Quote
Old 09-21-05, 12:20 AM   #9 (permalink)
ericcumbee
Guest
 
Posts: n/a
i did that, i doubled check to make sure they were the right case, but then it ran fine on another computer.
  Reply With Quote
Old 09-21-05, 12:39 AM   #10 (permalink)
JonMisurda
Guest
 
Posts: n/a
hmm, the only other thing off the top of my head that i could think of it being is the dreaded CLASSPATH environment variable.

Try this and see if it works:

java -cp . filename

Jon
  Reply With Quote
Old 09-21-05, 01:05 PM   #11 (permalink)
ericcumbee
Guest
 
Posts: n/a
no dice all it does is comes up with a list of different switches
  Reply With Quote
Old 09-21-05, 01:09 PM   #12 (permalink)
JonMisurda
Guest
 
Posts: n/a
Oh, then try:

java -classpath . CLASSNAME

Jon
  Reply With Quote
Old 09-21-05, 06:12 PM   #13 (permalink)
ericcumbee
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 09-21-05, 06:55 PM   #14 (permalink)
JonMisurda
Guest
 
Posts: n/a
The values look good, except for the last one. Will 11 get displayed? What happens when the loop condition switches to false?

Also, two nitpicking things (sorry, I am a teacher afterall):

1) The n should be lowercase. Remember case matters with everything, including identifiers.

2) Something your spreadsheet doesn't capture is how the output will look on the screen. Describe it. Is it one per line?

Jon
  Reply With Quote
Old 09-21-05, 08:10 PM   #15 (permalink)
ericcumbee
Guest
 
Posts: n/a
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.
  Reply With Quote
Reply

Tags
java

Sponsor Ads

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:18 PM.
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Copyright © 2003-09 LeckMedia, LLC