Question

In: Computer Science

Write a class GBP2HKD containing the following methods: (a) constructor : which builds the frame shown...

Write a class GBP2HKD containing the following methods:

(a) constructor : which builds the frame shown on the right side. The frame consists of

• a pull-down menu "Action" with menu items "Convert" and "Exit",

• a textfield to enter GBP amount. Some text is in the text field to provide information to the user on what to input. The user needs to delete the text before entering the numbers.

• a label to display the amount of HKD (please use the exchange rate of 1 GBP to exchange for 9.9 HKD).

Copy the class, including import statement(s), as the answers to this part.

(b) actionPerformed() : which perform necessary actions when each menu item is selected. When "Convert" is selected, the amount of HKD is calculated and displayed on the label. When "Exit" is selected, the program terminates.

(c) main() : which creates an GBP2HKD object and sets it visible for testing.

Solutions

Expert Solution

GBP2HKD.java:

   import java.awt.FlowLayout;
   import java.awt.GridLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JMenu;
   import javax.swing.JMenuBar;
   import javax.swing.JMenuItem;
   import javax.swing.JOptionPane;
   import javax.swing.JPanel;
   import javax.swing.JTextField;

   public class GBP2HKD extends JFrame implements ActionListener {
       private static JMenuItem convert_Menu, exit_Menu;
       private static JTextField inp_Field;
       private static JLabel res_Label;
       private static final double GBP_TO_HKD = 9.9;
       public GBP2HKD()
       {
           // initialize the main frame and the main panel
           JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
          
           // initialize the menu bar, menu and menu items
           JMenuBar menu_Bar = new JMenuBar();
           JMenu menu = new JMenu("Action");
           convert_Menu = new JMenuItem("Convert");
           exit_Menu = new JMenuItem("Exit");
           menu.add(convert_Menu);
           menu.add(exit_Menu);
           menu_Bar.add(menu);
          
           // initialize the panel and the textfield and the result label to the panel
           JPanel panel_1 = new JPanel(new GridLayout(2, 0));
           inp_Field = new JTextField(20);
           inp_Field.setText("GBP Amount");
           res_Label = new JLabel("Result:");
           panel_1.add(inp_Field);
           panel_1.add(res_Label);
          
           // add the sub panel to the main panel
           panel.add(panel_1);
          
           // finally, add the main panel to the JFrame
           setJMenuBar(menu_Bar);
           add(panel);
          
           // action listener for menu items
           convert_Menu.addActionListener(this);
           exit_Menu.addActionListener(this);
       }
       @Override
       public void actionPerformed(ActionEvent ae)
       {
           //ae = action event
           if(ae.getSource() == convert_Menu)
           {
                   // convert the amount
               if(inp_Field.getText().equals(""))
               {
                   JOptionPane.showMessageDialog(null, "Please enter a GBP amount");
                   return;
               }
               double amnt = Double.parseDouble(inp_Field.getText().trim());
               double res = convertAmount(amnt);
               res_Label.setText(String.format("$%.2f", res));
           }
           else if(ae.getSource() == exit_Menu)
               System.exit(0);
       }
  
       // method to convert GBP amount to HKD amount
       private static double convertAmount(double amnt)
       {
           return(amnt * GBP_TO_HKD);
       }
   }

GBP_Test.java:

   import javax.swing.JFrame;

   public class GBP_Test {
       public static void main(String[] args) {
           GBP2HKD gpb_Object = new GBP2HKD();
           gpb_Object.setTitle("GBP To HKD");
           gpb_Object.setSize(250, 120);
           gpb_Object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           gpb_Object.setLocationRelativeTo(null);
           gpb_Object.setVisible(true);
       }
   }

Output Screenshot:


Related Solutions

- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs);...
class DoubleLinkedList { public: //Implement ALL following methods. //Constructor. DoubleLinkedList(); //Copy constructor. DoubleLinkedList(const DoubleLinkedList & rhs); //Destructor. Clear all nodes. ~DoubleLinkedList(); // Insert function. Returns true if item is inserted, // false if the item it a duplicate value bool insert(int x); // Removes the first occurrence of x from the list, // If x is not found, the list remains unchanged. void remove(int x); //Assignment operator. const DoubleLinkedList& operator=(const DoubleLinkedList & rhs); private: struct node{ int data; node* next;...
1. Write a class Rectangles which manages an array of Rectangle objects. The constructor of the...
1. Write a class Rectangles which manages an array of Rectangle objects. The constructor of the Rectangles takes an array of Rectangle objects. You can assume the array is filled Provide these methods An averageArea method that returns the average area of the Rectangle objects in the array. Only divide one time A method swapMaxAndMin which swaps the Rectangle with the largest area with the one with the smallest area in the array. Only use one loop A method toString...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for the given class. Create an instance of the class in a main method using a Practice class. Note: The structure of the class can be compiled and tested without having bodies for the methods. Just be sure to put in dummy return values for methods that have a return type other than void.      
Write anoyher overloaded constructor for this class. Thr constructor should accept an argumrny for each field....
Write anoyher overloaded constructor for this class. Thr constructor should accept an argumrny for each field. C++ I need help thanks.
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
Write a class named TestScores. The class constructor should accept an array of test scores as...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT