Question:

Methods (in java language)

by  |  earlier

0 LIKES UnLike

The question goes like this:

Write a main method that does the following:

i) Declare and initialize an array names with the names of 5 students: "John", "Mary", "Jack", "Amy" and "Ivy".

My answer is:

String [ ] names = {"John", "Mary", "Jack", "Amy", "Ivy"}

ii) Declare and create an array scores for storing the scores of 5 students.

My answer is:

int [ ] scores = int [5]

iii) Use a for loop to call getScore() method and store the returned score of each student into the array scores.

[This part I have no idea.]

iv) Call the countPasses() method, and display the number of students who passed the test.

v) Prompt the user to enter a student's name before displaying the score for that student.

[Assume that John's score is stored in the first element of the score array, Mary's score is in the second element of the score array, and so on]

Those that I did not show my answers are those that I have no idea on how to do. Those that I have shown the answers, please help me check on them. Thanks. But sorry too for asking you check my work >_< I'm currently preparing for my java programming examination. Thanks.

 Tags:

   Report

3 ANSWERS


  1. Since you are preparing for the examination I would hate to give you ready made samples...going right to the source of the language - Java Language Reference Site : http://java.sun.com/docs/books/jls/secon...

    You can start with looking at the syntax and examples on how to declare and code methods in a class, array initializations and how to use local variables to iterate print or other statements

    All the best


  2. I like to post code on another forum which allows you to use code tags to preserve formatting such as indentation which makes the code easier to read.  Also, this forum allows longer lines of code and more characters than Yahoo! Answers does.  So here is my answer, on another forum.

    I encourage people to ask their questions here on Yahoo! Answers where the people are, but to post their code on this other forum:

    http://www.kaydell.com/forums/read.php?8...

  3. I remember gearing up for my Java exam, and It&#039;s been really long since I&#039;ve touched Java, but I&#039;ll try helping you anyway :)

    First of all, I think your syntax in declaring the array is wrong. You need to declare arrays like this:

    String names[ ] = {&quot;John&quot;, &quot;Mary&quot;, &quot;Jack&quot;, &quot;Amy&quot;, &quot;Ivy&quot;}

    Same goes for int or any other data type. Writing it your way would give you a syntax error. And your integer array declaration would be:

    int scores[]= new int [5]

    Anyway, coming to the third question, you need to declare a method called getScore() and invoke it in your main() method to get the scores of the students in names[] and accept integers from the user that will get stored in corresponding columns in scores[], and using a for loop only makes this easier. Eg (and I think i&#039;m doing your work for you here :P)

    for(i=0;i&lt;5;i++)

    {

    System.out.println(&quot;Input score for &quot;+names[i]+&quot;: &quot;);

    scores[i]=sc.nextInt();

    }

    That would say &quot;Input score for Jack:&quot; and accept it and so on. I haven&#039;t declared i and sc as an int and Scanner, but i&#039;m sure you&#039;re familiar with that.

    For part 4 of the question, they ask you to count the number of students who have passed, and unless they&#039;ve already given you a countPasses method, it should be something like this:

    new int count=0

    for(i=0;i&lt;5;i++)

    {

    if(scores[i]&gt;40)

    count++;

    }

    System.out.println(&quot;The number of passes in this class are: &quot;+count);

    count increments for everytime there is a student who has scored above 40 (i&#039;m assuming 40 is the pass mark here), it then prints it at the end.

    Finally, for the fifth question, you need to enter accept user input of a String, compare it to all the names in names[], find the corresponding scores[] column, and print the output.

    System.out.println(&quot;Enter the name of the student you wish to view scores for&quot;);

    s=sc.nextString();

    for(i=0;i&lt;5;i++)

    {

    if(s==names[i])

    System.out.println(&quot;The score for &quot;+names[i]+&quot; is &quot;+scores[i]);

    else

    System.out.println(&quot;Name not found in array&quot;);

    }

    Again keep in mind that sc is a Scanner and s is a preinitialised String

    There, that should be it. Email me if you need more help..

    Hope that helps!

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.