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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: StudentQuizzesObject

  1. #1
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default StudentQuizzesObject

    /**
     * Write a description of class Student here.
     * 
     * @author (Landon L.) 
     * @version (a version number or a date)
     */
    public class Student
    {
        // instance variables - replace the example below with your own
        private String name;
        private int qz1;
        private int qz2;
        private int qz3;
        private int qz4;
        private int qz5;
        private int qzn;
        /**
         * Constructor for objects of class Student
         */
        public Student(String name, int q1, int q2, int q3, int q4, int q5)
        {
            // initialise instance variables
            name = name;
            qz1 = q1;
            qz2 = q2;
            qz3 = q3;
            qz4 = q4;
            qz5 = q5;
        }
           //Given these  rules, how would I be able to create these methods(below)??? I mean you cant use an array and only certain instance variables
           //are allowed.
          // Student will need instance variables name , qz1, qz2, qz3, qz4, and 
         //qz5 (of types String and int, respectively). 
         // Student will need appropriate methods and constructors. To make things 
         //interesting, create a getQuiz() method that takes in a quiz number as input and 
         //then returns the appropriate quiz value. Likewise, setQuiz() will take as input 
         //a quiz number and quiz score, and then put the value into the right variable. Make 
         //sure to have a toString() method that prints the name of the student along 
         //with the quiz scores. 
        public int getQuiz()
        {
           //? 
        }
     
        public void setQuiz(int num, int grade )
        {
     
            //?
     
     
        }
     
        public void setName(String s)
        {
            name = s;
        }
     
        public String getName()
        {
            return name;
        }
     
        public String toString()
        {
            return name + "   " +  
                    qz1 + "   " + qz2 + "   " +  
                    qz3 + "   " + qz4 + "   " + qz5;
        }
    }


    --- Update ---

    anyone
    Last edited by llowe29; November 13th, 2013 at 04:45 PM. Reason: sorry small mistake

  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: StudentQuizzesObject

    What is your question?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    the question is indicated in the commented out part. What to do in the getQ/setQ methods

  4. #4
    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: StudentQuizzesObject

    What are you specific questions about the code?
    Can you copy the questions out of the code where they are hard to read into normal text?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    create a getQuiz() method that takes in a quiz number as input and
    then returns the appropriate quiz value. Likewise, setQuiz() will take as input
    a quiz number and quiz score, and then put the value into the right variable.
    Given these rules, how would I be able to create these methods(below)??? I mean you cant use an array(LIST) and only certain instance variables are allowed

  6. #6
    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: StudentQuizzesObject

    getQuiz() method that takes in a quiz number as input and then returns the appropriate quiz value.
    That gives a template for the method definition: takes an int, returns a value.

    setQuiz() will take as input a quiz number and quiz score, and then put the value into the right variable.
    The template there is the method takes two numbers and stores them somewhere.

    That's it for me tonight.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    but how to correlate quiz number and quiz score is my question

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StudentQuizzesObject

    If you cannot use any sort of data structure then your only option would be a long if statement.
    Improving the world one idiot at a time!

  9. #9
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    so like:
    if(num == 1)
    grade = qz1;
    else if(num == 2)
    grade = qz2;
    ........................

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StudentQuizzesObject

    What happened when you tried?
    Improving the world one idiot at a time!

  11. #11
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    now the problem is the get quiz method Ill show you the updated method
    /**
     * Write a description of class Student here.
     * 
     * @author (Landon L.) 
     * @version (a version number or a date)
     */
    public class Student
    {
        // instance variables - replace the example below with your own
        private String name;
        private int qz1;
        private int qz2;
        private int qz3;
        private int qz4;
        private int qz5;
        /**
         * Constructor for objects of class Student
         */
        public Student(String name, int q1, int q2, int q3, int q4, int q5)
        {
            // initialise instance variables
            name = name;
            qz1 = q1;
            qz2 = q2;
            qz3 = q3;
            qz4 = q4;
            qz5 = q5;
        }
           //Given these  rules, how would I be able to create these methods(below)??? I mean you cant use an array and only certain instance variables
           //are allowed.
          // Student will need instance variables name , qz1, qz2, qz3, qz4, and 
         //qz5 (of types String and int, respectively). 
         // Student will need appropriate methods and constructors. To make things 
         //interesting, create a getQuiz() method that takes in a quiz number as input and 
         //then returns the appropriate quiz value. Likewise, setQuiz() will take as input 
         //a quiz number and quiz score, and then put the value into the right variable. Make 
         //sure to have a toString() method that prints the name of the student along 
         //with the quiz scores. 
        public int getQuiz(int num)
        {
     
     
        }
     
        public void setQuiz(int num, int grade )
        {
          if(num == 1)
             grade = qz1;
               else if(num == 2)
                    grade = qz2;
               else if(num == 3)
                    grade = qz3;
               else if(num == 4)
                    grade = qz4;
               else if(num == 5)
                    grade = qz5;
          else 
          {
          String msg = " This quiz doesn't exist. ";
          throw new IllegalArgumentException(msg); 
          }
        } 
     
        public void setName(String s)
        {
            name = s;
        }
     
        public String getName()
        {
            return name;
        }
     
        public String toString()
        {
            return name + "   " +  
                    qz1 + "   " + qz2 + "   " +  
                    qz3 + "   " + qz4 + "   " + qz5;
        }
    }

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StudentQuizzesObject

    Take a close look at your setQuiz method. If you pass in 5 as the grade parameter, what happens to that value?

    The getQuiz method is simply the reverse of setQuiz (providing you get the logic correct).
    Improving the world one idiot at a time!

  13. #13
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    So, if I am undertanding you correctly my set quiz method is correct.

  14. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: StudentQuizzesObject

    int a = 3;
    int b = 4;
     
    b = a;
    What are the values of a and b? Is the left assigned to the right? or the right to the left?

  15. #15
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StudentQuizzesObject

    No it's not. Test your code, print out variables to see what values they have before and after something happens.
    Improving the world one idiot at a time!

  16. #16
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    right assigned to left, but Im not sure of your point

    --- Update ---

    But I dont think I can without having the getQuiz method working

  17. #17
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StudentQuizzesObject

    Then look at your code and see what variable is being assigned to what. BIG HINT:
    grade = qz1 is wrong. What is the only other possibility?
    Improving the world one idiot at a time!

  18. #18
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    But this works though I dont know if when the tester program that involves insertions and deletions will, if what youre saying is a problem???
    The only problem is that the getter and setter are basically the same.
    /**
     * Write a description of class Student here.
     * 
     * @author (Landon L.) 
     * @version (a version number or a date)
     */
    public class Student
    {
        // instance variables - replace the example below with your own
        private String name;
        private int qz1;
        private int qz2;
        private int qz3;
        private int qz4;
        private int qz5;
        /**
         * Constructor for objects of class Student
         */
        public Student(String name, int q1, int q2, int q3, int q4, int q5)
        {
            // initialise instance variables
            name = name;
            qz1 = q1;
            qz2 = q2;
            qz3 = q3;
            qz4 = q4;
            qz5 = q5;
        }
           //Given these  rules, how would I be able to create these methods(below)??? I mean you cant use an array and only certain instance variables
           //are allowed.
          // Student will need instance variables name , qz1, qz2, qz3, qz4, and 
         //qz5 (of types String and int, respectively). 
         // Student will need appropriate methods and constructors. To make things 
         //interesting, create a getQuiz() method that takes in a quiz number as input and 
         //then returns the appropriate quiz value. Likewise, setQuiz() will take as input 
         //a quiz number and quiz score, and then put the value into the right variable. Make 
         //sure to have a toString() method that prints the name of the student along 
         //with the quiz scores. 
        public int getQuiz(int num)
        {
            int grade;
         if(num == 1)
             grade = qz1;
               else if(num == 2)
                    grade = qz2;
               else if(num == 3)
                    grade = qz3;
               else if(num == 4)
                    grade = qz4;
               else if(num == 5)
                    grade = qz5;
          else 
          {
          String msg = " This quiz doesn't exist. ";
          throw new IllegalArgumentException(msg); 
          }
         return grade; 
        }
     
        public void setQuiz(int num, int grade )
        {
          if(num == 1)
             grade = qz1;
               else if(num == 2)
                    grade = qz2;
               else if(num == 3)
                    grade = qz3;
               else if(num == 4)
                    grade = qz4;
               else if(num == 5)
                    grade = qz5;
          else 
          {
          String msg = " This quiz doesn't exist. ";
          throw new IllegalArgumentException(msg); 
          }
        } 
     
        public void setName(String s)
        {
            name = s;
        }
     
        public String getName()
        {
            return name;
        }
     
        public String toString()
        {
            return name + "   " +  
                    qz1 + "   " + qz2 + "   " +  
                    qz3 + "   " + qz4 + "   " + qz5;
        }
    }

  19. #19
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: StudentQuizzesObject

    Quote Originally Posted by llowe29 View Post
    But this works though
    Show the code you used to test and verify that it works.

  20. #20
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    What I had
    /**
     * Write a description of class Student here.
     * 
     * @author (Landon L.) 
     * @version (a version number or a date)
     */
    public class Student
    {
        // instance variables - replace the example below with your own
        private String name;
        private int qz1;
        private int qz2;
        private int qz3;
        private int qz4;
        private int qz5;
        /**
         * Constructor for objects of class Student
         */
        public Student(String name, int q1, int q2, int q3, int q4, int q5)
        {
            // initialise instance variables
            name = name;
            qz1 = q1;
            qz2 = q2;
            qz3 = q3;
            qz4 = q4;
            qz5 = q5;
        }
           //Given these  rules, how would I be able to create these methods(below)??? I mean you cant use an array and only certain instance variables
           //are allowed.
          // Student will need instance variables name , qz1, qz2, qz3, qz4, and 
         //qz5 (of types String and int, respectively). 
         // Student will need appropriate methods and constructors. To make things 
         //interesting, create a getQuiz() method that takes in a quiz number as input and 
         //then returns the appropriate quiz value. Likewise, setQuiz() will take as input 
         //a quiz number and quiz score, and then put the value into the right variable. Make 
         //sure to have a toString() method that prints the name of the student along 
         //with the quiz scores. 
        public int getQuiz(int num)
        {
            int grade;
         if(num == 1)
             grade = qz1;
               else if(num == 2)
                    grade = qz2;
               else if(num == 3)
                    grade = qz3;
               else if(num == 4)
                    grade = qz4;
               else if(num == 5)
                    grade = qz5;
          else 
          {
          String msg = " This quiz doesn't exist. ";
          throw new IllegalArgumentException(msg); 
          }
         return grade; 
        }
     
        public void setQuiz(int num, int grade )
        {
          if(num == 1)
             grade = qz1;
               else if(num == 2)
                    grade = qz2;
               else if(num == 3)
                    grade = qz3;
               else if(num == 4)
                    grade = qz4;
               else if(num == 5)
                    grade = qz5;
          else 
          {
          String msg = " This quiz doesn't exist. ";
          throw new IllegalArgumentException(msg); 
          }
        } 
     
        public void setName(String s)
        {
            name = s;
        }
     
        public String getName()
        {
            return name;
        }
     
        public String toString()
        {
            return name + "   " +  
                    qz1 + "   " + qz2 + "   " +  
                    qz3 + "   " + qz4 + "   " + qz5;
        }
    }
    Tester
    /**
     * Write a description of class TestStudent here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class TestStudent
    {
     
        public static void main(String[] args) 
        {
     
            Student k = new Student("Emily", 90, 95, 95, 90, 92);
            //k.setQuiz(1, 90);
            int a = k.getQuiz(1);
            System.out.println(a);
        }
    }

  21. #21
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StudentQuizzesObject

    Try calling setQuiz before getQuiz and then tell us if it works.

    By that i mean use this line instead of your commented out one:
    k.setQuiz(1, 50);
    Improving the world one idiot at a time!

  22. #22
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    yes it does

  23. #23
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StudentQuizzesObject

    No it doesn't!

    --- Update ---

    public class TestStudent
    {
     
        public static void main(String[] args) 
        {
     
            Student k = new Student("Emily", 90, 95, 95, 90, 92);
            k.setQuiz(1, 50);
            int a = k.getQuiz(1);
            System.out.println(a);
        }
    }
    That will print out 90 when it should print out 50.
    Improving the world one idiot at a time!

  24. #24
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: StudentQuizzesObject

    oh i thought you meant setQuiz(1,90) again

    --- Update ---

    Right it doesnt work, sometimes.

  25. #25
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: StudentQuizzesObject

    Yeah the only times it "does work" is when you try setting the same value. Because you code does nothing it seems like it works. Post 17 gave you a BIG HINT.
    Improving the world one idiot at a time!

Page 1 of 2 12 LastLast