angrypikachu
Lurker
So I'm out of school and as a summer project I decided to teach myself how to develop android applications. Right now I am trying to write a very simple app that calculates miles per gallon. i have wrote two versions in regular JAVA but after the past few day I just have gotten stuck trying to teach myself how i can "port" it over to work on an android phone. If anyone can show me a basic way how to do this I would be very very grateful.
Here are my two versions:
This one is just works within the compiler
This one I made with buttons and a separate window
I'm trying to get a simple app set up like the 2nd version, but I'm having trouble setting up the boxes for user input and re displaying it once calculated.
Thank you any good guy greg that helps me out.
Here are my two versions:
This one is just works within the compiler
Code:
import java.util.Scanner;
public class Mpg
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
float g;
float m;
float mpg;
System.out.print("Enter miles");
m = keyboard.nextFloat();
System.out.print("enter gallons");
g=keyboard.nextFloat();
mpg = m/g;
System.out.println(mpg);
}
}
This one I made with buttons and a separate window
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// we can make a window from JFrame
public class MilesPerGallon extends JFrame implements ActionListener
{
private JLabel lblMiles;
private JLabel lblGallons;
private JLabel lblResult;
private JLabel lblResultUnits;
private JTextField tfMiles;
private JTextField tfGallons;
private JButton btnCalc;
// FeetToMeters Constructor
public MilesPerGallon()
{
super("Simple MPG");
setLayout( new GridLayout(3,2) );
// these two lines are needed in almost every window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250,100);
lblMiles = new JLabel("Miles");
tfMiles = new JTextField(8);
lblGallons = new JLabel("Gallons");
tfGallons = new JTextField(8);
lblResult = new JLabel("0");
lblResultUnits = new JLabel("mpg");
btnCalc = new JButton("Calculate");
add(lblMiles);
add(tfMiles);
add(lblGallons);
add(tfGallons);
add(lblResult);
add(btnCalc);
btnCalc.addActionListener( this );
setVisible(true); // show the window
}
public void actionPerformed(ActionEvent e)
{
float Miles;
float Gallons;
float mpg;
Miles = Float.parseFloat( tfMiles.getText() );
Gallons = Float.parseFloat( tfGallons.getText() );
mpg = Miles / Gallons ;
lblResult.setText( Float.toString( mpg ) );
lblResultUnits.setText("mpg");
}
public static void main(String args[])
{
// make a FeetToMeters object
// this calls the constructor which builds the window
MilesPerGallon frame = new MilesPerGallon();
}
}
I'm trying to get a simple app set up like the 2nd version, but I'm having trouble setting up the boxes for user input and re displaying it once calculated.
Thank you any good guy greg that helps me out.