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

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern