how did the teacher get this?(JAVA)

Grazehell

2[H]4U
Joined
Jun 13, 2001
Messages
2,660
Code:
import java.util.*;
public class painindarse
{public static void main(String[] args)
{double L=100;
 double i;
for (i=5; i<90; i=L+20)

L=(L-i);
System.out.print(L);


 }
}


well the professor put that on the board(unless I took it down wrong) and asked the class to figure out the output of L. well I guessed 95 which was wrong she worked something out and figured it was -125.

No I get home and am trying to understand how she got this and decided to just compile it and see and what do you know it gets 95.
 
Either the profesor made a mistake, or you copied the code incorrectly. I'm not psychic, so I can't determine what really happened.
 
mikeblas said:
Either the profesor made a mistake, or you copied the code incorrectly. I'm not psychic, so I can't determine what really happened.

she made a mistake because even in class I was like "how in the world did she get
that"(the rest of the class are just doing programming for the first time so everything is a foreign concept and they will accept anything she tells then so they did not find it strange) I will just e-mail her.
Thanks
 
Rewrote it in perl, it is 95.
Code:
#!/usr/bin/perl
$i=0;
$L=100;
for ($i=5; $i<90; $i=$L+20) {
$L = $L - $i;
}
print "Final L: $L \n";

spirit@quickesthosting ~ $ ./g.pl
Final L: 95

So unless java has hinky int handling, it should be 95.
 
The answer is 95 because...
You assign 100 to L
You assign 5 to i
Is i less than 90? Yes.
Assign 95.0 (L-i) to L
Assign 115 (L+20) to i
Is i less than 90? No.
Print L.

The piece "i=L+20" is run after the code segment "L=(L-i);", so your professor must be cracked out or you copied it wrong.

Adding in a few more print statement shows this.

Code:
public class Test
{

public static void main(String[] args)
{
	double L=100;
	double i;
	for (i=5; i<90; i=L+20)
	{
		L=(L-i);
		System.out.println(L);
		System.out.println(i);
	}
	System.out.println(i);

}

}
 
Code:
double L=100;
double i;
for (i=5; i<90; i=L+20)
  L=(L-i);
System.out.print(L);

you're right, this does indeed spit out 95.

this however, spits out -125. i think that's what the instructor's error was (or maybe your's, if you copied it down wrong or something).

Code:
double L=100;
double i;
for (i=5; i<90; i=i+20)
  L=(L-i);
System.out.print(L);
 
I feel sorry for you, if your teacher gave code to you like that, its no wonder why there are so many bad programmers.
 
fluxion said:
Code:
double L=100;
double i;
for (i=5; i<90; i=L+20)
  L=(L-i);
System.out.print(L);

you're right, this does indeed spit out 95.

this however, spits out -125. i think that's what the instructor's error was (or maybe your's, if you copied it down wrong or something).

Code:
double L=100;
double i;
for (i=5; i<90; i=i+20)
  L=(L-i);
System.out.print(L);


that def what she meant to write because I just double checked with a guy that sits next to me in class and it was an i=L+20.
I do hope this was a one time thing thought.
 
Grazehell said:
that def what she meant to write because I just double checked with a guy that sits next to me in class and it was an i=L+20.
I do hope this was a one time thing thought.

always be wary, even my best CS instructors screw up fairly often.
 
mikeblas said:
Why do you say that, Pyrolistical?

Given the code she gave, I say its pretty obvious that she isn't a programmer. She is just a teacher that got stuck with teaching programming.

Her code is so poorly formated that it will confuse people. On top of that, it'll teach the students a bad coding style to start with. Student do not know that the formating of the code does not affect it. (You know what I am trying to say)

And look at the code! She's using double where you usally use an integer. Using the largest/move flexible type is a sign of a new programmer. By using double for everything, they don't have to worry about type checking. (Hey, it works when I use all doubles! No more funny error messages about this thing called "casting")

The code can be much clearer if she:
- used spaces between operators
- used blank lines after imports
- didn't use such ugly braces! (ie. should use matching braces)
- don't use useless brackets

Unfortunately, students who get teachers like this won't become programers. That's sad, since its the teacher's fault, not the students. I bet if all students re-took programmer with a good teach, a lot more of them will continue to learn and love it.
 
I took intro to programming at my high school. Yeah, pretty sure it was all HTML.

No joke...
 
Pyrolistical said:
Her code is so poorly formated that it will confuse people.
Oh, I see. Indeed, double doesn't seem necessary, but I thought that Grazehell typed in and formatted the code, not the professor.
insanarchist said:
I took intro to programming at my high school. Yeah, pretty sure it was all HTML.
Er, why can't you remember for sure?
 
mikeblas said:
Er, why can't you remember for sure?

Ahh, I forgot slang in one part of the country doesn't necessarily work in other parts! It is mostly sarcasm; I really didn't mean I wasn't sure. Up here in MN we try to work as much sarcasm into the simplest statements as we can. My bad :)
 
i suppose it's besides the point that she's using doubles to store integer values.....
 
It is most definitely not beside the point, especially in an introductory class like this where programmers will be forming their most basic habits. I can easily picture them walking away thinking, "I'll just use a double for everything, since it can hold the biggest numbers!"
 
Here it is in C++ just for because I was bored.

Code:
#include <iostream>
using namespace std;

int main()
{
	int x, i;
	x = 100; // sub X for L to make it more readable

	for (i = 5; i < 90; i=x+20)
	{
		x = (x-i);
	}

	cout << x << endl;
	system("pause");

	return 0;
}


Here was the output: 95

Here was the output when I changed the i=x+20 to i=i+20: -125

So yeah, I think it's pretty clear what happened here. It's fairly common when you use variables like i and l together. If my variables are one letter then I try to make them as different as I can like x, i, u, g, instead of i, l, t, and so on.

Good luck with your java class, is it just java or is it Object Oriented Programming?
 
I put myself through college at Penn State Uni. in the IST major (like compsci) and I never had one competent prof in my 5 years. Don't expect much, 99% of what you will gain will be from life experiences and personal research/interests.

You'll have plenty more crap coding classes, don't worry, you learn to accept it.
 
Another reason I like Clarion University of Pennsylvania. We have a few professors who are kinda flakey (smart people, just not great teachers) and then we have some REALLY good professors, one who comes to mind is a guy named Professor Wyatt. The man is so freaking smart he's just really spazy (it's quite amusing).
 
My CS teachers would rip her a new one for crap looking like that, one of the first things we learn is don't use 1 character identifiers. Hell my CS3 teacher takes points off seriously for that. that and using i and l, any one with some decent teaching experience wouldn't do that, because they look far to similar, especially handwritten. Damn stupid teacher if you ask me.
 
I only one letter it if I am using a counter or something similar. My variable names are usually pretty long.

ex: int average_lag_under_full_load;
 
Back
Top