Q:

Write a class “Student” that has Private instance variables, Constructor and methods

0

Write a class “Student” that has the following:

  1. Private instance variables - “ID” of type integer. - “name” of type String. - “numCourses” of type integer. - “scores” an array of integers with the size “numCourses”.
  2. Constructor that initializes student ID, name, and the number of courses.
  3. Void method “fillScores” that asks the user to enter the student scores and store them in the “scores” array. This method should handle the InputMismatchException.
  4. Integer method “getOneScore” takes an index of the student score and returns its value. This method should handle the ArrayIndexOutOfBoundsException.
  5. Void method that prints all information of the student. In the deriver class, define an object of the Student class and test its functionalities. The program output may be as the following:

 

output example:

--------------

Enter score

90

Enter score

80

Enter score 

70

Enter score

60

Enter score

50

Enter index to get its score

3

60

Student Information

ID: 123

Name: Ahmed

Number of courses: 5    90    80    70    60    50

 

=================

Enter score

90

Enter score

80

Enter score

70

Enter score

test

java.util.InputMismatchException

Enter score

60

Enter index to get its score

999

java.lang.ArrayIndexOutOfBoundsException: 999

-1

Student Information

ID: 123

Name: Ahmed

Number of courses: 5    90    80    70    0    6

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.InputMismatchException;
import java.util.Scanner;

public class JavaApplication120 {

    public static void main(String[] args) {
        Student s1=new Student(123,"mohammad",4);
        s1.fillScores();
        s1.printInfo();

    }
    
}

class Student
{
private int ID;
private String name;
private int numCourses;
private int[] scores;
public Student(int ID, String name, int numCourses)
{
    this.ID=ID;
    this.name=name;
    this.numCourses=numCourses;
    scores=new int[numCourses];
}
public void fillScores()
{
    Scanner input=new Scanner(System.in);
    System.out.println("enter the student's scores:");
    for(int i=0;i<numCourses;i++)
    {
        try{
        scores[i]=input.nextInt();
        }
        catch(InputMismatchException ex)
        {
            System.out.println(ex.toString());
        }
    }
    System.out.println("finished.");
}
public int getOneScore(int scoreIndex)
{
    try{
    return scores[scoreIndex];
    }
    catch(ArrayIndexOutOfBoundsException ex)
    {
        System.out.println("element not found!");
        return -1;
    }
}
public void printInfo()
{
    System.out.println("ID: "+ID+"\nStudent Name: "+name+"\nNumber of courses: "+numCourses+"\n===========\nScores:\n--------\n");
    for(int i=0;i<numCourses;i++)
    {
        System.out.println("score"+(i+1)+" : "+scores[i]);
    }
}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now