Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Team Constructor | Classes
Java Extended

book
Challenge: Team Constructor

Task

Swipe to start coding

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.

  1. Write a constructor with 4 parameters: name, sport, yearFounded, and city.
  2. Initialize the name field with the value of the name parameter.
  3. Initialize the sport field with the value of the sport parameter.
  4. Initialize the yearFounded field with the value of the yearFounded parameter.
  5. Initialize the city field with the value of the city parameter.

Solution

java

solution

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?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 7
package com.example;

class Team {
String name;
String sport;
int yearFounded;
String city;

public ___(___, ___, ___, ___) {
___ = ___;
___ = ___;
___ = ___;
___ = ___;
}

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();
}
}
toggle bottom row
some-alt