MpgCalculator - a input1 = new TextField(10 add(input1/put...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* Author: Eric Adlam * Section #: D5 * Instructor: Coach * Project Name: MpgCalculator * Assignment #: 4 * Due Date: 9/17/09 * * Description: Java Applet which calculates miles per gallon * for a given input as well as the total miles per * gallon of all inputs entered during the session. */ package week3; p import java.applet.Applet; import java.awt.*; import java.awt.event.*; i public class MpgCalculator extends Applet implements ActionListener { Label prompt1; // prompt user to input first value TextField input1; // input first value here Label prompt2; // prompt user to input second value TextField input2; // input second value here float miles, gallons, t_miles, t_gallons; // store input values f // setup the graphical user interface components // and initialize variables public void init(){ prompt1 = new Label("Enter Miles:"); add(prompt1); //put prompt1 on applet
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: a input1 = new TextField(10); add(input1); //put input1 on applet a prompt2 = new Label("Enter Gallons:"); add(prompt2); //put prompt2 on applet a input2 = new TextField(10); input2.addActionListener(this); add(input2); //put input2 on applet } //display the results public void paint(Graphics g){ // If dividing by zero then total = 0 float mpg = (gallons == 0)?(0):(miles/gallons); float t_mpg = (t_gallons == 0)?(0):(t_miles/t_gallons); f // put totals on applet g.drawString("MPG this tankful:" + mpg, 70, 75); g.drawString("Total MPG:" + t_mpg, 70, 100); } //process user's action on the input2 text field public void actionPerformed( ActionEvent e ){ miles = Float.parseFloat( input1.getText() ); gallons = Float.parseFloat( input2.getText() ); t_miles = t_miles + miles; t_gallons = t_gallons + gallons; repaint(); } }...
View Full Document

This note was uploaded on 10/12/2009 for the course CS 120 taught by Professor Robertdependahl during the Fall '09 term at Santa Barbara City.

Page1 / 2

MpgCalculator - a input1 = new TextField(10 add(input1/put...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online