Question

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

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

Dear Student,

Please find the requested code below:

(Check the comments in the code for better understanding of every step in the code)

SOURCE CODE:

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

//creating the class Cow
class Cow
{
  
}


//creating the class having a method to remove Cow ArrayList
class X
{

//method to remove ArrayList

    void rem(ArrayList x)
    {
      
    //clear() -- predefined method to remove arraylist
      x.clear();
    }
}


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

//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();
Cow c3=new Cow();

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

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


X y=new X();

//calling the method to remove ArrayList
y.rem(list);

//checking whether ArrayList removed or not
System.out.println(list);
}
}

OUTPUT:

[Cow@2a139a55, Cow@15db9742, Cow@6d06d69c]
[]

Please LIKE the answer

Drop comments below if you have any doubts

THANK YOU

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

  • 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.

  • 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

  • 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...

  • A. Create a class called StudentRecord that has the following private variables: year, GPA Implem...

    In Java a. Create a class called StudentRecord that has the following private variables: year, GPA Implement 2 constructors: default constructor (doesn't take any parameters, has b. an empty body) and a constructor, that initializes all variables. according to the following template: Implement Comparable interface, so that StudentRecord class has ĢompareTo c. Implement a toString() method that returns text representation of an object First name: Benoit, last name: Mandelbrot, year: 3, GPA: 3.68 d. method, that performs comparison as the...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Create a Ruby class Book1. Each book is characterized by the instance variables authnr rf t class . For this class create the initializer and the method show1 that displays the instance variables of...

    Create a Ruby class Book1. Each book is characterized by the instance variables authnr rf t class . For this class create the initializer and the method show1 that displays the instance variables of the Bookl. class Create the class Book2 that inherits properties from the class Bookl and ad variables publisher and price. Write the following Book2 methods e read2 that prompts the user and reads from the keyboard all data for Book e show2 that displays all instance...

  • create a class in Java for a Plane the class will include String Data Fields for...

    create a class in Java for a Plane the class will include String Data Fields for the make, model, and year. (as well as mutators and accessors) Defined constructor and copy constructor. there must be a toString method as well as a copy method. runner program the program will create an array for Plane class. The size of the array will be set to user input. then the user will input the information into each plane object in the array....

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • 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...

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