Java: Accepts students name, id, and marks and display the highest score and the lowest score
Java Basic: Exercise-245 with Solution
Highest and Lowest Scores from Student Data
Write a Java program that accepts students' names, ids, and marks and displays their highest and lowest scores.
The student name and id are all strings of no more than 10 characters. The score is an integer between 0 and 100.
Visual Presentation:
Sample Solution:
Java Code:
// Importing the Scanner class to read input from the user
import java.util.Scanner;
// Defining the Student class to represent student information
class Student {
// Instance variables to store student name, ID, and score
String name;
String stu_id;
int score;
// Default constructor with default values
public Student() {
this(" ", " ", 0);
}
// Parameterized constructor to initialize instance variables with given values
public Student(String initName, String initId, int initScore) {
name = initName;
stu_id = initId;
score = initScore;
}
}
// Main class named "Main"
public class Main {
// Main method, the entry point of the program
public static void main(String[] args) {
// Creating a Scanner object to read input from the user
Scanner in = new Scanner(System.in);
// Prompting the user to input the number of students
System.out.println("Input number of students:");
// Reading the number of students from the user and trimming excess whitespaces
int n = Integer.parseInt(in.nextLine().trim());
// Prompting the user to input Student Name, ID, Score
System.out.println("Input Student Name, ID, Score:");
// Creating Student objects to store information about the students
Student stu = new Student();
Student max = new Student();
Student min = new Student(" ", " ", 100);
// Loop to read information about each student
for (int i = 0; i < n; i++) {
// Reading student name, ID, and score from the user
stu.name = in.next();
stu.stu_id = in.next();
stu.score = in.nextInt();
// Checking if the current student has the highest score
if (max.score < stu.score) {
max.name = stu.name;
max.stu_id = stu.stu_id;
max.score = stu.score;
}
// Checking if the current student has the lowest score
if (min.score > stu.score) {
min.name = stu.name;
min.stu_id = stu.stu_id;
min.score = stu.score;
}
}
// Printing the name and ID of the highest score and the lowest score students
System.out.println("name, ID of the highest score and the lowest score:");
System.out.println(max.name + " " + max.stu_id);
System.out.println(min.name + " " + min.stu_id);
// Closing the Scanner to release system resources
in.close();
}
}
Sample Output:
Input number of students: 3 Input Student Name, ID, Score: Devid v1 72 Peter v2 68 Johnson v3 85 name, ID of the highest score and the lowest score: Johnson v3 Peter v2
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program which reads a list of pairs of a word and a page number, and prints the word and a list of the corresponding page numbers.
Next: Write a Java program to convert 3 digits positive number in above format. For example, 234 should be output as BBSSS1234 because it has 2 "hundreds", 3 "ten", and 4 of the ones.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/basic/java-basic-exercise-245.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics