Click here to Skip to main content
14,979,217 members
Home / Discussions / Java
   

Java

 
AnswerRe: Android Studio submission Pin
Gammill18-Apr-20 13:52
MemberGammill18-Apr-20 13:52 
QuestionIntroduction to object oriented programming Pin
Member 1479832810-Apr-20 6:59
MemberMember 1479832810-Apr-20 6:59 
AnswerRe: Introduction to object oriented programming Pin
Richard MacCutchan10-Apr-20 21:17
mveRichard MacCutchan10-Apr-20 21:17 
GeneralMessage Closed Pin
16-Apr-20 8:47
MemberMember 1217774316-Apr-20 8:47 
Questioncannot resolve Pin
MallardsReach30-Mar-20 3:32
MemberMallardsReach30-Mar-20 3:32 
AnswerRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 3:50
mveRichard MacCutchan30-Mar-20 3:50 
GeneralRe: cannot resolve Pin
MallardsReach30-Mar-20 6:29
MemberMallardsReach30-Mar-20 6:29 
GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 6:45
mveRichard MacCutchan30-Mar-20 6:45 
Rather than make these variables at class level, make them parameters to showAnswer (actually calcAnswer would be a better name) method. And the final result shoud be the return value. Something like:
String showAnswer(double decNum1, double decNum2, int dPlaces){
     Double numAnswer = decNum1 * decNum2;
     String strAnswer = String.format("%,." + dPlaces + "f", numAnswer);

    return strAnswer;
}

That keeps everything neatly within the method.

You can reduce all the duplication by using the same handler for all the buttons.
public void actionPerformed(ActionEvent e) {
    int numDecimals = // get the number from the button text

    double decNum1 = Double.parseDouble(txtNumOne.getText());
    double decNum2 = Double.parseDouble(txtNumTwo.getText());
    String strResult = showAnswer(decNum1, decNum2, numDecimals); //call method
    lblDPAnswer.setText(strResult);
}

Then add that to each button:
rbOneDecimal.addActionListener(actionPerformed);
rbTwoDecimal.addActionListener(actionPerformed);

You may need to check the syntax requirements for these last two/three items
GeneralRe: cannot resolve Pin
MallardsReach30-Mar-20 10:32
MemberMallardsReach30-Mar-20 10:32 
GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 21:34
mveRichard MacCutchan30-Mar-20 21:34 
GeneralRe: cannot resolve Pin
Richard MacCutchan30-Mar-20 22:52
mveRichard MacCutchan30-Mar-20 22:52 
GeneralRe: cannot resolve Pin
MallardsReach30-Mar-20 23:22
MemberMallardsReach30-Mar-20 23:22 
QuestionRadio Button Selected Pin
MallardsReach25-Mar-20 9:50
MemberMallardsReach25-Mar-20 9:50 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 10:10
professionalZurdoDev25-Mar-20 10:10 
AnswerRe: Radio Button Selected Pin
MallardsReach25-Mar-20 10:33
MemberMallardsReach25-Mar-20 10:33 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 10:39
professionalZurdoDev25-Mar-20 10:39 
AnswerRe: Radio Button Selected Pin
MallardsReach25-Mar-20 11:11
MemberMallardsReach25-Mar-20 11:11 
QuestionRe: Radio Button Selected Pin
ZurdoDev25-Mar-20 12:19
professionalZurdoDev25-Mar-20 12:19 
AnswerRe: Radio Button Selected Pin
Richard MacCutchan26-Mar-20 0:16
mveRichard MacCutchan26-Mar-20 0:16 
GeneralRe: Radio Button Selected Pin
MallardsReach26-Mar-20 0:28
MemberMallardsReach26-Mar-20 0:28 
GeneralRe: Radio Button Selected Pin
Richard MacCutchan26-Mar-20 0:36
mveRichard MacCutchan26-Mar-20 0:36 
GeneralRe: Radio Button Selected Pin
MallardsReach26-Mar-20 2:55
MemberMallardsReach26-Mar-20 2:55 
GeneralRe: Radio Button Selected Pin
MallardsReach26-Mar-20 4:15
MemberMallardsReach26-Mar-20 4:15 
GeneralMessage Closed Pin
12-Apr-20 18:39
MemberBrenda Jasprizza12-Apr-20 18:39 
QuestionUser defined Method Pin
MallardsReach24-Mar-20 2:56
MemberMallardsReach24-Mar-20 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.