Question

Create a Cow class. Make two cows and put them in a collection. Write a method...

Create a Cow class. Make two cows and put them in a collection. Write a method to count the cows from the collection that have exactly four legs.

using Java

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

Dear Student,

Please find the requested code below:

SOURCE CODE:

import java.util.*;
//importing the java.util.* package

//creating the class Cow
class Cow
{
    int NoOfLegs=0;
}

class TMP
{

//creating the method count to count the no. of cows having four legs
    int count(ArrayList x )
{int cnt=0;
    for(int i=0;i<x.size();i++)
    {
      
        if((x.get(i).NoOfLegs)==4)
        {cnt+=1;}

    }
    return cnt;
  

  
}  
  
}

//main class to test the code
public class Main extends Cow
{

//main method
public static void main(String[] args)
{
  
    //creating the ArrayList collection
ArrayList list = new ArrayList();

//creating the objects of Cow i.e say 3 cows
Cow c1=new Cow();
Cow c2=new Cow();
c1.NoOfLegs=4;
c1.NoOfLegs=6;


//adding into the ArrayList collection
list.add(c1);
list.add(c2);

//printing the collection to check whether inserted or not
System.out.println(list);
TMP xx=new TMP();

//storing the count value in FourLegsCows
int FourLegsCows=xx.count(list);

//and printing the count value

System.out.println("no. of cows having four legs="+FourLegsCows);
}}

OUTPUT:

[Cow@2a139a55, Cow@15db9742]

no. of cows having four legs=1

THANK YOU

Add a comment
Know the answer?
Add Answer to:
Create a Cow class. Make two cows and put them in a collection. Write a method...
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
  • Create a Cow class. Make cows and put them in a collection. Write a method that...

    Create a Cow class. Make cows and put them in a collection. Write a method that removes cows from the collection based on a) name, b) legs. using Java array list

  • use java code Write a collection of array utility methods. •Write a method to create a...

    use java code Write a collection of array utility methods. •Write a method to create a list of duplicate numbers in an array. •Write a method to create a list of duplicate Strings in an array. •Convert to using generics.

  • plz write if it is in another class or package Question 1: 1. Create a new...

    plz write if it is in another class or package Question 1: 1. Create a new project in Eclipse (File > New > Java Project.) Name it Homework2Q1 2. Create a package for your classes 3. Create an abstract superclass with only non default constructor(s) 4. Create two different subclasses of that superclass 5. Create another class that is not related (you need a total of four classes up to this point) 6. Create an interface 7. Make the class...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • 1. Assume you have a Car class that declares two private instance variables, make and model....

    1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...

  • Java Framework Collection Assign. Create a class called ArrayListExample Create a ArrayList object called languageList which...

    Java Framework Collection Assign. Create a class called ArrayListExample Create a ArrayList object called languageList which accept type of String Add English, French, Italian and Arabic strings into languageList array object Print languageList using Iterator object Sort ArrayList object alphabetically Print languageList using Iterator object Solution will produce following: ArrayListExample class ArrayList object called languageList ArrayListExampleTest class with main method

  • Create a class “MapIntersectClient” and write a method mapIntersect in that class that accepts two maps...

    Create a class “MapIntersectClient” and write a method mapIntersect in that class that accepts two maps whose keys are strings and whose values are integers as parameters and returns a new map containing only the key/value pairs that exist in both of the parameter maps. In order for a key/value pair to be included in your result, not only do both maps need to contain a mapping for that key, but they need to map it to the same value....

  • 42) Create a toString method for the LinkedStack class. This method should create and return a...

    42) Create a toString method for the LinkedStack class. This method should create and return a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the LinkedStack class and for testing and debugging applications that use the LinkedStack class. 46) Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The...

  • Create a java project and create Student class. This class should have the following attributes, name...

    Create a java project and create Student class. This class should have the following attributes, name : String age : int id : String gender : String gpa : double and toString() method that returns the Student's detail. Your project should contain a TestStudent class where you get the student's info from user , create student's objects and save them in an arralist. You should save at least 10 student objects in this arraylist using loop statement. Then iterate through...

  • JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a...

    JAVA Question a) Write a generic method public static <...> Collection<Pair<...>> sortPairCollection(Collection <Pair<....>> col) in a Utils class that takes as parameter a collection of Pair<K,V> objects and returns a new collection object (use ArrayList<...>) with the pair elements from collection col sorted in ascending order. For comparing pairs, the K type must implement Comparable<....> Use the proper type constraints. b) Write a main() function in Utils.java that tests the sortPairCollection() with K=String and V=Integer.

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