Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Microsoft Visual Studio Coding Requirements: Create a program to allow a user to

ID: 3787537 • Letter: M

Question

Microsoft Visual Studio

Coding Requirements: Create a program to allow a user to create and use student grades and display the associated student IDs, names and grades (number and letter) with the following conditions:

• Form with appropriate controls (i.e. title, buttons, labels, textboxes, listbox, etc. – as needed)

• Allow the user to add a new student (studentID, name, grade) to the program using a structured array

o Perform data validation for studentID, name and grade

• Use a two-dimensional array to convert the number grade for the student to a letter grade (see grading criteria below)

• Allow the user to display the studentID, name, number grade and letter grade

Program Requirements:

• Documentation – Program Name and Description, Author, Date, Comments

• Completeness – Runs without errors

• Creativity – logical design; uniqueness

Grading Criteria:

90-100 A

80-89 B

70-79 C

65-69 D

<65 F

Explanation / Answer

Answer:A student’s final grade is determined from the scores obtained in the class exams. Each student in the class may be given a different number of exams as determined by the instructor. The final grade is determined by the average of the scores in all the exams given. For each student, the program will input student id, name, the number of exams given and scores in each of the exams. The scores in each of the exams will vary from 0 to 100. Each exam will carry the same weight. The final grade will be computed by taking the average of the scores in all the exams given

Student class

Create a class Student that provides the following:

·       Private instance variables for holding student id (int), name (String), exam scores (an int array).

·       A public constructor with parameters for initializing student id (int), name (String) and exam scores. The exam scores are passed as an array of int.

·       A public method for returning the final grade as a String ( “A”, “B” etc).

·       Accessor (getter) methods for getting student id and name.

TestStudent class

Write a class TestStudent containing the main method.

Method main

The method main will do the following:

·       Prompt the user to enter the total number of students (say n).

·       Create an array of n references (for holding n Student object references). (The Student objects will be created in the next step).

·       Create n Student objects. Do this by setting up an n count loop. In each pass through the loop, do the following:

a.     Ask the user to enter data for one student in a single dialog box.

b.     Separate data elements using a StringTokenizer object.

c.     Create a Student object initialized with data provided by the user and store its reference in the appropriate element of the array of student references created above.

Display the student results by grade type. First display all students with grade A, then all students with grade B etc.

/The sample code below use method II discussed above for doing the output

public class Student

{

  private int id;

  private String name;

  private int [] exams;

  public Student(int id, String n, int [] ex)

  {

    //Below id refers to local variable.

    //this.id refers to the instance variable.

    this.id = id;

    name = n;

    //create exams array of same length as ex array

    exams = new int [ex.length];

    //copy contents from array ex to exams

    System.arraycopy(ex, 0, exams, 0, ex.length);

  }

  public String findGrade()

  {

    String grade;

    //enter code here for finding the final grade.

   

    return grade;

  }

  public int getId ( )

  {

     return id;

  }

  public String getName ( )

  {

     return name;

  }

}

public class TestStudent

{

  public static void main(String[] args)

  {

    String in, name, token;

    int nStudents, id, nExams;

    int [] scores;

    in = JOptionPane.showInputDialog("Enter number of students");

    nStudents = Integer.parseInt(in);

    // Create an array of nStudents references

    Student [] st = new Student [nStudents];

    // Create nStudents objects

    for (int i = 0; i < st.length; i++)

    {

      // Input one student data

      in = JOptionPane.showInputDialog("Enter one student data");

      // Tokenize student data using StringTokenizer

      // Create Student object

      st [i] = new Student (id, name, scores);

    }

    // Find student grades

    // Create an array out of 5 string references

    String [] out = new String [5];

    //Create 5 String objects. Initialize each with “”

    //and store their references in the above array of references.

    for (int i=0; i<out.length; i++)

          out [i] = new String ( “” ); //alternate form: out [i] = “”;

    //The above two parts can be done with a single statement as below:

    //String [] out = new String [ ] {“”, “”, “”, “”, “”};

    //find student grades and accumulate output for each type of student.

    String grade;

    for (int i = 0; i < st.length; i++)

    {

      grade = st[i].findGrade();

      if (grade.equalsIgnoreCase("A"))

      {

         //accumulate output in out[0] for A students.

      }

      else if (grade.equalsIgnoreCase("B"))

      {

        //accumulate output in out[1] for B students.

      }

      else if (grade.equalsIgnoreCase("C"))

      {

        //accumulate output in out[2] for C students.

      }

      else if (grade.equalsIgnoreCase("D"))

      {

        //accumulate output in out[3] for D students.

     

      }

      else

      {

        //accumulate output in out[4]  for F students.

   

      }

    }

    // Call diaplayResults to display grades

    displayResults (out);

  }

  public static void displayResults(String[] s)

  {

    //Create a String outAll initialized with an empty string.

    String outAll = "";

    //Accumulate elements of the received String array s into String outAll.

    for (int i = 0; i < s.length; i++)

    {

      outAll = outAll + s[i];

    }

    JOptionPane.showMessageDialog(null, outAll);

  }

}

Creating An Array Of String Objects

Method 1: Create Reference Array & Objects Separately

//create an array of 3 String references

String [ ] s = new String [3];

//Create 3 String Objects.

//Three String objects contain: “Hi1”, “Hi2”, and  “Hi3”

for (int i=0; i<3; i++)

          s [i] = new String ( “Hi”+i );

Method 2: Create Reference Array & Objects Separately

//create an array of 3 String references

String [ ] s = new String [3];

//Create 3 String Objects

//Three String objects contain: “Hi1”, “Hi2”, and “Hi3” .

for (int i=0; i<53; i++)

          s [i] = “Hi” + i ;