1. Create an Intellij project and name it “pa5_your-id” (without the quotes, of
ID: 3756325 • 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.
Explanation / Answer
package cheggSolution1;
import java.util.Scanner;
public class Main1 {
public static void main(String[] args) {
ChickenFeed[] chickens=new ChickenFeed[5];
Scanner sc=new Scanner(System.in);
for(int i=0; i<5;i++) {
System.out.println("Enter Quantity: ");
double quantity=sc.nextDouble();
System.out.println("Enter Price: ");
double price=sc.nextDouble();
ChickenFeed cf=new ChickenFeed(quantity, price);
chickens[i]=cf;
}
System.out.println(costPerPound(chickens));
}
public static double totalCost(ChickenFeed[] cf) {
double sum=0;
for (ChickenFeed chickenFeed : cf) {
sum+=chickenFeed.getCost();
}
return sum;
}
public static double totalAmount(ChickenFeed[] cf) {
double sum=0;
for (ChickenFeed chickenFeed : cf) {
sum+=chickenFeed.getQuantity();
}
return sum;
}
public static double costPerPound(ChickenFeed[] cf) {
return totalCost(cf)/totalAmount(cf);
}
}
package cheggSolution1;
public class ChickenFeed {
private double quantity;
private double cost;
public ChickenFeed() {
// TODO Auto-generated constructor stub
}
public ChickenFeed(double quantity, double cost) {
assert quantity>0 && cost>0;
this.quantity = quantity;
this.cost = cost;
}
public double getQuantity() {
return quantity;
}
public void setQuantity(double quantity) {
assert quantity>0;
this.quantity = quantity;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
assert cost>0;
this.cost = cost;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.