Java methods??
Results 1 to 6 of 6

Thread: Java methods??

  1. #1
    Banned
    Join Date
    Jun 2004
    Posts
    154

    Java methods??

    ok in my java programming class i am supposed to do this:

    CA 141 Lab 5
    Write a program which will manage test grades for a class of up to 30 students. The description below will describe each function in the program in turn.

    main will continously ask the user for a menu selection until the user choses the exit menu choice. Each selection below has its action defined:

    1. Input Grades calls get_grades function
    2. List Grades calls list_grades function
    3. Change Grade calls change_grade function
    4. Convert Grades calls convert_grades function
    5. Exit exits the program with a message

    The function prototypes for these functions are:

    int get_grades(int a[ ]);
    void list_grades(int a[ ], int end);
    void change_grade(int a[ ], int end);
    void convert_grades(int a[ ], int end);

    The main function will also define the array of integers to hold grades as well as an integer that indicates the number of valid array elements.

    get_grades will prompt the user for up to 30 grades, but allow the user to stop at any time by entering any negative number. Allowable values will be 0 - 100. The function returns the number of valid grades in the array. And if a negative number is entered to stop the entry process, it will not be considered a valid number

    list_grades will list the valid grades in the array passed to it using the integer end to list only valid grades. The two functions following also use end in the same manner

    change_grades will call list_grades and then ask the user which grade should be changed. The user will input the new grade and it will be placed in the appropriate place in the array. convert_grades will list each valid grade and its equilivant letter grade, one set per line. A switch statement must be used to do the number to letter conversion.

    A script will look like this:

    Welcome to grades

    1 - Input Grades
    2 - List Grades
    3 - Change Grades
    4 - Convert Grades
    5 - Exit

    Enter Choice 1

    Input Grades - any negative number will end input

    Input Element 0: 78
    Input Element 1: 82
    Input Element 2: 100
    Input Element 3: -4

    1 - Input Grades
    2 - List Grades
    3 - Change Grades
    4 - Convert Grades
    5 - Exit

    Enter Choice 2

    Element 0: 78
    Element 1: 82
    Element 2: 100


    1 - Input Grades
    2 - List Grades
    3 - Change Grades
    4 - Convert Grades
    5 - Exit

    Enter Choice 3

    Element 0: 78
    Element 1: 82
    Element 2: 100

    Enter element number: 1

    Enter new value for element 1: 88

    1 - Input Grades
    2 - List Grades
    3 - Change Grades
    4 - Convert Grades
    5 - Exit

    Enter Choice 4

    The letter grades are:
    78 C
    88 B
    100 A

    1 - Input Grades
    2 - List Grades
    3 - Change Grades
    4 - Convert Grades
    5 - Exit

    Enter Choice 5

    goodbye
    This is what i have so far on my program:

    Code:
    import java.io.*;
    
    public class lab5_141
    {
       public static void main (String [] args)
       {
            int choice;
            final int max = 30;
            int a[] = new int[max];
            int end = 0;
    
            do
            {       System.out.println("1. Input Grades");
                    System.out.println("2. List Grades");
                    System.out.println("3. Change Grades");
                    System.out.println("4. Convert Grades"); 
                    System.out.println("5. Exit");
                    System.out.println();
                    System.out.print("Enter your choice ");
                    choice = MyInput.readInt();
                    System.out.println(); 
                            
                    switch(choice)
                    {
                    
                            case 1: end = get_grades(a, max);
                                    break;
                            case 2: list_grades(a, end);
                                    break;
                            case 3: change_grades(a, end);
                                    break;
                            case 4: convert_grades(a, end);
    
                                    break;
                            case 5: break;
                    //              default:  System.out.println("invalid");
                    }
                }while(choice !=5);
                {
                System.out.println("Goodbye");
                }
       
       }
    
       static void list_grades(int a[], int max)
       {
          System.out.print("\n");
          for(int i=0; i < max; i++)
                    System.out.println(a[i]);
            System.out.println();
       }
    
       static void change_grades(int a[], int max)
       {
            int i;
            System.out.print("Enter which element to change: ");
            int s = MyInput.readInt();
    
            for(i=0; i < max; i++)
             if (s == a[i])
                    break;
    
             if (i == max)
               System.out.println(" no match");
    
             else
            {
               System.out.println("match on element #" + i);
               System.out.print("Enter new value for this element: ");
               a[i] = MyInput.readInt();
    
            }
            list_grades(a, max);
              
    // get score to change
    // loop to find element number
    // if you can find it, ask for new value
    // replace
    // for
       }
             
       static int get_grades(int a[], int max)
       {
                      
             
            System.out.println("Enter Grades (1 to 100), negative to end: ");
            int i;
              for(i=0;i < max;i++)
              {
    
               System.out.print("Element " + i +" ");
               a[i] = MyInput.readInt();
              if ((a[i] < 0) || (a[i] > 100))
                    break;
              }
            return i;
    
    }
       static void convert_grades(int a[], int max)
            {
            int i;
                    for(i=0; i< max; i++)
                      System.out.print(a[i] + "  ");
             
            a[i] /=10;
            
            switch(a[i])
                  {
    
                    case 10: System.out.print("Perfect A");
                                       break;
                    case 9:  System.out.print("A");
                                       break;
                    case 8:  System.out.print("B");
                                       break;
                    case 7:   System.out.print("C");
                                       break;
                    case 6:   System.out.print("D");
                                       break;
                    case 5:   System.out.print("F");
                                       break;             
                  } 
    
                                 
            // print element number, score, with print
            // integer divide score by 10
            // switch on the divided value
            // case 10   - perfect
            //case 9 90-99 println("A");
           // case 8  90-89
                                    
            }       
                    
                                    
            
     }
    The problem is when i run it everything works except option 4 (convert_grades). I will put numbers in for the grade, list them, and when i try to convert all it does is relist the numbers again. It seems like it isnt going through the lat switch statement at all. I have tried to ask my professor about this many times but he doesnt explain anything i can understand so i was hoping you guys could help me out.
    Thanks

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    It's cause the switch isn't in the for loop, you forgot to add { } around what's supposed ot be in your for loop.
    PHP Code:
    static void convert_grades(int a[], int max)
    {
       
    int i;
       for(
    i=0i&ltmaxi++)
       {
          
    System.out.print(a[i] + "  ");
             
          
    a[i] /=10;
            
          switch(
    a[i])
          {

                    case 
    10System.out.print("Perfect A");
                                       break;
                    case 
    9:  System.out.print("A");
                                       break;
                    case 
    8:  System.out.print("B");
                                       break;
                    case 
    7:   System.out.print("C");
                                       break;
                    case 
    6:   System.out.print("D");
                                       break;
                    case 
    5:   System.out.print("F");
                                       break;             
           } 
       }

                                 
            
    // print element number, score, with print
            // integer divide score by 10
            // switch on the divided value
            // case 10   - perfect
            //case 9 90-99 println("A");
           // case 8  90-89
                                    
     


  3. #3
    Banned
    Join Date
    Jun 2004
    Posts
    154
    ah ok ill try that

  4. #4
    Banned
    Join Date
    Jun 2004
    Posts
    154
    it works, but if i enter the numbers 34, 54, 77. It just gives the letter grade for 77.

  5. #5
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    It worked ok for me, but I had to make my own input stuff cause I didn't have MyInput like you.
    Code:
    C:\Documents and Settings\skiddieleet\Desktop>java blah
    1. Input Grades
    2. List Grades
    3. Change Grades
    4. Convert Grades
    5. Exit
    
    Enter your choice 1
    
    Enter Grades (1 to 100), negative to end:
    Element 0 100
    Element 1 50
    Element 2 75
    Element 3 88
    Element 4 54
    Element 5 -4
    1. Input Grades
    2. List Grades
    3. Change Grades
    4. Convert Grades
    5. Exit
    
    Enter your choice 2
    
    
    100
    50
    75
    88
    54
    
    1. Input Grades
    2. List Grades
    3. Change Grades
    4. Convert Grades
    5. Exit
    
    Enter your choice 3
    
    Enter which element to change: 1
     no match
    
    100
    50
    75
    88
    54
    
    1. Input Grades
    2. List Grades
    3. Change Grades
    4. Convert Grades
    5. Exit
    
    Enter your choice 3
    
    Enter which element to change: 50
    match on element #1
    Enter new value for this element: 55
    
    100
    55
    75
    88
    54
    
    1. Input Grades
    2. List Grades
    3. Change Grades
    4. Convert Grades
    5. Exit
    
    Enter your choice 4
    
    100  Perfect A55  F75  C88  B54  F1. Input Grades

  6. #6
    Banned
    Join Date
    Jun 2004
    Posts
    154
    i figured it out, i didnt have my {'s lined up. Thanks for everthing, im glad it was something simple.

Posting Permissions

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