Giải Bài Check Student's Data Format – Java OOP (LAB211 – FPT)

Giải bài Check Student’s Data Format trong Java. Nhập dữ liệu sinh viên, tính điểm trung bình, xếp loại và thống kê theo %.

Trong quá trình học LAB211 Java OOP tại FPT, một trong những bài tập quan trọng là Check student's data format.Đây là đề bài yêu cầu lập trình viên xây dựng chương trình quản lý sinh viên bằng Java OOP, trong đó có chức năng nhập liệu, kiểm tra tính hợp lệ của điểm số, tính trung bình, phân loại học lực và thống kê tỷ lệ phần trăm theo từng loại A, B, C, D. Bài tập này không chỉ giúp bạn hiểu rõ cách xử lý dữ liệu nhập vào (input validation) mà còn rèn luyện kỹ năng thiết kế lớp (class), phương thức (method) và áp dụng lập trình hướng đối tượng trong thực tế.

check student's data format java lab211 fpt
Giải bài Check Student's Data Format Java OOP LAB211 FPT

Mục tiêu bài LAB

  • Xây dựng class Student với các thuộc tính:
    • Tên sinh viên (Student Name)
    • Lớp (Class)
    • Điểm Toán, Lý, Hóa (Math, Physics, Chemistry)
    • Điểm trung bình (Average)
    • Xếp loại (Type: A, B, C, D)
  • Thực hiện nhập dữ liệu với điều kiện kiểm tra:
    • Điểm phải nằm trong khoảng 0 – 10.
    • Nếu nhập sai (ví dụ: âm, >10, không phải số), chương trình phải báo lỗi và yêu cầu nhập lại.
  • Phân loại sinh viên dựa trên điểm trung bình:
    • A: mark > 7.5
    • B: 6 <= mark <= 7.5
    • C: 4 <= mark < 6
    • D: mark < 4
  • Thống kê kết quả:
    • Tính tỷ lệ phần trăm sinh viên loại A, B, C, D.
    • Hiển thị thông tin chi tiết từng sinh viên kèm kết quả phân loại.

Đề bài gốc - Check Student's Data Forma

Title

Check student’s data format.

Background

N/A

Program Specifications

  • Create a program allows input:
    • Student information includes: Student name, class name, the marks Math, Physical and Chemistry in the range from 1 to 10.
    • Display on screen the information.
    • Student Type following the conditions:
      • A: mark > 7.5
      • B: 6 <= mark <= 7.5
      • C: 4 <= mark < 6
      • D: mark < 4
    • Student Type statistics by %.

Function details

Function 1: Display GUI And Input Data

  • Users run the program. The program prompts users to input Student data.
  • When users stop inputting Student data, next Function 2.

Function 2: Perform function

  • The program classifies students and gives student rank statistics by %.
  • Display notify result of students together with statistic result and exit the program.

Expectation of User Interface


====== Management Student Program ======
Name:Nghia
Classes:FU1
Maths:11
Maths is less than equal ten
Maths:-1
Maths is greater than equal zero
Maths:
Maths is digit
Maths:10
Chemistry:11
Chemistry is less than equal ten
Chemistry:-1
Chemistry is greater than equal zero
Chemistry:
Chemistry is digit
Chemistry:10
Physics:11
Physics is less than equal ten
Physics:-1
Physics is greater than equal zero
Physics:
Physics is digit
Physics:10
Do you want to enter more student information?(Y/N):Y
Name:Nghia 2
Classes:FU1
Maths:10
Chemistry:10
Physics:10
Do you want to enter more student information?(Y/N):N

------ Student1 Info ------
Name:Nghia
Classes:FU1
AVG:10.0
Type:A
------ Student2 Info ------
Name:Nghia 2
Classes:FU1
AVG:10.0
Type:A
--------Classification Info -----
A: 100.0%
B: 0.0%
C: 0.0%
D: 0.0%

Guidelines

Student must implement the methods:

  • createStudent
  • averageStudent
  • getPercentTypeStudent

Suggestion:

  • Create a class Student contains the following properties: Student Name, Class, Math, Physical, Chemistry, Average, Type.
  • Create a class Mark Calculation, classify students, calculate Student Type statistics, and set the results on Student object.
  • Give the statistics:
    • A:? %
    • B:? %
    • C:? %
    • D:? %

Function 1: Input student information

Must create function:

Student createStudent(String name, String classes, double maths, double chemistry, double physics)

Input:

  • name: Student name
  • classes: Class
  • maths: Math mark
  • chemistry: Chemistry mark
  • physics: Physical mark

Return:

  • Student object

Function 2: Student classification

Write function:

List<Student> averageStudent(List<Student> students)

Input: students: the list of students not classified yet.

Return: the list of students already classified.

Function 3: Student Type Statistics

Must create function:

HashMap<String, Double> getPercentTypeStudent(List<Student> students)

Input: students: the list of students already classified.

Return: Student Type statistics by % by the key A, B, C, D.

Source Code Check Student's Data Format (LAB211 FPT)

Dưới đây là full source code project Check Student's Data Format (LAB211 FPT) viết bằng Java, bạn có thể copy về chạy trực tiếp trong NetBeans hoặc IntelliJ:

📁 SE1905S04.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se1905s04;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * S04 - Check student’s data format.
 *
 * @author TruongNTCE180140
 */
public class SE1905S04 {

    /**
     * The main method
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Create an instance of MarkCalculation to handle student data
        StudentManagement calculation = new StudentManagement();

        // Call the method to process students (input, calculate averages, display results)
        calculation.processStudents();
    }
}
📁 Student.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se1905s04;

import java.text.DecimalFormat;

/**
 * S04 - Check student’s data format.
 *
 * @author TruongNTCE180140
 */
public class Student {

    // Private fields for storing student information and grades
    private String name;            // The name of the student
    private String className;       // The class or section the student belongs to
    private double mathMark;        // The student's grade in Mathematics
    private double chemistryMark;   // The student's grade in Chemistry
    private double physicsMark;     // The student's grade in Physics
    private double averageMark;     // The average grade calculated from mathMark, chemistryMark, and physicsMark
    private String type;            // Classification of the student based on averageMark (e.g., A, B, C, D)

    /**
     * Constructor to initialize the student object and calculate average and
     * classification.
     *
     * @param name the name of the student
     * @param className the class of the student
     * @param mathMark the mark in Mathematics
     * @param chemistryMark the mark in Chemistry
     * @param physicsMark the mark in Physics
     */
    public Student(String name, String className, double mathMark, double chemistryMark, double physicsMark) {
        this.name = name;
        this.className = className;
        this.mathMark = mathMark;
        this.chemistryMark = chemistryMark;
        this.physicsMark = physicsMark;
        this.averageMark = calculateAverage();
        this.type = classifyStudent();
    }

    /**
     * Calculate the average grade of the subjects.
     *
     * @return the average grade
     */
    public double calculateAverage() {
        double average = (mathMark + chemistryMark + physicsMark) / 3;
        DecimalFormat df = new DecimalFormat("#.#");
        return Double.parseDouble(df.format(average));
    }

    /**
     * Classify the student based on the average grade.
     *
     * @return the classification type (A, B, C, or D)
     */
    public String classifyStudent() {
        if (averageMark > 7.5) {
            return "A";
        } else if (averageMark >= 6) {
            return "B";
        } else if (averageMark >= 4) {
            return "C";
        } else {
            return "D";
        }
    }

    /**
     * Getter for the student's name.
     *
     * @return the student's name
     */
    public String getName() {
        return name;
    }

    /**
     * Getter for the class name.
     *
     * @return the class name
     */
    public String getClassName() {
        return className;
    }

    /**
     * Getter for the average grade.
     *
     * @return the average grade
     */
    public double getAverageMark() {
        return averageMark;
    }

    /**
     * Getter for the student type.
     *
     * @return the classification type
     */
    public String getType() {
        return type;
    }

    /**
     * Displays the information of a student based on the given student number.
     *
     * @param studentNumber The student's index or identifier used for display.
     */
    public void displayStudentInfo(int studentNumber) {

        System.out.println("------ Student " + studentNumber + " Info ------");
        System.out.println("Name: " + name);
        System.out.println("Class: " + className);
        System.out.println("AVG: " + averageMark);
        System.out.println("Type: " + type);
    }
}
📁 StudentManagement.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se1905s04;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * S04 - Check student’s data format.
 *
 * @author TruongNTCE180140
 */
public class StudentManagement {

    private final List<Student> students = new ArrayList<>(); // Internal storage for students
    private final HashMap<String, Double> typePercentages = new HashMap<>(); // Store percentages for each classification
    private final Scanner scanner = new Scanner(System.in); // Scanner instance for all input operations

    /**
     * Add a student to the list.
     *
     * @param student the student to add
     */
    public void addStudent(Student student) {
        students.add(student);
    }

    /**
     * Calculate the average grades of the students and update their
     * classifications.
     */
    public void calculateAverages() {
        for (Student student : students) {
            student.calculateAverage();
        }
    }

    /**
     * Calculate the percentage of students in each classification (A, B, C, D)
     * and update the global `typePercentages` map.
     */
    public void calculatePercentTypeStudent() {
        int countA = 0, countB = 0, countC = 0, countD = 0;

        for (Student student : students) {
            String type = student.getType();
            switch (type) {
                case "A":
                    countA++;
                    break;
                case "B":
                    countB++;
                    break;
                case "C":
                    countC++;
                    break;
                case "D":
                    countD++;
                    break;
            }
        }

        int totalStudents = students.size();
        if (totalStudents > 0) {
            typePercentages.put("A", (countA * 100.0) / totalStudents);
            typePercentages.put("B", (countB * 100.0) / totalStudents);
            typePercentages.put("C", (countC * 100.0) / totalStudents);
            typePercentages.put("D", (countD * 100.0) / totalStudents);
        } else {
            System.out.println("No students available to calculate percentages.");
        }
    }

    /**
     * Display the percentage of students by classification.
     */
    public void displayStatistics() {
        System.out.println("-------- Classification Info --------");
        typePercentages.forEach((type, percentage)
                -> System.out.printf("%s: %.2f%%%n", type, percentage));
    }

    /**
     * Display information for all students.
     */
    public void displayAllStudentsInfo() {
        if (students.isEmpty()) {
            System.out.println("No students available to display.");
        } else {
            for (int i = 0; i < students.size(); i++) {
                students.get(i).displayStudentInfo(i + 1); // Pass the student number (1-based index)
            }
        }
    }

    /**
     * Process student data input, calculations, and display results.
     */
    public void processStudents() {
        inputStudentData();           // Get the list of students from input
        calculateAverages();          // Calculate average marks
        calculatePercentTypeStudent(); // Calculate classification percentages
        displayAllStudentsInfo();     // Display information for all students
        displayStatistics();          // Display classification statistics
    }

    /**
     * Input information for multiple students.
     */
    private void inputStudentData() {
        boolean moreStudents = true;

        while (moreStudents) {
            try {
                addStudent(createStudentFromInput()); // Create and add a student from user input

                // Ask if the user wants to add more students
                boolean validInput = false;

                while (!validInput) {
                    System.out.print("Do you want to enter more student information? (Y/N): ");
                    String answer = scanner.nextLine().trim().toUpperCase();

                    if (answer.equals("Y")) {
                        validInput = true;
                        moreStudents = true;
                    } else if (answer.equals("N")) {
                        validInput = true;
                        moreStudents = false;
                    } else {
                        System.out.println("Invalid input. Please enter 'Y' for yes or 'N' for no.");
                    }
                }
            } catch (IllegalArgumentException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }
    }

    /**
     * Create a new student from user input.
     *
     * @return the newly created student
     * @throws IllegalArgumentException if the input data is invalid
     */
    private Student createStudentFromInput() {
        String name = "";
        String className = "";

        while (true) {
            try {
                System.out.println("====== Management Student Program ======");
                System.out.print("Name:");
                name = scanner.nextLine().trim();
                if (!name.matches("[A-Z][a-z]+( [A-Z][a-z]+)?( [0-9]+)?")) {
                    throw new IllegalArgumentException("Invalid name. The name should only contain letters and spaces.");
                }
                break;
            } catch (IllegalArgumentException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }

        while (true) {
            try {
                System.out.print("Class:");
                className = scanner.nextLine().trim().toUpperCase();
                if (!className.matches("FU[1-9][0-9]*$")) {
                    throw new IllegalArgumentException("Invalid class name. It should be in the format 'FU1'.");
                }
                break;
            } catch (IllegalArgumentException e) {
                System.out.println("Error: " + e.getMessage());
            }
        }

        double mathMark = getMark("Math");
        double chemistryMark = getMark("Chemistry");
        double physicsMark = getMark("Physics");

        return new Student(name, className, mathMark, chemistryMark, physicsMark);
    }

    /**
     * Get a valid mark for a specific subject.
     *
     * @param subject the name of the subject
     * @return the valid mark entered by the user
     */
    private double getMark(String subject) {
        double mark = -1;
        while (mark < 0 || mark > 10) {
            System.out.print(subject + ":");
            try {
                mark = Double.parseDouble(scanner.nextLine());
                if (mark < 0) {
                    System.out.println(subject + " is greater than equal zero");
                }
                if (mark > 10) {
                    System.out.println(subject + " is less than equal ten");
                }
            } catch (NumberFormatException e) {
                System.out.println(subject + " is digit");
            }
        }
        return mark;
    }
}
Open Source! Hãy click vào từng folder để xem được code nhé anh em.

Kết luận

Bài tập Check Student’s Data Format trong môn LAB211 Java OOP giúp sinh viên nắm vững kỹ năng xử lý dữ liệu đầu vào, kiểm tra tính hợp lệ và áp dụng lập trình hướng đối tượng vào một bài toán thực tế. Qua việc xây dựng class Student, viết các hàm tính toán trung bình, phân loại học lực và thống kê theo tỷ lệ %, sinh viên không chỉ củng cố kiến thức về Java OOP mà còn rèn luyện tư duy thiết kế phần mềm có cấu trúc rõ ràng. Đây là nền tảng quan trọng để phát triển các chương trình quản lý quy mô lớn hơn trong tương lai.

Nếu bạn đang học LAB211 Java OOP tại FPT hoặc muốn tự ôn tập, hãy lưu lại project này để luyện tập. Đừng quên theo dõi series trên TruongDevs để cập nhật thêm nhiều bài giải lab kèm source code chi tiết nhé!

Thông tin! Mình có nhận giải lab thuê, hỗ trợ project, fix bug, training nhanh cho các môn ngành kỹ thuật từ toán đến lập trình phù hợp sinh viên FPT học đúng tiến độ.
Cam kết uy tín - đúng deadline - bảo mật tuyệt đối.
Liên hệ: Zalo.me/0973898830

About the author

TruongDevs
Không phải bug nào cũng xấu, có bug giúp ta tỉnh ra

Post a Comment