Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Get Information About the Object | Classes
Java Extended

book
Challenge: Get Information About the Object

Uppgift

Swipe to start coding

  1. Define the type for each field in the Cat class.
  2. Implement the infoCat() method to display the cat’s information, including its name, age, and color.
  3. Call the infoCat() method for each Cat object to display the cat information.

Lösning

java

solution

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?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 5
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

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt