Here's the program to solve the problem as described:
Source Code..
package oop.solutions;
//@author Java_Programs
import java.util.Scanner;
public class 2D_Array
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Grade g = new Grade();
g.inputMarks();
g.calculate_Percentage();
g.printMarks();
g.printGrade();
}
}
//Taking another class for calculation(for good practice)
class Grade
{
Scanner sc = new Scanner(System.in);
int total_studs = 5, total_courses = 5;
float [][] marks = new float[total_studs][total_courses];
float [] obtainedMarks = new float[total_studs];
float [] percentage = new float[total_studs];
public void inputMarks()
{
for(int row=0; row<total_studs; row++)
{
System.out.println("\nEnter marks for student "+(row+1)+": ");
for(int col=0; col<total_courses; col++)
{
System.out.println("Enter marks for course "+(col+1)+": ");
marks[row][col] = sc.nextFloat();
obtainedMarks[row]= obtainedMarks[row]+marks[row][col];
}
}
}
public void calculate_Percentage()
{
for(int row=0; row<total_studs; row++)
{
obtainedMarks[row]=0;
for(int col=0; col<total_courses; col++)
{
obtainedMarks[row]= obtainedMarks[row]+marks[row][col];
percentage[row] = (( obtainedMarks[row]/(100*total_courses) )*100);
}
}
}
public void printMarks()
{
for(int row=0; row<total_studs; row++)
{
System.out.println("\nStudent "+(row+1)+" marks: ");
for(int col=0; col<total_courses; col++)
{
System.out.println("Marks of course "+(col+1)+": " +marks[row][col]);
}
System.out.println("Total Obtained Marks: "+obtainedMarks[row]);
System.out.println("Percentage: "+percentage[row]);
}
}
public void printGrade()
{
for(int row=0; row<total_studs; row++)
{
if(percentage[row]>=80)
{
System.out.println("\nStudent "+(row+1)+" Grade: A+");
}
else if(percentage[row]>=70 && percentage[row]<=79)
{
System.out.println("\nStudent "+(row+1)+" Grade: A");
}
else if(percentage[row]>=60 && percentage[row]<=69)
{
System.out.println("\nStudent "+(row+1)+" Grade: B");
}
else if(percentage[row]>=50 && percentage[row]<=59)
{
System.out.println("\nStudent "+(row+1)+" Grade: C");
}
else
{
System.out.println("\nStudent "+(row+1)+" Grade: F");
}
}
}
}
The given code is a Java program that takes the marks of
five students in five courses using a 2D array and calculates their percentage
and grade.
The program uses a Scanner class to take input from the user
for the marks of each student in each course. Then, it calculates the total
obtained marks and percentage for each student using a for loop and stores them
in an array. Finally, it prints the marks, total obtained marks, percentage,
and grade for each student using another for loop.
The Grade class is used to encapsulate the data and the
methods related to the calculation and printing of the student grades. The main
method creates an object of the Grade class and calls its inputMarks,
calculate_Percentage, printMarks, and printGrade methods to perform the
necessary tasks.
This program demonstrates the use of 2D arrays and for loops
to perform calculations and print data. It also showcases the importance of
encapsulation and the use of classes for better organization and readability of
the code.
In conclusion, this Java program is an example of how to use
arrays, loops, and classes to calculate and print student grades based on their
marks
No comments:
Post a Comment