Jump to content

My code is possessed.


Emeralis00

Recommended Posts

So, please tell me the difference between these two sets of code. Both are methods of a Rational Class.

set 1

public int add(Rational op2) {
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;

return (sum, commonDenominator);
}

set 2

public int add(Rational op2) {
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;

return (sum, commonDenominator);
}

that's right. They are exactly the same. So here is how the problem comes in.

I entered the first set of code and it didn't work properly.

Entering in two sets of rational numbers, (1/2) and (1/2), and gives out (3/2).

I retyped the code the exact same way, and it worked.

I entered (1/2) and (1/2) and got 1.

So does anyone know why java seems to do this. It happened to me once before. No syntax or runtime errors were generated.

Link to comment
Share on other sites

Code is missing. Behavior with globals has almost always been undefined.

it is likely to be like this

public class Rational{
 private int numerator;
 private int denominator;
 // along with getters (and maybe setters) for each of those values
 public int add(Rational op2)
 {
   // snip
 }
}

Though there is some issues, the add function should be returning a Rational should it not?

And the return statement should be `new Rational(sum, commonDenominator);` ?

Link to comment
Share on other sites

Your kinda missing the point of the post. I typed in the code the first time and it didn't work, then I typed it EXACTLY the same and it worked.The rest of the code worked. It was just that particular method that wasn't working and I want to know why java wanted me to retype it.

And the return statement should be `new Rational(sum, commonDenominator);` ?

My bad, it was that in the code, I was typing from memory before.

Full code below if you really want to look:

Main Class

import java.util.Scanner;

public class chp4_4_7 {

   public static void main(String[] args) {
   	int numer = 0, denom = 0;
   	Scanner input = new Scanner(System.in);
   	Rational r3 = new Rational(1, 1);

   	/*First Rational Number*/
   	do {
   		System.out.print("Enter Numerator: ");
   		numer = input.nextInt();
   	} while (numer == 0);

   	System.out.print("Enter Denominator: ");
   	denom = input.nextInt();

   	Rational r1 = new Rational(numer, denom);
   	System.out.println("");

   	/*Second Rational Number*/
   	do {
   		System.out.print("Enter second Numerator: ");
   		numer = input.nextInt();
   	} while (numer == 0);

   	System.out.print("Enter second Denominator: ");
   	denom = input.nextInt();

   	Rational r2 = new Rational(numer, denom);

   	switch (getSign()) {
   		case 1: r3 = (r1.add(r2)); break;
   		case 2: r3 = (r1.subtract(r2)); break;
   		case 3: r3 = (r1.multiply(r2)); break;
   		case 4: r3 = (r1.divide(r2)); break;
   	}

   	System.out.print("Answer: " + r3);
   }

   public static int getSign() {
   	int choice = 0;
   	Scanner input = new Scanner(System.in);

   	System.out.println("");
   	do {
   		System.out.print("1. add\n2. subtract\n3. multiply\n4. divide\n");
   		choice = input.nextInt();
   	} while (choice > 4 || choice < 1);

   	return (choice);
   }
}

Rational Class

public class Rational {
private int numerator, denominator;

public Rational(int numer, int denom) {
	if (denom == 0) 
		denom = 1;


	if (denom < 0) {
		numer *= -1;
		denom *= -1;
	}

	numerator = numer;
	denominator = denom;

	reduce();
}

public int getNumerator() {
	return (numerator);
}

public int getDenominator() {
	return (denominator);
}

public Rational Reciprocal() {
	return new Rational (denominator, numerator);
}

public Rational add(Rational op2) {
	int commonDenominator = denominator * op2.getDenominator();
	int numerator1 = numerator * op2.getDenominator();
	int numerator2 = op2.getNumerator() * denominator;
	int sum = (numerator1 + numerator2);

	return new Rational (sum, commonDenominator);
}

public Rational subtract(Rational op2) {
	int commonDenominator = denominator * op2.getDenominator();
	int numerator1 = numerator * op2.getDenominator();
	int numerator2 = op2.getNumerator() * denominator;
	int difference = numerator1 - numerator2;

	return new Rational (difference, commonDenominator);
}

public Rational multiply(Rational op2) {
	int numer = numerator * op2.getNumerator();
	int denom = denominator * op2.getDenominator();

	return new Rational (numer, denom);
}

public Rational divide(Rational op2) {
	return multiply(op2.Reciprocal());
}

public boolean equals(Rational op2) {
	return (numerator == op2.getNumerator() &&
	     denominator == op2.getDenominator());
}

public String toString() {
	String result;

	if (numerator == 0) {
		result = "0";
	} else {
		if (denominator == 1) {
			result = numerator + " ";
		} else {
			result = numerator + "/" + denominator;
		}
	}
	return (result);
}

private void reduce() {
	if (numerator != 0) {
		int common = gcd(Math.abs(numerator), denominator);

		numerator /= common;
		denominator /= common;
	}
}

private int gcd(int num1, int num2) {
	while (num1 != num2) {
		if (num1 > num2) {
			num1 -= num2;
		} else {
			num2 -= num1;
		}
	}
	return (num1);
}
}

Link to comment
Share on other sites

What he said is still true, undefined means that literally anything could happen, from a segfault to printing out the meaning of life. It is the behaviour that is undefined, and it may be fixed at compile time, run time, or even bath time.

Link to comment
Share on other sites

What he said is still true, undefined means that literally anything could happen, from a segfault to printing out the meaning of life. It is the behaviour that is undefined, and it may be fixed at compile time, run time, or even bath time.

Can you explainwhat that means or point me to a spot where I can learn?

Link to comment
Share on other sites

Guest Jacob Santos

I'm unsure if it is hindsight bias, but I believe it has happened to me before as well. The simple answer might be the compiler "optimized" something out and it wasn't until the next go through that it finally figured what you were attempting and got it right during the second compilation.

Unless you can compare the two Bytecodes (did compilation fail the first time), then who can say? Perhaps you mistyped something the first time and after.

Simple truth is that it works and that is all that matters. However, that seems frustrating because there have been times when I've known my code was correct and it should work, but did not. Trying everything and refactoring everything usually came down to a misspelled variable name or some other nonsense that I had mistaken put in.

Cheerio, say no more, say no more.

Link to comment
Share on other sites

i'd just like to point out a couple of things.

Java doesn't really have "globals" per say, since everything is a member of a class, and Java was made, by design, to have as few undefined operations as possible (unlike C/C++ which had many of them included, again, by design).

Off hand, i would say that something like a dangling pointer would be the cause of this, if it wasn't for the fact that its basically impossible (at least to my knowledge) to have dangling/wild pointers in java, so i guess that doesn't really help.....

Though, i second the notion that it was probably some errant/wrong compiler optimization. I've had things happen like that to me before where it works odd/incorrectly, and i copy and paste the code and recompile it and it works. My guess is that "refreshing" the code is pretty much kicking the compiler in the pants telling it to recompile that bit, instead of working from cache somewhere or something. Though i haven't worked with java in particular enough to really speak anything on it (my java experience was mostly in college in the early 2000's, so its both stale and limited. I prefer C#, and have worked professionally in that and all manner of basic-like languages)

Though, of course, it could always be magic. Not something you should rule out.

Link to comment
Share on other sites

My guess is that "refreshing" the code is pretty much kicking the compiler in the pants telling it to recompile that bit, instead of working from cache somewhere or something.

This. Whenever something bizarre happens with my code, the first thing I do is delete the build products and recompile. A lot of the time, it's just that, in the process of repeated incremental compiles, something is out of date, or things have gotten stuck together wrong. This is especially true of IDEs that compile the current file after every save, like Eclipse. In your case, (if you're using Eclipse), all retyping the code gained you was an expensive clean-and-recompile.

Link to comment
Share on other sites

This. Whenever something bizarre happens with my code, the first thing I do is delete the build products and recompile. A lot of the time, it's just that, in the process of repeated incremental compiles, something is out of date, or things have gotten stuck together wrong. This is especially true of IDEs that compile the current file after every save, like Eclipse. In your case, (if you're using Eclipse), all retyping the code gained you was an expensive clean-and-recompile.

I'm using Jcreator, because its free and that is what my school put on the computers.

Link to comment
Share on other sites

I'm using Jcreator, because its free and that is what my school put on the computers.

well, the point should still stand. Basically, what we're saying, is that the compiler that's doing the compiling is probably saving a cache of pre-compiled bits so that it can do its job faster, and the pieces probably weren't exactly fit together right, and it really needed to recompile one bit instead of working from the same cache again and again. You retyping it so that it thought the code was all new basically flagged it to recompile the part, and once it did, it replaced the bad bit that it had sitting around that it was using over and over.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...