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

Thread: Exercise 115

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Exercise 115

    I took care of exercise 105. I don't know what to do with 115 though. I've worked a little bit more and progressed a slight bit, but here's the exercise:

     
    A team of biologists is conducting an experiment that involves collecting data on animals founds in a 1 km square area of woodland. As each animal is identified, a record is made of its name, the time of its discovery, and the initials of the scientist who found it. The data are to be recorded on a laptop. Design and implement a system for storing the data, and test your code thoroughly.
     
    [Hint: Think in terms of creating an object for each discovery. What information should each object store, and in what will you store these objects?]

    Here is my code:

    // put class definitions here
     
     
     
    public class Record{
     
        String name;
     
        String initials;
     
        String time;
     
     
     
        public Record{
     
            this.name = name;
     
            this.initials = initials;
     
            this.time = time;
     
        }
     
    }

    Here's another section for me to test my solution in:

    public static void main( String[] args )
     
    {
     
      // test your solution here
     
     
     
    }

    So I know I'm supposed to make a new Object out of the variables, but do I need to use getters and setters or something? If that was the case it would probably be a lot easier than I'm assuming.


    Thx


  2. #2
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Exercise 115

    Okay, well what you have right now is a default constructor. I would change that into a constructor with parameters.
    public Record(name, initials, time)
    {
     
    }

    Now you would be able to create an object with given values for those three instance fields!

    Something you may not have learned yet is how to use multiple classes. A class can call another class if it is imported or if they are in the same package.

    I would create a second class that is a "higher" level class. This class would store multiple instances of the class "Record".

    In other words you would have an array that holds multiple "Record" objects.

    Getters would be nice to view the individual instance fields or alternatively you could have a "displayRecord()" function in the Record class that displays all three instance variables.

    Then in your larger function you could call the displayRecord() function for each object in your array of records!

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 115

    So, how do I use multiple classes?

    --- Update ---

    I think that's just nesting classes right?

    --- Update ---

    For this example, what do I put in the subclass?

    // put class definitions here
     
     
     
    public class Record{
     
        private String name;
     
        private String initials;
     
        private String time;
     
     
     
        public Record(String name, String initials, String time){
     
            this.name = name;
     
            this.initials = initials;
     
            this.time = time;
     
        }
     
       public class diffRecords{
     
           diffRecords(){
     
           }
     
       }
     
    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Exercise 115

    First, build one class that includes all of the variables needed to completely describe and store a "discovery." To that class, add at least one constructor, perhaps more than one, and any getter and setter methods you think will be useful. Post that when you're done.

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 115

    Here's my updated code:

    // put class definitions here
     
     
     
    public class Record {
     
     
     
       private String name;
     
     
     
       private String initials;
     
     
     
       private String time;
     
     
     
       public Record (String name, String initials, String time){
     
           this.name = name;
     
     
     
           this.initials = initials;
     
     
     
           this.time = time;
     
       }
     
     
     
       public String getName(){
     
           return name;
     
       }
     
     
     
       public String getInitials(){
     
           return initials;
     
       }
     
     
     
       public String getTime(){
     
           return time;
     
       }
     
     
     
    ArrayList diffRecords = new ArrayList<Record>();
     
    }


    --- Update ---

    I think there are syntax errors.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Exercise 115

    Your code is so simple yet hard to read because of the extra linefeeds, no comments, and inconsistent formatting. Please, start applying standard coding practices consistently. It would be much nicer if your code looked like:
    public class Record
    {
        private String name;
        private String initials;
        private String time;
     
        public Record ( String name, String initials, String time )
        {
            this.name = name;
            this.initials = initials;
            this.time = time;
        }
     
        public String getName()
        {
            return name;
        }
     
        public String getInitials()
        {
            return initials;
        }
     
        public String getTime()
        {
            return time;
        }
     
        ArrayList diffRecords = new ArrayList<Record>();
     
    }
    Now go through the assignment's description of a "record" and copy those descriptions as comments into your code. Something like:
    /**
     * used to record each animal identified, including its name, the time of
     * its discovery, and the initials of the scientist who found it.
     */
    public class Record
    {
        // instance variables to store the animal's name, the initials of the
        // discovering scientist, and the time of discovery
        private String name;
        private String initials;
        private String time;
     
        // a constructor that sets name, scientist's initials, and time
        public Record ( String name, String initials, String time )
        {
            this.name = name;
            this.initials = initials;
            this.time = time;
     
        } // end constructor
     
    // etc . . .
    Your work has gotten much better! However, there is no reason for you to post "I think there are syntax errors." If there are, fix them. If you need help with them, post them, and ask for help. A collection of Records does not belong in the Record class.

    Make any corrections/improvements to your class that you'd like and post the result. If questions occur to you, please ask them.

    For extra Forum cred, show some initiative and try to do the next step on your own: create a driver class that collects the data and then creates and stores the data in Record instances.

  7. #7
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 115

    Here's what I need help with. There's a separate code section to try out the solution. Here's my code for that. I'm getting a "cannot find symbol" error:

    public static void main( String[] args )
     
    {
     
     System.out.println("name is: " + name + ", time is: " + time);
     
     System.out.println("The scientist who discovered it was: " + initials);
     
    }

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Exercise 115

    You should know by now to post the error messages exactly as they appear to you, copied and pasted, not your interpretation or abbreviated version of them. Also, why isn't the new code you've posted indented or spaced properly?

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exercise 115

    Lol. My teacher saw my code and looked at it, including the driver class. He said the test area I could think of as my driver class.

     
    public static void main( String[] args )
     
    {
     
       // This is essentially a driver class
     
        ArrayList diffRecords = new ArrayList<Record>();
     
        diffRecords.add(new Record("Rhino", "JFK", "3:30"));
     
        diffRecords.add(new Record("Dino", "LBJ", "4:30"));
     
        diffRecords.add(new Record("Robot", "MF", "2:00"));
     
     
     
        for (Record a : diffRecords){
     
             System.out.println(a);
     
        }
     
    }


    --- Update ---

    I hadn't even said much to him. He just looked at the class and said it was there.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Exercise 115

    I don't understand what you're saying. Is there a question in there? Or are you reporting success?

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 11:31 PM
  4. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 09:40 PM
  5. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM

Tags for this Thread