Question

Consider the following partial UML class diagram for the class Student:

Consider the following partial UML class diagram for the class Student:


Student
-studentID: String
-name: String
-school: String
+validateStudentID (s: Student):boolean 

studentID is a string that consists of a letter representing the school followed by 5 digits. The codes used for the schools in this scenario are shown below:

SchoolCodeExample of student ID
FASC'A'"A12345" refers to a student from FASC.
FAFB'B'"B88888" refers to a student from FAFB.

Write the code for the method validateStudentID (Student student) such that it performs validations on the parameter's student ID to ensure that:

- the format is correct (i.e. the student ID consists of a letter followed by 5 digits) and

- the letter value corresponds to the student's school.


2 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

Program: In this program, we create a class Student with three attributes - studentID(String), name(String), school(String). We create a boolean method validateStudentID(Student s) which takes a student class object as its parameter and checks if the student id is in the correct format, if it is, it returns true else return false.

To test the function we also need to assign the values to the attributes and for that we create an additional parameterized constructor in our class which assigns values to the attributes.

We also create a main() method in which we can create our class object and test our function.

Code:

public class Student {

    //Class attributes
    private String studentID;
    private String name;
    private String school;

    //Constructor to initialize
    public Student(String school, String name, String studentID){
        this.school = school;
        this.name = name;
        this.studentID = studentID;
    }

    //Method validateStudentID
    public boolean validateStudentID(Student student){
        String id = this.studentID;

        //The length of string studentID must be of length 6
        if(id.length() < 6 || id.length() > 6){
            return false;
        }

        //Check if id contains a letter at the start
        if(!Character.isLetter(id.charAt(0))){
            return false;
        }

        //Check if rest of characters after letter are digits
        for (int i = 1; i < id.length(); i++) {
            if(!Character.isDigit(id.charAt(i))){
                return false;
            }
        }

        //Check if the first letter corresponds to A or B
        if(id.charAt(0) != 'A' && id.charAt(0) != 'B'){
            return false;
        }
        else {

            //If all conditions are true return true
            return true;
        }
    }

    //Main method
    public static void main(String[] args) {
        Student s = new Student("FASC","A","A12345");
        System.out.println(s.validateStudentID(s));
    }
}

W N public class Student { 4 5 //Class attributes private String studentID; private String name private String school; 6 8 9

نها 46 } 47 48 49 //Main method public static void main(String[] args) { Student s = new Student ( school: EASC, name: A,

Output:

true Process finished with exit code o I

Add a comment
Know the answer?
Add Answer to:
Consider the following partial UML class diagram for the class Student:
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • What this Lab Is About: Given a UML diagram, learn to design a class Learn how...

    What this Lab Is About: Given a UML diagram, learn to design a class Learn how to define constructor, accessor, mutator and toStringOmethods, etc Learn how to create an object and call an instance method. Coding Guidelines for All ments You will be graded on this Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc) Keep identifiers to a reasonably short length. Use upper case for constants. Use title case (first letter is u case)...

  • Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed...

    Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed to the constructor Methods: Accessors int getID( ) String getName( ) Class Roster: This class will implement the functionality of all roster for school. Instance Variables a final int MAX_NUM representing the maximum number of students allowed on the roster an ArrayList storing students Constructors a default constructor should initialize the list to empty strings a single parameter constructor that takes an ArrayList<Student> Both...

  • 1) This exercise is about Inheritance (IS-A) Relationship. A) First, draw the UML diagram for class...

    1) This exercise is about Inheritance (IS-A) Relationship. A) First, draw the UML diagram for class Student and class ComputerSystemsStudent which are described below. Make sure to show all the members (member variables and member functions) of the classes on your UML diagram. Save your UML diagram and also export it as a PNG. B) Second, write a program that contains the following parts. Write each class interface and implementation, in a different .h and .cpp file, respectively. a) Create...

  • Last picture is the tester program! In this Assignment, you will create a Student class and...

    Last picture is the tester program! In this Assignment, you will create a Student class and a Faculty class, and assign them as subclasses of the superclass UHPerson. The details of these classes are in the UML diagram below: UHPerson - name : String - id : int + setName(String) : void + getName(): String + setID(int) : void + getID(): int + toString(): String Faculty Student - facultyList : ArrayList<Faculty - rank: String -office Hours : String - studentList...

  • PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class...

    PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class has 4 attributes: (1) student ID: protected, an integer of 10 digits (2) student name: protected (3) student group code: protected, an integer (1 for undergraduates, 2 for graduate students) (4) student major: protected (e.g.: Business, Computer Sciences, Engineering) Class Student declaration provides a default constructor, get-methods and set-methods for the attributes, a public abstract method (displayStudentData()). PART II: Create a Java class named...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • in java language please Question 2 (14 marks) a) Complete the following UML diagram as lava...

    in java language please Question 2 (14 marks) a) Complete the following UML diagram as lava classes (8 marks): Write a constructor for Computer ser class based on the information you have in the UML Complete the hasSpaces method to check if a string has space or not il. Extend ComputerUser class with two subclasses, as shown below in UML diagram and write the appropriate methods listed in the UML diagram <<abstract>> ComputerUser # username: String #password: String + Computer...

  • Create a java project and create Student class. This class should have the following attributes, name...

    Create a java project and create Student class. This class should have the following attributes, name : String age : int id : String gender : String gpa : double and toString() method that returns the Student's detail. Your project should contain a TestStudent class where you get the student's info from user , create student's objects and save them in an arralist. You should save at least 10 student objects in this arraylist using loop statement. Then iterate through...

  • You will create a class to keep student's information: name, student ID, and grade. The program...

    You will create a class to keep student's information: name, student ID, and grade. The program will have the following functionality: - The record is persistent, that is, the whole registry should be saved on file upon exiting the program, and after any major change. - The program should provide the option to create a new entry with a student's name, ID, and grade. - There should be an option to lookup a student from his student ID (this will...

  • python programming Goal: Implement and test the OnlineStudent class as shown in the Student/OnlineStudent UML Diagram....

    python programming Goal: Implement and test the OnlineStudent class as shown in the Student/OnlineStudent UML Diagram. Input Files: students.txt  study-groups.txt The study_group instance variable is a list that contains the username values of the other members in the student's study group. Details: Implement the OnlineStudent class in the module onlinestudent.py as specified by the Student/OnlineStudent UML Diagram. OnlineStudent is the derived class of the base classStudent. The __lt__ method for an OnlineStudent object should return true if self.username < other.username...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT