BleepingComputer.com: Java Programming - parameters and objects

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Java Programming - parameters and objects newbie question

#1 User is offline   funnytim 

  • Distinguished Member
  • PipPipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 619
  • Joined: 27-April 06
  • Gender:Male
  • Location:Vancouver, BC, Canada

Posted 01 October 2009 - 05:41 AM

Hey guys,

A few newbie questions on Java programming:

I have a program code here:
public class Mystery {
   public static void main(String[] args) {
	  int x = 1, y = 2, z = 3;
	  z = mystery(x, z, y);
	  System.out.println(x + "  " + y + "  " + z);
	 x = mystery(z, z, x);
	 System.out.println(x + " " + y + " " + z);
	 y = mystery (y, y, z);
	 System.out.println(x + " " + y + " " + z);
}


public static int mystery(int z, int x, int y) {
  z--;
  x= 2 * y + z;
  y = x - 1
  System.out.println(y + " " + z);
  return x;
}

}


Apparantely that is supposed to yield:
3 0
1 2 3
4 3
5 2 4
8 1
5 9 4


I'm not sure how the code makes the result it's supposed to make. In particular, i don't quite understand that "z = mystery(x, z, y);" stuff (along with other stuff).


Thanks!
Thanks.

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 01 October 2009 - 12:18 PM

Quote

z = mystery(x, z, y);


This is a call the a method called mystery, and it passes three ints to that method. A method is a short block of code that completes some function. In this case, it subtracts one from z, sets x = to 2 * y + z. and sets y = to x-1. Then it prints out the value of y and z, and returns the value of x, which is then stored in global variable z. That single line I quoted above sets all of that in action. Parameter passing was a little weird for me to understand initially, so lets try a bad analogy. Think of a method as a box. You have no idea what happens inside of the box. Let's pretend out box is called 'toaster'. Most everybody knows that if you input bread to 'toaster', it will return 'toast'. We have no idea what is happening inside, nor do we need to. Maybe your 'toaster' produces output by using heated electric coils. Maybe my 'toaster' used heated air to produce output. Our toasters are interchangeable. Either one will produce toast; we don't care how it gets done. You will hear a term called encapsulation that is used to describe self-contained blocks of code.

The other part that is confusing is that idea of scope. Scope is a term to describe what variables are visible to which code. In the main method, three ints are declared and instantiated: int x = 1, y = 2, z = 3;. In the method, three new variables are declared and are instantiated when the method is called:public static int mystery(int z, int x, int y) The variables declared in the main method are not the same variables declared in the mystery method. From your standpoint they look the same because they have the same letters, but to the computer, they are just memory addresses.

The variables declared in the main method can not be seen by the mystery method because the only exist inside of the main method. At the same time, the variables created in the mystery method are invisible to the main method. So when the values are passed, internally the method call looks like this:
z = mystery(1, 3, 2)

Inside the method, the values are assigned to new, completely unrelated variables, where z=1, x=3, and x=2. Once mystery is done executing, the result is returned to the main method and stored in main method variable z.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#3 User is offline   funnytim 

  • Distinguished Member
  • PipPipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 619
  • Joined: 27-April 06
  • Gender:Male
  • Location:Vancouver, BC, Canada

Posted 02 October 2009 - 04:07 AM

Thanks for the detailed explanation...I *think* I get it :thumbsup:

On another note, I have to write a TuringTest program , which includes making it so that it gives the user their horoscope based on user input.

I'm having some problems:

	public static void doHoroscope()
		{
			Scanner scanner = new Scanner(System.in);
			int x = scanner.nextLine();
				for(double x = 03.21)   //Problem Here, How can I make it read between integer values?
				{
					System.out.println("Aries");
				}
															 
		}



Basically what I'm trying to do is, after asking for the user's birthday in the format (MM.DD) , it gives out info depending on the sign. Eg. a response from 03.21 - 04.19 would result in output of "Aries". But i'm not sure how to make the code read between the two integers like that.

Thanks again!

This post has been edited by funnytim: 02 October 2009 - 04:08 AM

Thanks.

#4 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 02 October 2009 - 10:46 AM

Those are doubles, not ints, and you can't use doubles in a for loop. You need to use if statements. For example:
if(x > 1.20 && x <2.22){
   system.out.println("Capricorn");
} else if (x > 2.22 && x<3.22{
   .....
}

"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   funnytim 

  • Distinguished Member
  • PipPipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 619
  • Joined: 27-April 06
  • Gender:Male
  • Location:Vancouver, BC, Canada

Posted 05 October 2009 - 02:22 AM

OK, thanks!

And yet another problem: I want to be able to make the program output a random sentence (from a pre-defined set of sentences i write) eg:
Choose and display ONE of the following:
Sentence 1
Sentence 2
Sentence 3


How can i do that, if possible? Thanks again
Thanks.

#6 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 05 October 2009 - 02:55 PM

You would generate a random number from 1 to 3, and depending on which one was chosen, output the corresponding sentence. Before you ask a question about how to do something, you should at least spend a bit of time coming up with the answer on your own. :thumbsup: We are happy to help you figure out where you are having problems, but we are not going to write code for you.

Java Almanac has some sample code should be helpful.
JavaPractices has good examples.

Incidentally, you should actually copy and paste the code and compile it. Then tinker with it and see what happens. Just looking at it will not teach you anything.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#7 User is offline   funnytim 

  • Distinguished Member
  • PipPipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 619
  • Joined: 27-April 06
  • Gender:Male
  • Location:Vancouver, BC, Canada

Posted 18 October 2009 - 04:39 AM

View Postgroovicus, on Oct 5 2009, 12:55 PM, said:

Before you ask a question about how to do something, you should at least spend a bit of time coming up with the answer on your own. :thumbsup: We are happy to help you figure out where you are having problems, but we are not going to write code for you.


I understand, and sorry if thats what I seemed to be asking for. I want to learn how to write it too, but a lot of times I just don't know where or how to start (hence my other thread).

I managed to write it by using the random generator you mentioned, thanks again for your help!
Thanks.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users