java

Create a class Student with following attributes


I'm trying to learn the array object in Java but don't understand. I did understand how to store input into an array object but fail to understand how to compare each items inside an array to do #7 and #8. I tried to search up on the internet but stuck from there.

  1. Create a class Student with following attributes: Name, Age, Address, Email address

  2. Create an empty constructor that initializes empty values to all the attributes.

  3. Create a constructor that takes all the parameters and initializes all the attributes with it.

  4. Create accessor and mutator methods for all attributes.

  5. Create a toString method to return the details of the student.

  6. Ask the user to enter the details of any 5 students and store them in an array.

  7. Ask the user to enter an address and print all the students who live in that address.

  8. Print all the students whose email address contains “gmail.com”.

import java.util.*;

public class Student{
    private String name;
    private int age;
    private String address;
    private String email;

    public Student(){}

    public Student(String name, int age, String address, String email){
       this.name = name;
       this.age = age;
       this.address = address;
       this.email = email;
    }

    public void setName(String newName){
       name = newName;
    }
    public void setAge(int newAge){
       age = newAge;
    }
    public void setAddress(String newAddress){
       address = newAddress;
    }
    public void setEmail(String newEmail){
       email = newEmail;
    }

    public String getName(){
      return name;
    }
    public int getAge(){
      return age;
    }
    public String getAddress(){
      return address;
    }
    public String getEmail(){
      return email;
    }

    public String toString() {
        return "Name: " + name + ", Age: " + age + ", Address: " + address + ", Email: " + email;
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Student[] s= new Student[];5

        for(int i = 0; i < 5; i++){
            System.out.print("Name: ");
            String name = sc.nextLine();
            System.out.print("Age: ");
            int age = sc.nextInt();
            System.out.print("Address: ");
            sc.nextLine();
            String address = sc.nextLine();
            System.out.print("Email: ");
            String email = sc.nextLine();

            s[i] = new Student(name,age,address,email);
        }
        for(int i = 0; i < s.size(); i++){
            if(s)
        }

        System.out.println(Arrays.toString(s));

    }
}

Solution

  • For 7, use the scanner to retrieve a string from the user, then compare it to each user's address

    System.out.print("Please enter an address: ");
    String anyAddress = sc.nextLine();
    for (int i = 0; i < s.length; i++) {
        if (s[i].getAddress().equals(anyAddress))
            System.out.println(s[i]);
    }
    

    For 8, it's quite the same, iterate on the student (I show there a for-each loop), then verify if "gmail.com"is in their email, if true, then print it

    for (Student student : s) {
        if (student.getEmail().contains("gmail.com"))
            System.out.println(student);
    }