Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Animal Information Display | Classes Advanced
Java Extended

book
Challenge: Animal Information Display

Tarea

Swipe to start coding

Your task is to bypass the private access modifier and create two objects with the necessary parameters, then display them.

  1. Create two objects of the Animal class — one for a cat and one for a dog.
    • You need to create an object cat of the Animal class with the type "Cat", name "Garfield", and color "Red".
    • You also need to create an object dog of the Animal class with the type "Dog", name "Maks", and color "Black".
  2. Create and use the Animal(String type, String name, String color) constructor to pass the parameters type, name, and color for each object.
  3. After creating the objects, use System.out.println() to print information about each animal.

Solución

java

solution

package com.example;

public class Main {
public static void main(String[] args) {
Animal cat = new Animal("Cat", "Garfield", "Red");
System.out.println(cat);
Animal dog = new Animal("Dog", "Maks", "Black");
System.out.println(dog);
}
}

class Animal {
private String type;
private String name;
private String color;

public Animal(String type, String name, String color) {
this.type = type;
this.name = name;
this.color = color;
}

@Override
public String toString() {
return "Animal{" + "type='" + type + '\'' + ", name='" + name + '\'' + ", color='" + color + '\'' + '}';
}
}
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 4
package com.example;

public class Main {
public static void main(String[] args) {
// You should write your solution here.
}
}

class Animal {
private String type;
private String name;
private String color;

@Override
public String toString() {
return "Animal{" + "type='" + type + '\'' + ", name='" + name + '\'' + ", color='" + color + '\'' + '}';
}
}
toggle bottom row
We use cookies to make your experience better!
some-alt