4 Replies - 813 Views - Last Post: 09 August 2013 - 03:12 AM Rate Topic: -----

#1 Jnb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 08-August 13

If method

Posted 08 August 2013 - 05:47 PM

Hi guys! New to the forum en new to Java! I'm following a book I got from school and I'm stuck.
You have to give 2 numbers and then you have to orden them automaticaly from lowest to highest. Afterwards you print it with a sysout.

This is the code
public class Kleinste {
	private int number1, number2;
	
	public Kleinste(int number1, int number2) {
		this.number1 = number1;
		this.number2 = number2;
	}
	
	public void print() {
		int help;
		if (number1 > number2) {
			help = number1;
			number1 = number2;
			number2 = help;
		}
		System.out.println("Getallen in stijgende volgorde: " + number1 + " " + number2);
	}
}



Words you probably won't be able to understand are in Dutch :) but I changed the most important ones.
Now my question: I don't understand the code after the if function. Why work that way with the help integer?

Is This A Good Question/Topic? 0
  • +

Replies To: If method

#2 StrongJoshua   User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 170
  • Joined: 19-July 13

Re: If method

Posted 08 August 2013 - 05:56 PM

It creates a temporary integer to hold the value of number1. You have to do it this way because otherwise you'd overwrite the value of number1 with the value of number2 and would lose the original value of number1. Hope this is understandable :)
Was This Post Helpful? 1
  • +
  • -

#3 Jnb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 08-August 13

Re: If method

Posted 08 August 2013 - 06:13 PM

View PostStrongJoshua, on 08 August 2013 - 05:56 PM, said:

It creates a temporary integer to hold the value of number1. You have to do it this way because otherwise you'd overwrite the value of number1 with the value of number2 and would lose the original value of number1. Hope this is understandable :)/>


Ok I think I get it! But it only does that when number1 is higher then number2?! Otherwise it will just skip to sysout?
Was This Post Helpful? 1
  • +
  • -

#4 StrongJoshua   User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 170
  • Joined: 19-July 13

Re: If method

Posted 08 August 2013 - 07:43 PM

Exactly :) Remember to rate posts that were helpful ;)

EDIT: You could even put the sorting in the constructor of the class so that it doesn't have to recheck every time you print it, especially since you aren't changing the values after instantiating the object (although you could, and then I would recommend making a private method called sort() and calling that whenever the values get changed).

This post has been edited by StrongJoshua: 08 August 2013 - 07:46 PM

Was This Post Helpful? 1
  • +
  • -

#5 Jnb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 08-August 13

Re: If method

Posted 09 August 2013 - 03:12 AM

Ok thanks :)!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1