Complete Application

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

"Calories from Fat"

Complete Application

Here is the complete application, suitable for copying to an editor, saving to a file, and running.

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
   
public class PercentFat extends JFrame implements ActionListener
{
  JLabel heading   = new JLabel("Percent of Calories from Fat");
  JLabel fatLabel  = new JLabel("Enter grams of fat:   ");
  JLabel calLabel  = new JLabel("Enter total calories: ");
  JLabel perLabel  = new JLabel("Percent calories from fat: ");
    
  JTextField inFat  = new JTextField( 7 );
  JTextField inCal  = new JTextField( 7 );
  JTextField outPer = new JTextField( 7 );
    
  JButton    doit   = new JButton("Do It!");
    
  double calories ;  // input: total calories per serving
  double fatGrams ;  // input: grams of fat per serving
  double percent;   // result: percent of calories from fat
    
  public PercentFat()   
  {  
    setTitle( "Calories from Fat" );
    setLayout( new FlowLayout() );
   
    add( heading );  
    add( fatLabel );  
    add( inFat );    
    add( calLabel );  
    add( inCal );    
    add( perLabel );   
    add( outPer );   
    outPer.setEditable( false );    
   
    add( doit );     
    doit.addActionListener( this );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
  }
   
  // The application
  public void calcPercent( )  
  {
    percent = ((fatGrams * 9) / calories) * 100 ;
  }
   
  public void actionPerformed( ActionEvent evt)  
  {
    String userIn ;
    userIn    = inFat.getText()  ;
    fatGrams  = Double.parseDouble( userIn ) ;
   
    userIn    = inCal.getText()  ;
    calories  = Double.parseDouble( userIn ) ;
   
    calcPercent() ;
   
    // Convert percent to a nice string and write it to the output field
    outPer.setText( new DecimalFormat("#0.0##").format( percent ) );
    repaint();                  
  }
   
  public static void main ( String[] args )
  {
    PercentFat fatApp  = new PercentFat() ;
    fatApp.setSize( 280, 200 );     
    fatApp.setVisible( true );         
  }
}

The next to final line of the listener uses DecimalFormat to make the output look better. Look at Chapter 24B if you want to review this.

QUESTION 17:

If the user enters bogus data into the text fields, does this program crash?