Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Java Programming - parameters and objects


  • Please log in to reply
8 replies to this topic

#1 funnytim

funnytim

  • Members
  • 624 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Local time:08:27 PM

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!

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:27 PM

Posted 01 October 2009 - 12:18 PM

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.

#3 funnytim

funnytim
  • Topic Starter

  • Members
  • 624 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Local time:08:27 PM

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!

Edited by funnytim, 02 October 2009 - 04:08 AM.


#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:27 PM

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{
   .....
}


#5 funnytim

funnytim
  • Topic Starter

  • Members
  • 624 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Local time:08:27 PM

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

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:09:27 PM

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.

#7 funnytim

funnytim
  • Topic Starter

  • Members
  • 624 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Local time:08:27 PM

Posted 18 October 2009 - 04:39 AM

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!

#8 RogerMatthew

RogerMatthew

  • Members
  • 2 posts
  • OFFLINE
  •  
  • Local time:08:57 AM

Posted 19 March 2018 - 08:59 AM

You can refer this resource with beautiful explanation and examples.



#9 GG49

GG49

  • Members
  • 11 posts
  • OFFLINE
  •  
  • Local time:11:27 PM

Posted 19 March 2018 - 01:22 PM

This was posted in 2009...don't think the OP will responde after 9 years






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users