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

Thread: constructor and class

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default constructor and class

    I'm trying to tackle the problem step by step. What I am having trouble is where to start lol
    I started with writing the constructor but not sure if i'm going in the right direction for this problem. Seems pretty simple to me in concept. But I am getting errors so i must not be working it out right? Any pointers would be great

    /*
    63. Write a class encapsulating the concept of student grades on a test,
    assuming student grades are composed of a list of integers between
    0 and 100.
    Write the following methods:
    ❑ A constructor with just one parameter, the number of stu-
    dents; all grades can be randomly generated
    ❑ Accessor, mutator, toString, and equals methods
    ❑ A method returning an array of the grades sorted in ascending
    order
    ❑ A method returning the highest grade
    ❑ A method returning the average grade
    ❑ A method returning the median grade (Hint: The median grade
    will be located in the middle of the sorted array of grades.)
    ❑ A method returning the mode (the grade that occurs most
    often) 
    * 
    * (Hint: Create an array of counters; count how many
    times each grade occurs; then pick the maximum in the array
    of counters; the array index is the mode.)
    Write a client class to test all the methods in your class.
     */
    package natashaepperson_chapter_08_exercise_63;
    import java.util.Random;
     
     
     
    public class Student 
    {
    //array random grades
    private int []grades;
    private String [] counters = { "How many times grade occurs",
                                   "Max Grade", };
     
         //constructor with just one parameter, the number of students; all grades can be randomly generated                              
    public StudentGrades()
    {
        grades = new int[grade.lenght];
        fillGrades();
    }
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: constructor and class

    Either you don't really understand constructors, or you were just careless. Regardless, I'll try to keep this short and simple. A java class has a name ("Student" in your case). This java class represents a java object (a Student object, in your case). A constructor is how new instances (an instance is a single constructed version of an object) of a class are created.
    So, when you have a constructor like:
    public StudentGrades() {...}
    you are suggesting that you are attempting to create a StudentGrades object, which means the StudentGrades constructor should be located in the StudentGrades class.
    However, you have declared the class with the name: "Student" when you wrote this:
    public class Student {
    ...
    Since this is the Student class, the only constructors the class can contain are constructors for Student objects. Attempting to create a constructor for a StudentGrades object in the Student class is not allowed, and will cause compile errors.

    To fix this problem, you can do a few things:
    1. If you want Student and StudentGrades to be two different objects, you need to give each of them their own class. So you would create a StudentGrades class, and you would put your StudentGrades constructor in there, and you would then create a constructor for Student in the Student class.
    2. If you just made a careless mistake and you want a StudentGrades object, but not a Student object, you need to rename the class from: "Student" to "StudentGrades"
    3. If you just made a careless mistake and you want a Student object, but not a StudentGrades object, you need to rename the constructor from: "StudentGrades" to "Student"

    Tell me if any of that was confusing.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    nepperso (November 25th, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Re: constructor and class

    I think I understand, I get a little lost in some of the naming conventions. I haven’t had as much time to devote to java as I’d like to since I’m taking Calc III at the same time.
    public class StudentGrades
    {
    //array random grades
    private int []grades;
    private String [] counters = { "How many times grade occurs",
                                   "Max Grade", };
     
         //constructor with just one parameter, the number of students; all grades can be randomly generated                              
    public StudentGrades()
    {
        grades = new int[grade.lenght];
        fillGrades();
    }
    }
    Doesn’t this make the class and constructor naming redundant?

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: constructor and class

    Doesn’t this make the class and constructor naming redundant?
    No, not when you consider that a constructor is allowed to have parameters. For example, you create a second constructor which accepts an int, and you can use that int to set the length of the grades array or something. A class can have as many different constructors as you want, as long as they are all constructing the class (by "different constructors", I mean constructors with different parameter options).
    And, from a practicality point of view, consider an extremely large project with hundreds or thousands of class files. Then imagine you wanted to create a new StudentGrades object. How does the computer know where to find the code to create that object? If you didn't have the naming convention like it is, the constructor for that object could be in any of those files, and having to search for the correct constructor would considerably add to the runtime of the program. Because of the naming convention, the computer knows that it will find the StudentGrades constructor in the StudentGrades class, and it doesn't have to look anywhere else.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    nepperso (November 25th, 2013)

  7. #5
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Re: constructor and class

    Okay, how does this look now? I'm adding more to the code
    package natashaepperson_chapter_08_exercise_63;
    import java.util.Random;
    /**
     *
     * @author Natasha.Epperson
     */
    public class StudentGrade 
    {
    //array random grades
    private int []grades;
    private String [] counters = { "How many times grade occurs",
                                   "Max Grade", };
     
         //constructor with just one parameter, the number of students; all grades can be randomly generated                              
    public StudentGrades()
    {
        grades = new int[grades.length];
        fillGrades();
    }
    /* method generatotes Grade Member IDs 
    *  stores in array
    */
    private void fillGrade()
    {
        Random rand = new Random ();
        for (int i = 0; i < grades.length; i++)
        {
            grades[i] = rand.nextInt ()+1;
     
        }
    }    
    }

  8. #6
    Member
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    70
    My Mood
    Cool
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: constructor and class

    Quote Originally Posted by nepperso View Post
    Okay, how does this look now? I'm adding more to the code
    package natashaepperson_chapter_08_exercise_63;
    import java.util.Random;
    /**
     *
     * @author Natasha.Epperson
     */
    public class StudentGrade 
    {
    //array random grades
    private int []grades;
    private String [] counters = { "How many times grade occurs",
                                   "Max Grade", };
     
         //constructor with just one parameter, the number of students; all grades can be randomly generated                              
    public StudentGrades()
    {
        grades = new int[grades.length];
        fillGrades();
    }
    /* method generatotes Grade Member IDs 
    *  stores in array
    */
    private void fillGrade()
    {
        Random rand = new Random ();
        for (int i = 0; i < grades.length; i++)
        {
            grades[i] = rand.nextInt ()+1;
     
        }
    }    
    }
    Above code will result in nullpointerexception when an object to StudentGrades is created as you are trying to access the length of grades before initialization.

Similar Threads

  1. How to avoid a constructor of class A be extended to Class B?
    By chaitra1987 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 28th, 2013, 11:46 PM
  2. Working with a constructor class.
    By Blues03 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 21st, 2013, 04:07 AM
  3. **Constructor in class cannot be applied to given types;...
    By bassie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2012, 03:15 PM
  4. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  5. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM