Question

In: Computer Science

Develop a Java program for the question below: A running race among animals: In a race...

Develop a Java program for the question below:

A running race among animals:

In a race among animals different types of animals are differenciated wih their running styles. In each iteration of the race, each participating animal makes a move acording to its style of running. Total length of the racecourt is NTotal (store this variable properly in the test class). Determine the winner animal and completion time of the race using polymorphism. Each of these animal classes extends from Animal class having a move method with no arguments. The current position of an animal is an integer atatring value is 0. In addition, each animal has a name attribute as well.

For generating random variables create an object from the Random class and invoke its randomize method with an integer argument (n).which returns an integer random number between 0 and (n-1).

  • Animal class: have a move method implemented differently for each animal. It also has other instance variable(s) or method(s) required by the program.
  • Rabbit class: Rabbits move as follows: with 0.4 probability a rabit sleaps makede no movment its current position in the race does not change, and with 0.6 probability make a three units move forwards. (its position increases by three units)

Note you can generate 0.4 and 0,6 proababilities with the randomize method. Invoke the method with an argument of 5 if 0 or 1 comes the rabit sleapes at this iteration, if 2,3 or 4 comes, the rabit makes a move.

  • Turtle class: turtles always moves only one unit forward. Their positions increased by one unit.
  • Test class: Create an array of animals. There can be more than one rabit or turlle in the race. At each iteration of the race (each time), all animals in the array makes a move in the order indicated by the indices in the array. Position of the animal is updated by the move method. Initially all positions are zero. At the end of each iteration or time, check whether one or more animals completed the race. The winner is the animal whose position exceeding the racecourts length, Ifd more than one animals’ position exceeds the racecscourts leength first during an iteration. This is a simple way of determining the winner, not the best way. Print the name, and score (in how many interations it completes the race) of the winner. Do not pay attension to the case of more then one winner (two ro more animals with same score exceeding the racecourt length at the same iteration)

Solutions

Expert Solution

import java.util.Random;

class Animal {

    String name;
    int position, score;

    public Animal(String name) {
        this.name = name;
        this.position = 0;
        this.score = 0;
    }

    void move() {
        this.score++;
    }
}

class Rabbit extends Animal {

    public Rabbit(String name) {
        super(name);
    }

    @Override
    void move() {
        Random random = new Random();
        if (random.nextInt(5) < 2) {
            this.position += 3;
        }
        super.move();
    }
}

class Turtle extends Animal {

    public Turtle(String name) {
        super(name);
    }

    @Override
    void move() {
        this.position += 1;
        super.move();
    }
}

class Test {

    public static void main(String[] args) {
        int NTotal = 3;
        Animal[] animals = new Animal[4];
        animals[0] = new Rabbit("harry");
        animals[1] = new Rabbit("jason");
        animals[2] = new Turtle("alpha");
        animals[3] = new Turtle("thor");
        loop:
        while (true) {
            for (int i = 0; i < animals.length; i++) {
                animals[i].move();
                if (animals[i].position > NTotal) {
                    System.out.println("Winner is " + animals[i].name + " with score " + animals[i].score);
                    break loop;
                }
            }
        }
    }

}

Related Solutions

We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
IN JAVA Lab 10 This program reads times of runners in a race from a file...
IN JAVA Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you created and copy the Main class below into the file. There are 7 parts...
IN JAVA Lab 10 This program reads times of runners in a race from a file...
IN JAVA Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you created and copy the Main class below into the file. There are 7 parts...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class •...
Java Proect Project Outcomes Develop a Java program that: • creates a user designed class • uses proper design techniques including reading UML Class Diagrams • reads input from the keyboard using a Scanner Object and its methods • uses selection (if and if else) statements • uses iteration (while, do while or for) statements • uses String comparison methods. • follows standard acceptable programming practices. • handles integer overflow errors Prep Readings: Zybooks chapter 1 - 6 1. General...
URGENT JAVA Develop a Java computer program to simulate access control to a system network. The...
URGENT JAVA Develop a Java computer program to simulate access control to a system network. The username and password authentication mechanism is used as an access control mechanism in this project. The program needs to authenticate the user with their username and hashed password stored in Access Manager User (AMU) file. To create an Access Manager User file, you need to develop a java program that takes username and password as input and then generates a file containing the username,...
Classwork Java Program A zoo is developing an educational safari game with various animals. You will...
Classwork Java Program A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose.   Part A Think through common characteristics of animals, and write a class ZooAnimal. ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for.   It should also have at least two methods for behaviors that all animals do; these can simply print out a String stating what the...
JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or...
JAVA PROGRAM (Make sure that programs are running. Please discuss, if there is any compilation or run error.) Q. 1 Calculate the expression a)         x= 7.0 + (12 %3)*5 – 3;                    b)         x= 7 + (11 / 2)*5 + 3;            Q 2: Write a program that prompts the user to enter five test scores and then prints the average test score. (Assume that the test scores are decimal numbers.) Q 3. Write a program that prompts the capacity, in gallons,...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Develop a Java program for this problem where the user inputs an Earth age and the...
Develop a Java program for this problem where the user inputs an Earth age and the program will then display the age on Mercury, Venus, Jupiter, and Saturn. The values for d are listed in the table. Planet d = Approximate Number of Earth Days for This Planet to Travel Around the Sun Mercury 88 Venus 225 Jupiter 4380 Saturn 10767
Question 14: Find the running time function, T(n), of the program below. def prob14(L): if len(L)...
Question 14: Find the running time function, T(n), of the program below. def prob14(L): if len(L) <= 1: return 0 output = 0 for x in L: for y in L: output += x*y for x in L: output += x left = L[ 0 : len(L)//2 ] right = L[ len(L)//2 : len(L) ] return output + prob15(left) + prob15(right) ​ ANSWER: T(n) = ... ***Big-O, Omega, Theta complexity of functions, Running time equations of iterative functions & recursive...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT