12 Replies - 2111 Views - Last Post: 22 November 2008 - 05:02 PM Rate Topic: -----

#1 learnpro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-November 08

Arrays in Methods- Java?

Post icon  Posted 21 November 2008 - 12:07 PM

Hello Everyone,
I am a very beginner in programming and in the process of creating a huge program and am stuck for the part of the program. Here is the method where I am stuck,

public static void convert ()
{
Scanner input = new Scanner (System.in); 
System.out.println(" Please enter a Celsius Temprature value "); 
double celsius= input.nextDouble(); 
double fahrenheit = (9.0/5)* celsius + 32; 
System.out.println( celsius + " C " + fahrenheit + " F " ); 
}


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.

Thanks very much in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays in Methods- Java?

#2 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: Arrays in Methods- Java?

Posted 21 November 2008 - 12:25 PM

Of course you could just loop for single values - why do you need an array?
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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
Scanner sc = new Scanner(System.in);
for(int i = 0; i < a.length; i++)
{
  a[i] = sc.nextDouble();
}


length = the size of the array
a[i] = element of array 'a' number 'i'

this was a very brief explanation, you can surf google for more details or even better, get this book... java, how to program 6th edition. good luck :)
Was This Post Helpful? 0
  • +
  • -

#5 learnpro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-November 08

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.
public static void convert ()
		{
		 Scanner input = new Scanner (System.in);  
		 System.out.println(" Please enter a Celsius Temprature value  ");  
		 double[] array= new double[10];
		 for (int i=1; i<=array.length; i++){
		 celsius[i]= input.nextDouble();  
					 double fahrenheit = (9.0/5)* celsius + 32;  
		double[] newtemp = new double[fahrenheit];												System.out.println(  celsius  +  "  C  " + fahrenheit +  "  F  " );			
		}



Can you suggest what am I doing wrong and where i need to go from here? Thanks very much again. I really appreciate everyone's assistance.

This post has been edited by learnpro: 21 November 2008 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Arrays in Methods- Java?

Posted 21 November 2008 - 08:08 PM

How about something like the following. I had this lying around form a few years back in my collections.

import java.util.*;

class Temp
{
     private double[] celsius;
     private double[] farenheit;
	
	 Scanner tempgetter = new Scanner(System.in);
	
     Temp()
      {
         celsius    = new double[5];
         farenheit  = new double[5];
      }

     void convertCelToFar()
     {
     	System.out.println("Please Enter " +celsius.length+ " celsius inputs then press <enter> " );
     	for (int i = 0; i < celsius.length; i++)
     	{
     		celsius[i] = tempgetter.nextInt();
     		farenheit[i] = celsius[i] + 29;	//some conversion for celsius to farenheit     		
     	}
     }
     
     void convertFarToCel()
     {
     	System.out.println("Please Enter " + farenheit.length+ " farenheit inputs then press <enter> ");
     	for (int i = 0; i < farenheit.length; i++)
     	{
     		farenheit[i] = tempgetter.nextInt();
     		celsius[i]   = farenheit[i] - 29; //some conversion for farenheit to celsius
     	}     	     	
     }
     
     void printResults()
     {
     	System.out.println("\n\nCelsius\t\tFarenheit");
     	for(int i = 0; i < farenheit.length; i++)
     		System.out.println(celsius[i] +"\t\t"+ farenheit[i] );
     	
     }
}


import java.util.*;

public class TempInterface {

	Scanner input = new Scanner(System.in);
	
    void run() 
    {
    	int usersChoice;
    	
    	Temp myConverter = new Temp();	//create the new temperature
    	
    	System.out.println("Choose Option\n1) Celsius to Farenheit\n2) Farenheit to Celsius\n");
    	usersChoice = input.nextInt();
    	
    	if (usersChoice == 1)
    	myConverter.convertCelToFar();			// convert Celsius to Farenheit    	
    	else
    		myConverter.convertFarToCel();		// convert Farenheit to Celsius
    		
    	  		
    	myConverter.printResults();				// print the results	
    	
    	
    }   
    
    public static void main(String[] args)
    {
    	TempInterface myInterface = new TempInterface();
    	myInterface.run();
    }
    
}

Was This Post Helpful? 1
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Arrays in Methods- Java?

Posted 21 November 2008 - 08:34 PM

ok, not bad..but the for loop should start from i = 0 and not 1, because arrays start from 0 till n-1
Scanner input = new Scanner(System.in);
double [] celsius = new double[10];
double [] fahrenheit = new double[10];
for(int i = 0; i < celsius.length; i++)
{
	 celsius[i] = input.nextDouble();
	 double conversion = (9.0/5)* celsius[i] + 32;
	 fahrenheit[i] = conversion;
}


ok this code here created 2 arrays, one to store the celsius values and the other to store the fahrenheit values after conversion..hope this helped :)

This post has been edited by mostyfriedman: 21 November 2008 - 08:36 PM

Was This Post Helpful? 1
  • +
  • -

#8 learnpro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-November 08

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. :)
Was This Post Helpful? 0
  • +
  • -

#9 learnpro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-November 08

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.

View Postmostyfriedman, 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
Scanner input = new Scanner(System.in);
double [] celsius = new double[10];
double [] fahrenheit = new double[10];
for(int i = 0; i < celsius.length; i++)
{
	 celsius[i] = input.nextDouble();
	 double conversion = (9.0/5)* celsius[i] + 32;
	 fahrenheit[i] = conversion;
}


ok this code here created 2 arrays, one to store the celsius values and the other to store the fahrenheit values after conversion..hope this helped :)

Was This Post Helpful? 0
  • +
  • -

#10 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Arrays in Methods- Java?

Posted 22 November 2008 - 12:22 AM

View Postlearnpro, 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.

Hope this sorta helped :)
Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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?
Was This Post Helpful? 0
  • +
  • -

#12 learnpro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-November 08

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.

Thanks
Was This Post Helpful? 0
  • +
  • -

#13 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Arrays in Methods- Java?

Posted 22 November 2008 - 05:02 PM

ok then, write the whole thing and then we'll take a look at it, good luck with it :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1