Assume that you are in the process of storing the student details of a University in a file. Code a Java application to handle the following members,Student Register Number - StringStudent Name - StringAge – IntegerGetstudents method to write details on the filePrintstudents method to display the details of the studentsIf the student age is less than 17, display a message that “Invalid Age”. Handle this exception in a proper way using one of the exception keyword.Consider storing three student details and display them.
Question
Assume that you are in the process of storing the student details of a University in a file. Code a Java application to handle the following members,Student Register Number - StringStudent Name - StringAge – IntegerGetstudents method to write details on the filePrintstudents method to display the details of the studentsIf the student age is less than 17, display a message that “Invalid Age”. Handle this exception in a proper way using one of the exception keyword.Consider storing three student details and display them.
Solution
Here is a simple Java application that meets your requirements:
import java.io.*;
class Student {
String registerNumber;
String name;
int age;
Student(String registerNumber, String name, int age) {
this.registerNumber = registerNumber;
this.name = name;
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("001", "John", 18);
students[1] = new Student("002", "Jane", 19);
students[2] = new Student("003", "Doe", 16);
getStudents(students);
printStudents(students);
}
public static void getStudents(Student[] students) {
try {
FileOutputStream fileOut = new FileOutputStream("./student.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(students);
out.close();
fileOut.close();
} catch (IOException i) {
i.printStackTrace();
}
}
public static void printStudents(Student[] students) {
for (Student s : students) {
try {
if (s.age < 17) {
throw new IllegalArgumentException("Invalid Age");
}
System.out.println("Register Number: " + s.registerNumber);
System.out.println("Name: " + s.name);
System.out.println("Age: " + s.age);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
}
This program creates a Student class with the required fields and two methods in the Main class: getStudents and printStudents. The getStudents method writes the student details to a file, and the printStudents method reads the details from the file and prints them. If a student's age is less than 17, it throws an IllegalArgumentException with the message "Invalid Age".
Similar Questions
write a java program Create a class called “Student” with the following details: Name (should contain only alphabets –No numbers and special characters) Register Number in the format (two numbers three characters four numbers) Age (should be greater than 18 and less than 21) Phone number (10 numbers). Write appropriate Exception handling block (separate block for each type of user defined exception) to handle invalid data entry for all data items. If the valid data is entered construct the email id and password of the student and display Email ID format: first name four-digit year @ vitstudent.ac.in and password is “uppercase of first four characters of name + last four numbers of register number+@” Input Format Enter Name: Enter Reg. Number: Enter Age: Enter Phone Number: Output Format Aditya 19BCE1234 20 1234567890
Design a Student Mark List Generation System using Java.Define a class to store student's register number, name,attendance percentage and an integer array to store five subject marks.Define methods to read and print the student's details. Generate "InvalidMarksException: Marks must be within the range 0 to 30" , If the marks entered are less than 0 or greater than 30. Generate “InsufficientAttendanceException: Minimum Attendance Required is 75%” , if the attendance is less than 75%. Print the marklist only if there are no exceptions generated.Input FormatRegno – intName – StringAttendance – doubleMark1, Mark2, Mark3, Mark4, Mark5 – int Array elementsOutput FormatIf Exception is generated, display the message thrown by theException.Else, display the student details as shown below.Regno:Name:Attendance:Mark1:Mark2:Mark3:Mark4:Mark5:
exception handling in java
Copy the code of this Java file: ExceptionSample.javaUsing JCreator, edit ExceptionSample so it can catch exceptions wherein text is entered as String input instead of integer data type input.Submit a copy of your completed Java program to the instructor before the start of the next class.
Develop a Student Record Management System in Java for a university. The system should enable administrators to effectively manage student records, including adding new students, updating student information, and viewing student details.Requirements:Student Class:Create a Student class with private instance variables for storing student information such as name, ID, age, and grade.Student Management Class:Create a StudentManagement class with private static variables to store a list of students and the total number of students.Administrator Interface:Display a menu with options to add a new student, update student information, and view student details.Prompt the administrator for necessary inputs and perform the requested operations using the StudentManagement class.Error Handling:Implement error handling to handle cases where the student ID is not found or invalid inputs are provided.Documentation:Provide comprehensive documentationInclude instructions for running the program and interacting with the administrator interface.Remember to use appropriate variable names and follow coding best practices.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.