In Java - Part 1 - Improve the output quality of the investment application in S
ID: 3920833 • Letter: I
Question
In Java -
Part 1 - Improve the output quality of the investment application in Section 10.3.2. Format the numbers with two decimal digits, using the String.format method. Set the font of the text area to a fixed width font, using the call
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
Part 2 - Add error handling to the program in Section 10.3.2. If the interest rate is not a floating-point number, or if it less than 0, display an error message, using a JOptionPane (see Special Topic 2.5).
PLEASE USE JAVADOC AND CODE COMMENT
PLEASE INDICATE HOW MANY CLASSES OR PACKAGES SHOULD BE CREATED. Appreciate if you could include images of your code. I use Eclipse
Here is the code:
Explanation / Answer
here is your modified code : ----------------->>>>>>>>>>>>
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
public class InvestmentFrame3 extends JFrame{
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 250;
private static final int AREA_ROWS = 10;
private static final int AREA_COLUMNS = 30;
private static final double DEFAULT_RATE = 5;
private static final double INITIAL_BALANCE = 1000;
private JLabel rateLabel;
private JTextField rateField;
private JButton button;
private JTextArea resultArea;
private double balance;
public InvestmentFrame3(){
balance = INITIAL_BALANCE;
resultArea = new JTextArea(AREA_ROWS,AREA_COLUMNS);
resultArea.setFont(new Font(Font.MONOSPACED,Font.PLAIN,12));
resultArea.setText(String.format("%.2f",balance)+" ");
resultArea.setEditable(false);
createTextField();
createButton();
createPanel();
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createTextField(){
rateLabel = new JLabel("Interest Rate: ");
final int FIELD_WIDTH = 10;
rateField = new JTextField(FIELD_WIDTH);
rateField.setText(" "+DEFAULT_RATE);
}
class AddInterestListener implements ActionListener{
public void actionPerformed(ActionEvent event){
double rate = -2;
try{
rate = Double.parseDouble(rateField.getText());
if(rate < 0){
throw new Exception("");
}
double interest = balance * rate / 100;
balance = balance + interest;
resultArea.append(String.format("%.2f",balance)+" ");
}catch(Exception e){
JOptionPane.showMessageDialog(null,"Interest Rate is Not Valid !!!");
}
}
}
private void createButton(){
button = new JButton("Add Interest");
ActionListener listener = new AddInterestListener();
button.addActionListener(listener);
}
private void createPanel(){
JPanel panel = new JPanel();
panel .add(rateLabel);
panel.add(rateField);
panel.add(button);
JScrollPane scrollPane = new JScrollPane(resultArea);
panel.add(scrollPane);
add(panel);
}
public static void main(String[] args) {
InvestmentFrame3 f = new InvestmentFrame3();
f.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.