Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Other Programming (https://www.askmehelpdesk.com/forumdisplay.php?f=437)
-   -   Java (https://www.askmehelpdesk.com/showthread.php?t=812465)

  • Jun 2, 2015, 10:15 AM
    arko93
    Java
    class Cons
    {
    int a,c,k=0;
    int number()
    {
    for(a=1;a<=10;a++)
    {


    for(c=1;c<=a;c++)
    {

    if(a%c==0)
    k++;
    }
    }

    if(k==2)
    {
    System.out.println("prime numbers are"+a);
    }

    }
    int number(int x,int y)
    {
    a=x;
    c=y;
    for(a=1;a<=10;a++)
    {


    for(c=1;c<=a;c++)
    {

    if(a%c==0)
    k++;
    } } }



    }
    public class Prime
    {
    public static void main(String[]args)
    {
    Cons t=new Cons();
    System.out.println("result = "+t.number());
    Cons t1=new Cons(20,30);
    System.out.println("result = "+t1.number());

    }
    }

    Why I am getting an error in this program
    error- Prime.java:22: error: missing return statement
    }
    ^
    Prime.java:35: error: missing return statement
    } } }
    ^
    Prime.java:46: error: constructor Cons in class Cons cannot be applied to given types;
    Cons t1=new Cons(20,30);
    ^
    required: no arguments
    found: int,int
    reason: actual and formal argument lists differ in length
  • Jun 2, 2015, 01:26 PM
    CravenMorhead
    The general form of a function declaration is:;
    |---------- Return value type.
    \/
    int funcationName(int Parameters)

    A simple function might be:

    int add2num(int num1, int num2)
    {
    int sum = num1 + num2;
    return sum;
    }

    Quote:

    Originally Posted by arko93 View Post
    class Cons
    {
    int a,c,k=0; // CMH: You Can't initialize members in a class declaration,
    // Do in the constructor. Maybe these should be local to your methods and not class
    // members
    int number()
    { //fun
    for(a=1;a<=10;a++)
    {//for a


    for(c=1;c<=a;c++)
    {//for c

    if(a%c==0)
    k++;
    } //for c
    }//for a

    if(k==2)
    {
    System.out.println("prime numbers are"+a);
    }

    } // fun

    // You just ended the declaration of the above member. You have the value K, what are you doing with it here? Should you return it?

    int number(int x,int y)
    {
    a=x;
    c=y;
    for(a=1;a<=10;a++)
    {


    for(c=1;c<=a;c++)
    {

    if(a%c==0)
    k++;
    } } }

    // You just ended the declaration of the above member. You have the value K, what are you doing with it here? Should you return it?

    }
    public class Prime
    {
    public static void main(String[]args)
    {
    Cons t=new Cons(); // You only need to allocate this one.
    System.out.println("result = "+t.number());
    Cons t1=new Cons(20,30); // The numbers here
    System.out.println("result = "+t1.number());// should be the parameter list for this.

    }
    }

    Why I am getting an error in this program
    error- Prime.java:22: error: missing return statement
    }
    ^
    Prime.java:35: error: missing return statement
    } } }
    ^
    Prime.java:46: error: constructor Cons in class Cons cannot be applied to given types;
    Cons t1=new Cons(20,30);
    ^
    required: no arguments
    found: int,int
    reason: actual and formal argument lists differ in length

    I added comments to your code. I am not going to rewrite it for you, goodness knows it's tempting, but it is easy enough to fix.

  • All times are GMT -7. The time now is 06:00 PM.