Results 1 to 18 of 18

Thread: Exam Question

  1. #1
    joepeger is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Exam Question

    Hey I'm new to Java and have been trying to teach myself using a book but I'm being particularly unsuccessful. I have an exam coming up and this is the sample question he gave us as an example.

    QUESTION:

    Write a Java class called ExamMarks, which specifies a module name (String), module code (int), year (int) and an array of 20 integer marks. The class should have an appropriate three argument constructor, a toString() method and methods displaying the highest mark and the average mark. It should have a separate method to obtain up to 20 marks from the user via the console.

    Use appropriate visibility modifiers to ensure encapsulation. In the main(String[]) method create an ExamMarks object.

    Marks will be given for good program design, code layout, and choice of identifiers as well as a satisfactory implementation of the requirements above. No marks will be given for comments or the inclusion of extra functionality not described above.

    THIS IS WHAT I HAVE SO FAR:

    class examMarks {
    String name;
    int studentNumber;
    int marks;

    public static void main (String [] args){
    int year = 2013;
    examMarks [] marks = new examMarks[20];
    int z = 0;

    while (z < 20) {
    z = z + 1;
    if (z == 1){
    marks[z] = new examMarks();
    marks[z].name = "Bob";
    marks[z].studentNumber = 106708901;
    marks[z].

    System.out.println("Student name = " + marks[z].name);
    System.out.println("Student number = " + marks[z].studentNumber);
    System.out.println("Year = " + year);
    }}}}

    I know I haven't started encapsulation yet I was going to add that at the end. Can anyone point me in the right direction? I'm kinda stuck. Any thoughts appreciated thanks for your help.

    Joe
    Last edited by joepeger; 01-16-2013 at 01:45 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Exam Question

    A comment on coding style: Do NOT code more than one } on a line.
    }s should be vertically inline below the start of the line with the pairing {

    Please wrap code in code tags. See: http://www.java-forums.org/misc.php?do=bbcode#code


    This is amazing:
    No marks will be given for comments
    What a poor way to teach programming.
    Last edited by Norm; 01-16-2013 at 01:26 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Exam Question

    I agree with Norm about the comments. On the formatting, start the class with a Capital letter. (And repost or edit the code so that it's readable.)

    Go the problem as it was given to you and identify each of the things the class is is supposed to have. If you are unsure about what they are, say the things you have identified. If there is something in particular that you have identified but have no clue about how to implement it, then you will have something specific to ask.

  4. #4
    joepeger is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Exam Question

    Thanks for the tips on the formatting. Is this closer to the standard? I have added to the code and have a more specific question: Can you see why I'm getting a run-time error from this code?

    Much appreciated.

    Joe.




    Java Code:
    
    
    class ExamMarks {
      String name;
      int studentNumber;
      int grade; 
      
      public static void main (String [] args){
        int year = 2013;
        ExamMarks [] marks = new ExamMarks[20];
        int z = -1;
        
        while (z < 20) {
          
          z++;
          
          if (z == 0) {
             marks[z] = new ExamMarks();
             marks[z].name = "Bob";
             marks[z].studentNumber = 106708901;
             marks[z].grade= 67;
          }
          
          if (z == 1) {
             marks[z] = new ExamMarks();
             marks[z].name = "Alan";
             marks[z].studentNumber = 106708902;
             marks[z].grade= 82;   
          }
          
        System.out.println("Student name = " + marks[z].name);
        System.out.println("Student number = " + marks[z].studentNumber);
        System.out.println("Year = " + year);
        System.out.println("Grade = " +marks[z].grade);
        System.out.println("************************");
        
        if (z > z + 1){
          z=z;
        }
        
        System.out.println("Highest grade = " +marks[z].grade);
        
        }
       
      }
    }
    Last edited by joepeger; 01-16-2013 at 03:57 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Exam Question

    The formatting looks much better now. There are still some statements that are not properly indented. What statement does the } on line 41 belong to? Looks like lines 29 to 39 be indented?

    getting a run-time error
    Please copy the full text of the error and post it.
    Last edited by Norm; 01-16-2013 at 04:06 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    joepeger is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Exam Question

    } line 41 belongs to the while loop. 29-39 indent corrected: thank you.

    ERROR CODE:

    Welcome to DrJava. Working directory is C:\Users\jdp1\Downloads
    > run ExamMarks
    Student name = Bob
    Student number = 106708901
    Year = 2013
    Grade = 67
    ************************
    Highest grade = 67
    Student name = Alan
    Student number = 106708902
    Year = 2013
    Grade = 82
    ************************
    Highest grade = 82
    java.lang.NullPointerException
    at ExamMarks.main(examMarks.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Exam Question

    java.lang.NullPointerException
    at ExamMarks.main(examMarks.java:29)
    There is a variable on line 29 with a null value when the code is executed. Look at line 29, find the variable with the null value and then backtrack to find out why it does not have a valid value.

    This is an interesting statement:
    Java Code:
      if (z > z + 1){
    When will it be true?
    Last edited by Norm; 01-16-2013 at 04:48 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Exam Question

    Quote Originally Posted by Norm View Post
    This is an interesting statement:
    Java Code:
      if (z > z + 1){
    When will it be true?
    When z == Integer.MAX_VALUE :)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Exam Question

    Thanks, I guess that happens when the value wraps: 7F to 80
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    joepeger is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Exam Question

    Alright I'm getting closer. Thanks for all your help. After a lot of time tweaking I've come up with this. The array works but I haven't done it in the class. How would I go about doing that? Any other suggestions?

    Cheers

    Joe




    Java Code:
    
    
    import java.util.*;
    
    class ExamMarks{
      String moduleName;
      int moduleCode;
      int year;
        
      public static void main (String [] args){
        ExamMarks grade = new ExamMarks();
        grade.moduleName="Object Oriented Programming";
        grade.moduleCode=5015;
        grade.year = 2013;
            
        Scanner percentages = new Scanner(System.in);
        int [] numbers = new int [20];
        System.out.println("Please enter 20 marks");
        
        for (int i = 0; i < numbers.length; i++){
          numbers[i] = percentages.nextInt();
          System.out.println("Mark acheived: " + numbers[i]);
          System.out.println("The title of this module is : " + grade.moduleName);
          System.out.println("The code number of this module is: " +grade.moduleCode);
          System.out.println("The year of the Exam is: " + grade.year);
        }
      }
    }
    Last edited by joepeger; 01-18-2013 at 02:06 PM.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Exam Question

    I haven't done it in the class
    Can you explain what you mean and what you are trying to do?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    joepeger is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Exam Question

    Quote Originally Posted by joepeger View Post
    Write a Java class called ExamMarks, which specifies a module name (String), module code (int), year (int) and an array of 20 integer marks.
    Please correct me if I am wrong. I have three specifications in the class: moduleName, moduleCode & year but I did the int [] in the 'main'. Is this OK with respect to the original specifications?

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Exam Question

    The array needs to be defined at the class level not inside of a method.
    It can be given values in the method.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    joepeger is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Exam Question

    Slowly but surely wins the race. I feel like I'm learning!

    How do I go about writing a method for the averageMarks and the highestMarks? There isn't a variable [i] yet defined so I can't yet write the code in the method. How would you suggest I go about doing this?
    Any other advice appreciated.

    Cheers, Joe.


    Java Code:
    import java.util.*;
     
    public class ExamMarks{
    
      //fields
      private String moduleName;
      private int moduleCode;
      private int year;
      private int [] marks;
      
      //methods
      public void highestMark(){
        //code for highestMark
      }
      
      public void averageMark(){
        //code for averageMark
      }
       
      //Constructor
      public ExamMarks(String ExamMarksModuleName , int ExamMarksModuleCode, int ExamMarksYear){
        moduleName = ExamMarksModuleName;
        moduleCode = ExamMarksModuleCode;
        year = ExamMarksYear;
      }
      
      public static void main (String [] args){
        ExamMarks grade = new ExamMarks("Object Oriented Programming", 5015, 2013);
        Scanner percentages = new Scanner(System.in);
        int [] numbers = new int [20];
        System.out.println("Please enter 20 marks");
         
        for (int i = 0; i < numbers.length; i++){
          numbers[i] = percentages.nextInt();
          System.out.println("Mark acheived: " + numbers[i]);
          System.out.println("The title of this module is : " + grade.moduleName);
          System.out.println("The code number of this module is: " +grade.moduleCode);
          System.out.println("The year of the Exam is: " + grade.year);
        }
      }
    }
    Last edited by joepeger; 01-28-2013 at 03:47 PM.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Exam Question

    How would you suggest I go about doing this?
    What is the "this" you want to do?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    joepeger is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Exam Question

    Java Code:
    //methods
      public void highestMark(){
        int highest = 0;
        int i=0;
        for (i = 0; i < numbers.length; i++){  
          if (numbers[i]>numbers[i+1]){  
            highest = numbers[i];
          }
        } 
      }
      public void averageMark(){
        int sum = 0;  
        double average = 0.000;
        int i=0;
        for (i = 0; i < 19; i++){  
          sum = numbers [i] + sum;  
          average = sum/numbers.length;  
        }
      }
    I've had a go at creating the methods. This is what I have come up with. It's compiling but I'm not entirely sure how to access it in the object.

    System.out.println(grade.averageMark()); doesn't work... Forgive me if it's a silly question: you may have guessed I'm just feeling my way through it at the moment. How do I check if it's working?

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default Re: Exam Question

    Neither of the methods are defined to return a value. They are both defined as returning void.

    If the methods are to return anything they needed to be defined differently. See the tutorial:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    joepeger is offline Member
    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Default Re: Exam Question

    Will read those. thank you for the advice

Similar Threads

  1. Help with an Java exam question
    By boodruns4ever in forum New To Java
    Replies: 1
    Last Post: 04-20-2012, 04:02 AM
  2. Java exam question help
    By eoins2345 in forum New To Java
    Replies: 7
    Last Post: 08-20-2011, 07:04 PM
  3. The exam is over!
    By elliotHenry in forum New To Java
    Replies: 2
    Last Post: 06-15-2011, 10:27 PM
  4. Exam prep Question
    By tonto2010 in forum New To Java
    Replies: 4
    Last Post: 05-10-2011, 01:11 AM
  5. Preparing for Exam
    By AWPtic in forum New To Java
    Replies: 4
    Last Post: 04-13-2009, 02:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •