Passing an array value from one class to another - java

I want all my array data from this class to be passed to another class and I don't know how to do it.
Here is my code:
public static void main(String[] args) {
String menu[] = {"", "Hotsilog", "Porksilog",
"Bangsilog", "Tapsilog", "Chicksilog"};
double priceList[] = {0, 20.00, 20.00, 20.00, 20.00, 20.00};
int quantOrder[] = new int[100];
int foodChoice[] = new int[100];
int qntty = 0;
Scanner scan = new Scanner(System.in);
System.out.printf(" %3s%14s\n", "Menu", "Price");
for (int i = 1; i <= 5; i++) {
System.out.printf("[%d] %-15s%.2f\n", i, menu[i], priceList[i]);
}
System.out.print("How many items do you want to order? ");
qntty = scan.nextInt();
for (int i = 1; i <= qntty; i++) {
System.out.println("Enter the number of your choice: ");
foodChoice[i] = scan.nextInt();
System.out.println("You choose " + menu[foodChoice[i]] + "!");
System.out.print("Quantity of order :");
quantOrder[i] = scan.nextInt();
}
}
And I want all the datas from up there to be passed to this class and compute all the prices
public class methOds {
double Price[] = new double[100];
double totalPrice = 0;
methOds() {
Price[i] = priceList[foodChoice[i]] * quantOrder[i];
totalPrice = totalPrice + Price[i];
}
}
Can some explain me how to work with it?
Thanks in advance!

You need to initialize the pricelist and quantity ordered arrays to the method class and that you can pass into the constructor.
public static void main(String[] args) {
String menu[] = {"", "Hotsilog", "Porksilog",
"Bangsilog", "Tapsilog", "Chicksilog"};
double priceList[] = {0, 20.00, 20.00, 20.00, 20.00, 20.00};
int quantOrder[] = new int[100];
int foodChoice[] = new int[100];
int qntty = 0;
Scanner scan = new Scanner(System.in);
System.out.printf(" %3s%14s\n", "Menu", "Price");
for (int i = 1; i <= 5; i++) {
System.out.printf("[%d] %-15s%.2f\n", i, menu[i], priceList[i]);
}
System.out.print("How many items do you want to order? ");
qntty = scan.nextInt();
for (int i = 1; i <= qntty; i++) {
System.out.println("Enter the number of your choice: ");
foodChoice[i] = scan.nextInt();
System.out.println("You choose " + menu[foodChoice[i]] + "!");
System.out.print("Quantity of order :");
quantOrder[i] = scan.nextInt();
}
methOds method_obj = new methOds(priceList, quantOrder);
System.out.println(method_obj.totalPrice());
}
Modify your constructor and make it a parametrized one by passing both the quantOrder and foodChoice arrays so that the values could be calculated in the
totalPrice method.
public class methOds {
private double Price[] = new double[100];
private double totalPrice = 0;
private int quantOrder[];
private int foodChoice[];
public methOds(quantOrder, foodChoice) {
this.quantOrder = quantOrder;
this.foodChoice = foodChoice;
}
public int totalPrice() {
//Method to calculate the total price
for (int i; i < Price.length; i++) {
Price[i] = priceList[foodChoice[i]] * quantOrder[i];
totalPrice = totalPrice + Price[i];
}
return totalPrice;
}
}

Related

How do you connect a single scanner to two arrays?

Basically, I'm trying to ask the user's input and the input should store in two arrays using a single scanner. Using two would ask the user twice and that would be impractical. The code looks like this
int record = 0;
Scanner midOrFinal = new Scanner(System.in);
Scanner scansubjects = new Scanner(System.in);
Scanner scangrades = new Scanner(System.in);
System.out.println("Press 1 to Record for Midterm");
System.out.println("Press 2 to Record for Final Term");
record = midOrFinal.nextInt();
int midterm[] = new int[8];
int grades[] = new int[8];
{
if ( record == 1 )
System.out.println("Enter 8 subjects and their corresponding grades:");
System.out.println();
int i = 0;
for( i = 0; i < 8; i++ )
{
System.out.println(subjects[i]);
System.out.print("Enter Grade: ");
grades[i] = scangrades.nextInt();
if( i == ( subjects.length) )
System.out.println();
}
System.out.println("Enter Grade Successful");
}
If the user chooses option 1, the user will be given some subjects in an array (which I didn't include) and asked to input the grades. The input shall then proceed to the midterm OR finalterm array but I can't seem to do it by using one scanner.
If there are better ideas than my proposed idea, then please share. I'm still very new in Java and my first time using stackoverflow. Thanks!
Break out the grade collection into a new function, and pass along the array you want to collect the grades into.
public static void main(String[] args) throws IOException {
int gradeType = 0;
// Use a single scanner for all input
Scanner aScanner = new Scanner(System.in);
System.out.println("Press 1 to Record for Midterm");
System.out.println("Press 2 to Record for Final Term");
gradeType = aScanner.nextInt();
String[] subjects = { "Subject A", "Subject B" };
int[] midtermGrades = new int[subjects.length];
int[] finalGrades = new int[subjects.length];
int[] gradesToCollect;
// Use gradesToCollect to reference the array you want to
// collect into.
//
// Alternatively, we could call collectGrades() in both the if/else
// condition
if (gradeType == 1) {
gradesToCollect = midtermGrades;
} else {
gradesToCollect = finalGrades;
}
collectGrades(subjects, gradesToCollect, aScanner);
System.out.println("\n\nThese are the collected grades");
System.out.println("Mid Final");
for (int i = 0; i < subjects.length; i++) {
System.out.format("%3d %3d\n", midtermGrades[i], finalGrades[i]);
}
}
// Collect a grade for each subject into the given grades array.
public static void collectGrades(final String[] subjects, final int[] grades, Scanner scn) {
System.out.format("Enter %s subjects and their corresponding grades:",
subjects.length);
System.out.println();
for (int i = 0; i < subjects.length; i++) {
System.out.format("Enter Grade for %s : ", subjects[i]);
grades[i] = scn.nextInt();
if (i == (subjects.length))
System.out.println();
}
System.out.println("Enter Grade Successful");
}
class Main {
public static final Scanner in = new Scanner(System.in);
public static void main(String[] args) {
in.useDelimiter("\r?\n");
Student student = new Student();
System.out.println("Press 1 to Record for Midterm");
System.out.println("Press 2 to Record for Final Term");
int record = in.nextInt();
if (record == 1) {
student.setTerm(TermType.MID);
System.out.println("Enter 8 subjects and their corresponding grades:");
System.out.println("Enter Subject and grades space separated. Example - \nMaths 79");
System.out.println();
for (int i = 0; i < 8; i++) {
System.out.println("Enter Subject " + (i + 1) + " details");
String subjectAndGrade = in.next();
int index = subjectAndGrade.lastIndexOf(" ");
String subject = subjectAndGrade.substring(0, index);
int grade = Integer.parseInt(subjectAndGrade.substring(index + 1));
student.getSubjects().add(new Subject(grade, subject));
}
System.out.println("Enter Grade Successful");
System.out.println("========================================================");
System.out.println("Details: ");
System.out.println("Term Type " + student.getTerm());
for(int i = 0; i< student.getSubjects().size(); i++) {
System.out.println("Subject: " + student.getSubjects().get(i).getSubjectName() + ", Grade: " + student.getSubjects().get(i).getGradeScore());
}
}
}
}
class Student {
private List<Subject> subjects = new ArrayList<>();
private TermType term;
public List<Subject> getSubjects() {
return subjects;
}
public void setSubjects(List<Subject> subjects) {
this.subjects = subjects;
}
public TermType getTerm() {
return term;
}
public void setTerm(TermType term) {
this.term = term;
}
}
class Subject {
private int gradeScore;
private String subjectName;
public Subject(int gradeScore, String subjectName) {
this.gradeScore = gradeScore;
this.subjectName = subjectName;
}
public double getGradeScore() {
return gradeScore;
}
public void setGradeScore(int gradeScore) {
this.gradeScore = gradeScore;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
}
scanners work by separating the input into a sequence of 'tokens' and 'delimiters'. Out of the box, 'one or more whitespace characters' is the delimiter.

how do i print a string array at array index?

Having trouble getting my program to output the index[1] of my array "nArray", if nArray[0] = bob, and nArray[1] = jim. When I'm trying to print the input, it will print nArray[0] bob, but when it gets to nArray[1] it does not output.
public String toString(){
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
public static double salePercent(double[] sArray, String[] nArray){
double total = 0;
for (int b =0 ; b < sArray.length; b++){ // sum calculator
total = total + sArray[b];
}
double percent = 0;
for (int k = 0; k < nArray.length; k++){
System.out.println(nArray[k]);
}
return total;
}
edit
showing how i am creating and declaring and creating my arrays
in my mainclass
System.out.println("How Many Employees :");
int size = input.nextInt();
input.nextLine(); //dummy
String[] nArray = new String[size]; // array for staff names
double[] sArray = new double[size]; // array for staff sales
for (int i = 0 ; i < size; i++){
sales.nameArray(nArray,i, input);
input.nextLine();
sales.saleArray(sArray,i,input);
}
sales.salePercent(sArray, nArray);
in my class
public String inputStaff(){
Scanner user = new Scanner(System.in);
int size;
System.out.print("How Many Employees :\n");
size = user.nextInt();
user.nextLine(); //dummy to grab \n value from nextInt so nextline can function
String[] nArray =new String[size];
double[] dArray = new double[size];
int i =0;
for(i = 0 ; i < nArray.length;i++){
System.out.print("Enter name: ");
nArray[i] = user.nextLine();
System.out.print("Enter sales ($): ");
dArray[i] = user.nextDouble();
user.nextLine();
}
return null;
}
public static String[] nameArray(String[] nArray,int i,Scanner input){
System.out.print("Enter name: ");
nArray[i] = input.nextLine();
return nArray;
}
public static double[] saleArray(double[] sArray,int i,Scanner input){
System.out.print("Enter sales ($): ");
sArray[i] = input.nextDouble();
return sArray;
}

displays the student with the highest score

Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score.
I stuck at how do I display their name?
Here's my code:
package Exercises;
import java.util.Scanner;
public class Page93
{
public static void main(String[] args)
{
String name = null;
int count;
double score = 0;
double highest = 0;
Scanner input = new Scanner (System.in);
System.out.print("Enter the number of student : ");
int numberofstudent = input.nextInt();
for (count=0; count<numberofstudent; count++)
{
System.out.print("\nStudent name : ");
name = input.next().toUpperCase();
System.out.print("Score : ");
score = input.nextInt();
if (highest<score)
highest=score;
}
System.out.print("\nThe highest score : " + highest );
}
}
Define a variable studentWithHighestScore to store Student with the highest score. Update this variable whenerver you update highest.
if (highest<score) {
highest=score;
studentWithHighestScore = name
}
package Exercises;
import java.util.Scanner;
public class Page93
{
public static void main(String[] args)
{
String name = null;
int count;
double score = 0;
double highest = 0;
String highestName;
Scanner input = new Scanner (System.in);
System.out.print("Enter the number of student : ");
int numberofstudent = input.nextInt();
for (count=0; count<numberofstudent; count++)
{
System.out.print("\nStudent name : ");
name = input.next().toUpperCase();
System.out.print("Score : ");
score = input.nextInt();
if (highest<score)
{
highest=score;
highestName = name;
}
}
System.out.print("\nThe highest student : " + highestName + " score : " + highest );
}
}
import java.util.Scanner;
class Student {
String name;
String stu_id;
int score;
public Student() {
}
public Student(String initName, String initId, int initScore) {
name = initName;
stu_id = initId;
score = initScore;
}
}
class accept {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Input number of students:");
int n = Integer.parseInt(in.nextLine().trim()) ;
System.out.println("Input Student Name, ID, Score :");
Student stu = new Student();
Student max = new Student();
Student min = new Student("","", 0);
String [] arr1=new String [n];
String [] arr2=new String [n];
int [] arr3=new int [n];
for (int i = 0; i < n; i ++) {
arr1[i]=in.next();
arr2[i]=in.next();
arr3[i]=in.nextInt();
stu.name = arr1[i];
stu.stu_id = arr2[i];
stu.score = arr3[i];
if (max.score < stu.score) {
max.name = stu.name;
max.stu_id = stu.stu_id;
max.score = stu.score; }}
for(int j = 0; j < n; j ++){
stu.name = arr1[j];
stu.stu_id = arr2[j];
stu.score = arr3[j];
if (min.score < stu.score&&stu.score!=max.score) {
min.name = stu.name;
min.stu_id = stu.stu_id;
min.score = stu.score;
}
}
System.out.println("name, ID of the highest score and the second highest score:");
System.out.println(max.name + " " + max.stu_id);
System.out.println(min.name + " " + min.stu_id);
in.close();
}
}

Name and age application with specific output using arrays and output

For my university lab work we have to finish 4 tasks. I'm currently on 6 of 9 and have for the most part completed it, but I'm having difficulty in completing the final parts of it. This is the description of what we must do:
Write a program that defines two arrays - one of strings and one of integers, both of size 10.
Your program should then ask the user to enter the a string representing a persons name,
and an integer representing their age. It should continue to do this until either the user
enters ‘done’ instead of a name, or until the array is full (that is, 10 pairs of names and ages
have been entered). It should then print out the names and ages as well as the names of the
youngest and oldest.
Hint: One tricky part is making sure that once you’ve typed ‘done’ to Finish entering names,
your program does not then ask you for the age of the person with name ‘done’ - be careful
about this.
I've highlighted the issues I'm having above in bolded text. Below is the code I currently have, but I'm not sure how to properly accomplish the bolded text.
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
public class nameAge {
public static void main(String[] args){
String[] name = new String[10];
int[] age = new int[10];
Scanner in = new Scanner(System.in);
String NAME_REQUEST = ("Please enter name");
String AGE_REQUEST = ("Please enter age");
System.out.println("Please enter the name of a person and then their age. Do this for up to 10 people and once finished, type 'done'");
name[0] = in.nextLine();
System.out.println(AGE_REQUEST);
age[0] = in.nextInt();
System.out.println(NAME_REQUEST);
name[1] = in.next();
System.out.println(AGE_REQUEST);
age[1] = in.nextInt();
System.out.println(NAME_REQUEST);
name[2] = in.next();
System.out.println(AGE_REQUEST);
age[2] = in.nextInt();
System.out.println(NAME_REQUEST);
name[3] = in.next();
System.out.println(AGE_REQUEST);
age[3] = in.nextInt();
System.out.println(NAME_REQUEST);
name[4] = in.next();
System.out.println(AGE_REQUEST);
age[4] = in.nextInt();
System.out.println(NAME_REQUEST);
name[5] = in.next();
System.out.println(AGE_REQUEST);
age[5] = in.nextInt();
System.out.println(NAME_REQUEST);
name[6] = in.next();
System.out.println(AGE_REQUEST);
age[6] = in.nextInt();
System.out.println(NAME_REQUEST);
name[7] = in.next();
System.out.println(AGE_REQUEST);
age[7] = in.nextInt();
System.out.println(NAME_REQUEST);
name[8] = in.next();
System.out.println(AGE_REQUEST);
age[8] = in.nextInt();
System.out.println(NAME_REQUEST);
name[9]= in.next();
System.out.println(AGE_REQUEST);
age[9] = in.nextInt();
System.out.println(NAME_REQUEST);
int size = name.length;
int sizeN = age.length;
for (int i=0; i < size; i++) {
System.out.println("Name: " + name[i]);
System.out.println("Age: " + age[i]);
}
int smallest = age[0];
int largetst = age[0];
for(int i=1; i< age.length; i++)
{
if(age[i] > largetst)
largetst = age[i];
else if (age[i] < smallest)
smallest = age[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
You have to take a look on loop doc in java
this code may help you
public static void main(String[] args) {
int youngest =0,older=0;
String[] name = new String[10];
int[] age = new int[10];
String NAME_REQUEST = ("Please enter name");
String AGE_REQUEST = ("Please enter age");
for(int i=0 ; i< 10;i++){
Scanner in = new Scanner(System.in);
System.out.println(NAME_REQUEST);
String tmpName = in.nextLine();
if(tmpName.equalsIgnoreCase("done"))
break;
name[i] = tmpName;
System.out.println(AGE_REQUEST);
age[i] = in.nextInt();
if(age[i] > age[older])
older = i;
if(age[i] < age[youngest])
youngest = i;
}
System.out.println("OLDER is : " + name[older]);
System.out.println("Younger : " + name[youngest]);
}
Try this out, I have tested it and it's working fine. Hope that helps. Happy coding.
package com.pearson.nextgen.aggregatedsessionservice;
import java.util.Scanner;
public class NameAgeTest {
public static void main(String[] args) {
String[] name = new String[10];
int[] age = new int[10];
Scanner in = new Scanner(System.in);
String NAME_REQUEST = "Please enter name";
String AGE_REQUEST = "Please enter age";
int count = 0;
while (count < 10) {
System.out.println(NAME_REQUEST);
String nameInput = in.next();
if (nameInput.equalsIgnoreCase("done")) {
break;
}
name[count] = nameInput;
System.out.println(AGE_REQUEST);
age[count] = in.nextInt();
count++;
}
int[] minAndMaxIndex = findMinAndMaxIndex(age, count);
System.out.println("Youngest Person: " + name[minAndMaxIndex[0]]);
System.out.println("Oldest Person: " + name[minAndMaxIndex[1]]);
}
private static int[] findMinAndMaxIndex(int[] inputArray, int count) {
int min, max = 0;
int minIndex = 0, maxIndex = 0;
max = min = inputArray[0];
for (int i = 0; i < count; i++) {
if (inputArray[i] > max)
maxIndex = i;
else if (inputArray[i] < min)
minIndex = i;
}
return new int[] { minIndex, maxIndex };
}
}

Error: The operator += is undefined for the argument type(s) double, int[]

I am trying to use the operator += for one of the methods in my program which takes the total of the amounts of food for the gerbils, and divides it by the number of gerbils, in kind of like an average. The errors are the following:
"The operator += is undefined for the argument type(s) double, int[]" at the following code:
average += g.getAmountFood();
"The operator / is undefined for the argument type(s) Gerbil, int" at the following code:
average = gerbil[i] / gerbil.length
Here is my code for my first main Class:
import java.util.Scanner;
public class Gerbilfood {
static int n8;
static int n3;
static String n55;
static String n35;
static String n2;
public static Gerbil[] gerbil;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please input how many types of food items the gerbils eat as an integer");
String n0 = scanner.nextLine();
int n1 = Integer.parseInt(n0);
String[] food = new String[n1];
for (int i = 0; i < n1; i++) {
System.out.println("Please enter a food name");
String n2 = scanner.nextLine();
food[i] = n2;
int[] maximum = new int[n1];
System.out.println("Please enter maximum amount of this food per day");
String n33 = scanner.nextLine();
int n3 = Integer.parseInt(n33);
maximum[i] = n3;
}
System.out.println("Please enter in the number of gerbils in the lab");
String n73 = scanner.nextLine();
int n4 = Integer.parseInt(n73);
//gerbil = new Gerbil[n4];
gerbil = new Gerbil[n4];
int[] combo = new int[n4];
String[] ids = new String[n4];
for (int i = 0; i < n4; i++) {
//Gerbil g = new Gerbil(n1);
System.out.println("Please enter in the lab id for one of the gerbils");
String n5 = scanner.nextLine();
//g.setId(n5);
//ids[i] = n5;
//String[] names = new String[n4];
System.out.println("Please enter in the name given to gerbil whose lab id you just entered");
String n6 = scanner.nextLine(); // gerbil name
//g.setName(n6);
//String[] amountfood = new String[n1];
int[] amountfood = new int[n1];
for (int j = 0; j < n1; j++) {
System.out.println("how much of " + food[j]
+ " did this gerbil eat");
String n8 = scanner.nextLine();
//amountfood[j = n8;
amountfood[j] = Integer.parseInt(n8);
}
boolean[] bite = new boolean[n4];
System.out
.println("Does this Gerbil bite? Enter True or False");
String n77 = scanner.nextLine();
if (n77.equalsIgnoreCase("True")) {
bite[i] = true;
} else {
bite[i] = false;
}
boolean[] escape = new boolean[n4];
System.out
.println("Does this Gerbil escape? Enter True or False");
String n89 = scanner.nextLine();
if (n89.equalsIgnoreCase("True")) {
escape[i] = true;
} else {
escape[i] = false;
}
gerbil[i] = new Gerbil(n5, n6, amountfood, escape[i], bite[i], food);
}
System.out.println("What information would you like to know?");
String n55 = scanner.nextLine();
String n33 = "search";
String n34 = "average";
String n35 = "restart";
String n36 = "quit";
if (n55.equalsIgnoreCase(n34)) {
System.out.println(averagefood());
} else {
if (n55.equalsIgnoreCase(n33)) {
System.out.println("Please type the lab id of the gerbil you wish to search for");
String n87 = scanner.nextLine();
Gerbil g = searchForGerbil(n87);
Gerbil gerbilattributes = searchForGerbil(n87);
String gerbid = g.getId();
String gerbname = g.getName();
boolean gerbbite = g.getBite();
boolean gerbescape = g.getEscape();
for (int i = 0; i < n1; i++) {
food = g.getTypeFood();
}
int[] gerbfoods = g.getAmountFood();
System.out.print("Lab :"+gerbid + " Name:"+ gerbname + " ("+ ((gerbbite==true)?"will bite":"will not bite") + "," + ((gerbescape==true)?"will escape":"will not escape") + ")");
for (int i = 0; i < n1; i++) {
System.out.print( " " + food[i] + ":"+ gerbfoods[i]);
}
} else {
if (n55.equalsIgnoreCase(n35)) {
//GO BACK
} else {
if (n55.equalsIgnoreCase(n36)) {
System.exit(0);
} else {
System.out.println("ERROR");
}
}
}
}
}
public static String averagefood() {
// girbil[0] .. girbil[n] / n = average!!!
average = gerbil[i] / gerbil.length
double average = 0.0;
for (int i = 0; i <= gerbil.length; i++) {
Gerbil g = gerbil[i];
average += g.getAmountFood();
}
average /= gerbil.length;
for (int i = 0; i <= gerbil.length; i++) {
Gerbil g = gerbil[i];
String gid = g.getId();
String gname = g.getName();
String everything = gid + " " + gname + " " + average + "\n";
}
int i = 0;
Gerbil g = gerbil[i];
String gid = g.getId();
String gname = g.getName();
long percent = Math.round(n8 * 100.0 / n3);
String everything = gid + " " + gname + " " + percent + "\n";
for (i = 0; i <= gerbil.length; i++) {
//turn everything;
}
return everything;
}
public static Gerbil searchForGerbil(String n87) {
for (int i = 0; i < gerbil.length; i++) {
Gerbil g = gerbil[i];
if (n87.equals(g.getId())) {
return gerbil[i];
}
// return (new Gerbil[i]);
}
return null;
}
}
The following is my second "gerbil" class
public class Gerbil {
private String id;
private String name;
private int[] amountfood;
private int numbergerbils;
private String[] food;
private boolean escape;
private boolean bite;
public Gerbil(String n5, String n6, int[] numOfFood, boolean newEscape, boolean newBite, String[] n2) {
id = n5;
name = n6;
amountfood = numOfFood;
escape = newEscape;
bite = newBite;
food = n2;
}
public Gerbil(String[] typefood) {
food = typefood;
}
public Gerbil(int[] numOfFood) {
amountfood = numOfFood;
}
public int[] getAmountFood() {
return amountfood;
}
public boolean getBite() {
return bite;
}
public boolean getEscape() {
return escape;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setId(String newId) {
id = newId;
}
public void setName(String newName) {
name = newName;
}
public String[] getTypeFood() {
return food;
}
}
Just being general about what the errors mean :
The operator += is undefined for the argument type(s) double, int[]"
at the following code: average += g.getAmountFood();
Your getFoodAmount() is returning an int[] which is an array of integers. If you want to add the values, you need to loop over the array and use += on each number in the array.
Your second error:
The operator / is undefined for the argument type(s) Gerbil, int" at
the following code: average = gerbil[i] / gerbil.length
Well, uh, I couldn't locate the line. Help me out, please, and I shall gladly help you. ;D
The operator += is undefined for the argument type(s) double, int[]" at the following code: average += g.getAmountFood();
This is because method getAmountFood() is returning an array of integers.Declare an array and assign the array returned by the method getAmountFood() to the new array,then you can loop through it to access the individual elements.So you can do like this:
int[] amountFood = g.getAmountFood();
double average = 0;
for(int i : amountFood){
average += i;
}
average /= gerbil.length;
"The operator / is undefined for the argument type(s) Gerbil, int" at the following code: average = gerbil[i] / gerbil.length
Here you used i which is undeclared in this scope.And you declared i in previous for loops which can't be accessed from method averagefood().
And you should write
food = g.getTypeFood();
instead of :
for (int i = 0; i < n1; i++) {
food = g.getTypeFood();
}

Categories