Click to See Complete Forum and Search --> : No error messages but doesn't give the required output..


kanakatam
04-07-2005, 01:44 PM
Hello folks,
I wrote the program below to calculate cost of phone call when the user enters values for hours, minutes and seconds. I have to convert hours and seconds to minutes so that i can calculate cost of phone call which is 10 cents per min. Code compiles without any errors but does not display the output. I would greatly appreciate any help.
Thanx,
kanaka


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class PhoneCall extends Applet
implements ActionListener
{
private TextField hourTF, minTF, secTF, outputTF;
private Button calculateButton, clearButton;
private int hours, mins, secs, totalMin, totalCost;

public void init()
{
Label inputLbl, hrLbl, minLbl, secLbl, outputLbl;
inputLbl = new Label("Please enter an integer value only");
outputLbl = new Label("The cost of phone call in cents:");

hrLbl = new Label("Hours:");
minLbl = new Label("Minutes:");
secLbl = new Label("Seconds:");

hourTF = new TextField(7);
minTF = new TextField(7);
secTF = new TextField(7);
outputTF = new TextField(7);

calculateButton = new Button("CALCULATE");
clearButton = new Button("CLEAR");

add(inputLbl);
add(hrLbl);
add(hourTF);
add(minLbl);
add(minTF);
add(secLbl);
add(secTF);
add(calculateButton);
add(clearButton);
add(outputLbl);
add(outputTF);

hourTF.addActionListener(this);
minTF.addActionListener(this);
secTF.addActionListener(this);
outputTF.addActionListener(this);
clearButton.addActionListener(this);
calculateButton.addActionListener(this);

}

public void actionPerformed (ActionEvent event)
{
outputTF.setEditable(false);

if(event.getSource() == calculateButton)
{
calculateTotal();
outputTF.setText(Integer.toString(totalCost));
}

if(event.getSource() == clearButton)
outputTF.setText("");


}

public int calculateTotal()
{
totalMin = hours * 60 + mins + secs / 60;
totalCost = totalMin * 10;
return totalCost;
}


}

Khalid Ali
04-07-2005, 02:06 PM
here is the problem code

if(event.getSource() == calculateButton)
{
calculateTotal();
outputTF.setText(Integer.toString(totalCost));
}

you are calling calculate() method but what you are not doing is getting the hours, minutes and secs values from text fields