School project - Pizza price calculator












0














I've been working on a program for my ICS3U course at school, and it's not finished yet in terms of formatting the decimal place, but even without that, I still can't get the code to run properly. The project requires a text field where a customer can enter the diameter of their pizza in inches. This value can be saved in a variable, but is automatically a string. The conent provided for the assignment says that we then have to convert it to a double variable to use in the calculations. I suspect this is where the problem has occurred, but I'm not certain. I would greatly appreciate if anybody would be willing to review my code and help me identify the error and how to fix it. Thanks. The assignment and code are as follows:



Assignment:
The cost of making pizza at a local shop is as follows:
Labour cost is $1.00 per pizza, regardless of size.
Store cost is $1.50 per pizza, regardless of size.
Materials is $0.50 * diameter (diameter is measured in inches).
Create a Java application that prompts the user for the size of a pizza and then displays the cost of making the pizza.
The application should look similar to the image below. Notice that the output is formatted to 2 decimal places with a dollar sign in front. Remember that this can be accomplished using DecimalFormat as seen in previous activities.



Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/



public class PizzaAnyoneGUI extends javax.swing.JFrame {



/**
* Creates new form PizzaAnyoneGUI
*/
public PizzaAnyoneGUI() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

titleLabel = new javax.swing.JLabel();
calculateButton = new javax.swing.JButton();
inchesInput = new javax.swing.JTextField();
inchesLabel = new javax.swing.JLabel();
outputLabel = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

titleLabel.setText("Pizza Price Calculator");

calculateButton.setText("Calculate");
calculateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateButtonActionPerformed(evt);
}
});

inchesInput.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
inchesInputActionPerformed(evt);
}
});

inchesLabel.setText("Enter the diameter of the pizza in inches.");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(164, 164, 164)
.addComponent(titleLabel)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(calculateButton))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(inchesLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
.addComponent(inchesInput, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGap(26, 26, 26))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(titleLabel)
.addGap(68, 68, 68)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(inchesInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(inchesLabel))
.addGap(29, 29, 29)
.addComponent(calculateButton)
.addGap(18, 18, 18)
.addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 110, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void inchesInputActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
String inches ;
int labourcost ;
double storecost ;
double materialcost ;
double total ;

inches = Double.parseDouble (inchesInput.getText ()) ;
labourcost = 1 ;
storecost = 1.5 ;
materialcost = inches * 0.5 ;
total = labourcost + storecost + materialcost ;

outputLabel.setText ("The cost of the pizza is"+total+".") ;
}

/**
* @param args the command line arguments
*/
public static void main(String args) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PizzaAnyoneGUI().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton calculateButton;
private javax.swing.JTextField inchesInput;
private javax.swing.JLabel inchesLabel;
private javax.swing.JLabel outputLabel;
private javax.swing.JLabel titleLabel;
// End of variables declaration


}









share







New contributor




Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    I've been working on a program for my ICS3U course at school, and it's not finished yet in terms of formatting the decimal place, but even without that, I still can't get the code to run properly. The project requires a text field where a customer can enter the diameter of their pizza in inches. This value can be saved in a variable, but is automatically a string. The conent provided for the assignment says that we then have to convert it to a double variable to use in the calculations. I suspect this is where the problem has occurred, but I'm not certain. I would greatly appreciate if anybody would be willing to review my code and help me identify the error and how to fix it. Thanks. The assignment and code are as follows:



    Assignment:
    The cost of making pizza at a local shop is as follows:
    Labour cost is $1.00 per pizza, regardless of size.
    Store cost is $1.50 per pizza, regardless of size.
    Materials is $0.50 * diameter (diameter is measured in inches).
    Create a Java application that prompts the user for the size of a pizza and then displays the cost of making the pizza.
    The application should look similar to the image below. Notice that the output is formatted to 2 decimal places with a dollar sign in front. Remember that this can be accomplished using DecimalFormat as seen in previous activities.



    Code:
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */



    public class PizzaAnyoneGUI extends javax.swing.JFrame {



    /**
    * Creates new form PizzaAnyoneGUI
    */
    public PizzaAnyoneGUI() {
    initComponents();
    }

    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    titleLabel = new javax.swing.JLabel();
    calculateButton = new javax.swing.JButton();
    inchesInput = new javax.swing.JTextField();
    inchesLabel = new javax.swing.JLabel();
    outputLabel = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    titleLabel.setText("Pizza Price Calculator");

    calculateButton.setText("Calculate");
    calculateButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    calculateButtonActionPerformed(evt);
    }
    });

    inchesInput.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    inchesInputActionPerformed(evt);
    }
    });

    inchesLabel.setText("Enter the diameter of the pizza in inches.");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(164, 164, 164)
    .addComponent(titleLabel)
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(calculateButton))
    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
    .addGap(31, 31, 31)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(0, 0, Short.MAX_VALUE))
    .addGroup(layout.createSequentialGroup()
    .addComponent(inchesLabel)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
    .addComponent(inchesInput, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))))
    .addGap(26, 26, 26))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addComponent(titleLabel)
    .addGap(68, 68, 68)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(inchesInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(inchesLabel))
    .addGap(29, 29, 29)
    .addComponent(calculateButton)
    .addGap(18, 18, 18)
    .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(0, 110, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void inchesInputActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String inches ;
    int labourcost ;
    double storecost ;
    double materialcost ;
    double total ;

    inches = Double.parseDouble (inchesInput.getText ()) ;
    labourcost = 1 ;
    storecost = 1.5 ;
    materialcost = inches * 0.5 ;
    total = labourcost + storecost + materialcost ;

    outputLabel.setText ("The cost of the pizza is"+total+".") ;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClassName());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new PizzaAnyoneGUI().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton calculateButton;
    private javax.swing.JTextField inchesInput;
    private javax.swing.JLabel inchesLabel;
    private javax.swing.JLabel outputLabel;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration


    }









    share







    New contributor




    Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      I've been working on a program for my ICS3U course at school, and it's not finished yet in terms of formatting the decimal place, but even without that, I still can't get the code to run properly. The project requires a text field where a customer can enter the diameter of their pizza in inches. This value can be saved in a variable, but is automatically a string. The conent provided for the assignment says that we then have to convert it to a double variable to use in the calculations. I suspect this is where the problem has occurred, but I'm not certain. I would greatly appreciate if anybody would be willing to review my code and help me identify the error and how to fix it. Thanks. The assignment and code are as follows:



      Assignment:
      The cost of making pizza at a local shop is as follows:
      Labour cost is $1.00 per pizza, regardless of size.
      Store cost is $1.50 per pizza, regardless of size.
      Materials is $0.50 * diameter (diameter is measured in inches).
      Create a Java application that prompts the user for the size of a pizza and then displays the cost of making the pizza.
      The application should look similar to the image below. Notice that the output is formatted to 2 decimal places with a dollar sign in front. Remember that this can be accomplished using DecimalFormat as seen in previous activities.



      Code:
      /*
      * To change this license header, choose License Headers in Project Properties.
      * To change this template file, choose Tools | Templates
      * and open the template in the editor.
      */



      public class PizzaAnyoneGUI extends javax.swing.JFrame {



      /**
      * Creates new form PizzaAnyoneGUI
      */
      public PizzaAnyoneGUI() {
      initComponents();
      }

      /**
      * This method is called from within the constructor to initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is always
      * regenerated by the Form Editor.
      */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">
      private void initComponents() {

      titleLabel = new javax.swing.JLabel();
      calculateButton = new javax.swing.JButton();
      inchesInput = new javax.swing.JTextField();
      inchesLabel = new javax.swing.JLabel();
      outputLabel = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      titleLabel.setText("Pizza Price Calculator");

      calculateButton.setText("Calculate");
      calculateButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      calculateButtonActionPerformed(evt);
      }
      });

      inchesInput.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      inchesInputActionPerformed(evt);
      }
      });

      inchesLabel.setText("Enter the diameter of the pizza in inches.");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addGap(164, 164, 164)
      .addComponent(titleLabel)
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(calculateButton))
      .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
      .addGap(31, 31, 31)
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGap(0, 0, Short.MAX_VALUE))
      .addGroup(layout.createSequentialGroup()
      .addComponent(inchesLabel)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
      .addComponent(inchesInput, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))))
      .addGap(26, 26, 26))
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addComponent(titleLabel)
      .addGap(68, 68, 68)
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(inchesInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addComponent(inchesLabel))
      .addGap(29, 29, 29)
      .addComponent(calculateButton)
      .addGap(18, 18, 18)
      .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGap(0, 110, Short.MAX_VALUE))
      );

      pack();
      }// </editor-fold>

      private void inchesInputActionPerformed(java.awt.event.ActionEvent evt) {
      // TODO add your handling code here:
      }

      private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
      String inches ;
      int labourcost ;
      double storecost ;
      double materialcost ;
      double total ;

      inches = Double.parseDouble (inchesInput.getText ()) ;
      labourcost = 1 ;
      storecost = 1.5 ;
      materialcost = inches * 0.5 ;
      total = labourcost + storecost + materialcost ;

      outputLabel.setText ("The cost of the pizza is"+total+".") ;
      }

      /**
      * @param args the command line arguments
      */
      public static void main(String args) {
      /* Set the Nimbus look and feel */
      //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
      /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
      * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
      */
      try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
      if ("Nimbus".equals(info.getName())) {
      javax.swing.UIManager.setLookAndFeel(info.getClassName());
      break;
      }
      }
      } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      }
      //</editor-fold>

      /* Create and display the form */
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      new PizzaAnyoneGUI().setVisible(true);
      }
      });
      }

      // Variables declaration - do not modify
      private javax.swing.JButton calculateButton;
      private javax.swing.JTextField inchesInput;
      private javax.swing.JLabel inchesLabel;
      private javax.swing.JLabel outputLabel;
      private javax.swing.JLabel titleLabel;
      // End of variables declaration


      }









      share







      New contributor




      Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I've been working on a program for my ICS3U course at school, and it's not finished yet in terms of formatting the decimal place, but even without that, I still can't get the code to run properly. The project requires a text field where a customer can enter the diameter of their pizza in inches. This value can be saved in a variable, but is automatically a string. The conent provided for the assignment says that we then have to convert it to a double variable to use in the calculations. I suspect this is where the problem has occurred, but I'm not certain. I would greatly appreciate if anybody would be willing to review my code and help me identify the error and how to fix it. Thanks. The assignment and code are as follows:



      Assignment:
      The cost of making pizza at a local shop is as follows:
      Labour cost is $1.00 per pizza, regardless of size.
      Store cost is $1.50 per pizza, regardless of size.
      Materials is $0.50 * diameter (diameter is measured in inches).
      Create a Java application that prompts the user for the size of a pizza and then displays the cost of making the pizza.
      The application should look similar to the image below. Notice that the output is formatted to 2 decimal places with a dollar sign in front. Remember that this can be accomplished using DecimalFormat as seen in previous activities.



      Code:
      /*
      * To change this license header, choose License Headers in Project Properties.
      * To change this template file, choose Tools | Templates
      * and open the template in the editor.
      */



      public class PizzaAnyoneGUI extends javax.swing.JFrame {



      /**
      * Creates new form PizzaAnyoneGUI
      */
      public PizzaAnyoneGUI() {
      initComponents();
      }

      /**
      * This method is called from within the constructor to initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is always
      * regenerated by the Form Editor.
      */
      @SuppressWarnings("unchecked")
      // <editor-fold defaultstate="collapsed" desc="Generated Code">
      private void initComponents() {

      titleLabel = new javax.swing.JLabel();
      calculateButton = new javax.swing.JButton();
      inchesInput = new javax.swing.JTextField();
      inchesLabel = new javax.swing.JLabel();
      outputLabel = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      titleLabel.setText("Pizza Price Calculator");

      calculateButton.setText("Calculate");
      calculateButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      calculateButtonActionPerformed(evt);
      }
      });

      inchesInput.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      inchesInputActionPerformed(evt);
      }
      });

      inchesLabel.setText("Enter the diameter of the pizza in inches.");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addGap(164, 164, 164)
      .addComponent(titleLabel)
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
      .addGroup(layout.createSequentialGroup()
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addComponent(calculateButton))
      .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
      .addGap(31, 31, 31)
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGap(0, 0, Short.MAX_VALUE))
      .addGroup(layout.createSequentialGroup()
      .addComponent(inchesLabel)
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
      .addComponent(inchesInput, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)))))
      .addGap(26, 26, 26))
      );
      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
      .addComponent(titleLabel)
      .addGap(68, 68, 68)
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(inchesInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addComponent(inchesLabel))
      .addGap(29, 29, 29)
      .addComponent(calculateButton)
      .addGap(18, 18, 18)
      .addComponent(outputLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
      .addGap(0, 110, Short.MAX_VALUE))
      );

      pack();
      }// </editor-fold>

      private void inchesInputActionPerformed(java.awt.event.ActionEvent evt) {
      // TODO add your handling code here:
      }

      private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
      String inches ;
      int labourcost ;
      double storecost ;
      double materialcost ;
      double total ;

      inches = Double.parseDouble (inchesInput.getText ()) ;
      labourcost = 1 ;
      storecost = 1.5 ;
      materialcost = inches * 0.5 ;
      total = labourcost + storecost + materialcost ;

      outputLabel.setText ("The cost of the pizza is"+total+".") ;
      }

      /**
      * @param args the command line arguments
      */
      public static void main(String args) {
      /* Set the Nimbus look and feel */
      //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
      /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
      * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
      */
      try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
      if ("Nimbus".equals(info.getName())) {
      javax.swing.UIManager.setLookAndFeel(info.getClassName());
      break;
      }
      }
      } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(PizzaAnyoneGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      }
      //</editor-fold>

      /* Create and display the form */
      java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
      new PizzaAnyoneGUI().setVisible(true);
      }
      });
      }

      // Variables declaration - do not modify
      private javax.swing.JButton calculateButton;
      private javax.swing.JTextField inchesInput;
      private javax.swing.JLabel inchesLabel;
      private javax.swing.JLabel outputLabel;
      private javax.swing.JLabel titleLabel;
      // End of variables declaration


      }







      java





      share







      New contributor




      Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 9 mins ago









      Liam

      1




      1




      New contributor




      Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Liam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Liam is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210200%2fschool-project-pizza-price-calculator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Liam is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Liam is a new contributor. Be nice, and check out our Code of Conduct.













          Liam is a new contributor. Be nice, and check out our Code of Conduct.












          Liam is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210200%2fschool-project-pizza-price-calculator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Feedback on college project

          Futebolista

          Albești (Vaslui)