/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
import Exceptions.*;
public class Main
{
public static void main(String[] args) {
Classroom classroom = new Classroom("a", 4);
int[] grades = {45, 87, 96};
try{
classroom.setGrades(grades);
System.out.println(classroom.toString());
printIntArr(classroom.getGrades());
System.out.println(classroom.getGrade(4));
}
catch (TooManyGradesException | GradeOutOfBoundException | GohstStudentException | NoGradesException e){
System.out.println(e.getMessage());
}
}
public static void printIntArr(int[] arr){
for (int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
}
}
import Exceptions.*;
public class Classroom{
private String teacher;
private int[] grades;
private int numOfGrades = 0;
public Classroom(String teacher, int numOfStudent){
this.teacher = teacher;
this.grades = new int[numOfStudent];
}
public String getTeacher(){
return this.teacher;
}
public int[] getGrades(){
return this.grades;
}
public void setGrades(int[] grades) throws TooManyGradesException, GradeOutOfBoundException{
if (grades.length > this.grades.length - numOfGrades){
throw new TooManyGradesException();
}
for (int i = numOfGrades; i < grades.length; i++, numOfGrades++){
if (grades[i] < 0 || grades[i] > 100){
throw new GradeOutOfBoundException();
}
this.grades[i] = grades[i];
while (i >= grades.length && this.grades.length > i){
this.grades[i] = 0;
i++;
}
}
}
public int getGrade(int i) throws GohstStudentException, NoGradesException{
if (i > this.grades.length){
throw new GohstStudentException();
}
if (this.grades[i] == 0){
throw new NoGradesException();
}
return grades[i];
}
@Override
public String toString(){
return "teacher: " + teacher + " have: " + grades.length + " with grades: " + Arrays.toString(grades);
}
}
package Exceptions;
public class TooManyGradesException extends Exception{
public TooManyGradesException(){
super("to many grades");
}
}
package Exceptions;
public class GradeOutOfBoundException extends Exception{
public GradeOutOfBoundException(){
super("the grade in invalid");
}
}
package Exceptions;
public class GohstStudentException extends Exception{
public GohstStudentException(){
super("student doesn't exist");
}
}
package Exceptions;
public class NoGradesException extends Exception{
public NoGradesException(){
throw new Exception("this student doesn't have a grade");
}
}