Question

VISUAL BASIC FOR APPLICATIONS PLEASE!!!! A long distance provider charges the following rates for telephone calls:...

VISUAL BASIC FOR APPLICATIONS PLEASE!!!!

A long distance provider charges the following rates for telephone calls:

Rate Category Rate Per minute

Daytime (6:00A.M through 5:59P.M) $0.07

Evening (6:00P.M through 11:59P.M) $0.12

Off Peak (12:00A.M through 5:59A.M) $0.05

Create an application that allows the user to select a rate category and enter the number of minutes of the call, then display the charges.

Use the following test data to determine if the application is calculating properly:

Rate Category and Minutes Charge

Daytime, 20 minutes $1.40

Evening, 20 minutes $2.40

Offpeak, 20 minutes $1.00

Homework Answers

Answer #1

//RatePanel.java

import java.text.DecimalFormat;

//computes the rate for the minutes

public class RatePanel

{

     //default constructor

     RatePanel()

     {       

     }

     public double computeRate(String time, double value)

     {

          DecimalFormat df=new DecimalFormat("0.##");

          double rate=0.00;

          if(time.equals("DayTime"))

          {

              rate= value * 0.07;

          }

          if(time.equals("EveningTime"))

          {

              rate=value*0.12;

          }

          if(time.equals("PeakOffTime"))

          {

              rate= value*0.05;

          }

          return Double.parseDouble(df.format(rate));

     }

}

-------------------------------------------------------------------------------------------------

//MinutesPanel.java

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

//this class will develop the panel

// and add the listeners to the events

class MinutesPanel extends JFrame

{

     RatePanel rp=new RatePanel();

     private JPanel panel;         // A holding panel

     private JLabel messageLabel; // A message to the user

     private JTextField minutesTextField;

     // To hold user input

     private JRadioButton DayButton;      

     private JRadioButton EveningButton;  

     private JRadioButton Off_peakButton;

     // To group radio buttons

     private ButtonGroup radioButtonGroup;

     /* Constructor */

     public MinutesPanel()

     {       

          // Build the panel and add it to the frame.

          buildPanel();

          // Add the panel to the frame's content pane.

          add(panel);      

          // Specify an action for the close button.

          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     }

     /**

     The buildPanel method adds a label, text field, and

     and three buttons to a panel.

     */

     private void buildPanel()

     {

          JPanel panel1=new JPanel(new GridLayout(1,2));

          JPanel panel2=new JPanel(new GridLayout(1,3));

          // Create the label, text field

          messageLabel = new

          JLabel(" Enter number of minutes ");

          minutesTextField = new JTextField(10);

          //add the components to panel1

          panel1.add(messageLabel);

          panel1.add(minutesTextField);

        

          //Create radio buttons

          DayButton = new JRadioButton("Day Time");

          EveningButton = new JRadioButton(" Evening Time");

          Off_peakButton = new JRadioButton("Off-peak");

          //add the components to panel2

          panel2.add(DayButton);

          panel2.add(EveningButton);

          panel2.add(Off_peakButton);

          // Group the radio buttons.

          radioButtonGroup = new ButtonGroup();

          radioButtonGroup.add(DayButton);

          radioButtonGroup.add(EveningButton);

          radioButtonGroup.add(Off_peakButton);

          // Add action listeners to the radio buttons.

          DayButton.addActionListener(new RadioButtonListener());

          EveningButton.addActionListener(new RadioButtonListener());

          Off_peakButton.addActionListener (new RadioButtonListener());

          // Create a panel and add the components to it.

          panel = new JPanel(new FlowLayout());

          panel.add(panel1);

          panel.add(panel2);    

     }

     /**

     Private inner class that handles the event when

     the user clicks one of the radio buttons.

     */

     private class RadioButtonListener   implements ActionListener

     {

          public void actionPerformed(ActionEvent e)

          {

              String input;   // To hold the user's input

              double result = 0.0;   // To hold the conversion

              input = minutesTextField.getText();

              // Determine which radio button was clicked.

              if (e.getSource() == DayButton)

              {

                   result = rp.computeRate("DayTime", Double.parseDouble(input));

              }

              else if (e.getSource() == EveningButton)

              {

                   result = rp.computeRate("EveningTime", Double.parseDouble(input));

              }

              else if (e.getSource() == Off_peakButton)

              {

                   result = rp.computeRate("PeakOffTime", Double.parseDouble(input));

              }

              // Display the conversion.

              JOptionPane.showMessageDialog (null," Call charges is: $ " + result );

          }

     }

}

----------------------------------------------------

//Main.java

import javax.swing.JFrame;

public class Main

{

     private static final int WINDOW_WIDTH = 350; // Window width

     private static final int WINDOW_HEIGHT = 150; // Window height

     public static void main(String args[])

     {

          //create object to the MinutesPanel class

          MinutesPanel mp=new MinutesPanel();      

          // Set the size of the window.

          mp.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

          // Display the window.

          mp.setVisible(true);

          // Set the title.

          mp.setTitle("Long Distance calls");

     }

}

Know the answer?
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
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT