Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code compiles and runs OK, but when I click on the buttons (for 10%, 15%,an

ID: 3565116 • Letter: T

Question

This code compiles and runs OK, but when I click on the buttons (for 10%, 15%,and 20%) it doesn't seem to run the calculations -- the tip and total amounts show up as 0.00

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;

public class MyActivity extends Activity {

private static final NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance();
private static final NumberFormat percentFormat =
NumberFormat.getPercentInstance();
private double FDAmount;
private double tipPercent;
private double tipAmount;
private double totalAmount;
private TextView FDTV;
private TextView taxTV;
private TextView tipTV;
private TextView totalTV;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b10= (Button) findViewById(R.id.radioButton);
Button b15= (Button) findViewById(R.id.radioButton2);
Button b20= (Button) findViewById(R.id.radioButton3);
b10.setOnClickListener(buttonListener);
b15.setOnClickListener(buttonListener);
b20.setOnClickListener(buttonListener);

FDTV = (TextView) findViewById(R.id.textView_FDA);
taxTV = (TextView) findViewById(R.id.textView2_TaxAmount);
tipTV = (TextView) findViewById(R.id.textView3_TipAmount);
totalTV = (TextView) findViewById(R.id.textView4_TotalAmount);
}


public OnClickListener buttonListener = new OnClickListener()
{

public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.radioButton :
tipPercent = .10;

break;
case R.id.radioButton2 :
tipPercent = .15;

break;
case R.id.radioButton3 :
tipPercent = .20;

break;

}
tipAmount = (FDAmount * tipPercent );
totalAmount = FDAmount + tipAmount;

tipTV.setText(currencyFormat.format(tipAmount));
totalTV.setText(currencyFormat.format(totalAmount));
}
};
}

this is main

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Food_Drink_Amount"
android:id="@+id/textView_FDA" android:phoneNumber="true" android:editable="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText" android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="39dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Tax_Amount"
android:id="@+id/textView2_TaxAmount"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2" android:layout_weight="0.10"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tenPercent"
android:id="@+id/radioButton"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fifteenPercent"
android:id="@+id/radioButton2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/twentyPercent"
android:id="@+id/radioButton3"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Calculate"
android:id="@+id/button"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/TipAmount"
android:id="@+id/textView3_TipAmount"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3" android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/TotalAmount"
android:id="@+id/textView4_TotalAmount"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText4" android:layout_weight="1"/>
</LinearLayout>

</LinearLayout>

Explanation / Answer

// TipCalculator.java // Calculates bills using 5, 10, 15 and custom percentage tips. package com.deitel.tipcalculator; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; // main Activity class for the TipCalculator public class TipCalculator extends Activity { // constants used when saving/restoring state private static final String BILL_TOTAL = "BILL_TOTAL"; private static final String CUSTOM_PERCENT = "CUSTOM_PERCENT"; private double currentBillTotal; // bill amount entered by the user private int currentCustomPercent; // tip % set with the SeekBar private EditText tip10EditText; // displays 10% tip private EditText total10EditText; // displays total with 10% tip private EditText tip15EditText; // displays 15% tip private EditText total15EditText; // displays total with 15% tip private EditText billEditText; // accepts user input for bill total private EditText tip20EditText; // displays 20% tip private EditText total20EditText; // displays total with 20% tip private TextView customTipTextView; // displays custom tip percentage private EditText tipCustomEditText; // displays custom tip amount private EditText totalCustomEditText; // displays total with custom tip // Called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // call superclass's version setContentView(R.layout.main); // inflate the GUI // check if app just started or is being restored from memory if ( savedInstanceState == null ) // the app just started running { currentBillTotal = 0.0; // initialize the bill amount to zero currentCustomPercent = 18; // initialize the custom tip to 18% } // end if else // app is being restored from memory, not executed from scratch { // initialize the bill amount to saved amount currentBillTotal = savedInstanceState.getDouble(BILL_TOTAL); // initialize the custom tip to saved tip percent currentCustomPercent = savedInstanceState.getInt(CUSTOM_PERCENT); } // end else // get references to the 10%, 15% and 20% tip and total EditTexts tip10EditText = (EditText) findViewById(R.id.tip10EditText); total10EditText = (EditText) findViewById(R.id.total10EditText); tip15EditText = (EditText) findViewById(R.id.tip15EditText); total15EditText = (EditText) findViewById(R.id.total15EditText); tip20EditText = (EditText) findViewById(R.id.tip20EditText); total20EditText = (EditText) findViewById(R.id.total20EditText); // get the TextView displaying the custom tip percentage customTipTextView = (TextView) findViewById(R.id.customTipTextView); // get the custom tip and total EditTexts tipCustomEditText = (EditText) findViewById(R.id.tipCustomEditText); totalCustomEditText = (EditText) findViewById(R.id.totalCustomEditText); // get the billEditText billEditText = (EditText) findViewById(R.id.billEditText); // billEditTextWatcher handles billEditText's onTextChanged event billEditText.addTextChangedListener(billEditTextWatcher); // get the SeekBar used to set the custom tip amount SeekBar customSeekBar = (SeekBar) findViewById(R.id.customSeekBar); customSeekBar.setOnSeekBarChangeListener(customSeekBarListener); } // end method onCreate // updates 10, 15 and 20 percent tip EditTexts private void updateStandard() { // calculate bill total with a ten percent tip double tenPercentTip = currentBillTotal * .1; double tenPercentTotal = currentBillTotal + tenPercentTip; // set tipTenEditText's text to tenPercentTip tip10EditText.setText(String.format("%.02f", tenPercentTip)); // set totalTenEditText's text to tenPercentTotal total10EditText.setText(String.format("%.02f", tenPercentTotal)); // calculate bill total with a fifteen percent tip double fifteenPercentTip = currentBillTotal * .15; double fifteenPercentTotal = currentBillTotal + fifteenPercentTip; // set tipFifteenEditText's text to fifteenPercentTip tip15EditText.setText(String.format("%.02f", fifteenPercentTip)); // set totalFifteenEditText's text to fifteenPercentTotal total15EditText.setText( String.format("%.02f", fifteenPercentTotal)); // calculate bill total with a twenty percent tip double twentyPercentTip = currentBillTotal * .20; double twentyPercentTotal = currentBillTotal + twentyPercentTip; // set tipTwentyEditText's text to twentyPercentTip tip20EditText.setText(String.format("%.02f", twentyPercentTip)); // set totalTwentyEditText's text to twentyPercentTotal total20EditText.setText(String.format("%.02f", twentyPercentTotal)); } // end method updateStandard // updates the custom tip and total EditTexts private void updateCustom() { // set customTipTextView's text to match the position of the SeekBar customTipTextView.setText(currentCustomPercent + "%"); // calculate the custom tip amount double customTipAmount = currentBillTotal * currentCustomPercent * .01; // calculate the total bill, including the custom tip double customTotalAmount = currentBillTotal + customTipAmount; // display the tip and total bill amounts tipCustomEditText.setText(String.format("%.02f", customTipAmount)); totalCustomEditText.setText( String.format("%.02f", customTotalAmount)); } // end method updateCustom // save values of billEditText and customSeekBar @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putDouble(BILL_TOTAL, currentBillTotal); outState.putInt(CUSTOM_PERCENT, currentCustomPercent); } // end method onSaveInstanceState // called when the user changes the position of SeekBar private OnSeekBarChangeListener customSeekBarListener = new OnSeekBarChangeListener() { // update currentCustomPercent, then call updateCustom @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // sets currentCustomPercent to position of the SeekBar's thumb currentCustomPercent = seekBar.getProgress(); updateCustom(); // update EditTexts for custom tip and total } // end method onProgressChanged @Override public void onStartTrackingTouch(SeekBar seekBar) { } // end method onStartTrackingTouch @Override public void onStopTrackingTouch(SeekBar seekBar) { } // end method onStopTrackingTouch }; // end OnSeekBarChangeListener // event-handling object that responds to billEditText's events private TextWatcher billEditTextWatcher = new TextWatcher() { // called when the user enters a number @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // convert billEditText's text to a double try { currentBillTotal = Double.parseDouble(s.toString()); } // end try catch (NumberFormatException e) { currentBillTotal = 0.0; // default if an exception occurs } // end catch // update the standard and custom tip EditTexts updateStandard(); // update the 10, 15 and 20% EditTexts updateCustom(); // update the custom tip EditTexts } // end method onTextChanged @Override public void afterTextChanged(Editable s) { } // end method afterTextChanged @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } // end method beforeTextChanged }; // end billEditTextWatcher } // end class TipCalculator