Answer to Question #271741 in Java | JSP | JSF for jade

Question #271741
  •  Create a public class Student that has: o three private attributes:
  • name (String)
  •  an array of grades (array of integers).
  • finalGrade (float) which contains the average of the grades

entered in the array

  • The following public methods:
  •  A constructor allowing only to initialize the name of the student.
  •  A setter for the attribute name
  •  A method void addGrade(int nbr) in which nbr is the size
  • of the array. The method should ask the user to enter the grades and then computes the average (finalGrade) without displaying it.
  •  A method String toString() that returns the a String in the following format:
  • Name of the student: name Final grade: finalGrade
  •  Create a test class Test in which you:
  •  Create an instance of the class Student
  •  Change the name of your Student instance o Invoke the addGrade(int nbr) method
  •  Invoke the toString() method.
1
Expert's answer
2021-11-26T17:43:11-0500
import java.util.Scanner;

public class Student {
    private String name;
    private int[] grades;
    private float finalGrade;

    public Student(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addGrade(int nbr) {
        Scanner in = new Scanner(System.in);
        grades = new int[nbr];
        finalGrade = 0;
        for (int i = 0; i < nbr; i++) {
            System.out.print("Enter a grade: ");
            grades[i] = in.nextInt();
            finalGrade += grades[i];
        }
        finalGrade /= grades.length;
    }

    @Override
    public String toString() {
        return "Name of the student: " + name + " Final grade: " + finalGrade;
    }
}


public class Test {
    public static void main(String[] args) {
        Student student = new Student("Mark");
        student.setName("Tom");
        student.addGrade(3);
        System.out.println(student);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS