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

Thread: Help with Control statements/methods!!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Control statements/methods!!

    Hi, I am having trouble figuring out how to put all of this code together since it combines methods and if else statements. I was wondering if anyone could help me out. Here is the problem:

    Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. In addition to the main method, write the following methods in the program:
    • calcAverage – This method should accept five test scores as arguments and return the average of the scores.
    • determineGrade – This method should accept a test score as an argument and return a letter grade for the score, based on the following grade scale A=100-90 , B=89-80, C=79-70, D=69-60, F= below 60.

    Thanks!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Control statements/methods!!

    Do you have any specific questions about your assignment?

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Control statements/methods!!

    yes, i know how to create the two sub methods but what i am confused about is how to create the main method to pass it to the two sub methods.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Control statements/methods!!

    how to create the main method to pass it to the two sub methods.
    I'm not sure of your terminology. The "pass it to" part is confusing.
    The main method would call the two sub methods and pass them the necessary values as arguments.

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Control statements/methods!!

    I apologize for the confusion. but yes that is what i ment. I am confused on how to construct the main method in order for it to call the two sub methods and pass them the arguments

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Control statements/methods!!

    Assuming you have the arguments: arg1, arg2, ...

    You call a method: methodName(arg1, arg2, ...);

    Replace the ... with more args or remove them.

    Put that code inside of the main method.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Control statements/methods!!

    For more info on how to use methods see the Tutorial. Go to this site and Find methods:
    Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)

  8. #8
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Control statements/methods!!

    and then you write that methodName outside the main method and describe what the method should be calc. right?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Control statements/methods!!

    Yes, you define the methods outside of the main method.
    No method can be defined inside of another method.

  10. #10
    Junior Member
    Join Date
    Apr 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Control statements/methods!!

    I have written my code and almost everything is write except i can not get the determineGrade method to work. the calcAverage method works great but i can not get the other method to work.

    here is my code:

    package review_package;
    import
    javax.swing.JOptionPane;
    public class REVIEW_prog_Gaines_Elmore {

    /**
    * @param args
    */
    public static void main(String[] args) {
    String x=JOptionPane.showInputDialog(
    "Enter test score");
    String q=JOptionPane.showInputDialog(
    "Enter test score");
    String w=JOptionPane.showInputDialog(
    "Enter test score");
    String b=JOptionPane.showInputDialog(
    "Enter test score");
    String z=JOptionPane.showInputDialog(
    "Enter test score");
    int y=Integer.parseInt(x);
    int c=Integer.parseInt(q);
    int d=Integer.parseInt(w);
    int e=Integer.parseInt(b);
    int f=Integer.parseInt(z);

    calcAverage(y,c,d,e,f);
    determineGrade(y,c,d,e,f);

    }
    public static void calcAverage(int g, int h, int i, int l,int m){

    int average=0;
    average=(g+h+i+l+m)/5;
    System.out.println(average);


    }
    public static void determineGrade(int g, int h, int i, int l,int m){
    int grade=0;
    if ((grade > 0)&& (grade<=59))

    System.
    out.println("Grade is an F");

    else if ((grade > 59)&& (grade<=60))

    System.
    out.println("Grade is a D");
    else if ((grade > 69)&& (grade<=79))

    System.
    out.println("Grade is a C");
    else if ((grade > 79)&& (grade<=89))

    System.
    out.println("Grade is a B");
    else if ((grade > 89)&& (grade<=100))

    System.
    out.println("Grade is an A");
    }
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Help with Control statements/methods!!

    The method are supposed to return a value. You use the return statement to do that.

    Reread the specifications for the determineGrade method. Yours is all wrong.

Similar Threads

  1. Using loops and control statements to draw lines
    By john.adam in forum Object Oriented Programming
    Replies: 3
    Last Post: January 19th, 2012, 09:51 AM
  2. [SOLVED] DoublyLinkedList out of control!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 07:06 AM
  3. Applet using .mp3 control in java
    By ychopade in forum Java Applets
    Replies: 0
    Last Post: July 15th, 2010, 07:26 AM
  4. process control
    By ttsdinesh in forum Java Native Interface
    Replies: 6
    Last Post: October 27th, 2009, 06:29 PM
  5. How can i control keyboard through programming?
    By Mohd in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2009, 07:10 AM

Tags for this Thread