Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-03-2005, 06:58 PM   #1
Member (1 million bit!)
 
mrmister1's Avatar
 
Join Date: Feb 2003
Location: NY
Posts: 1,160
Return multiple values in Java

I've written two simple programs for my java class, but I need to know how to return multiple values from a method to the main method, so I can use them both in the main.

Working class:
Code:
// CurrencyConversion

public class CurrencyConversion {
	public double convert(double amountEuro, double exchangeRate) {

		// Define variables
		double exchangeFee = .0120;

		// Calculate
		double amountDollars = amountEuro * exchangeRate;

		// Subtract exchange fee
		double amountDollarsAfterFee = amountDollars - (amountDollars * exchangeFee);

		// Round to nearest tenth
		double amountDollarsRounded = (amountDollarsAfterFee * 10 + .5) / 1.0;

		return amountDollarsAfterFee;
                return amountDollarsRounded;
	}
}
Test class:
Code:
// TestCurrencyConversion

public class TestCurrencyConversion {
	public static void main(String[] args) {

		// Get input
		double amountEuro = MyInputPane.readDouble("Euros to be converted to Dollars:");
		double exchangeRate = MyInputPane.readDouble("Exchange Rate:");

		// Create a convert object
		CurrencyConversion doConvert = new CurrencyConversion();
		double amountDollarsRounded = doConvert.convert(amountEuro, exchangeRate);

		// Print results
		System.out.println("Euros to be converted: " + amountEuro);
		System.out.println("Exchange Rate: " + exchangeRate);
		System.out.println("");
		System.out.println("Exchanged US Dollars: " + amountDollarsAfterFee);
		System.out.println("Payable US Dollars: " + amountDollarsRounded);
	}
}
What I need to know is where to put the return amountDollarsRounded;, so I can print it to the screen in the main.

Thanks in advance
__________________
Black X-Dreamer Case | Intel Pentium 4 2.66 GHz | Intel D845PESVL | 512 MB PC2700 DDR-SDRAM | WD 120 GB Special Edition | Pioneer 16x DVD-ROM | Mitsumi 3.5-inch 1.44 MB | ATi Radeon 9800 Pro | Creative Labs Sound Blaster Live! 5.1

mrmister1
mrmister1 is offline   Reply With Quote
Old 10-03-2005, 07:13 PM   #2
Dark
 
Dark Nova's Avatar
 
Join Date: May 2004
Posts: 1,128
Send a message via MSN to Dark Nova
ok here is a program that might help you, the names of things are in french, but its basicly the same thing

// The "Carte" class.
import java.awt.*;
import hsa.Console;
import java.io.*;
public class Assurance
{
static Console c; // The output console

public static void main (String[] args) throws IOException
{
c = new Console ();
int choix, diff;
//Creer une instance de PrintWriter pour creer un nouveau fichier
PrintWriter sortie = new PrintWriter (new FileWriter ("carte"));
c.println ("Desirez-vous voir le cout de tes assurances? Oui = 1 : Non = 0");
c.print ("choix: ");
choix = c.readInt ();
while( choix != 0)
{
Auto assurance = new Auto ();



c.print("Entree votre Nom: ");
assurance.nom = c.readLine ();

c.print("Entree votre Prenom: ");
assurance.prenom = c.readLine ();

c.print("Entree votre age SVP: ");
assurance.age = c.readInt ();

c.print("Entree votre sex (M ou F): ");
assurance.genger = c.readChar ();
c.print(assurance.calcul(Diff,age));

c.print("Entree le nombre d'infraction: ");
assurance.infraction = c.readInt ();

c.print("Entree le model de la voiture: ");
assurance.model = c.readLine ();


c.print("Entree l'annee de l'achat: ");
assurance.anneeauto = c.readInt ();
if (annee==true);
{


sortie.println (assurance.nom + ", " + assurance.prenom);
sortie.println ("Age de " +assurance.age+ " ans");
sortie.println ("Sexe: " +assurance.genger);
sortie.println ("As recus " +assurance.infraction+ " billet d'infraction(s) avec ce auto");
sortie.println ("Model: "+ assurance.model);
sortie.println ("l'annee de l'achat "+ assurance.anneeauto);
sortie.println (" Voici le couts en assurance que vous aurrez avec cette auto "+assurance.calcul(Diff, age)+" $.");
sortie.println ();
}
c.println ("Desirez-vous ajouter un eleve a la liste? Oui = 1 : Non = 0");
c.print ("choix: ");
choix = c.readInt ();

}

sortie.close ();
c.close();
// Place your program here. 'c' is the output console
} // main method
} // Carte class



AND



// The "Auto" class.
import java.awt.*;
import hsa.Console;

public class Auto
{

String nom, prenom, model;
int age, infraction, anneeauto, dif;
char genger;

public static int calcul (int Diff, int age)
{
int calcul;
if(genger=='M' || genger=='m')
return calcul= (100*(15-Diff))-(2*age)+150;
if(genger=='F' || genger=='f')
return calcul= (100*(15-Diff))-(2*age);

}

public static boolean Verification(int anneeauto)
{
int Diff;
if(anneeauto>16)
return assurance.annnee==false;
if(anneeauto<=15 && anneeauto>=1)
return assurance.annee==true;
Diff=2005-anneeauto;
}
// Place your program here. 'c' is the output console
// main method
} // Auto class

Last edited by Dark Nova; 10-03-2005 at 07:16 PM.
Dark Nova is offline   Reply With Quote
Old 10-03-2005, 07:25 PM   #3
Dark
 
Dark Nova's Avatar
 
Join Date: May 2004
Posts: 1,128
Send a message via MSN to Dark Nova
Quote:
Originally Posted by mrmister1

System.out.println("Euros to be converted: " + amountEuro);
System.out.println("Exchange Rate: " + exchangeRate);
System.out.println("");
System.out.println("Exchanged US Dollars: " + amountDollarsAfterFee);
System.out.println("Payable US Dollars: " + amountDollarsRounded);
}
}
dont forget (amountDollarsRounded) you need to add the second page like
convort(whatever is the name).amountDollarsRounded
Dark Nova is offline   Reply With Quote
Old 10-04-2005, 12:17 AM   #4
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Quote:
Originally Posted by mrmister1
I've written two simple programs for my java class, but I need to know how to return multiple values from a method to the main method, so I can use them both in the main.
If you want to return multiple values, you could either return them in an array or a Vector.
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
Force Flow is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:58 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2