Before I actually go posting the problem, I would like to let everyone know that I have been searching for help with this for a while now. I have posted on two Java Forums, but I have not received the assistance I need.

I'm a student taking Java Programming. I thought I had a good understanding until I received feedback from my teacher, who told me that even though my program did what it was supposed to do that I had done it the hard way. He gave me instructions on how to correct it, but I am now utterly confused and completely frustrated.

Before I post the code and questions, I would like to know if it would be acceptable to ask for help in this forum.

Thanks!
Of course it would be acceptable, as long as you're not asking us to do your homework for you.
Looks like you accidentally made 2 of these topics. I'm deleting the other.


I, and at least one of the other administrators, have experience as Java instructors, and a good number of the general population have experience with the language.
Indeed, and many of the programming tricks for Java parallel tricks and techniques for other languages as well. In other words, let's hear about these two possible methods.
Thank you so much!!

My assignment was to create 5 classes: Instructor, Student, Course, Section, and a class, which I named Testing01, to house the main method. I had no problem with this part.

The second part was to create an Instructor object, a Course object, and an Student object with 15 student names. I also needed to have these objects displayed. I accomplished the student list with the PrintWriter() function (since we hadn't learned arrays, yet).

The third part was to create the ArrayList() for the students and another List for the grades, having the program display the two ArrayLists (I do realize I could have done a two-dimensional ArrayList).

At this point, I had the four classes with getters and setters, and the fifth class with the main method. I created the new objects like this:

Code:
Instructor inst = new Instructor ("George Jones");
Course crse = new Course ("Java Programming");


and then the ArrayList like this:


Code:
ArrayList<String> students = new ArrayList<String>();


Next, I added the student names using the ArrayList.add method (I could make it two dimensional to add the grade list as well). I also used a for loop to cycle through the list of names:


Code:
            for (int i = 0; i < students.size(); i++){
                // Instantiation of Student class
                Student stdt = new Student();                                     
                stdt.setFullName(students.get(i));
                stdt.setLevel(grades[i]);
                // Print the current value of the variable
                System.out.printf("%s\t\t%s\n", stdt.getFullName(), stdt.getLevel());   
}


That was what I handed in as my assignment. I received this as feedback:

Quote:
You're pretty close on this one. You have the modeling down up to the section and then you made things a little too hard on yourself. Take a look at the Section class that I posted in the discussion this week. You can use this Section class to make an object that has a course, instructor and roster of students. That is a much more sensible place to bring the elements together and then you just have the main method be an exercise of instantiating the Section object and populating it and then printing its values. This is a good effort, though.


His Section class looks like this:


Code:


import java.util.ArrayList;

public class Section {
    private Course course;
    private Instructor instructor;
    private ArrayList<Student> students;

    public Section() {
        super();
    }
   
    public Section(Course course) {
       
    }
   
    public Section(Course course, Instructor instructor) {
       
    }
   
    public Section(Course course, Instructor instructor, ArrayList<Student> students) {

    }
      // getters and setters

    public void setCourse(Course course)
      {
        this.course = course;
      }

    public Course getCourse()
      {
        return course;
      }

    public void setInstructor(Instructor instructor)
      {
        this.instructor = instructor;
      }

    public Instructor getInstructor()
      {
        return instructor;
      }

    public void setStudents(ArrayList<Student> students)
      {
        this.students = students;
      }

    public ArrayList<Student> getStudents()
      {
        return students;
      }

    public void addStudent(Student student)
      {
        students.add(student);  // add a student to the section roster
      }   
    }


I can't find anything in my reading about how to create a Section object with an Instructor object, Course object, and a student ArrayList object.

Could anyone help me to understand this kind of a process or maybe even walk me through the steps? I will be so grateful!

Code:
    public Section(Course course, Instructor instructor, ArrayList<Student> students) {

    }

Use this constructor to create the Section object.
Well, the first thing that catches my eye is that having created a Student object, you then instead create an ArrayList of strings rather than an array of students. I would expect a Course to hold one Instructor, one Course, and an ArrayList of Students, just as his Section class indicates. Of course, it also allowed for constructing a Section with only a Course and with only a Course and an Instructor. For your Student class, the initializer argument is a string, the name of the student. There isn't anything conceptually different about passing an object like a Course or an Instructor or even an ArrayList containing items of type Student as an initializer instead.
souvik1997 wrote:

Code:
    public Section(Course course, Instructor instructor, ArrayList<Student> students) {

    }

Use this constructor to create the Section object.


That was what I thought, so I tried this:


Code:
        Section sec = new Section("Java Programming", "George Jones", students);


but I receive this error:

Quote:
no suitable constructor found for Section(String,String)
constructor Section.Section(Course,Instructor,ArrayList<Student>) is not applicable
(actual and formal argument lists differ in length)
constructor Section.Section(Course,Instructor) is not applicable
(actual argument String cannot be converted to Course by method invocation conversion)
constructor Section.Section(Course) is not applicable
(actual and formal argument lists differ in length)
constructor Section.Section() is not applicable
(actual and formal argument lists differ in length)


Did I write it correctly, or is there another way to write it?
KermMartian wrote:
Well, the first thing that catches my eye is that having created a Student object, you then instead create an ArrayList of strings rather than an array of students. I would expect a Course to hold one Instructor, one Course, and an ArrayList of Students, just as his Section class indicates. Of course, it also allowed for constructing a Section with only a Course and with only a Course and an Instructor. For your Student class, the initializer argument is a string, the name of the student. There isn't anything conceptually different about passing an object like a Course or an Instructor or even an ArrayList containing items of type Student as an initializer instead.


Oh, so instead of:


Code:
ArrayList<String> students = new ArrayList<String>();


It should have been:


Code:
ArrayList<Student> students = new ArrayList<Student>();

?

Code:
        Section sec = new Section(new Course("Java Programming"), new Instructor("George Jones"), students);


Try this instead -- you put Strings in place of objects such as Course that hold string values (but are not "family" classes of String in terms of polymorphism/inheritance), which is invalid as the constructor does not take direct string values.
Ashbad wrote:

Code:
        Section sec = new Section(new Course("Java Programming"), new Instructor("George Jones"), students);


Try this instead -- you put Strings in place of objects such as Course that hold string values (but are not "family" classes of String in terms of polymorphism/inheritance), which is invalid as the constructor does not take direct string values.


After implementing your suggested changes, I am now receiving this error:

Quote:
cannot find symbol
symbol: variable students
location: class Testing01
Ashbad wrote:

Code:
        Section sec = new Section(new Course("Java Programming"), new Instructor("George Jones"), students);


Try this instead -- you put Strings in place of objects such as Course that hold string values (but are not "family" classes of String in terms of polymorphism/inheritance), which is invalid as the constructor does not take direct string values.


The last part of that code (the "students") is supposed to be the variable for the ArrayList. Is that the correct syntax to construct it? Because I am receiving an error for this one word.
remember, students needs to already be an ArrayList of Student objects. So you need something like:


Code:
 students = new ArrayList<Student>;
students.add(new Student("John Smith");
students.add(new Student("Mickey Smith");
students.add(new Student("Sara Jane Smith");

Section sec = new Section(new Course("Java Programming"), new Instructor("George Jones"), students);


First you need to build the ArrayList of students, then use it.
KermMartian wrote:
remember, students needs to already be an ArrayList of Student objects. So you need something like:


Code:
 students = new ArrayList<Student>;
students.add(new Student("John Smith");
students.add(new Student("Mickey Smith");
students.add(new Student("Sara Jane Smith");

Section sec = new Section(new Course("Java Programming"), new Instructor("George Jones"), students);


First you need to build the ArrayList of students, then use it.


Hang on. I need to create the ArrayList first? Shock Should I do that in the Section class constructor?
Well, if the constructor for the Section object takes an ArrayList as an argument, then you certainly need an ArrayList to use for that argument, do you not? Smile The reason I did it before is that you also need to stuff all the students into the ArrayList before you give that ArrayList to the Section.
[row of animated smilies removed]

I'm so confused....
mysdyva wrote:
[row of animated smilies removed]

I'm so confused....
Please be more specific. Which part are you confused about?
So, I need to make the ArrayList in the Section class. Do I add the names to the list in the main method or the Section class.

I just wish that these concepts were more straight forward. I learn much better by practicing rather than reading about it. When I find examples in my reading book, I can't apply the same logic to my assignment. For example, every example in my ArrayList reading involve nothing but integers, but I need to make the ArrayList out of objects (Students), which complicates everything.

Every time I think I have grasped a concept, my instructor writes out a piece of code that contradicts everything I just learned.
Think of the ArrayList as a series of containers tied to yarn.. When you create a new empty ArrayList, you reserve a string; as you add items to the ArrayList, you add them as containers tied onto the yarn. You can put anything in those containers: strings, objects like Student, numbers, etc. In this case, you're creating a new Student object in each container. Then, you take that whole piece of yarn with all the containers full of Student objects attached, and stuff it into the big bucket that is the Section object. You also toss in a Course and an Instructor object. One of the key concepts of Java is that everything is an object: strings, numbers, Students, are all objects. You can create an ArrayList of any particular type, as ArrayList just means that you're putting a bunch of the particular object end-to-end in some related way. It doesn't care what you actually use as that object.

And of course, you need to construct the ArrayList before you create the Section, because you need the yarn-and-buckets combination to hand off to the Section. If you haven't made one yet and filled it with Student objects, you'll have nothing to give to the Section.
Thank you. That was helpful.

Can you tell me if this code is correct?


Code:
 
ArrayList<ArrayList<Student>> students = new ArrayList<ArrayList<Student>>();

Section sec = new Section(new Course("Java Programming"), new Instructor("George Jones"), students);
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement