The program below is the answer to Deitel’s Java How to Program (9th Edition) Chapter 2 Exercise 2.26.Question: Write an…
The program below is the answer to Deitel’s Java How to Program (9th Edition) Chapter 2 Exercise 2.24.Question: Write an…
The program below is the solution to Deitel’s Java How to Program (9th Edition) Chapter 2 Exercise 2.24.Question: Write an…
Java application to calculate the sum, average, product, smallest and largest of three numbers. The program below is the solution…
The program below is the answer to Deitel’s Java How to Program (9th Edition) Chapter 2 Exercise 2.18. Write an application…
Simple Java GUI Calculator that performs simple interest calculation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.border.TitledBorder; public class ModifiedGUISimpleInterestCalculator extends JFrame { //create TextFields for principal, rate and time private JTextField principal = new JTextField(); private JTextField rate = new JTextField(); private JTextField time = new JTextField(); private JTextField simpleInterest = new JTextField(); // calculate JButton private JButton clear = new JButton("Clear"); private JButton calculate = new JButton("Calculate"); //private JButton clear = new JButton("Clear"); //create JTextArea JTextArea area = new JTextArea(1, 1); public ModifiedGUISimpleInterestCalculator() { // panel panel to hold labels and text fields JPanel panel1 = new JPanel(new GridLayout(5, 2)); panel1.add(new JLabel("Enter Principal")); panel1.add(principal); panel1.add(new JLabel("Enter Rate")); panel1.add(rate); panel1.add(new JLabel("Enter Time")); panel1.add(time); panel1.add(new JLabel("Simple Interest")); panel1.add(simpleInterest); panel1.setBorder(new TitledBorder("Enter Principal, Rate (as decimal e.g 0.1 for 10%) and Time.")); principal.setBorder(BorderFactory.createEtchedBorder()); rate.setBorder(BorderFactory.createEtchedBorder()); time.setBorder(BorderFactory.createEtchedBorder()); simpleInterest.setBorder(BorderFactory.createEtchedBorder()); simpleInterest.setEditable(false); simpleInterest.setForeground(Color.RED); // Panel p2 to hold the button JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT)); //panel2.add(clear); panel2.add(clear); panel2.add(calculate); // Add the panels to the frame add(panel1, BorderLayout.CENTER); add(panel2, BorderLayout.SOUTH); area.setForeground(Color.BLACK); area.setBackground(Color.WHITE); area.setLineWrap(true); area.setWrapStyleWord(true); area.setEditable(false); calculate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Get values from text fields try { double amount = Double.parseDouble(principal.getText()); double rateOfInterest = Double.parseDouble(rate.getText()); double year = Double.parseDouble(time.getText()); String interest = "" + (amount * rateOfInterest * year); // Display simple interest simpleInterest.setText((interest)); } catch (Exception f) { JOptionPane.showMessageDialog(rootPane, "ERROR: " + (f.getMessage())); } String amountField = principal.getText(); String rateOfInterestField = rate.getText(); String yearField = time.getText(); if (e.getSource() == calculate) { if ("".equals(amountField) || "".equals(rateOfInterestField) || "".equals(yearField)) { String emptyFieldWarning; emptyFieldWarning = "One or more fields is/are empty!"; JOptionPane.showMessageDialog(rootPane, emptyFieldWarning); } } } }); clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Get values from text fields try { principal.setText(""); rate.setText(""); time.setText(""); simpleInterest.setText(""); } catch (Exception f) { } } }); } //create main method to run application public static void main(String[] args) { ModifiedGUISimpleInterestCalculator calculator = new ModifiedGUISimpleInterestCalculator(); calculator.setSize(360, 200); calculator.setTitle(" Simple Interest Calculator "); calculator.setResizable(false); calculator.setVisible(true); calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
Below is a screenshot of the output when the program is run. Click…
Program to accept five integer values from the user and calculate the sum, product, average, highest value and lowest value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <stdio.h> int main (void) { int a, b, c, d, e, sum, prod, ave; printf("Enter five integer numbers: "); scanf("%d%d%d%d%d", &a, &b, &c, &d, &e); sum = a + b + c + d + e; printf("\n\nSum is %d\n", sum); prod = a * b * c * d * e; printf("Product is %d\n", prod); ave = (a + b + c + d + e) / 5; printf("Average is %d\n", ave); /*calculate for highest number*/ if (a > b && a > c && a > d && a > e){ printf("Highest number is %d\n", a); } else if (b > a && b > c && b > d && b > e){ printf("Highest number is %d\n", b); } else if (c > a && c > b && c > d && c > e){ printf("Highest number is %d\n", c); } else if (d > a && d > b && d > c && d > e){ printf("Highest number is %d\n", d); } else if (e > a && e > b && e > c && e > d){ printf("Highest number is %d\n", e); } else { puts ("One or more numbers are equal.\n"); } /*calculate for lowest number*/ if (a < b && a < c && a < d && a < e){ printf("Lowest number is %d\n", a); } else if (b < a && b < c && b < d && b < e){ printf("Lowest number is %d\n", b); } else if (c < a && c < b && c < d && c < e){ printf("Lowest number is %d\n", c); } else if (d < a && d < b && d < c && d < e){ printf("Lowest number is %d\n", d); } else if (e < a && e < b && e < c && e < d){ printf("Lowest number is %d\n", e); } else { puts ("One or more numbers are equal.\n"); } return 0; } |
Below…
The program below separates the digits of a five-digit number using C programming language.Question: Write a program that inputs one…
A simple C program to add two integer numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main (void) { int x, y, z; x = 5; y = 52; z = x + y; printf("\n\nx = 5 \ny = 52 \n"); printf("z = x + y = %d", z); return 0; } |
Question: Write an application that displays a checkerboard pattern, as follows:
1 2 3 4 5 6 7 8 9 |
public class CheckerboardPatternOfAsterisks { public static void main (String [ ] args) { System.out.print("********\n ********\n********\n ********\n" + "********\n ********\n********\n ********\n"); } } |
Note that this program can also be written using eight System.out.println…
The program below separate digits in an integer using Java. It is the solution to Deitel’s Java How to Program…
Below is a simple program to compare two numbers using Java. It is the solution to Deitel’s Java How to…