|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 | |
|
Member (1 million bit!)
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;
}
}
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);
}
}
Thanks in advance
__________________
![]() |
|
|
|
|
|
|
#2 |
|
Dark
|
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. |
|
|
|
|
|
#3 | |
|
Dark
|
Quote:
convort(whatever is the name).amountDollarsRounded |
|
|
|
|
|
|
#4 | |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
Quote:
__________________
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.
|
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|