The document describes three Java programs for calculating simple mathematical operations and values:
1. A calculator program that allows users to enter two numbers and select a mathematical operation (addition, subtraction, multiplication, or division) to perform the calculation.
2. A program that calculates severance pay values based on a user's time of service and monthly salary.
3. A program that provides budget estimates for different construction/remodeling jobs based on area size and material selections.
2. EXERCICIO 1 – CALCULADORA
Source
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package calculadora;
/**
*
* @author CAO VeraCymbron
*/
public class Calculadora extends javax.swing.JFrame {
/**
* Creates new form Calculadora
*/
public Calculadora() {
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() {
valor1 = new javax.swing.JLabel();
valor2 = new javax.swing.JLabel();
TF2 = new javax.swing.JTextField();
3. bsoma = new javax.swing.JButton();
TF3 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
bdivisao = new javax.swing.JButton();
bsubtraccao = new javax.swing.JButton();
bmultiplicacao = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
valor1.setText("Numero1");
valor1.setToolTipText("");
valor2.setText("Numero2");
TF2.setToolTipText("");
bsoma.setMnemonic('s');
bsoma.setText("Soma");
bsoma.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bsomaActionPerformed(evt);
}
});
TF3.setToolTipText("");
jLabel3.setText("Resultado:");
bdivisao.setMnemonic('s');
bdivisao.setText("Divisão");
bdivisao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bdivisaoActionPerformed(evt);
}
});
bsubtraccao.setMnemonic('s');
bsubtraccao.setText("Subtracção");
bsubtraccao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bsubtraccaoActionPerformed(evt);
}
});
bmultiplicacao.setMnemonic('s');
bmultiplicacao.setText("Multiplicação");
bmultiplicacao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bmultiplicacaoActionPerformed(evt);
}
});
5. .addComponent(TF2, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bsoma)
.addComponent(bsubtraccao))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bdivisao)
.addComponent(bmultiplicacao))
.addGap(18, 18, 18)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void bsomaActionPerformed(java.awt.event.ActionEvent evt) {
double num1, num2, res; //variáveis auxiliares
//converter o numero digitado em Float
num1 = Double.parseDouble(TF2.getText());
num2 = Double.parseDouble (TF3.getText());
res = num1 + num2;
//converte o resultado em String e mostrar
jLabel3.setText(String.valueOf("Resultado: " +res));
TF2.setText(" ");//Limpar o JTextField
TF3.setText(" ");
TF3.requestFocus();//muda o foco para o JTextField1
}
private void bdivisaoActionPerformed(java.awt.event.ActionEvent evt) {
double num1, num2, res; //variáveis auxiliares
//converter o numero digitado em Float
num1 = Double.parseDouble(TF2.getText());
num2 = Double.parseDouble (TF3.getText());
res = num1 / num2;
//converte o resultado em String e mostrar
jLabel3.setText(String.valueOf("Resultado: " +res));
TF2.setText(" ");//Limpar o JTextField
TF3.setText(" ");
TF3.requestFocus();//muda o foco para o JTextField1
}
private void bsubtraccaoActionPerformed(java.awt.event.ActionEvent evt) {
double num1, num2, res; //variáveis auxiliares
//converter o numero digitado em Float
num1 = Double.parseDouble(TF2.getText());
num2 = Double.parseDouble (TF3.getText());
6. res = num1 - num2;
//converte o resultado em String e mostrar
jLabel3.setText(String.valueOf("Resultado: " +res));
TF2.setText(" ");//Limpar o JTextField
TF3.setText(" ");
TF3.requestFocus();//muda o foco para o JTextField1
}
private void bmultiplicacaoActionPerformed(java.awt.event.ActionEvent evt) {
double num1, num2, res; //variáveis auxiliares
//converter o numero digitado em Float
num1 = Double.parseDouble(TF2.getText());
num2 = Double.parseDouble (TF3.getText());
res = num1 * num2;
//converte o resultado em String e mostrar
jLabel3.setText(String.valueOf("Resultado: " +res));
TF2.setText(" ");//Limpar o JTextField
TF3.setText(" ");
TF3.requestFocus();//muda o foco para o JTextField1
}
/**
* @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(Calculadora.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (IllegalAccessException ex) {
7. java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calculadora.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculadora().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField TF2;
private javax.swing.JTextField TF3;
private javax.swing.JButton bdivisao;
private javax.swing.JButton bmultiplicacao;
private javax.swing.JButton bsoma;
private javax.swing.JButton bsubtraccao;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel valor1;
private javax.swing.JLabel valor2;
// End of variables declaration
}
EXERCICIO 2 – CALCULAR VALOR DA INDEMINIZAÇÃO
SOURCE
/*
8. * To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package valorindeminizacao;
/**
*
* @author CAO 12
*/
public class ValorIndemnizacao extends javax.swing.JFrame {
/**
* Creates new form ValorIndemnizacao
*/
public ValorIndemnizacao() {
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() {
valor1 = new javax.swing.JLabel();
valor2 = new javax.swing.JLabel();
num1 = new javax.swing.JTextField();
num2 = new javax.swing.JTextField();
num3 = new javax.swing.JTextField();
calcular = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
valor3 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
valor1.setText("Tempo de serviço (MESES)");
valor2.setText("Vencimento mensal");
calcular.setText("Cacular");
calcular.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calcularActionPerformed(evt);
}
});
jLabel4.setText("Valor da Indemnização é :");
valor3.setText("No caso de ter dias de férias por gozar indique");
10. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(valor3)
.addComponent(num3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calcular)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(149, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void calcularActionPerformed(java.awt.event.ActionEvent evt) {
double valor1, valor2, valor3, valor = 0;
//variáveis auxiliares
//converter o numero digitado em Float
valor1 = Double.parseDouble(num1.getText());
valor2 = Double.parseDouble (num2.getText());
valor3 = Double.parseDouble (num3.getText());
if (valor1<=6) {
valor = ((valor1*3*valor2)/30)+((valor3*valor2)/30);
}
else
if (valor1 >=7) {
valor = ((valor1*2*valor2)/30)+((valor3*valor2)/30);
}
//converte o resultado em String e mostrar
jLabel4.setText(String.valueOf("O valor da indemnização é: " +valor+ "€"));
num1.setText(" ");//Limpar o JTextField
num2.setText(" ");
num3.setText(" ");
num1.requestFocus();//muda o foco para o JTextField1
}
/**
* @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())) {
12. EXERCICIO 3 – ORÇAMENTO
SOURCE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package orcamento;
import javax.swing.JComboBox;
/**
*
* @author CAO 12
*/
public class Orcamento extends javax.swing.JFrame {
/**
* Creates new form Orcamento
*/
public Orcamento() {
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">
13. private void initComponents() {
jComboBox2 = new javax.swing.JComboBox();
area = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
num1 = new javax.swing.JTextField();
saida = new javax.swing.JLabel();
jcomboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "->",
"Estuco", "Teto falso com Plauduro normal", "Teto falso com Plauduro hidrofugado" }));
jComboBox2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox2ActionPerformed(evt);
}
});
area.setText("Área m2");
jLabel2.setText("Seleccione o serviço pretendido");
saida.setText("O valor é:");
jcomboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "23", "16"
}));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 173,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(area)
.addGap(18, 18, 18)
.addComponent(num1, javax.swing.GroupLayout.PREFERRED_SIZE, 63,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 369,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(saida, javax.swing.GroupLayout.PREFERRED_SIZE, 343,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jcomboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
15. //converte o resultado em String e mostrar
saida.setText(String.valueOf("O valor do serviço é: " +resultado+"€"));
num1.setText(" ");//Limpar o JTextField
num1.requestFocus();//muda o foco para o JTextField1
}
/**
* @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(Orcamento.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Orcamento.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Orcamento.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Orcamento.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Orcamento().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel area;
private javax.swing.JComboBox jComboBox2;
16. private javax.swing.JLabel jLabel2;
private javax.swing.JComboBox jcomboBox1;
private javax.swing.JTextField num1;
private javax.swing.JLabel saida;
// End of variables declaration
}
EXERCICIO 4 – CALCULO DA MASSA CORPORAL
SOURCE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package massacorporal;
/**
*
* @author CAO 12
*/
public class MASSACORPORAL extends javax.swing.JFrame {
/**
* Creates new form MASSACORPORAL
*/
public MASSACORPORAL() {
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.