Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to create a parametrized constructor ,with parameters accepting from a accept() function?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    My Mood
    Hungover
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to create a parametrized constructor ,with parameters accepting from a accept() function?

    For example,

    class cons
    {
    int d;
    cons(int a)
    {
    d=a;
    }
    void accept(String args[])throws IOException
    {


    What should I do next?
    I want the accept() to accept value from the user, the then to initialize the instance variable with the help of the constructor.So I need to pass the accepted value to the constructor.But how? And I also want to create a main function to create a object and call the function accept().How to do so.I want to use only 1 constructor and not more.


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    My Mood
    Nerdy
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?

    You can have as many constructor you want in your class. There is no restriction on it.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    My Mood
    Hungover
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?

    Yes I know.but I don't want to create.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    My Mood
    Nerdy
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?

    Make accept() static and you can call it with class name directly.

    Static functions doesn't require object to call them they can be called using the class name only.
    static functions are called like this: className.functionName(functin parameters);

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    My Mood
    Hungover
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?

    See here is the question.I have to create the object and call the methods.It is required to call the object by creating object only.

    Define a class student described as below:-
    Data Members-name,age,m1,m2,m3(marks in 3 sub),maximum,average
    Member Methods-
    1)A parametrized constructor to initialize the data members
    2)To accept the details of a student
    3)To compute average and maximum out of 3 marks
    4)To display name,age,marks in 3sub,max,avg
    Write a main method create an object and call the above method.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?

    Do you have any code you are having problems with? Please post the code and your questions about the problems you are having.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    17
    My Mood
    Hungover
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?

    import java.io.*;
    class student
    {
    String name;
    int age,m1,m2,m3,maxi;
    float avg;
    student(String n,int a,int m4,int m5,int m6)
    {
    age=a;
    m1=m4;
    m2=m5;
    m3=m6;
    name=n;
    maxi=0;
    avg=0;
    }
    void accept(String args[])throws IOException
    {
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter name");
    String a=in.readLine();
    System.out.println("Enter age");
    int ae=Integer.parseInt(in.readLine());
    System.out.println("Enter marks1");
    int m7=Integer.parseInt(in.readLine());
    System.out.println("Enter marks 2");
    int m8=Integer.parseInt(in.readLine());
    System.out.println("Enter marhks 3");
    int m9=Integer.parseInt(in.readLine());
    student obj=new student(a,ae,m7,m8,m9);
    }
    void compute()
    {
    int t=m1+m2+m3;
    avg=(float)(t/3f);
    if(m1>m2&&m1>m3)
    maxi=m1;
    else if(m2>m1&&m2>m3)
    maxi=m2;
    else
    maxi=m3;
    }
    void display()
    {
    System.out.println("Name="+name);
    System.out.println("marks 1 ="+m1);
    System.out.println("marks 2 ="+m2);
    System.out.println("marks 3="+m3);
    System.out.println("avg= "+avg);
    System.out.println("max="+maxi);
    }
    public static void main(String args[])throws IOException
    {
    student obj1=new student();
    obj1.accept();
    obj1.compute();
    obj1.display();
    }
    }

    The above returns a syntax error:cannot find symbol-constructor student
    How to correct this error?

    --- Update ---

    import java.io.*;
    class student
    {
    String name;
    int age,m1,m2,m3,maxi;
    float avg;
    student(String n,int a,int m4,int m5,int m6)
    {
    age=a;
    m1=m4;
    m2=m5;
    m3=m6;
    name=n;
    maxi=0;
    avg=0;
    }
    void accept(String args[])throws IOException
    {
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter name");
    String a=in.readLine();
    System.out.println("Enter age");
    int ae=Integer.parseInt(in.readLine());
    System.out.println("Enter marks1");
    int m7=Integer.parseInt(in.readLine());
    System.out.println("Enter marks 2");
    int m8=Integer.parseInt(in.readLine());
    System.out.println("Enter marhks 3");
    int m9=Integer.parseInt(in.readLine());
    student obj=new student(a,ae,m7,m8,m9);
    }
    void compute()
    {
    int t=m1+m2+m3;
    avg=(float)(t/3f);
    if(m1>m2&&m1>m3)
    maxi=m1;
    else if(m2>m1&&m2>m3)
    maxi=m2;
    else
    maxi=m3;
    }
    void display()
    {
    System.out.println("Name="+name);
    System.out.println("marks 1 ="+m1);
    System.out.println("marks 2 ="+m2);
    System.out.println("marks 3="+m3);
    System.out.println("avg= "+avg);
    System.out.println("max="+maxi);
    }
    public static void main(String args[])throws IOException
    {
    student obj1=new student();
    obj1.accept();
    obj1.compute();
    obj1.display();
    }
    }

    The above returns a syntax error:cannot find symbol-constructor student
    How to correct this error?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?

    a syntax error:cannot find symbol-constructor student
    Please copy the full text of the error message and post it here. The error message should show what line the error was on and more details about the problem.

    Also please edit the code and properly format it with indentations for nested statements. All statements should not start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?

    look at the constructor you created and then go to the main method and look at the object you instantiated.... does the new object you created match the definition you made for a Student object???

Similar Threads

  1. setCursor() Not Accepting java.awt.Cursor
    By Destro in forum AWT / Java Swing
    Replies: 2
    Last Post: July 14th, 2012, 08:54 PM
  2. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 05:42 PM
  3. How to not accept empty jfield?
    By myexternall11 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 20th, 2012, 04:38 AM
  4. [SOLVED] problem with accepting int!
    By arvindbis in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 9th, 2011, 03:10 AM
  5. How do I create an object(function)?
    By amarettoCoffee in forum Object Oriented Programming
    Replies: 3
    Last Post: January 18th, 2011, 07:54 PM