1. Download our Official Android App: Forums for Android!

Apps How to calculate? Please kindly help me

Discussion in 'Android Development' started by Leye, Jul 27, 2016.

  1. Leye

    Leye Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Jul 22, 2016

    Jul 22, 2016
    4
    0
    5
    Male
    I want to create app. I have attached the layout.

    HTML:
    1. <p><b>My codes are:</b></p>
    Code (Java):
    1. public class MainActivity extends Activity {
    2.  
    3.     @Override
    4.     protected void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    6.         setContentView(R.layout.activity_main);
    7.        
    8.         //This is where my codes will go
    9.        
    10.         //linking EditText widgets
    11.        
    12.         final EditText eAss1 = (EditText)findViewById(R.id.ass1);
    13.         final EditText eAss2 = (EditText)findViewById(R.id.ass2);
    14.         final EditText eAss3 = (EditText)findViewById(R.id.Ass3);
    15.         final EditText eExamMark = (EditText)findViewById(R.id.ExamMark);
    16.        
    17.         //linking TextView widgets
    18.        
    19.         final TextView errorMessage = (TextView)findViewById(R.id.errorView);
    20.         final TextView efinalPerce = (TextView)findViewById(R.id.finalPerc);
    21.         final TextView efailOrPass = (TextView)findViewById(R.id.failOrPass);
    22.        
    23.         //linking the button widget
    24.        
    25.         Button but1 = (Button)findViewById(R.id.btnCalc);
    26.         but1.setOnClickListener(new OnClickListener(){
    27.             public void onClick(View v){
    28.                
    29.                 //this is where my codes for calculations will go
    30.                 double eAss1Val = Double.valueOf(eAss1.getText());
    31.                 double eAss2Val = Double.valueOf(eAss2.getText());
    32.                 double eAss3Val = Double.valueOf(eAss3.getText());
    33.                 double eExamMark = Double.valueOf(eExamMark);
    34.            
    35.                 double yearMark = eAss1Val * 0.15 + eAss2Val * 0.35 + eAss3Val * 0.5;
    36.                 double finalMark = yearMark * .49 + eExamMark * .51;
    37.                
    38.                
    39.                }//end of onClick
    40.         });
    41.  
    HTML:
    1. <p>But am getting error saying</p>
    2.  
    3. <p>The method valueOf(string) in the type double is not applicable for the arguments(Editable)
    4.  
    5. <p>examMark.getText() cannot invoke getText() on the primitive type double</p>
    6.  
    7. <p>Please, someone kindly assist me on how to do the calculation of the layout I attached. </p?
    8.  
    9. <p>Thanks</p>
     

    Attached Files:


    Advertisement

  2. Unforgiven

    Unforgiven OK Google
    Moderator
    Rank:
     #1
    Points:
    4,238
    Posts:
    35,920
    Joined:
    Jun 23, 2010

    Jun 23, 2010
    35,920
    41,292
    4,238
    Male
    Douglas, MA
    Application Development thread moved to the Development forum for better exposure.:)
     
    mikedt and Mikestony like this.
  3. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,282
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,282
    10,384
    1,988
    Male
    Software developer
    South West of England
    Wow, you are *really* going to struggle if you don't understand what's going wrong here.
    My advice is to get a good book on Java. Read it and learn the basics of the language. Or follow an online tutorial, which explains about basic types and their equivalent object counterparts.

    If you had any understanding of the Double class, you would know that there are methods on this class which are used to create instances of Double, and convert to its primitive type equivalent - double.

    The valueOf() method takes a String. Rather than just giving up and asking basic questions, try to work out why this is causing a problem. Hint: what type is 'eExamMark'?

    And the lines of code preceding it (which I gave you) should give you a big hint.

    Code (Text):
    1.  
    2. double eExamMark = Double.valueOf(eExamMark);
    3.  
    As this is clearly a homework assignment, you should try to learn, rather than just ask for spoon-fed answers on a forum.
     
    #3 LV426, Jul 27, 2016
    Last edited: Jul 27, 2016
    Bg260 and mikedt like this.
  4. ac4android

    ac4android Well-Known Member
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    At the beginning of 2016, I bought a book on Java, it was 2nd hand, cheap and nasty and full of mistakes and deprecated classes and methods. But I figured it out eventually. Hope this helps:

    package java101;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class WorkWithFloating {

    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    InputStreamReader reader = new InputStreamReader(System.in);
    BufferedReader input = new BufferedReader(reader);
    System.out.print("Enter radius: ");

    String text = input.readLine();
    Double x = new Double(text);
    double r = x.doubleValue();
    // double r = new Double(text).doubleValue(); // another way of coding it.
    /*
    * Difference between variables e.g. double and objects e.g. Double
    * Object-oriented programming : the choice of objects used in a program is based on the operations that have to be performed
    * 1. Input a floating point number;
    * 2. apply the formula A = PI*r*r to it;
    * 3. output the result
    * =================================================================
    * x is a variable of the Double CLASS
    * r is a variable of type double
    * They both represent the real number 100.0, the reason that we have these two separate objects
    * representing the same thing is because of the different operations we have to perform on the value!
    * As an instance of the Double CLASS, the object x can obtain its value from the text object which holds the value
    * ... 100 in four characters '1', '0', '0' and '\n'.
    * These flowed in from the reader object, which held the value 100 in the four bytes 49, 48,48 and 13.
    * So the x object has the ability to convert a numeric value from its character representations into a numeric representation.
    * But implementing the formula A=PI*r*r requires the use of the multiplication operator *, which only works on numeric
    * variables, not objects, so the object x has to be converted to the variable r using the doubleValue() method on x.
    */
    System.out.println("Area of the circle with radius " + r);
    double area = Math.PI*r*r;
    System.out.println(" is " + area);


    }

    }
     
  5. mikedt

    mikedt 你好
    Rank:
     #6
    Points:
    2,238
    Posts:
    24,631
    Joined:
    Sep 22, 2010

    Sep 22, 2010
    24,631
    13,632
    2,238
    Teachaaa
    Jinan, China
  6. ac4android

    ac4android Well-Known Member
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    Yeah, internet tutes are more up-to-date!

    The best book on Android by far is "Head First Android Development " by the Griffiths, from O'Reilly.

    Nothing beats doing it yourself. Download an IDE (Eclipse is ideal) and dive in deep end, head first.
     

Share This Page

Loading...