Java Code and Line-by-Line Explanations
1. Find Total Marks
Java Code:
import [Link].*;
class Student {
int studentId;
String studentName;
float studentMarks;
public Student(int studentId, String studentName, float studentMarks) {
[Link] = studentId;
[Link] = studentName;
[Link] = studentMarks;
}
}
public class Solution {
public static float findTotalStudentMarks(Student[] students) {
float total = 0;
for (Student s : students) total += [Link];
return total;
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner([Link]);
int n = [Link]([Link]());
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
int id = [Link]([Link]());
String name = [Link]();
float marks = [Link]([Link]());
students[i] = new Student(id, name, marks);
}
float totalMarks = findTotalStudentMarks(students);
[Link](totalMarks);
}
}
Explanation:
- Defines a Student class with fields: id, name, marks.
- Reads number of students.
- For each student, reads data and creates an object.
- Adds up all student marks in a separate method.
- Prints the total marks.
Java Code and Line-by-Line Explanations
2. Find Minimum Marks
Java Code:
public static float findMinimumStudentMarks(Student[] students) {
float min = students[0].studentMarks;
for (Student s : students) {
if ([Link] < min) {
min = [Link];
}
}
return min;
}
Explanation:
- Starts with the first student's marks as the minimum.
- Iterates over each student.
- Updates the minimum if a lower mark is found.
- Returns the lowest mark.
Java Code and Line-by-Line Explanations
3. Find Average Marks
Java Code:
public static float findAverageStudentMarks(Student[] students) {
float total = 0;
for (Student s : students) total += [Link];
return total / [Link];
}
Explanation:
- Sums all student marks.
- Divides by number of students.
- Returns the average.
Java Code and Line-by-Line Explanations
4. Find Maximum Marks
Java Code:
public static float findMaximumStudentMarks(Student[] students) {
float max = students[0].studentMarks;
for (Student s : students) {
if ([Link] > max) {
max = [Link];
}
}
return max;
}
Explanation:
- Starts with the first student's marks as the max.
- Iterates through each student.
- Updates max if a higher mark is found.
- Returns the maximum.
Java Code and Line-by-Line Explanations
5. Find Student by Name
Java Code:
public static int findStudent(Student[] students, String name) {
for (Student s : students) {
if ([Link](name)) {
return [Link];
}
}
return -1;
}
Explanation:
- Iterates through student array.
- Checks if name matches (case-insensitive).
- If found, returns student ID.
- If not found, returns -1.
Java Code and Line-by-Line Explanations
6. Store and Print Person Array
Java Code:
import [Link].*;
class Person {
int personId;
String personName;
String personAddress;
public Person(int personId, String personName, String personAddress) {
[Link] = personId;
[Link] = personName;
[Link] = personAddress;
}
}
public class Solution {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner([Link]);
int n = [Link]([Link]());
Person[] persons = new Person[n];
for (int i = 0; i < n; i++) {
int id = [Link]([Link]());
String name = [Link]();
String address = [Link]();
persons[i] = new Person(id, name, address);
}
for (Person p : persons) {
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
}
}
Explanation:
- Defines a Person class with id, name, and address.
- Reads n people from input.
- Stores each as a Person object in an array.
- Loops over the array and prints details.