Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework
QUESTION 1
A long-distance call provider charges the following rate for telephone calls:
Rate Category Rate per Minute
Daytime (6 am - 5.59 pm) $0.07
Evening (6 pm - 11.59 pm) $0.12
Off-Peak (12 am - 5.59 am) $0.05
Create a Java program that allows the user to select a specific rate category (from a set of
radio buttons), and enter hours and minutes of the call. Convert the hours to minutes in
order to get the charges and then display the charges for the call as a text field in twodecimal
points. Hint: Use DecimalFormat class.
Refer Figure Q1 as the output screen. You are not required to write the HTML file.
 
Must be displayed in
two decimal points
 
-------------------------------------------------------------------
my half answer :
import java.awt.*;
import javax.swing.*;
 
public class PYQMAY2010 extends JFrame
{
	private JLabel lb1, lb2, lb3, lb4;
	private JTextField tf1, tf2, tf3;
	private JRadioButton rb1, rb2, rb3;
 
public static void main(String []arg)
{
	PYQMAY2010 l = new PYQMAY2010();
 
	l.setSize(400,300);
	l.setVisible(true);
	l.setTitle("Applet Viewer: LongDistanceCalls");
	l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
 
	public PYQMAY2010()
	{
		lb1 = new JLabel("Enter hours of the call:");
		lb2 = new JLabel("Enter minutes of the call:");
		lb3 = new JLabel("Rate category:");
		lb4 = new JLabel("Charges for the call :RM");
		tf1 = new JTextField(20);
		tf2 = new JTextField(20);
		tf3 = new JTextField(10);
		rb1 = new JRadioButton("Daytime (6 am - 5.59 am)");
		rb2 = new JRadioButton("Evening (6 pm - 11.59 pm)");
		rb3 = new JRadioButton("Off-Peck (12am - 5.59 am)");
 
		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout (2,1));
		p1.add(lb1);
		p1.add(tf1);
		p1.add(lb2);
		p1.add(tf2);
 
		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(4,0));
		p2.add(lb3);
		p2.add(rb1);
		p2.add(rb2);
		p2.add(rb3);
 
		JPanel p3 = new JPanel();
		p3.setLayout(new FlowLayout(FlowLayout.CENTER));
		p3.add(lb4);
		p3.add(tf3);
 
		setLayout(new BorderLayout());
		add(p1,BorderLayout.NORTH);
		add(p2,BorderLayout.CENTER);
		add(p3,BorderLayout.SOUTH);
	}
}
Posted 29-Aug-12 18:52pm
Edited 29-Aug-12 19:32pm
v3
Comments
Sandeep Mewara at 30-Aug-12 1:30am
   
And your issue/question where you are stuck?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

OK, that's the basic GUI to your homework.
Now you need to make some action:
 
Your Radiobuttons feel lonely. Make them a group so they can interact and have friends:
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(4,0));
ButtonGroup buttonGroup = new ButtonGroup();
group.add(rb1);
group.add(rb2);
group.add(rb3);
p2.add(group);
 
Your Radiobuttons also need to have a listener:
rb1 = new JRadioButton("Daytime (6 am - 5.59 am)");
rb2 = new JRadioButton("Evening (6 pm - 11.59 pm)");
rb3 = new JRadioButton("Off-Peck (12am - 5.59 am)");
 
rb1.addActionListener(this);
rb2.addActionListener(this);
rb3.addActionListener(this);
 
and you will be asked to add the interface ActionListener to your Class:
public class PYQMAY2010 extends JFrame implements ActionListener
 
which leads to implement the function actionPerformed from that Listener:
@Override
public void actionPerformed(ActionEvent e) {
 // Party on
}
 
now you've probably noticed that the same Actionlistener is used for all the Radiobuttons - so how should you know which one is in Action?
Each Swing Component has an ActionCommand. That is a defined String, that can be set not regarding the Label (Labels can change, the ActionCommand is fixed!):
// class members
private final String // fixed def of the Strings that can be referred to later
  AC_RB1 = "rb1 - day",
  AC_RB1 = "rb2 - evening",
  AC_RB1 = "rb3 - off peck";
 
// while defining rbs in PYQMAY2010
rb1.setActionCommand(AC_RB1);
rb2.setActionCommand(AC_RB2);
rb3.setActionCommand(AC_RB3);
That Command is now defined (private final) and we can rely on it to be steady.
So we can refer to it:
@Override
public void actionPerformed(ActionEvent e) {
 JRadioButton oButton = (JRadioButton)e.getSource();
 if(oButton.getActionCommand.equals(AC_RB1){
  // daytime action
 }
 if(oButton.getActionCommand.equals(AC_RB2){
  // night time action
 }
 if(oButton.getActionCommand.equals(AC_RB3){
  // off peck action
 }
}
 
Have fun.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Nirav Prabtani 268
1 OriginalGriff 217
2 Mika Wendelius 185
3 Sergey Alexandrovich Kryukov 176
4 CPallini 170
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Mika Wendelius 185
3 _Amy 170
4 CPallini 170


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 30 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100