Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Java program that meets your requirements:

import java.util.Scanner;
import java.util.regex.Pattern;

class InvalidNameException extends Exception {
    InvalidNameException(String s) {
        super(s);
    }
}

class InvalidRegNumberException extends Exception {
    InvalidRegNumberException(String s) {
        super(s);
    }
}

class InvalidAgeException extends Exception {
    InvalidAgeException(String s) {
        super(s);
    }
}

class InvalidPhoneNumberException extends Exception {
    InvalidPhoneNumberException(String s) {
        super(s);
    }
}

class Student {
    String name;
    String regNumber;
    int age;
    String phoneNumber;

    Student(String name, String regNumber, int age, String phoneNumber) throws InvalidNameException, InvalidRegNumberException, InvalidAgeException, InvalidPhoneNumberException {
        if (!name.matches("[a-zA-Z]+")) {
            throw new InvalidNameException("Name should contain only alphabets");
        }
        if (!Pattern.matches("\\d{2}[a-zA-Z]{3}\\d{4}", regNumber)) {
            throw new InvalidRegNumberException("Invalid Registration Number");
        }
        if (age <= 18 || age >= 21) {
            throw new InvalidAgeException("Age should be greater than 18 and less than 21");
        }
        if (!Pattern.matches("\\d{10}", phoneNumber)) {
            throw new InvalidPhoneNumberException("Phone Number should be 10 digits");
        }
        this.name = name;
        this.regNumber = regNumber;
        this.age = age;
        this.phoneNumber = phoneNumber;
    }

    String getEmail() {
        return name.toLowerCase() + "[email protected]";
    }

    String getPassword() {
        return name.toUpperCase().substring(0, 4) + regNumber.substring(regNumber.length() - 4) + "@";
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Name:");
        String name = sc.nextLine();
        System.out.println("Enter Reg. Number:");
        String regNumber = sc.nextLine();
        System.out.println("Enter Age:");
        int age = sc.nextInt();
        System.out.println("Enter Phone Number:");
        String phoneNumber = sc.next();

        try {
            Student student = new Student(name, regNumber, age, phoneNumber);
            System.out.println("Email: " + student.getEmail());
            System.out.println("Password: " + student.getPassword());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

This program creates a Student class with the required fields and checks for valid input in the constructor. If the input is invalid, it throws an exception. The getEmail and getPassword methods generate the email and password according to your specifications. The main method prompts the user for input and creates a Student object. If the input is invalid, it prints the error message.

This problem has been solved

Similar Questions

reate 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+@”

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.

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:

Write a java program to define a class Person having name of the person , age of the person and gender of the person. Include accept() and toString() method to display the data. Create 3 objects and display name of the person who is youngest

Design a class named Person and its two subclasses named Student and Employee.  Make Faculty and Staff subclasses of Employee. A person has a name,  address, phone_number, and e-mail address. A student has a status (freshman, sophomore, junior, or senior).  An employee  has an office, salary. A faculty member has office_hours and a rank. A staff member has a title. Override the toString() method in each of these classes to display their details. Write a Java application and subsequent pseudocode to implement/simulate the same.Input :First line must read the Type of the Object(Person(P)/Employee(E)/Faculty(F)/Student(S)/Staff(T).Second line onwards read details such as name, address, phoneNo, Email and soonOutput :Print ClassName : Name, Address, PhonNo, Email, and rest of the datamembers of that class. For Example :if Type = P, then print Person : Name, Address, PhoneNo, Emailif Type = S, then print Student : Name, Address, PhoneNo, Email, Statusif Type = E, then print Employee : Name, Address, PhoneNo, Email, Office, Salaryif Type = T, then print Staff : Name, Address, PhoneNo, Email, Office, Salary, Titleif Type = F, then print Faculty : Name, Address, PhoneNo, Email, Office, Salary, Office_Hrs, RankNote :The Status for Student is freshman, sophomore, junior, or senior.Overide only toString() method in all the Classes.

1/3

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.