Question

In: Computer Science

Complete class Cow so that it has: (a) an int attribute to hold a number of...

Complete class Cow so that it has:

(a) an int attribute to hold a number of legs,
(b) a constructor that takes one parameter, a number of legs (c) a getter method for the attribute
(d) a method to convert any instance of Cow to a string

    class Cow{
        ____________________________________
        ____________________________________
        Cow(________________________){
        ____________________________________
        ____________________________________
        }
        ____ getNumLegs(){
        ____________________________________
        ____________________________________
        }
        ____________________________________
        ____________________________________
        ____________________________________
        ____________________________________
        ____________________________________

}

public class MinOfSet {

    /**
     * Write a method (use the code provided) that takes
     * a collection of Cows and
     * returns the Cow that has the minimum number of legs.
     * Assume the collection contains at least one cow.
     */

    public static Cow minCow(Collection<Cow> herd){
        Cow answer = null;
        Iterator<Cow> icow = herd.iterator();
         ____________________________________
         ____________________________________
         ____________________________________
         ____________________________________
         ____________________________________
         ____________________________________
         ____________________________________
         ____________________________________

}

public static void main(String[] args) {
//Put two cows in the herd and then print the cow with minimum legs
Collection<Cow> herd = new HashSet<>();
____________________________________
____________________________________
____________________________________
____________________________________

Solutions

Expert Solution


import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

class Cow {
   private int numLegs;

   //constructor
   public Cow(int aNumLegs) {
       super();
       numLegs = aNumLegs;
   }
//getter
   public int getNumLegs() {
       return numLegs;
   }
//tostring
   public String toString() {
       return "Cow [Num of Legs : " + numLegs + "]";
   }

}

public class MinOfSet {
   public static Cow minCow(Collection<Cow> herd) {
       Cow answer = null;
       Iterator<Cow> icow = herd.iterator();
       Cow temp = null;
       answer = icow.next();
       //iterating objects from set
       while (icow.hasNext()) {
           //checking if cow has less legs than other
           // if yes swap
           temp = icow.next();
           if (temp.getNumLegs() < answer.getNumLegs())
               answer = temp;
       }
       return answer;
   }
   public static void main(String[] args) {
       Collection<Cow> herd = new HashSet<>();
       //creating and adding cow object to hasset
       herd.add(new Cow(3));
       herd.add(new Cow(4));
       System.out.println(minCow(herd));
   }
}

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

Suppose that class OrderList has a private attribute double cost[100] which hold the cost of all...
Suppose that class OrderList has a private attribute double cost[100] which hold the cost of all ordered items, and a private attributes int num_of_items which hold the number of items ordered. For example, if num_of_items is 5, then cost[0], cost[1], ..., cost[4] hold the cost of these 5 items. Implement the member function named total_cost which returns the total cost of this OrderList.
Complete the attached code so that if the user enters the number 22 for the number...
Complete the attached code so that if the user enters the number 22 for the number of items purchased and 10.98 for the price of each, the following results are displayed: The total bill is $241.56 Hint: It must include cin and variables. // This program will read the quantity of an item and its price // Then print the total price. // The input will come from the keyboard and the output will go to the monitor. #include <iostream>...
create a class matrix.Let U and V be the two matrices of type Int and number...
create a class matrix.Let U and V be the two matrices of type Int and number of rows and columns are user defined. Write the following member functions of the class a) Add Add the two matrices U and V. For this, add the corresponding entries, and place their sum in the corresponding index of the result matrix. b) Subtract Subtract the two matrices U and V. For this, subtract the corresponding entries, and place this answer in the corresponding...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the...
java circular linked list /* * Complete the playGame(int players, int passes) method * Complete the addPlayers(int players) method * Complete the passPotatoe(int passes) method * No other methods/variables should be added/modified */ public class A3CircleLL {    /*    * Grading:    * Correctly uses helpers to play game - 1pt    * Prints correct winner when game is complete - 0.5pt    */    public void playGame(int players, int passes) {        /*        * Use...
In Java The Order class should have:  Seven instance variables: the order number (an int),...
In Java The Order class should have:  Seven instance variables: the order number (an int), the Customer who made the order, the Restaurant that receives the order, the FoodApp through which the order was placed, a list of food items ordered (stored as an array of FoodItem), the total number of items ordered (an int), and the total price of the order (a double).  A class constant to set the maximum number of items that can be ordered...
Python Please Define a class that has one data attribute of type float. The initializer does...
Python Please Define a class that has one data attribute of type float. The initializer does not accept an argument, however, it initializes the data attribute to zero. Include in class the following methods: 1. func that will accept as two arguments. The first argument a value of float type and the second argument if string. The function will perform the following depending on the string argument: a. if string is '+', the first argument will be added to the...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
Complete the C++ class Triple below so that it represents a vector with 3 elements: (a,...
Complete the C++ class Triple below so that it represents a vector with 3 elements: (a, b, c) Most of these function bodies can be written in only a few lines. Error checking is not required. Test your class using the test.h file. The triple.cpp file is the code unit tested on submission. main.cpp #include #include #include #include "triple.h" #include "test.h" using namespace std; int main() { myTest(); return 0; } triple.h #ifndef TRIPLE_H #define TRIPLE_H #include #include using namespace...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy()...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name,...
Implement an Employee class that includes data fields to hold the Employee’s ID number, first name, last name, and hourly pay rate (use of string datatype is not allowed). Create an array of five Employee objects. Input the data in the employee array. Write a searchById() function, that ask the user to enter an employee id, if its present, your program should display the employee record, if it isn’t there, simply print the message ”Employee with this ID doesn’t exist”....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT