Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create an Intellij project and name it “pa5_your-id” (without the quotes, of

ID: 3756238 • Letter: 1

Question

1. Create an Intellij project and name it “pa5_your-id” (without the quotes, of course). 2. Add a file that contains a class named ChickenFeed to the same package of the project that contains file “Main.java” (i. e. class Main). 3. Class ChickenFeed models a single purchase of (you guessed it) chicken feed. A single purchase of chicken feed consists of the quantity in pounds and cost in dollars. The quantity and the cost must be above zero to be a valid. Beyond the quantity and cost, the class must include a default (no parameter) constructor, a parametrized constructor, plus setters (mutators) and getters (accessors) for the quantity and cost. Tip: Once the class has been created, verify that it functions correctly by creating a couple of class ChickenFeed reference variables and associated objects, then call all the methods to verify that they operate correctly. 4. In class Main, (a) in method main, create an array of at least 5 elements of class ChickenFeed. Then, using user input values for cost and amount, populate all of the elements of the array with objects of class ChickenFeed. (b) create three static methods that receive an array of type ChickenFeed. i. One method returns the total cost of the chicken feed as stored in the array. ii. One method returns the total amount of chicken feed as stored in the array. iii. One method returns the average cost per pound of the chicken feed as stored in the array.   
Requirements: • All user input must be via an object of class Scanner. • All fields (data members) of the class must be private. • All constructors, getter (accessors) methods and setter (mutator) methods of the class must be public. • If the default constructor is called, the cost and amount in the object must be set to zero.
1. Create an Intellij project and name it “pa5_your-id” (without the quotes, of course). 2. Add a file that contains a class named ChickenFeed to the same package of the project that contains file “Main.java” (i. e. class Main). 3. Class ChickenFeed models a single purchase of (you guessed it) chicken feed. A single purchase of chicken feed consists of the quantity in pounds and cost in dollars. The quantity and the cost must be above zero to be a valid. Beyond the quantity and cost, the class must include a default (no parameter) constructor, a parametrized constructor, plus setters (mutators) and getters (accessors) for the quantity and cost. Tip: Once the class has been created, verify that it functions correctly by creating a couple of class ChickenFeed reference variables and associated objects, then call all the methods to verify that they operate correctly. 4. In class Main, (a) in method main, create an array of at least 5 elements of class ChickenFeed. Then, using user input values for cost and amount, populate all of the elements of the array with objects of class ChickenFeed. (b) create three static methods that receive an array of type ChickenFeed. i. One method returns the total cost of the chicken feed as stored in the array. ii. One method returns the total amount of chicken feed as stored in the array. iii. One method returns the average cost per pound of the chicken feed as stored in the array.   
Requirements: • All user input must be via an object of class Scanner. • All fields (data members) of the class must be private. • All constructors, getter (accessors) methods and setter (mutator) methods of the class must be public. • If the default constructor is called, the cost and amount in the object must be set to zero.
1. Create an Intellij project and name it “pa5_your-id” (without the quotes, of course). 2. Add a file that contains a class named ChickenFeed to the same package of the project that contains file “Main.java” (i. e. class Main). 3. Class ChickenFeed models a single purchase of (you guessed it) chicken feed. A single purchase of chicken feed consists of the quantity in pounds and cost in dollars. The quantity and the cost must be above zero to be a valid. Beyond the quantity and cost, the class must include a default (no parameter) constructor, a parametrized constructor, plus setters (mutators) and getters (accessors) for the quantity and cost. Tip: Once the class has been created, verify that it functions correctly by creating a couple of class ChickenFeed reference variables and associated objects, then call all the methods to verify that they operate correctly. 4. In class Main, (a) in method main, create an array of at least 5 elements of class ChickenFeed. Then, using user input values for cost and amount, populate all of the elements of the array with objects of class ChickenFeed. (b) create three static methods that receive an array of type ChickenFeed. i. One method returns the total cost of the chicken feed as stored in the array. ii. One method returns the total amount of chicken feed as stored in the array. iii. One method returns the average cost per pound of the chicken feed as stored in the array.   
Requirements: • All user input must be via an object of class Scanner. • All fields (data members) of the class must be private. • All constructors, getter (accessors) methods and setter (mutator) methods of the class must be public. • If the default constructor is called, the cost and amount in the object must be set to zero.

Explanation / Answer

//ChickenFeed.java

class ChickenFeed

{

private double quantity;

private double cost;

ChickenFeed()

{

this.quantity = (double)0.0;

this.cost = (double)0.0;

}

ChickenFeed(double quant,double cost)

{

this.quantity = quant;

this.cost = cost;

}

public void setQuantity(double quant)

{

this.quantity = quant;

}

public double getQuantity()

{

return this.quantity;

}

public void setCost(double cost)

{

this.cost = cost;

}

public double getCost()

{

return this.cost;

}

}

//Main.java

import java.util.Scanner;

import java.util.ArrayList;

class Main

{

public static ArrayList<ChickenFeed> array = new ArrayList<ChickenFeed>();

public static void main(String args[])

{

int i;

Scanner in = new Scanner(System.in);

double cost,quantity;

for(i=0;i<5;i++)

{

System.out.print("Enter Quantity: ");

quantity = in.nextDouble();

System.out.print("Enter Cost: ");

cost = in.nextDouble();

if(cost>0.0 && quantity>0.0)

{

ChickenFeed ob = new ChickenFeed(quantity,cost);

array.add(ob);

}

else

{

System.out.println("Cost and Quantity Must be positive and >0");

}

}

double totalCost = calCost();

double totalAmount = calAmount();

double average = totalCost/totalAmount;

System.out.println("Average = "+average);

}

public static double calCost()

{

int i;

double cost = 0.0;

for(i=0;i<array.size();i++)

{

cost+= array.get(i).getCost();

}

System.out.println("Total Cost = "+cost);

return cost;

}

public static double calAmount()

{

int i;

double quant = 0.0;

for(i=0;i<array.size();i++)

{

quant+= array.get(i).getQuantity();

}

System.out.println("Total Quant = "+quant);

return quant;

}

}