Question

Java Programming II Homework 6 Create a class to represent a Farm object containing instances of...

Java Programming II Homework 6

Create a class to represent a Farm object containing instances of the Animal objects

Farm

View javadoc for Animal and Farm classes https://bit.ly/2X6yxzw

- animals : Animal [ ] 
- farmName : String
- numAnimals : int         //calculated controlled variable no setter

+ Farm()                //default 10 animals 
+ Farm(String)            //default 10 animals 
+ Farm(int)            //size of array
+ Farm(String, int)    

+ addAnimal(Animal) : void    //use the next available slot to add the Animal, resize the array if necessary
+ getFarmName() : String
+ setFarmName(String) : void
+ getAnimal(int) : Animal             //return null if index is invalid
+ getNumAnimals() : int        //notice no setter… this is a controlled variable
+ getFirstAnimal() : Animal       
+ getLastAnimal() : Animal    
+ getAnimals() : Animal[ ]
+ printAllDetails() : void    // prints Farm attributes followed by Animal attributes as formatted below : 
                                // "FarmName: %20s | Number of Animals: %4d | Farm Size: %4d\n" 
                                // "Name: %20s | Year of Birth: %4d | Weight: %10.2f | Gender: %c\n"
+ removeAnimal(int) : Animal     
+ removeAllAnimals() : void
+ getTotalWeightOfAllAnimals() : double
+ getAverageWeightOfAllAnimals() : double
+ getNumberOfAnimalsAboveWeight(double) : int
+ getNumberOfAnimalsBelowWeight(double) : int
+ increaseWeightOfAllAnimals() : void
+ increaseWeightOfAllAnimals(double) : void

View javadoc for Animal and Farm classes https://bit.ly/2X6yxzw

Submission Instructions

'Main.java', 'Animal.java', and 'Farm.java'

Compile command: javac Main.java Animal.java Farm.java -Xlint:all -encoding utf-8

We will use this command to compile your code

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

Please feel free add more test code in Main.java

/Animal.java

package homeworks.hw3;

public class Animal {
   private String name;
   private int birthYear;
   private double weight;
   char gender;
   
   public Animal() {
   }
   
   public Animal(String name, int birthYear, double weight, char gender) {
      this.name = name;
      this.birthYear = birthYear;
      this.weight = weight;
      this.gender = gender;
   }
   
   public String getName() {
      return name;
   }
   
   public void setName(String name) {
      this.name = name;
   }
   
   public int getBirthYear() {
      return birthYear;
   }
   
   public void setBirthYear(int birthYear) {
      this.birthYear = birthYear;
   }
   
   public double getWeight() {
      return weight;
   }
   
   public void setWeight(double weight) {
      this.weight = weight;
   }
   
   public char getGender() {
      return gender;
   }
   
   public void setGender(char gender) {
      this.gender = gender;
   }
   
   public int calculateAge(int currentYear){
      if(currentYear<this.birthYear)return -1;
      return currentYear=birthYear;
   }
   public boolean isFemale(){
      return this.gender == 'F'|| this.gender == 'f';
   }
   public void gainWeight(){
      setWeight(getWeight()+1);
   }
   public void gainWeight(double weight){
      setWeight(getWeight()+weight);
   }
   public void loseWeight() {
      if(this.weight-1>=0){
         setWeight(getWeight()-1);
      }
   }
   public void loseWeight(double weight){
      if(this.weight-weight>=0){
         setWeight(getWeight()-weight);
      }
   }
   public void printDetails(){
      System.out.printf( "Name: %20s | Year of Birth: %4d | Weight: %10.2f | Gender: %c\n",
            this.name, this.birthYear, this.weight, this.gender);
   }
}

//Farm.java

package homeworks.hw3;

import java.util.Arrays;

public class Farm {
   private Animal[] animals;
   private String farmName;
   private int numAnimals;
   
   public Farm() {
      animals = new Animal[10];
      this.numAnimals = 0;
      this.farmName = "";
   }
   
   public Farm(int maxAnimals) {
      if (maxAnimals < 0) maxAnimals = 0;
      animals = new Animal[maxAnimals];
      this.numAnimals = 0;
      this.farmName = "";
   }
   
   public Farm(String farmName) {
      animals = new Animal[10];
      this.farmName = farmName;
      this.numAnimals = 0;
   }
   
   public Farm(String farmName, int maxAnimals) {
      if (maxAnimals < 0) maxAnimals = 0;
      this.farmName = farmName;
      this.numAnimals = 0;
      animals = new Animal[maxAnimals];
   }
   
   public String getFarmName() {
      return farmName;
   }
   
   public void setFarmName(String farmName) {
      this.farmName = farmName;
   }
   
   public void addAnimal(Animal a) {
      if (this.numAnimals >= animals.length) resizeAnimalArray();
      animals[numAnimals++] = a;
   }
   
   public Animal getAnimal(int index) {
      if (index < 0 || index >= numAnimals) return null;
      return animals[index];
   }
   
   public int getNumAnimals() {
      return this.numAnimals;
   }
   
   public Animal getFirstAnimal() {
      return animals[0];
   }
   
   public Animal getLastAnimal() {
      return animals[numAnimals-1];
   }
   
   public Animal[] getAnimals() {
      return this.animals;
   }
   
   public Animal removeAnimal(int index) {
      if(index<0 || index >= numAnimals) return null;
      Animal[] temp = new Animal[animals.length];
      int j=0;
      Animal a = animals[index];
      for(int i = 0; i<numAnimals; i++) {
         if(i != index)
         {
            temp[j] = animals[i];
            j++;
         }
      }
      temp[animals.length-1] = null;
      animals = temp;
      numAnimals -= 1;
      return a;
   }
   
   public void removeAllAnimals() {
      this.numAnimals = 0;
   }
   
   public double getTotalWeightOfAllAnimals() {
      double sum = 0;
      for(int i0=0;i0<numAnimals;i0++)
         sum += animals[i0].getWeight();
      return sum;
   }
   
   public double getAverageWeightOfAllAnimals() {
      return getTotalWeightOfAllAnimals() / (numAnimals);
   }
   
   public int getNumberOfAnimalsAboveWeight(double weight) {
      int i = 0;
      for(int i0=0;i0<numAnimals;i0++)
         if (animals[i0].getWeight() > weight)
            i++;
      return i;
   }
   
   public int getNumberOfAnimalsBelowWeight(double weight) {
      
      int i = 0;
      for(int i0=0;i0<numAnimals;i0++)
         if (animals[i0].getWeight() < weight)
            i++;
      return i;
   }
   
   public void increaseWeightOfAllAnimals() {
      for(int i=0;i<numAnimals;i++)
         animals[i].gainWeight();
   }
   
   public void increaseWeightOfAllAnimals(double weight) {
      for(int i0=0;i0<numAnimals;i0++)
         animals[i0].gainWeight(weight);
   }
   
   public void printAllDetails() {
      System.out.printf("FarmName: %20s | Number of Animals: %4d | Farm Size: %4d\n",
            this.farmName, this.numAnimals, animals.length);
      for(int i=0;i<numAnimals;i++)
         animals[i].printDetails();
   }
   
   public void resizeAnimalArray() {
      if (animals.length == 0) {
         animals = Arrays.copyOf(animals, 2);
      } else
         animals = Arrays.copyOf(animals, animals.length * 2);
   }
   
}

//Main.java

package homeworks.hw3;

public class Main {
   
   public static void main(String[] args) {
      Farm f = new Farm("MyFarm",-1);
      f.addAnimal(new Animal("a1",1978,34.6d,'M'));
      f.addAnimal(new Animal("a2",2013,33.6d,'F'));
      f.addAnimal(new Animal("a3",2001,31.6d,'M'));
      
      f.removeAnimal(1);
      
      f.printAllDetails();
   }

}

//OUTPUT

FarmName: MyFarm | Number of Animals: 2 | Farm Size: 4
Name: a1 | Year of Birth: 1978 | Weight: 34.60 | Gender: M
Name: a3 | Year of Birth: 2001 | Weight: 31.60 | Gender: M

Add a comment
Know the answer?
Add Answer to:
Java Programming II Homework 6 Create a class to represent a Farm object containing instances of...
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
  • help with java OOP, here is the started code: package P2; public class Farm {   ...

    help with java OOP, here is the started code: package P2; public class Farm {    private double availableFood;    private Animal[] animals;    public Farm() {        setAvailableFood(1000);        animals = new Animal[4];        animals[0] = new Chicken();        animals[1] = new Cow();        animals[2] = new Llama();        animals[3] = new Llama();    }    public void makeNoise(){           // all animals make their sound (Moo, Cluck, etc)        for(Animal...

  • Assignment: You are to create seven classes that represent a Zoo. The classes are define as follo...

    Assignment: You are to create seven classes that represent a Zoo. The classes are define as follows: 1. Zoo 2. Enclosure 3. Animal 4. Crocodile 5. Gazelle 6. Lion 7. Zebra 1. Zoo: Required member variables: private String name; private String address; private Enclosure[] enclosures; private int area; private double budget; 2. Enclosure: Required member variables: private String biome; private Animal[] animals; 3. Animal Required member variables: private String name; private String genus; private String species; private Zoo currentZoo; protected...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

  • Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void...

    Hello, i need help with this homework: Code provided: public class DirectedWeightedExampleSlide18 { public static void main(String[] args) { int currentVertex, userChoice; Scanner input = new Scanner(System.in); // create graph using your WeightedGraph based on author's Graph WeightedGraph myGraph = new WeightedGraph(4); // add labels myGraph.setLabel(0,"Spot zero"); myGraph.setLabel(1,"Spot one"); myGraph.setLabel(2,"Spot two"); myGraph.setLabel(3,"Spot three"); // Add each edge (this directed Graph has 5 edges, // so we add 5 edges) myGraph.addEdge(0,2,9); myGraph.addEdge(1,0,7); myGraph.addEdge(2,3,12); myGraph.addEdge(3,0,15); myGraph.addEdge(3,1,6); // let's pretend we are on...

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