What needs to be done here is, one array that will store the input (celsius value here) and the other one that will store the conversion (fahrenheit value here). I have already used a loop that will allow user input and thus calculation as many as times needed, but if someone can help with the arrays part of this, it will be really appreciated.
In addition, any decent simple to follow tutorial for Java will be great as well.

Replies To: Arrays in Methods- Java?

Re: Arrays in Methods- Java?

Posted 21 November 2008 - 12:18 PM

Do you, before the loop, ask the user the size of the array? If you do not then you will have to either use a JCF class such as arraylist, or right your own class or method that would resize the arrays as appropriate, which if you use the ArrayList class this is done automatically.

Re: Arrays in Methods- Java?

Posted 21 November 2008 - 12:25 PM

ok, first of all you will need to know how to create an array,

double [] a = new double[10];

this will create a new array of doubles called 'a' with size 10(can carry 10 elements).. arrays are reference types as well and they store elements of the same type, which means you cant store booleans in an array of doubles or anything else..
to input elements into an array you will have to use a loop

Re: Arrays in Methods- Java?

Posted 21 November 2008 - 05:44 PM

Thanks to everyone who replied for your time and help. I will answer queries one by one:

1. Though the program does not ask for how many times it should run, the maximum it can is 10 times with a no minimum.
2. The use of arrays is requirement as a part of the assignment.
3. Now, with the array declaration and assignment, I followed what i learned and I am stuck again, please advise.

Re: Arrays in Methods- Java?

Posted 21 November 2008 - 10:14 PM

Thank you very much for sharing your program.
Would you mind sharing the program details a little bit so I understand the program? Are there two different source code files for the same program? I understand use of methods in the program to call user input method, then convert tempratures, and then print the results but what is the purpose of class Temp (block), Temp(). If you can advise, it will be greatly appreciated. if not, I still owe you a big thanks for your time and help.

Re: Arrays in Methods- Java?

Posted 21 November 2008 - 10:34 PM

Thank you again for helping me, the code and brief explanation does help and make sense.

This part of the program is working now, but it appears something messed up the other parts. I have eight different methods in the main method with a loop for user to input a choice of method after each consecutive method, however, after implementing this code the loop does not go back to main, but stays here for 10 iterations for the same method. Any idea what I have messed up with! Possibly arrays value chosen to 10, or maybe something else, and is there a way that I can understand and follow to fix it.

Again, you have been great and I truely appreciate all your input.

mostyfriedman, on 21 Nov, 2008 - 07:34 PM, said:

ok, not bad..but the for loop should start from i = 0 and not 1, because arrays start from 0 till n-1

Re: Arrays in Methods- Java?

Posted 22 November 2008 - 12:22 AM

learnpro, on 21 Nov, 2008 - 09:14 PM, said:

Thank you very much for sharing your program.
Would you mind sharing the program details a little bit so I understand the program? Are there two different source code files for the same program? I understand use of methods in the program to call user input method, then convert tempratures, and then print the results but what is the purpose of class Temp (block), Temp(). If you can advise, it will be greatly appreciated. if not, I still owe you a big thanks for your time and help.

No worries.

Firstly there are 2 separate sources
Temp.java
TempInterface.java

TempInterface is basically a class that contains the main method which creates an instance of the class Temp allowing you to access the member functions of the class Temp such as void convertCelToFar() . As you can see the TempInterface is using the console to take input and give outputs. So basically you can create an Instance of Temp class and use the TempInterface class to act on the instance... i think thats the right way of putting it.

So in TempInterface create and instance of the class Temp using the statement
Temp myConverter = new Temp();this creates an object of class type Temp

Then we ask if the user wants to firstly convert Celsius to Farenheit or Farenheit to Celsius based on the user entering 1 or 2 for the options / menu. We then go ahead and utilise a method of the class Temp based on this user input...

1 - we use the method - myConverter.convertCelToFar();
else - we use method - myConverter.convertFarToCel();

So if u now look inside the functions you can see how the array's are populated with the values.

After this we just use the print function of the class Temp to print the values.

Re: Arrays in Methods- Java?

Posted 22 November 2008 - 09:54 AM

Quote

Thank you again for helping me, the code and brief explanation does help and make sense.

This part of the program is working now, but it appears something messed up the other parts. I have eight different methods in the main method with a loop for user to input a choice of method after each consecutive method, however, after implementing this code the loop does not go back to main, but stays here for 10 iterations for the same method. Any idea what I have messed up with! Possibly arrays value chosen to 10, or maybe something else, and is there a way that I can understand and follow to fix it.

Again, you have been great and I truely appreciate all your input.

is it possible to upload your complete code so i can get a better picture?

Re: Arrays in Methods- Java?

Posted 22 November 2008 - 04:09 PM

I have to create eight different methods of total of which I had only three so far. When I will have all the methods done, I can post it here or actually, I can post it here..or for now, I used a simple for loop to run 10 times in main method. The loop will run one method, come back to main menu, select another method, come back to main menu, and so on...Maybe it is confusing like that. Sorry about that.
Thanks for your help. If you have any suggestions at this point, please throw them on my way or I will post the program here once I have methods done.