Question

(b) Write a class Conversion containing the following methods: (i) constructor: which builds the frame shown on the right side. The frame consists of a text field for inputting a NZD amount, a label with 10 spaces for an equivalent HKD amount, and a button to start the calculation. Declare any necessary attributes in the class and add appropriate action listeners for future use. Copy the class, including import statement(s), as the answers to this part Calculate (ii) actionPerformed : which performs the calculation and puts the result on the label when the button is pressed. You can assume one NZD is equivalent to 5.5 HKD. You can assume a valid realnumber1234 is entered in the textfield. Copy the method as the answers to this part 67.87 Calculate (iii) mainO which creates a Conversion object and sets it visible for testing. Copy the method as 120 the answers to this part
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Conversion.java

// import all required classes and interfaces

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

// create Conversion class which extends JFrame class

// and implements ActionListener interface

public class Conversion extends JFrame implements ActionListener {

// declare all required components

JLabel l;

JTextField t;

JButton b;

// constructor

public Conversion() {

super(); // calls super class constructor

// intantiate all declated components

l=new JLabel();

t=new JTextField(25);

b=new JButton("Calculate");

// setting bounds to all components

t.setBounds(10,15,100,25);

l.setBounds(110,15,100,25);

b.setBounds(10,40,200,28);

// add components into frame

add(t);

add(l);

add(b);

// setting action listener on button

b.addActionListener(this);

setLayout(null); // set layout as null

setSize(240,120); // set size of the frame

setVisible(true); // make frame visible

}

// override actionPerformed() method

public void actionPerformed(ActionEvent ae)

{

double nzd=Double.parseDouble(t.getText());

double hkd=nzd*5.5;

l.setText(" "+hkd);

}

public static void main(String[] args) {

new Conversion(); // anonymous object

}

}

Output
Calculate


Add a comment
Know the answer?
Add Answer to:
(b) Write a class Conversion containing the following methods: (i) constructor: which builds the frame shown...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java language (a) Create a class HexEditor with a constructor which creates a 5x10 text area...

    Java language (a) Create a class HexEditor with a constructor which creates a 5x10 text area in a Frame. Also add a pull-down menu with a menu item "Load". Copy the class as the answer to this part. (b) Create another class TestHexEditor with a main() method which creates an object anEditor of the class HexEditor and displays the frame in part (a) using setVisible(true). Copy the class as the answer to this part. (c) Make changes to the class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT