I need help making a program that will take the int input of the user as the number of students. At the moment I have to manually add the students in the code if I want more student. ive added my other class aswell. please help if possible.
import java.util.Scanner;
// the name of our class its public
public class ClassArray {
//void main
public static void main (String[] args){
//declare class
Student[] s = new Student[2];
s[0] = new Student();
s[1] = new Student();
//call functions
s[0].getdata();
s[1].getdata();
s[0].finalmark();
s[1].finalmark();
s[0].finalgrade();
s[1].finalgrade();
System.out.printf("Name\tDefinitive\tLetter\tTest 1\tTest 2\tAssignments\tFinalExam \n");
s[0].print();
s[1].print();
}
}
}
//declare class
public static class Student {
//declare variables.
private Double finalmark;
private int test1,test2,assignments1,finalexam;
private String studentname,finalgrade;
//functions should be public if needed to access from other class
public void getdata()
{
//print message to enter numbers
Scanner input = new Scanner(System.in);
System.out.println("Enter name of student:");
studentname = input.next();
while (!studentname.matches("[a-zA-Z]+")) { // Checks to see if only letters are used in the name
System.out.println("Please re-enter your name, use alphabets only");
studentname = input.nextLine(); // if anything other than letters are used, the user must re-enter his/her name using letters
}
System.out.println("Enter mark test 1 for student:");
test1 = input.nextInt();
while (test1 > 100 || test1 < 0){
System.out.println("Please enter a double value between 0 and 100");
while(!input.hasNextInt()){
input.next();
}
test1 = input.nextInt();
}
System.out.println("Enter mark test 2 for student:");
test2 = input.nextInt();
while (test2 > 100 || test2 < 0){
System.out.println("Please enter a double value between 0 and 100");
while(!input.hasNextInt()){
input.next() ;
}
test2 = input.nextInt();
}
System.out.println("Enter mark assignments for student:");
assignments1 = input.nextInt();
while (assignments1 > 100 || assignments1 < 0){
System.out.println("Please enter a double value between 0 and 100");
while(!input.hasNextInt()){
input.next() ;
}
assignments1 = input.nextInt();
}
System.out.println("Enter mark final exam for student:");
finalexam = input.nextInt();
while ( finalexam > 100 || finalexam < 0){
System.out.println("Please enter a double value between 0 and 100");
while(!input.hasNextInt()){
input.next() ;
}
finalexam = input.nextInt();
}
}
public void finalmark(){
finalmark = (test1 * 0.15) + (test2 * 0.25) + (assignments1 * 0.25) + (finalexam *
0.35);
}
public void finalgrade()
{
if(finalmark >= 100)
finalgrade="A+";
else if(finalmark >= 90)
finalgrade="A+";
else if(finalmark >= 80)
finalgrade="A";
else if(finalmark >= 75)
finalgrade="B+";
else if(finalmark >= 70)
finalgrade="B";
else if(finalmark >= 65)
finalgrade="C+";
else if(finalmark >= 60)
finalgrade="C";
else if(finalmark >= 50)
finalgrade="D";
else
finalgrade="F";
}
public void print(){
System.out.printf("%s\t%.2f\t%s\t%d\t%d\t%d\t\t%d\n", studentname, finalmark,
finalgrade, test1, test2, assignments1, finalexam);
}
}