How to use array to record the student profiles ?
How to Create a Student Class?
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
package com.company;
//Let us define a class called Student
class Student{
public static void main(String arg[])
{
//declare and initialize an array with student names
String student_names[] = { "Kelly Daniel", "Fred Syoks", "Eve Wambo", "Morreen Mutua", "Flex Asava" };
//declare and initializa an array with marks
int student_marks[] = { 45, 78, 83, 58, 93 };
//Now let us print the students details and the grade
int array_length = student_marks.length;
System.out.println("\n");
for(int i = 0; i < array_length; i++)
{
if(student_marks[i]>=70 && student_marks[i]<=100)
{
System.out.println(student_names[i]+ " has "+student_marks[i]+"marks and the grade is A");
}
else if(student_marks[i]>=60)
{
System.out.println(student_names[i]+ " has "+student_marks[i]+" marks and the grade is B");
}
else if(student_marks[i]>=50)
{
System.out.println(student_names[i]+ " has "+student_marks[i]+" marksmarks and the grade is C");
}
else if(student_marks[i]>=40)
{
System.out.println(student_names[i]+ " has "+student_marks[i]+" marks and the grade is D");
}
else if(student_marks[i]>=0)
{
System.out.println(student_names[i]+ " has "+student_marks[i]+" marks and the grade is F");
}
else
{
System.out.println("Invalid marks for student"+student_names[i]);
}
}
}
}
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment