68 249
Assignments Done
99%
Successfully Done
In November 2018

Answer to Question #197 in Java | JSP | JSF for Tracy

Question #197
I wanted to know how put checkboxes in this code and if the code is right.

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;


public class JMyNewHome extends JFrame implements ActionListener
{
static final double MODEL_ASPEN = 100000;
static final double MODEL_BRITTANY = 120000;
static final double MODEL_COLONIAL = 180000;
static final double MODEL_DARTMOOR = 250000;
static final double FEE_BEDROOM = 105000;
static final double FEE_CAR = 7775;

private JLabel lblModels = new JLabel("The models: ");
private JRadioButton[] models = new JRadioButton[] {
new JRadioButton("The Aspen"),
new JRadioButton("The Brittany"),
new JRadioButton("The Colonial"),
new JRadioButton("The Dartmoor")};

private JLabel lblBedrooms = new JLabel("The number of bedrooms: ");
private JRadioButton[] bedrooms = new JRadioButton[] {
new JRadioButton("Two"),
new JRadioButton("Three"),
new JRadioButton("Four")};

private JLabel lblGarage = new JLabel("The garage type: ");
private JRadioButton[] garage = new JRadioButton[] {
new JRadioButton("Zero-car"),
new JRadioButton("One-car"),
new JRadioButton("Two-car"),
new JRadioButton("Three-car")};

private JButton btnCalculate = new JButton("Calculate");
private JLabel lblTotal = new JLabel("Total fee is: ");
private JTextField txtTotal = new JTextField(10);

public JMyNewHome()
{
super("My New Home");

setLayout(new GridLayout(4, 1));

// add models
JPanel pnModels = new JPanel();
pnModels.setLayout(new FlowLayout());
pnModels.add(lblModels);
ButtonGroup groupModels = new ButtonGroup();
for (int i = 0; i < models.length; i++)
{
pnModels.add(models[i]);
groupModels.add(models[i]);
}
models[0].setSelected(true);
add(pnModels);

// add bedrooms
JPanel pnBedrooms = new JPanel();
pnBedrooms.setLayout(new FlowLayout());
pnBedrooms.add(lblBedrooms);
ButtonGroup groupBedrooms = new ButtonGroup();
for (int i = 0; i < bedrooms.length; i++)
{
pnBedrooms.add(bedrooms[i]);
groupBedrooms.add(bedrooms[i]);
}
bedrooms[0].setSelected(true);
add(pnBedrooms);

// add garage type
JPanel pnGarage = new JPanel();
pnGarage.setLayout(new FlowLayout());
pnGarage.add(lblGarage);
ButtonGroup groupGarage = new ButtonGroup();
for (int i = 0; i < garage.length; i++)
{
pnGarage.add(garage[i]);
groupGarage.add(garage[i]);
}
garage[0].setSelected(true);
add(pnGarage);

// add result panel
JPanel pnResult = new JPanel();
pnResult.setLayout(new FlowLayout());
pnResult.add(btnCalculate);
pnResult.add(lblTotal);
pnResult.add(txtTotal);
add(pnResult);

btnCalculate.addActionListener(this);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 250);
}

public void actionPerformed(ActionEvent e)
{
double total = 0;
if (models[0].isSelected())
total += MODEL_ASPEN;
else if (models[1].isSelected())
total += MODEL_BRITTANY;
else if (models[2].isSelected())
total += MODEL_COLONIAL;
else
total += MODEL_DARTMOOR;

if (bedrooms[0].isSelected())
total += 2 * FEE_BEDROOM;
else if (bedrooms[1].isSelected())
total += 3 * FEE_BEDROOM;
else
total += 4 * FEE_BEDROOM;

if (garage[1].isSelected())
total += FEE_CAR;
else if (garage[2].isSelected())
total += 2 * FEE_CAR;
else if (garage[3].isSelected())
total += 3 * FEE_CAR;

// output to text field
txtTotal.setText(String.format("$%.2f", total));

}

public static void main(String[] args)
{
new JMyNewHome().setVisible(true);
}



}imp
Expert's answer
Dear customer. This question is too complicated for this section. You can submit your assignment on our site and we will help you.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions