Challenge Challenge

Task

Here is a class called Team written for you. Your task is to write a constructor that takes all 4 parameters and initializes all 4 fields. Please do not modify the Main class and its methods. If the constructor is correctly implemented, the necessary information about the team should be displayed. Good luck!

java

Main.java

  1. 1. Analyze the Team class and note the four fields: name, sport, yearFounded, and city. These fields need to be initialized using a constructor;
    1. 2. Inside the Team class, write a constructor that takes four parameters: name, sport, yearFounded, and city. The constructor should have the same name as the class;
    2. 3. Inside the constructor, use the this keyword to assign the parameter values to the corresponding fields. For example, this.name = name; assigns the value of the name parameter to the name field.

package com.example;

class Team {
    String name;
    String sport;
    int yearFounded;
    String city;
    
    public Team(String name, String sport, int yearFounded, String city) {
        this.name = name;
        this.sport = sport;
        this.yearFounded = yearFounded;
        this.city = city;
    }

    public void displayTeamInfo() {
        System.out.println("Team: " + name);
        System.out.println("Sport: " + sport);
        System.out.println("Year Founded: " + yearFounded);
        System.out.println("City: " + city);
    }
}

public class Main {
    public static void main(String[] args) {
        Team team = new Team("Lakers", "Basketball", 1947, "Los Angeles");
        team.displayTeamInfo();
    }
}
      

Everything was clear?

Section 4. Chapter 7