Question

Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void...

Horse Team(Java) .

Horse

-name:String

-age:int

-weight:double

+Horse():void

+Horse(String, int double):void

+getName():String

+getAge():int

+getWeight():double

+setName(String):void

+setAge(int):void

+setWeight(double):void

+compareName(String):boolean

+compareTo(Horse):int

+toString():String

Team

-team:ArrayList<Horse>

+Team():void

+addHorse(String, int, double):void

+addHorse(Horse):void

+averageAge():int

+averageWeight():int

+getHorse(int):Horse

+getSize():int

+findHorse(String):Horse

+largestHorse():Horse

+oldestHorse():Horse

+removeHorse(String):void

+smallestHorse():Horse

+toString():String

+youngestHorse():Horse

HorseTeamDriver

+main(String[]):void

Create a class (Horse from the corresponding UML diagram) that handles names of horse, the age of the horse, and the weight of the horse. In addition to the standard accessor and mutator methods from the instance fields, the class will have two different compare methods and a method that will output the data in a user friendly format.

Create a class (Team from the corresponding UML diagram) that maintains the team of horses. It should be able to add a new horse to the team and remove a horse identified by name from the team. It should also be able to determine the average weight, the average age, the largest horse, the smallest horse, the youngest horse and the oldest horse in the team.

Finally, create a class (HorseTeamDriver from the corresponding UML diagram) that will allow the user to input how may horses are in the team and data on the horses (create a new Horse object based on the Horse class for each of the horses to inputted and outputs the following information to the screen: the name and weight of the largest horse, the name and weight of the smallest horse, the name and age of the youngest horse, and the name and age of the oldest horse, average weight of the horses and average age of the horses.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here are the classes you will be needing.

Thank you & please a thumbs up !!

=====================================================================================

public class Horse implements Comparable<Horse> {

    private String name;
    private int age;
    private double weight;

    public Horse() {
        name = "";
        age = 0;
        weight = 0;
    }

    public Horse(String name, int age, double weight) {
        this.name = name;
        this.weight = weight;
        this.age =age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Horse o) {
        return (int) (this.getWeight() - o.getWeight());
    }

    public boolean compareName(String aName) {
        return this.getName().equalsIgnoreCase(aName);
    }

    @Override
    public String toString() {
        return "Name: " + getName() + ", Age: " + getAge() + ", Weight: " + getWeight();
    }
}

=====================================================================================

import java.util.ArrayList;

public class Team {

    ArrayList<Horse> horses;

    public Team() {
        horses = new ArrayList<>();
    }

    public void addHorse(String name, int age, double wt) {
        horses.add(new Horse(name, age, wt));
    }

    public void addHorse(Horse h) {
        horses.add(h);
    }

    public int averageAge() {
        int totalAge = 0;

        for (Horse h : horses) {
            totalAge += h.getAge();
        }
        return horses.size() == 0 ? 0 : totalAge / horses.size();
    }


    public double averageWeight() {
        double totalWeight = 0;

        for (Horse h : horses) {
            totalWeight += h.getWeight();
        }
        return horses.size() == 0 ? 0 : totalWeight / horses.size();
    }

    public Horse getHorse(int index) {
        return horses.size() >= index + 1 ? horses.get(index) : null;
    }

    public int getSize() {
        return horses.size();
    }

    public Horse findHorse(String name) {
        for (Horse h : horses) {
            if (h.compareName(name)) {
                return h;
            }
        }
        return null;
    }

    public Horse largestHorse() {

        Horse horse = null;
        double largestWt = Double.MIN_VALUE;
        for (Horse h : horses) {
            if (largestWt < h.getWeight()) {
                horse = h;
                largestWt = h.getWeight();
            }
        }
        return horse;
    }

    public Horse oldestHorse() {
        Horse horse = null;
        int age = 0;
        for (Horse h : horses) {
            if (age < h.getAge()) {
                horse = h;
                age = h.getAge();
            }
        }
        return horse;
    }

    public Horse youngestHorse() {
        Horse horse = null;
        int age = Integer.MAX_VALUE;
        for (Horse h : horses) {
            if (age > h.getAge()) {
                horse = h;
                age = h.getAge();
            }
        }
        return horse;
    }

    public void removeHorse(String name) {
        Horse remHorse = null;
        for (Horse h : horses) {
            if (h.compareName(name)) {
                remHorse = h;
                break;
            }
        }
        horses.remove(remHorse);
    }

    public Horse smallestHorse() {

        Horse horse = null;
        double smallestWt = Double.MAX_VALUE;
        for (Horse h : horses) {
            if (smallestWt > h.getWeight()) {
                horse = h;
                smallestWt = h.getWeight();
            }
        }
        return horse;
    }

    @Override
    public String toString() {
        StringBuilder desc = new StringBuilder();
        for (Horse h : horses) {
            desc.append(h).append("\n");
        }
        return desc.toString();
    }
}

=====================================================================================

import java.util.Scanner;

public class HorseTeamDriver {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("How many horses you want to add: ");
        int count = scanner.nextInt();
        Team stable = new Team();
        for (int c = 1; c <= count; c++) {

            System.out.print("Enter horse name: ");
            String name = scanner.nextLine();
            System.out.print("Enter age: ");
            int age = scanner.nextInt();
            System.out.print("Enter weight: ");
            double wt = scanner.nextDouble();

            stable.addHorse(name, age, wt);
        }
        System.out.println(stable);

        System.out.println("Avearge Weight : " + stable.averageWeight());
        System.out.println("Avearge Age    : " + stable.averageAge());
        System.out.println("Largest Horse  : " + stable.largestHorse());
        System.out.println("Smallest Horse : " + stable.smallestHorse());
        System.out.println("Oldest Horse   : " + stable.oldestHorse());
        System.out.println("Youngest Horse : " + stable.youngestHorse());


    }
}

=====================================================================================

Add a comment
Know the answer?
Add Answer to:
Horse Team(Java) . Horse -name:String -age:int -weight:double +Horse():void +Horse(String, int double):void +getName():String +getAge():int +getWeight():double +setName(String):void +setAge(int):void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram)

    Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...

  • Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double...

    Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double +setPrice(double): void +toString(): String The class has two attributes, title and price, and get/set methods for the two attributes. The first constructor doesn’t have a parameter. It assigns “” to title and 0.0 to price; The second constructor uses passed-in parameters to initialize the two attributes. The toString() method returns values for the two attributes. Notation: - means private, and + means public. 1....

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEm...

    Java Problem 2: Employee (10 points) (Software Design) Create an abstract class that represents an Employee and contains abstract method payment. Create concrete classes: SalaryEmployee, CommissionEmployee, HourlyEmployee which extend the Employee class and implements that abstract method. A Salary Employee has a salary, a Commision Employee has a commission rate and total sales, and Hourly Employee as an hourly rate and hours worked Software Architecture: The Employee class is the abstract super class and must be instantiated by one of...

  • using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name...

    using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name : String # age: int # year: int + Club_Card (all parameters) + Setters & Getters + toString() : String + equals(x: Object): boolean [10 pts] Define the class Club_Card, which contains: 1. All arguments constructor which accepts all required arguments from the main and instantiate a Club_Card object. 2. Set & get method for each data attribute. 3. A toString() method which returns...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...

    Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An abstract method    static double average(Measurable[] objects) { // A static method    double sum = 0;    for (Measurable obj : objects) {    sum = sum + obj.getMeasure();    }    if (objects.length > 0) { return sum / objects.length; }    else { return 0; }    } } Write a class, called Person, that has two instance variables, name (as...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT