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

book
Challenge: Team Constructor

Compito

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.

Soluzione

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();
}
}

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 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();
}
}

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt