Challenge: Get Information About the Object
Uppgift
Swipe to start coding
- Define the type for each field in the
Cat
class. - Implement the
infoCat()
method to display the cat’s information, including itsname
,age
, andcolor
. - Call the
infoCat()
method for eachCat
object to display the cat information.
Lösning
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
package com.example;
class Cat {
String name;
double age;
String color;
void infoCat() {
System.out.println("Name: " + name + ", Age: " + age + ", Color: " + color);
}
}
public class Main {
public static void main(String[] args) {
Cat michael = new Cat();
michael.name = "Mikey";
michael.age = 0.5;
michael.color = "Black";
michael.infoCat();
Cat garfield = new Cat();
garfield.name = "Garfield";
garfield.age = 4.7;
garfield.color = "Red";
garfield.infoCat();
}
}
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 4. Kapitel 5
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
package com.example;
class Cat {
___ name;
___ age;
___ color;
void infoCat() {
System.out.println("Name: " + ___ + ", Age: " + ___ + ", Color: " + ___);
}
}
public class Main {
public static void main(String[] args) {
Cat michael = new Cat();
michael.name = "Mikey";
michael.age = 0.5;
michael.color = "Black";
michael.___;
Cat garfield = new Cat();
garfield.name = "Garfield";
garfield.age = 4.7;
garfield.color = "Red";
garfield.___;
}
}
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal