Turtle and Mouse in Java

This is a cute little story about a turtle and a mouse where you get to enter the names and properties of the animals. It’s written in Java.

import java.util.Scanner;

class Animal {
  String name;
  String furColor;
  int age;
  float weight;

  public Animal(String name, String furColor, int age, float weight) {
    this.name = name;
    this.furColor = furColor;
    this.age = age;
    this.weight = weight;
  }
}

public class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.print("Enter the name of the turtle: ");
    String turtleName = scan.nextLine();
    System.out.print("Enter the fur color of the turtle: ");
    String turtleFurColor = scan.nextLine();
    System.out.print("Enter the age of the turtle: ");
    int turtleAge = scan.nextInt();
    System.out.print("Enter the weight of the turtle: ");
    float turtleWeight = scan.nextFloat();

    scan.nextLine(); // to consume the newline character after nextFloat()

    System.out.print("Enter the name of the mouse: ");
    String mouseName = scan.nextLine();
    System.out.print("Enter the fur color of the mouse: ");
    String mouseFurColor = scan.nextLine();
    System.out.print("Enter the age of the mouse: ");
    int mouseAge = scan.nextInt();
    System.out.print("Enter the weight of the mouse: ");
    float mouseWeight = scan.nextFloat();

    Animal turtle = new Animal(turtleName, turtleFurColor, turtleAge, turtleWeight);
    Animal mouse = new Animal(mouseName, mouseFurColor, mouseAge, mouseWeight);

    System.out.println("\nOnce upon a time, there was a turtle named " + turtle.name + ".");
    System.out.println("The turtle was " + turtle.furColor + " and weighed " + turtle.weight + " kg.");
    System.out.println("It was " + turtle.age + " years old.");

    System.out.println("And there was a mouse named " + mouse.name + ".");
    System.out.println("The mouse was " + mouse.furColor + " and weighed " + mouse.weight + " kg.");
    System.out.println("It was " + mouse.age + " years old.");

    System.out.println("\nOne day, " + turtle.name + " and " + mouse.name + " met each other.");
    System.out.println(turtle.name + " was shy and didn't know what to say, but " + mouse.name + " was friendly and started a conversation.");
    System.out.println("They talked about their interests and discovered that they had many things in common.");
    System.out.println("From that day on, " + turtle.name + " and " + mouse.name + " were best friends.");
  }
}Code language: JavaScript (javascript)

Leave a Comment