PDA

View Full Version : My professor is a bit ambiguous. Need help with a simple problem


M_Coupe
11-12-2008, 05:12 AM
Te book isn't very helpful with this situation, and to me, this seems like basic math. Before I send it in to him, I just want to know if this snippet of code really is as easy as it looks.

1 int number1;

2 int number2;

3 int result;

4

5 number1 = 5 * ( 4 + 6 );

6 number2 = 2 * 2;

7 result = number1 / number2;

Answer: number1 =

number2 =

result =


My answers are:

number1 = 50

number2 = 4

result = 12.5


The second piece would be this: find one logic error, and one syntax error

1 int number1;

2 int number2;

3 int result;

4

5 number1 = (4 * 6 - 4) / (10 % 4 – 2) ;

6 number2 = (16 \ 3) - 2 * 6 + 1;

7 result = number1 - number2;

My answer:

Syntax error is the "%"

Logic error is the "\"



This is a very basic java class, and for some reason, it's GUI design driven... which seems a little bass ackwards, but oh well.

Thanks in advance!

itsallkizza
11-12-2008, 05:51 AM
1) This is a javascript forum which is an entirely different language with an entirely different purpose and environment. Only things the same are vague similarities in syntax and object structure (however JS isn't even technically object oriented, it's prototype driven, but that's a different discussion).

2) Next time place your code in [ code][ /code] tags and don't number your lines.

3) You can combine your variable declarations to:

int number1,number2,result;


4) Your result will not equal 12.5 because result is an int, not a double. I forget whether that throws an error or if it floors the value, you'll want to check that out yourself.

5) "\" is the syntax error, not the logic error. Your logic error is in this line:

number1 = (4 * 6 - 4) / (10 % 4 – 2) ;

The red evaluates to 0 which most compilers won't detect when you compile your code, that's dangerous because it causes a runtime error. Well not dangerous, just bad coding.

6) Modulus (%) is a legitimate Java operator.

7) I'm not doing your homework again for you, that is unless you e-mail me the questions and paypal some cash ;)

Arty Effem
11-12-2008, 05:54 AM
Te book isn't very helpful with this situation, and to me, this seems like basic math. Before I send it in to him, I just want to know if this snippet of code really is as easy as it looks.You'd think he would have mentioned that JavaScript and Java are different languages find one logic error, and one syntax error

1 int number1;

2 int number2;

3 int result;

4

5 number1 = (4 * 6 - 4) / (10 % 4 – 2) ;

6 number2 = (16 \ 3) - 2 * 6 + 1;

7 result = number1 - number2;

My answer:

Syntax error is the "%"

Logic error is the "\"You're wrong - why do you think '%' is a syntax error? Consult your list of mathematical operators and you should see the problem in executing line 5.

'\' isn't an operator so that's a syntax error.

M_Coupe
11-12-2008, 06:31 AM
1) This is a javascript forum which is an entirely different language with an entirely different purpose and environment. Only things the same are vague similarities in syntax and object structure (however JS isn't even technically object oriented, it's prototype driven, but that's a different discussion).

2) Next time place your code in [ code][ /code] tags and don't number your lines.

3) You can combine your variable declarations to:

int number1,number2,result;


4) Your result will not equal 12.5 because result is an int, not a double. I forget whether that throws an error or if it floors the value, you'll want to check that out yourself.

5) "\" is the syntax error, not the logic error. Your logic error is in this line:

number1 = (4 * 6 - 4) / (10 % 4 – 2) ;

The red evaluates to 0 which most compilers won't detect when you compile your code, that's dangerous because it causes a runtime error. Well not dangerous, just bad coding.

6) Modulus (%) is a legitimate Java operator.

7) I'm not doing your homework again for you, that is unless you e-mail me the questions and paypal some cash ;)

Thanks for the info- and I do know Jscript and Java are different, I accidentally clicked the wrong section. That, and my brain is a little frazzled from doing other homework.

Since the result is an integer, would it be rounded to 13, or would it simply omit the decimal place and display 12?

I rarely ask for help when it comes to homework, but the book is just as obtuse as my professor was on this chapter, and I was confused.

Could you elaborate a little on why (10 % 4 – 2) evaluates to zero? This is both my first programming language, and a very basic coding course

Upon consulting my list of operators, % is described as "take reminder" which is a little more complex of an operation than what is trying to be described to us, so I now get why it's the logic error.

itsallkizza
11-12-2008, 06:38 AM
so wouldn't it be a logic error (correctly compiling code that displays an incorrect operation from what the programmer intended?)
That's not a logic error, a logic error is almost synonymous with a runtime error (ie the compiler will let your compile the program with no error messages, or just a warning). In this case the red piece of code evaluates to 0, and you can't divide anything by zero (this would throw an error in any language).

Modulus returns the remainder from a division of two numbers. If you can remember back to long division in high school (or was it junior high), it's that number at the end you divide by your divisor. However with modulus, you don't divide it by the divisor, you just return it.

Here's some examples:

2%2 = 0
4%2 = 0
6%2 = 0

3%2 = 1
5%2 = 1
7%2 = 1

5%4 = 1
5%3 = 2
5%2 = 1
5%1 = 0

It's most common use is finding out whether a given integer is even or odd (num%2 returns either a 1 or 0).

4) Your result will not equal 12.5 because result is an int, not a double. I forget whether that throws an error or if it floors the value, you'll want to check that out yourself.
It's been awhile since I've done Java so I forget whether or not it floors it or throws an error. It never rounds it up though. It doesn't even technically floor it, it "first-end"s it. You can always typecast it to be sure by throwing (int) right after the equals assignment.

M_Coupe
11-12-2008, 06:53 AM
That's not a logic error, a logic error is almost synonymous with a runtime error (ie the compiler will let your compile the program with no error messages, or just a warning). In this case the red piece of code evaluates to 0, and you can't divide anything by zero (this would throw an error in any language).

Modulus returns the remainder from a division of two numbers. If you can remember back to long division in high school (or was it junior high), it's that number at the end you divide by your divisor. However with modulus, you don't divide it by the divisor, you just return it.

Here's some examples:

2%2 = 0
4%2 = 0
6%2 = 0

3%2 = 1
5%2 = 1
7%2 = 1

5%4 = 1
5%3 = 2
5%2 = 1
5%1 = 0

It's most common use is finding out whether a given integer is even or odd (num%2 returns either a 1 or 0).


It's been awhile since I've done Java so I forget whether or not it floors it or throws an error. It never rounds it up though. It doesn't even technically floor it, it "first-end"s it. You can always typecast it to be sure by throwing (int) right after the equals assignment.

I never used a modulus in long division, actually. So it means to divide the two integers and take the remainder?

So to double check, we have 10/4, retrieving a remainder of 2, and then subtracting two, correct? I am unaware of the order with which the compiler follows, does it do M/D/A/S rule, or does it just go through the statement in order?

itsallkizza
11-12-2008, 07:28 AM
I never used a modulus in long division
Never said you did, only asked you to remember long division - it's the same remainder concept.

Java follows normal order of operations, same as in math class in high school. A modulus is the same as division.

rangana
11-12-2008, 08:46 AM
A modulus is the same as division.

I beg to disagree.

modulus (%) returns a division remainder while division (/) divides:

alert('5 modulus of 2 is: '+(5%2)+'\n5 divided by 2 is: '+(5/2));


For further reading:
http://www.w3schools.com/js/js_operators.asp

M_Coupe
11-12-2008, 08:49 AM
I beg to disagree.

modulus (%) returns a division remainder while division (/) divides:

alert('5 modulus of 2 is: '+(5%2)+'\n5 divided by 2 is: '+(5/2));


For further reading:
http://www.w3schools.com/js/js_operators.asp

Now I'm confused


% Modulus (division remainder) x=y%2 x=1

so 10%4 still does equal 2, correct?


And can someone explain why the 12.5 output would be incorrect, and how the result would be displayed as an integer? I'm doing a lot of searching online and through my book, and I just can't seem to find what I need

M_Coupe
11-12-2008, 09:08 AM
12.5 will be an incorrect output for what equation? I don't know your basis.



var x=12.5; // This is a float
var convert=parseInt(x); // Already converted to integer
alert(convert); // Outputs 12 since integer don't have decimal point


Hope that makes sense.

5 number1 = 5 * ( 4 + 6 );

6 number2 = 2 * 2;

7 result = number1 / number2;

Answer: number1 =

number2 =

result =

He's just asking for the answers to those fields, and I want to understand why 12.5 in the result field is incorrect. And what would converting it into an integer do? round up, or simply omit the decimal?


Again, thanks to everyone for the help. I don't want answers, per se, I want to understand why the answers were gotten.

rangana
11-12-2008, 09:35 AM
Number1= 50. Basic MDAS (Multiplication Division Addition Subtraction). Number within the parenthesis are computed first. So, 5*(4+6) would be disected into:

5*(4+6);
5*10
// answer is 50

Number2 = 4. Obviously, 2*2 equals 4.
Number3 = answer should be 12.5. Not unless you do it this way:

alert(5*(4+6)/2*2); // results 50 as to 5*10*2/2


Should be:

alert((5*(4+6))/(2*2)); // results 12.5


...or, if you are expecting an integer, the value should only be 12 as integer don't have decimal points:

alert(parseInt((5*(4+6))/(2*2))); // Outputs 12



Hope that makes sense.

Philip M
11-12-2008, 09:44 AM
5 number1 = 5 * ( 4 + 6 );

6 number2 = 2 * 2;

7 result = number1 / number2;

Answer: number1 = 5 * 10 = 50 but (5*4+6) evaluates to 26
number2 = 4

result = 12.5 truncated to 12 as the variable result is defined as an integer.

He's just asking for the answers to those fields, and I want to understand why 12.5 in the result field is incorrect. And what would converting it into an integer do? round up, or simply omit the decimal?

Conversion of a real (decimal) value to an integer truncates the value, so parseInt(12.99) = 12.

Javascript contains a good many built-in Math methods, including:-
Math.floor() - round a number down 12.99 > 12
Math.round() - round to the nearest integer 12.99 > 13 12.49 > 12
Math.ceil() - round a number up 12.01 > 13

Again, thanks to everyone for the help. I don't want answers, per se, I want to understand why the answers were gotten.

Google for more information

M_Coupe
11-12-2008, 03:03 PM
Number1= 50. Basic MDAS (Multiplication Division Addition Subtraction). Number within the parenthesis are computed first. So, 5*(4+6) would be disected into:

5*(4+6);
5*10
// answer is 50

Number2 = 4. Obviously, 2*2 equals 4.
Number3 = answer should be 12.5. Not unless you do it this way:

alert(5*(4+6)/2*2); // results 50 as to 5*10*2/2


Should be:

alert((5*(4+6))/(2*2)); // results 12.5


...or, if you are expecting an integer, the value should only be 12 as integer don't have decimal points:

alert(parseInt((5*(4+6))/(2*2))); // Outputs 12



Hope that makes sense.

Truncation. Got'cha. Thanks!

itsallkizza
11-12-2008, 03:05 PM
Modulus is the same as division
I beg to disagree.
If you had read my entire post and it's context you wouldn't disagree. We were talking about order of precedence, in which case it is the same as division. I had already told him exactly what modulus does in a previous post.

And it seems like everyone so far has done nothing but repeat my initial post (and incorrectly at that), except for Philip who cleared up the decimal assigned to int issue.

_Aerospace_Eng_
11-13-2008, 06:43 AM
rangana, everything you've given them deals with javascript solutions. There is no alert in javascript. To parse a float or double as an int its Integer.parseInt(someint).

Fou-Lu
11-13-2008, 07:40 AM
I lol'd.
I'm pretty sure AE meant that there is no alert in java, not javascript.
This thread is bothering me a bit. It appears to be java code (since its type strong), but the required functionality performed is difficult to differentiate between javascript and java.

Integer.parseInt usage?

public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
boolean isValid;
int iEntered = 0;
do
{
System.out.print("Enter a number... ");
try
{
iEntered = Integer.parseInt(s.next());
isValid = true;
}
catch (NumberFormatException ex)
{
System.out.println("Not a number, try again...");
isValid = false;
}
} while (!isValid);
System.out.println("You entered: " + iEntered);
}



Sorry I read bad too :P
Integer.parseInt actually requires a string, not a double. To convert a double, you'd create a new Double wrapper and execute the d.intValue method (although I could swear I've tried using Integer.parseInt on a double before...)

Or of course, you can just cast it too :)

Gox
11-13-2008, 08:54 AM
J2ME has an "Alert"....but now we're splitting hairs ;)

Fou-Lu
11-13-2008, 09:00 AM
J2ME has an "Alert"....but now we're splitting hairs ;)

Thats right, midp does have alert!
We've been told.
On the other hand... J2ME is to Java as Access is to Oracle :)

_Aerospace_Eng_
11-14-2008, 07:47 AM
Come again? I don't think I understand you on this part.


I don't exactly get what you mean here.

I thought, it was parseInt(n), where n could either be a number (includes float) or a string (in this case returns NaN).

Could you please give me an example on how Integer.parseInt(someint) is used?

I meant Java but as stated there is an "Alert" but not in the same sense as you are describing it. This entire thread deals with java but every solution you've given has been a javaSCRIPT solution.

rangana
11-14-2008, 07:49 AM
I did'nt notice that this thread was in the Java/JSP section. My apologies. Gotta go and delete each of them.

oracleguy
11-14-2008, 07:33 PM
I did'nt notice that this thread was in the Java/JSP section. My apologies. Gotta go and delete each of them.

The OP originally mistakenly posted it in the Javascript forum which might have led to the confusion. The thread was moved then to the Java forum.