Can you have code run in a terminal and applet?
For example in my program in Main we enter the data for menu and amount of the exchange but the result is displayed in an applet.
works great in the terminal window.
but when using an applet it will not work.
import java.awt.*;
import java.applet.*;
import javax.swing.* ;
import TerminalIO.*;
publicclass MetricConversionMain{
finalstatic String CRLF ="\n" ;
static String msgOut ;
publicstaticvoid main ( String[] args ){
int valid = 0;
int choose = 0;
double numUnits = 0.0;
double convertedUnits = 0.0;
// title, class, author and version info to
KeyboardReader entries =new KeyboardReader();//object for keyboard entry
while (valid != 1){
// menu for conversion selection 1 thru 4
System.out.println("\n\t\t1 \tFor inches to centimeters");
System.out.println("\t\t2 \tFor quarts to liters");
System.out.println("\t\t3 \tFor pounds to kilograms");
System.out.println("\t\t4 \tFor miles to Kilometer");
choose =// GW corrected my code to the present prior I was choose = choose.readInt etc etc
entries.readInt("\n\t\tPlease choose one of the following menu items to convert: ");
if (choose == 1 || choose == 2 || choose == 3 || choose ==4 ){
valid = 1 ;
}
else{
System.out.println("Invalid Selection.") ;
}
}
switch (choose){
case 1 : System.out.println("Converting inches to centimeters... ") ;break ;
case 2 : System.out.println("Converting quarts to liters... ") ;break ;
case 3 : System.out.println("Converting pounds to kilograms... ") ;break ;
case 4 : System.out.println("Converting miles to kilometers... ") ;break ;
}// Error checking, reiterates the user's current choice prior to next question
System.out.println();
//**********************************************************************
KeyboardReader useEntry =new KeyboardReader();//object for keyboard entry
numUnits =
useEntry.readDouble("\n\t\tNow please enter the amount you wish to convert: ");
System.out.println ("\t\tThe conversion comes to " + numUnits);
//KeyEntry equal = new KeyEntry();
//equal.setNumUnits
//(useEntry.readDouble("\n\t\tNow please enter the amount you wish to convert: "));
//System.out.println("\t\tThe conversion comes to " + getnumUnits());
switch (choose){
case 1 : Inch fromInch =new Inch(numUnits);break ;}
case 2 : Quart fromQuart =new Quart(numUnits) ;break ;
/*case 3 : Pound fromPound = new Pound( numUnits) ; break ;
case 4 : Mile fromMile = new Mile(numUnits) ; break ;
} // Switch statement that performs the correct conversion based on the user's input*/
// Instantiate US Standard Objects
/*Inch fromInch = new Inch(1) ;
Foot fromFoot = new Foot(1) ;
Yard fromYard = new Yard(1) ;
Mile fromMile = new Mile(1) ;
Ounce fromOunce = new Ounce(1) ;
Quart fromQuart = new Quart(1) ;
Gallon fromGallon = new Gallon(1) ;
Pound fromPound = new Pound(1) ;
// Instantiate Metric Objects
Centimeter fromCentimeter = new Centimeter(1) ;
Meter fromMeter = new Meter(1) ;
Kilometer fromKilometer = new Kilometer(1) ;
Gram fromGram = new Gram(1) ;
Liter fromLiter = new Liter(1) ;
Kilogram fromKilogram = new Kilogram(1) ;
LitGal fromLitGal = new LitGal(1) ; */
}// main
}//class MetricConversionMain
supper classpublicclass SuperConverter{
/*
*
*This class is the super class for a series of
*subclasses that perform conversion from metric to
*imperial or vice versa.
*
*/
protecteddouble numUnits ;
protecteddouble convertedUnits ;
protecteddouble factor ;
// Constructor
public SuperConverter (double argNumUnits,double argFactor ){
numUnits = argNumUnits ;
factor = argFactor ;
convertedUnits = numUnits * factor ;//convert() ;
}// Constructor
protecteddouble getnumUnits(){
return numUnits ;
}
protectedvoid setnumUnits(double argNumUnits ){
numUnits = argNumUnits ;
}
protecteddouble convert (){
this.convertedUnits = numUnits * factor ;
return convertedUnits ;
}
publicdouble getconvertedUnits(){
this.convertedUnits = convert() ;
return convertedUnits ;
}
}// end SuperConverter Class
subclass that works[publicclass Inchextends SuperConverter{
public Inch (double argNumUnits)
{
super ( argNumUnits, 0.914 ) ;
System.out.print("") ;
if (argNumUnits > 1)
{
System.out.println(argNumUnits +" inches = " + convertedUnits +" centimeters") ;
}
else
{
System.out.println(argNumUnits +" inch = " + convertedUnits +" centimeters") ;
}
}
}
/code]
The code I want to use with the applet is [code]import java.awt.*;
import java.applet.*;
import javax.swing.* ;
publicclass Inchextends SuperConverter{
finalstatic String CRLF ="\n" ;
static String msgOut ;
public Inch (double argNumUnits)
{
super ( argNumUnits, 2.54 ) ;
System.out.print("") ;
if (argNumUnits > 1)
{msgOut ="Metric Conversion Chart" + CRLF + CRLF;
msgOut +="US Standard to Metric" + CRLF ;
msgOut +="-" +" " + CRLF ;
msgOut = argNumUnits +" inches = " + convertedUnits +" centimeters" ;
}
else
{msgOut ="Metric Conversion Chart" + CRLF + CRLF;
msgOut +="US Standard to Metric" + CRLF ;
msgOut +="-" +" " + CRLF ;
msgOut += argNumUnits +" inch = " + convertedUnits +" centimeters" + CRLF + CRLF + CRLF ;
}JOptionPane.showMessageDialog ( null, msgOut ) ;
}
}

