Results 1 to 19 of 19
  1. #1
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Question Hello im new to java and i have code

    hello im new to java and i want to become a good programmer right now i only know the basics of this language which isnt very good but i am constantly trying to teach myself so i get better. can anyone give me some help with the program im suppose to write.

    Java Code:
    /**
     * DriverExam class
     */
    
    public class DriverExam
    {
       // Array containing the correct answers
       private char[] correct = { 'B', 'D', 'A', 'A',
                                  'C', 'A', 'B', 'A',
                                  'C', 'D', 'B', 'C',
                                  'D', 'A', 'D', 'C',
                                  'C', 'B', 'D', 'A' };
    
       private char[] student;       // The student's answers
       private int[] missed;         // The missed question numbers
       private int numCorrect = 0;   // The number correct
       private int numIncorrect = 0; // The number incorrect
       
       /**
        * Constructor
        * Accepts an array of the student's
        * answers as the argument. The contents
        * of the array are copied to the student
        * field.
        */
       
       public DriverExam(char[] s)
       {
          student = s;
          gradeExam();
       }
       
       /**
        * gradeExam method
        * This method determines the number of
        * correct and incorrect answers. It calls
        * the makeMissedArray method.
        */
       
       private void gradeExam()
       {
         /* Missing code starts here */
    	   
         /* Missing code ends here */
         makeMissedArray();
       }  
       
       /**
        * makeMissedArray method
        * This method makes the missed array and
        * stores the numbers of all the questions
        * that the student missed in it.
        */
       
       private void makeMissedArray()
       {
         /* Missing code starts here */
    
         /* Missing code ends here */
       }
       
       /**
        * passed method
        */
       
       public boolean passed()
       {
          boolean status;
          
          if (numCorrect >= 15)
             status = true;
          else
             status = false;
             
          return status;
       }
       
       /**
        * totalCorrect method
        */
    
       public int totalCorrect()
       {
          return numCorrect;
       }
    
       /**
        * totalIncorrect method
        */
    
       public int totalIncorrect()
       {
          return numIncorrect;
       }
    
       /**
        * questionsMissed method
        * Returns an array containing the numbers
        * of the missed questions. If no questions
        * were missed, returns null.
        */
    
       public int[] questionsMissed()
       {
           return missed;
       }
    }

    that is one class and here is another class

    Java Code:
    import java.util.Scanner;
    
    /**
     * DriverTest program.
     */
    
    public class DriverTest
    {
       public static void main(String[] args)
       {
    		// Constant for the number of answers
          final int NUM_ANSWERS = 20;
    		
    		// Array to hold the student's answers
          char[] answers = new char[NUM_ANSWERS];
    		
    		// This will reference an array holding the
    		// question numbers of the missed questions.
          int[] missedQuestions = null;
          
          // Create a Scanner object for keyboard input.
          Scanner keyboard = new Scanner(System.in);
    		
          // Get the user's answers to the questions.
          System.out.println("Enter your answers to the " +
                             "exam questions. (Make sure " +
                             "Caps Lock is ON!)");
    
          for (int i = 0; i < answers.length; i++)
          {
             System.out.print("Question " + (i + 1) + ": ");
    	 String input = keyboard.nextLine();
             answers[i] = input.charAt(0);
    
             // Validate the answer.
             while (!valid(answers[i]))
             {
                System.out.println("ERROR: Valid answers are " +
                                   "A, B, C, or D.");
                System.out.print("Question " + (i + 1) + ": ");
        			input = keyboard.nextLine();        
    				answers[i] = input.charAt(0);
             }
          }
          
          // Create a DriverExam object.
          DriverExam exam = new DriverExam(answers);
          
          // Get an array of the missed question numbers.
          missedQuestions = exam.questionsMissed();
          
          // Display a report.
          System.out.println("Correct answers: " + 
    		                   exam.totalCorrect());
          System.out.println("Incorrect answers: " + 
    		                   exam.totalIncorrect());
          
    		// Display whether the student passed or failed.
          if (exam.passed())
             System.out.println("You passed the exam.");
          else
             System.out.println("You failed the exam.");
          
    		// Display a list of missed questions, if any.
          if (missedQuestions != null)
          {
             System.out.println("You missed the following questions:");
             for (int i = 0; i < missedQuestions.length; i++)
                System.out.print(missedQuestions[i] + " ");
          }
       }
       
       /**
        * valid method
        * Returns true if the argument is A, B, C, or D.
        */
       
       public static boolean valid(char c)
       {
          boolean status;
          
          if (c == 'A' || c == 'B' || c == 'C' || c == 'D')
             status = true;
          else
             status = false;
          
          return status;
       }
    }
    Last edited by pbrockway2; 04-24-2012 at 01:18 AM. Reason: code tags added

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

    Default Re: Hello im new to java and i have code

    Hi Exceedinglife, welcome to the forums!

    When you post code, use the "code" tags: you put [code] at the start of the code and [code] at the end. That way the code will be readable when it appears here.

    It is also a good idea to actually ask a question! :) Everyone here is used to code that is rejected by the compiler, or which does something weird (or nothing at all...) when you run it. But we need to know, specifically, what is the problem you are trying to solve.

    If there are specific homework requirements, and you think they would clarify things, state them. (Be aware - as I'm sure you are - that no-one here does other people's homework for them.)

    Does your code compile? If not and you cannot understand the compiler's messages copy and post the entire message you are seeing.

    If the program compile's but does something unwanted at runtime (including "wrong" output or runtime errors) say what the observed behaviour is as well as what you intended the program to do.

    If you are just plain stuck on how to even approach some particular question, make the effort to express what you are trying to do in your own words, so others can comment on it. (The effort is useful for sharpening your own ideas as well as focussing your question for everyone else.)

  3. #3
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    Quote Originally Posted by pbrockway2 View Post
    Hi Exceedinglife, welcome to the forums!

    When you post code, use the "code" tags: you put [code] at the start of the code and [code] at the end. That way the code will be readable when it appears here.

    It is also a good idea to actually ask a question! :) Everyone here is used to code that is rejected by the compiler, or which does something weird (or nothing at all...) when you run it. But we need to know, specifically, what is the problem you are trying to solve.

    If there are specific homework requirements, and you think they would clarify things, state them. (Be aware - as I'm sure you are - that no-one here does other people's homework for them.)

    Does your code compile? If not and you cannot understand the compiler's messages copy and post the entire message you are seeing.

    If the program compile's but does something unwanted at runtime (including "wrong" output or runtime errors) say what the observed behaviour is as well as what you intended the program to do.

    If you are just plain stuck on how to even approach some particular question, make the effort to express what you are trying to do in your own words, so others can comment on it. (The effort is useful for sharpening your own ideas as well as focussing your question for everyone else.)
    hello thank you i never knew how to post code like that or anything im new to these forums as you can tell.. and i dont want the work done for me i wanna learn how to do it so if it were to come up on a test i would know what to do..

    my problem is what you last said im not sure how to approach this im suppose to finish the unfinished methods but im not sure where to start and was wondering if i could get some help

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

    Default Re: Hello im new to java and i have code

    Pick one of the methods and make a list of the steps you think it needs to do to solve its task.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    Starting with this method gradeExam()
    [code] {
    /* Missing code starts here */

    /* Missing code ends here */
    makeMissedArray();
    }

    [code]
    the stock exam with all the correct answers is correct[] which is a char[]
    i need to compare the user data to the correct[] the user data array is student[] which is also a char[]


    i am not sure what to use to compare arrays
    Last edited by Exceedinglife; 04-24-2012 at 01:58 AM.

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

    Default Re: Hello im new to java and i have code

    i need to compare the user data to the correct
    How is the data to be compared? For equality, or if > or < ?
    Are the arrays the same size?
    What results will be reported after the comparison?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    Quote Originally Posted by Norm View Post
    How is the data to be compared? For equality, or if > or < ?
    Are the arrays the same size?
    What results will be reported after the comparison?

    The correct data is private char[] correct = { 'B', 'D', 'A', 'A',
    'C', 'A', 'B', 'A',
    'C', 'D', 'B', 'C',
    'D', 'A', 'D', 'C',
    'C', 'B', 'D', 'A' };

    the user data is a notepad.txt file that is actually testdata.file well actually i think the data is probably to be entered but the answer which is what the program is to display is in this txt file which says:
    Enter your answers to the exam questions. (Make sure Caps Lock is ON!)
    Question 1: A
    Question 2: B
    Question 3: C
    Question 4: D
    Question 5: B
    Question 6: A
    Question 7: C
    Question 8: D
    Question 9: C
    Question 10: A
    Question 11: B
    Question 12: D
    Question 13: D
    Question 14: A
    Question 15: B
    Question 16: C
    Question 17: D
    Question 18: C
    Question 19: B
    Question 20: A
    Correct answers: 7
    Incorrect answers: 13
    You failed the exam.
    You missed the following questions:
    1 2 3 4 5 7 8 10 12 15 17 18 19

    so that is suppose to be the outcome of the data im just currently trying to figure out how to do the gradeExam()
    Both the arrays i believe are the same size im not sure how to check it by looking at the program just your suppose to answer 20 questions and display the results if you pass or not

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

    Default Re: Hello im new to java and i have code

    how to do the gradeExam()
    Is the method supposed to compare the contents of two arrays and report the results?
    What is the order of the contents of the arrays?
    What should the report look like?

    You can have the program tell you the size of an array by printing out the array's length attribute.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    Quote Originally Posted by Norm View Post
    Is the method supposed to compare the contents of two arrays and report the results?
    What is the order of the contents of the arrays?
    What should the report look like?

    You can have the program tell you the size of an array by printing out the array's length attribute.

    Yes it suppose to compare the correct[] and the student[] and report the results and in the main program it says public static void main(String[] args)
    {
    // Constant for the number of answers
    final int NUM_ANSWERS = 20;

    i think that is for the length of the array because there is 20 questions

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

    Default Re: Hello im new to java and i have code

    To look the the contents of an array element by element you would use a for loop.
    To compare an element of one array to an element of another array you would use an if statement.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    Quote Originally Posted by Norm View Post
    To look the the contents of an array element by element you would use a for loop.
    To compare an element of one array to an element of another array you would use an if statement.
    thank you sir,

    ill try and you can tell me how my code looks?

    well actually my loops are not very well...

    i know i will have to do something like this

    there is 2 arrays

    student[]
    correct[]

    i dont know how to write the loop to compare them but in the look it may be like such?

    for(int i=0; i<student.length; i++) "I'm not sure how to enter the arrays into this for loop"
    student[i].equals(correct[i])

    for the if statement

    if (student[].length() == 20)
    {
    if(student[].charAt(0) == correct[].charAt(0))
    if(student[].charAt(1) == correct[].charAt(1))
    }


    is this anywhere close?

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

    Default Re: Hello im new to java and i have code

    Not close.
    For a first attempt on how to use loops and arrays, write a loop that goes for the length of the arrays and prints out the elements from each array on one line with a few spaces between them.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    public class rayPrac {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] test = new int[3];
    test[0] = 3 ;
    test[1] = 9 ;
    test[2] = 4 ;

    for(int i=0; i<test.length; i++)
    System.out.println("The test array contains " +test[0]+ " next number is " +test[1] + " the last one " +test[2]);


    }

    }

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

    Default Re: Hello im new to java and i have code

    You need to use the loop control variable: i to index the arrays.
    I thought that there were two arrays. The exercise was to print one element from each array on one line.
    Your code prints all the elements from one array on one line and does not use the looping.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    Quote Originally Posted by Norm View Post
    You need to use the loop control variable: i to index the arrays.
    I thought that there were two arrays. The exercise was to print one element from each array on one line.
    Your code prints all the elements from one array on one line and does not use the looping.
    im not real sure how to do that i can keep trying

  16. #16
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    [code]

    public class rayPrac {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[] test = new int[3];
    test[0] = 3 ;
    test[1] = 9 ;
    test[2] = 4 ;

    for(int i=0; i<test.length; i++)
    System.out.println("The test array contains " +test[i]);


    }

    }
    [code]

    now i use the loop and it prints out each test[i] im not sure how to do it with 2 arrays tho

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

    Default Re: Hello im new to java and i have code

    That is what you will need to do to be able to use arrays in loops.
    Look at the tutorial:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Also look at the code you posted. There is a for loop with an array element reference in it.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Hello im new to java and i have code

    how to do it with 2 arrays tho
    array1[ix] + " " + array2[ix]
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default Re: Hello im new to java and i have code

    Quote Originally Posted by Norm View Post
    That is what you will need to do to be able to use arrays in loops.
    Look at the tutorial:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Also look at the code you posted. There is a for loop with an array element reference in it.
    ok i get why you posted that to show me that i can use the enhanced for loop to read the data off easily

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Translate Vb.net code into Java code
    By Radu in forum New To Java
    Replies: 5
    Last Post: 04-12-2011, 09:27 AM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 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
  •