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 18 of 18

Thread: how to do this simple java program?

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

    Default how to do this simple java program?

    Plz read the question very carefully.I need call all the member methods from the main method by creating object only.And all the member methods are necessary.The constructor is parametrized.I need the answer according to the question only and not in ur own way.
    Define a class student
    described as below:-
    Data Members-
    name,age,m1,m2,m
    3(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 3
    sub,max,avg
    Write a main method
    create an object and CALL
    THE ABOVE METHODS


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to do this simple java program?

    Please post the code you are having problems with and the questions about any problems you are having with it.
    If you don't understand my answer, don't ignore it, ask a question.

  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 do this simple java program?

    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();
    }
    }

    It shows a syntax error

  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 do this simple java program?

    @avistein You haven't defined a default constructor for the class.
    You have defined the parameterized constructor for your class and when there is a parameterized constructor in the class then you have to either call the parameterized constructor or define a default constructor for the class.
    Or you can also do the constructor chaining.

    This is my first post and hope this help you.
    Sorry for my bad english as i ain't a english speaker.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to do this simple java program?

    Please edit your post, format the code and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Statements should be indented to show the logic. They should not all start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: how to do this simple java program?

    @Bhawani, but the question says to create a constructor and not 2.So how to do that?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to do this simple java program?

    It shows a syntax error
    Please copy the full text of the error message and post it here.

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

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

    Default Re: how to do this simple java program?

    I don't know how to format it.I am new here.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to do this simple java program?

    Look at some other threads where code has been posted. There are many to look at.
    Also look at how the code is formatted in the tutorials:
    Code Conventions for the Java Programming Language: Contents
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: how to do this simple java program?

    Very sorry to say that I have posted a wrong code.Actually I was trying to modify the code earlier in my pc and and I have posted that code only.Here is the original code:
    //constructor program.
    //to accept values using a function accept()and then pass the values to the parametrized constructor and then call all the methods from the main method by creating an object,but how?
    import java.io.*;
    class student1
    {
    //instance variable
    String name;
    int age,m1,m2,m3,maxi;
    float avg;
    //parameterized constructor to initialize the instance variable
    student1(String n,int a,int m4,int m5,int m6)
    {
    age=a;
    m1=m4;
    m2=m5;
    m3=m6;
    name=n;
    }
    //to accept the details from the user and then pass them to the parametrized constructor
    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());
    student1 obj=new student1(a,ae,m7,m8,m9);
    }
    //to calculate the average and maximum
    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;
    }
    //display the details of the student
    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);
    }
    //main method
    public static void main(String args[])throws IOException
    {
    //I want to create the object to call all the member methods listed in the question,but how?
    student1 obj1=new student1(); // an error occurs here-cannot find symbol-constructor student1()
    obj1.accept();
    obj1.compute();
    obj1.display();
    }
    }
    //I want to use only 1 constructor and not constructor overloading as it is not said in the question

    The error is : cannot find symbol-constructor student1()

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

    Default Re: how to do this simple java program?

    Here is the errorError.jpgError message.jpg

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to do this simple java program?

    Please copy the full text of the error message and post it here. Don't post a screen image. They are hard to read and the text can not be copied and pasted for adding comments to.

    Also please edit the code and properly format it with indentations for nested statements.
    All statements should not begin in the first column.

    cannot find symbol-constructor student1()
    Where is the constructor that has args that matches how you are calling it?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: how to do this simple java program?

    @Norm,u r very bad.Why don't u see the program? Ur demand is 2 much.I have given the error,and then also u r demanding so.U must quit.U don't know,how to help ppl.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to do this simple java program?

    Have you read how to write class constructors?
    Constructors (The Java™ Tutorials > The Reflection API > Members)

    What don't you understand about creating and using a class's constructor? You must define all the constructors that you are trying to use. The compiler is telling you that you are trying to use a constructor that is not defined.

    Why are you refusing to properly format the code? No one likes to read unformatted code. Unformatted code is hard to read and understand.

    Why are you refusing to copy the full text of the error message and paste it here? Images are hard to read and it is impossible to copy any part of an image so it can be used it a response.
    Here is a sample of a compiler's error message:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: how to do this simple java program?

    Sorry, 4 being rude 2 u.But I really don't know what you really meant by 4matting the code.I really don't know what is indentation and how to do it.I am only a class 10 student,who has a xam in a few days.I don't know how to get that full text of error message in BlueJ.Plz help me.So how can I do the program according to the question.Plz help.And once again sorry!

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to do this simple java program?

    .I really don't know what is indentation
    Here is a sample of formatted code with indentations:
     
          for(int n=2; n < 6; n+=3) {
             int counter = 0; 
             for (int i = n; i <= 3*n; i++){
                System.out.println("n="+n + ", i="+i + ", cntr="+counter);
                while(i > +n){
                   i--;
                   counter++;
                }
             } //  end for(i)
             System.out.println("n="+n + " cntr="+counter);
          } // end for(n)

    how to get that full text of error message
    Compile the code using the javac command in a command prompt.

    IDEs are a problem for beginning students and are not recommended until later in the learning process when they know how to use them. IDEs hide too many important things that students don't know how to work around.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: how to do this simple java program?

    @Norm I don't know to format in command prompt.It's not in our syllabus.We have only BlueJ.When I saw the option in BlueJ it said that it is auto-intended.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: how to do this simple java program?

    option in BlueJ it said that it is auto-intended.
    Can you post code that is properly formatted now?

    know to format in command prompt
    The formatting is done in the editor.

    Does your IDE have a way you can copy the full text of the error messages and post them?
    Otherwise you will have to type in the text of the error messages so they are easy to read and so their text can be copied for commenting on.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Trying to make a simple java program
    By Alex Feldman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 27th, 2011, 12:37 PM
  2. Simple Java Program - hasTeen [HELP]
    By HeroFlame in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 24th, 2011, 09:16 AM
  3. Simple Java Program
    By Robbiep in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 24th, 2011, 07:27 AM
  4. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  5. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM