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

Thread: Array with Switch Construct

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Array with Switch Construct

    I am currently working on a final project for my Intro to Java course, and I am having a problem getting started. The main point of the program is to create a char array with grade letters (A, B, C, D, and F). I'm trying to make it so that when I put in a number (eg. 96) it gives me a letter grade from my array. I have no problem doing it this way...
    public class Grades {
     
    private char [ ] letterGrade = {'A', 'B', 'C', 'D', 'F'}; // Char array
     
    public void displayGrade(int percent) { // Overloaded Display method to be manipulated in test class
       if(percent <=100 && percent >=90) {
          System.out.println(letterGrade[0]);
          }
       }
    .......

    And so on...

    The problem is, the assignment calls for use of a switch construct. How can I use the array within the switch construct while still being able to input a percent in my test class and have the letter grade output?

    Last edited by helloworld922; February 6th, 2011 at 12:09 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array with Switch Construct

    Are you sure you're reading the assignment correctly? If-else is definitely the way to go here. If you are indeed required to write using a switch statement, you have a lot of copy-pasting in your future

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Array with Switch Construct


    That's what I was thinking. The assignment instructions are as follows...

    /* 1.Creates a grading program based on the Baker College grading standard. You will need to look up the current grade standard.
    You may use only the letter grades without the +/- signs.
    A = 100-90
    B = 89-80
    C = 79-70
    D = 69-60
    F = 59-0

    2. Uses a char array to hold the letter grades.

    3. Creates a Grade class with private grade attributes and two class methods to manipulate grades.
    One method will raise their grade point total by 10 points and one method will lower their grade by 15 points.
    Create 2 instances, ‘Bill’ and ‘Susan’ of the grade class.

    4. Creates appropriate display methods for the Grade class.

    5. Your program will first print out the Baker grade standard and Bill and Susan’s current letter grade (your choice).
    The program will then call the methods that will increase and decrease both bill and susan’s point grades and then print their
    corresponding letter grades. Please notify me in your output, that is, you may want to say:
    “Bill failed his math final and his final grade dropped to x-points and a letter grade of C.”

    6. You must use a Switch construct.

    Your output should be as follows:

    A. Print the entire Baker College grade standard

    B. Bill and Susan’s current grade point total and equivalent letter grades

    C. Bill and Susan’s current grade point total and equivalent letter grades after raising the grade by 10 points.

    D. Bill and Susan’s current grade point total and equivalent letter grades after lowering the grade by 15 points. */


    So at some point I have to use a switch construct. I just don't see any point in this program in which it would be useful, necessary, or even possible...











  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Talking Re: Array with Switch Construct


    I got it figured out! Here's the finished product...

    package seminarsixcode;
     
    public class Grades { // Start of class
     
       private char [ ] letterGrade = {'A', 'B', 'C', 'D', 'F'}; // Character array holding the 5 letter grades
       private int numberGrade;
       private char grade;
     
       public void setGrade(int percent) {          // Start of setGrade method
           if(percent <=100 && percent >=90) { // Start of if statement for grade A
               numberGrade = percent;
               grade = (char)letterGrade[0];
           }                                                            // End of if statement for grade A
           if(percent <=89 && percent >=80) { // Start of if statement for grade B
               numberGrade = percent;
               grade = (char)letterGrade[1];
           }                                                            // End of if statement for grade B
           if(percent <=79 && percent >=70) { // Start of if statement for grade C
               numberGrade = percent;
               grade = (char)letterGrade[2];
           }                                                            // End of if statement for grade C
           if(percent <=69 && percent >=60) { // Start of if statement for grade D
               numberGrade = percent;
               grade = (char)letterGrade[3];
           }                                                          // End of if statement for grade D
           if(percent <=59 && percent >=0) { // Start of if statement for grade F
               numberGrade = percent;
               grade = (char)letterGrade[4];
           }                                                         // End of if statement for grade F
       }                                                             // End of setGrade method
     
       public char getLetterGrade() { // Start of getLetterGrade method
           return grade;
       }                                                 // End of getLetterGrade method
     
       public int getNumberGrade() { // Start of getNumberGrade method
           return numberGrade;
       }                                                  // End of getNumberGrade method
     
       public void raiseGrade() { // Start of raiseGrade method
           numberGrade = numberGrade + 10; // Adds 10 to the numberGrade variable
           setGrade(numberGrade); // Returns to setGrade method with new value for numberGrade
       }                                         // End of raiseGrade method
     
       public void lowerGrade() { // Start of lowerGrade method
           numberGrade = numberGrade - 15; // Subtracts 15 from the numberGrade variable
           setGrade(numberGrade); // Returns to setGrade method with new value for numberGrade
       }                                           // End of lowerGrade method
     
       public void displayAllGrades() { // Start of displayAllGrades method
           System.out.println("100 - 90 = " + letterGrade[0]);
           System.out.println("89 - 80 = " + letterGrade[1]);
           System.out.println("79 - 70 = " + letterGrade[2]);
           System.out.println("69 - 60 = " + letterGrade[3]);
           System.out.println("59 - 0 = " + letterGrade[4]);
       }                                                    // End of displayAllGrades method
     
       public void students (char student) { // Start of students method
           switch(student) { // Start of switch construct
               case 'D': // D for default.  Prints the Baker College Grading Rubric
                   System.out.println("Baker College Grading Rubric:\n");
                   displayAllGrades();
                   break;
               case 'B': // B for Bill
                   setGrade(89); // Bills starting points
                   numberGrade = getNumberGrade();
                   grade = getLetterGrade();
                   System.out.println("\nBill started with " + numberGrade + " points and a grade of " + grade + " in his Java class.");
                   raiseGrade(); // 10 point increase
                   numberGrade = getNumberGrade();
                   grade = getLetterGrade();
                   System.out.println("Bill passed his quiz and his points rose to " + numberGrade + " points and a grade of " + grade + " in his Java class.");
                   lowerGrade(); // 15 point decrease
                   numberGrade = getNumberGrade();
                   grade = getLetterGrade();
                   System.out.println("Bill did not do so well on his coding project and his points dropped to " + numberGrade + " points and a final grade of " + grade + " in his Java class.");
                   break;
               case 'S': // S for Susan
                    setGrade(92); // Susans starting points
                   numberGrade = getNumberGrade();
                   grade = getLetterGrade();
                   System.out.println("\nSusan started with " + numberGrade + " points and a grade of " + grade + " in her Java class.");
                   lowerGrade(); // 15 point decrease
                   numberGrade = getNumberGrade();
                   grade = getLetterGrade();
                   System.out.println("Susan failed her quiz and her points dropped to " + numberGrade + " points and a grade of " + grade + " in her Java class.");
                   raiseGrade(); // 10 point increase
                   numberGrade = getNumberGrade();
                   grade = getLetterGrade();
                   System.out.println("Susan did very well on her coding project and her points have risen to " + numberGrade + " points and a final grade of " + grade + " in her Java class.\n");
                   break;
           } // End of switch construct
       } // End of students method
     
    } // End of class

    Last edited by WhiteSoup12; February 7th, 2011 at 01:50 PM.

  5. #5
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Array with Switch Construct

    Good Job WhiteSoup12..


    "Imagination is more important than knowledge.
    Knowledge is limited. Imagination encircles the world
    "
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  2. Construct a class that implement ActionListener with no constructor
    By striko_514 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2010, 03:15 PM
  3. Why Did You switch to Java?
    By Sisyphus in forum The Cafe
    Replies: 2
    Last Post: May 26th, 2010, 04:01 AM
  4. Switch Statment
    By 3XiLED in forum Java Theory & Questions
    Replies: 2
    Last Post: March 31st, 2010, 05:11 PM
  5. switch with Enums
    By chronoz13 in forum Loops & Control Statements
    Replies: 17
    Last Post: October 8th, 2009, 08:08 PM