第十二周

本周任务如下:

  1. 注册OnlineGDB并且加入课程群:https://onlinegdb.com/classroom/invite/Fd0aduv55

  2. 完成Person Class作业

Person Class

An instance variable can be assigned an initial value in its declaration, just like any other variable. For example, consider a class named Person. An object of this class will represent a person. It will contain three instance variables to represent its name, email and phone number:

Person.java
public class Person
{
    // instance variables
    private String name;
    private String email;
    private String phoneNumber;

    // constructor: construct a Person copying in the data into the instance
    // variables
    public Person(String initName, String initEmail, String initPhone)
    {
        name = initName;
        email = initEmail;
        phoneNumber = initPhone;
    }

    // Print all the data for a person
    public void print()
    {
        System.out.println("Name: " + name);
        System.out.println("Email: " + email);
        System.out.println("Phone Number: " + phoneNumber);
    }
    
    //To be implement in Exercise 1
    public boolean isValidEmail() {
        return false;
    }
}

Exercise1: Validate person's email

The '@' symbol is a crucial component of the syntax for email addresses. According to the standard format,

email address consists of two main parts: a local part and a domain part. These two parts are separated by the '@' symbol. For example, in the email address "example@example.com", "example" is the local part, and "example.com" is the domain part.

Your task is to add a method isValidEmail() to this class that checks if the person's email contains the '@' . This method should return true if the email contains '@', and false otherwise.

The method signature should be:

public boolean isValidEmail()

Exercise 2: Test your Person Class.

Create a driver class. Paste the code here. Create the following two Person Objects in the driver class.

Click test, then you can see the result.

When you are ready, submit your code.

Last updated