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.
- Write a constructor with 4 parameters:
name
,sport
,yearFounded
, andcity
. - Initialize the
name
field with the value of thename
parameter. - Initialize the
sport
field with the value of thesport
parameter. - Initialize the
yearFounded
field with the value of theyearFounded
parameter. - Initialize the
city
field with the value of thecity
parameter.
Soluzione
solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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?
Grazie per i tuoi commenti!
Sezione 4. Capitolo 7
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione