Create two array methods for the following program. 1. public void fillArray(Cla
ID: 3625917 • Letter: C
Question
Create two array methods for the following program.1. public void fillArray(ClassName[] arrayName) - will fill an array by reading class objects and putting them in the array.
2. public ClassName getAverage(ClassName[] arrayName) and will find the average of the array and return it to the demo program.
***************************************************************************************************
BottleDemo.java
import java.util.Scanner;
// test driver for the Bottle class
public class BottleDemo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int x;
Bottle b1 = new Bottle();
Bottle b2 = new Bottle();
Bottle b3 = new Bottle();
Bottle b4 = new Bottle();
Bottle bottleAve = new Bottle();
System.out.println("please enter a number for bottle1:");
b1.readBottle();
System.out.println("bottle1 is this value " + b1);
System.out.println("please enter a number for bottle2:");
b2.readBottle();
System.out.println("please enter a number for bottle3:");
b3.readBottle();
System.out.println("please enter a number for bottle4:");
b4.readBottle();
bottleAve = bottleAve.add(b1);
bottleAve = bottleAve.add(b2);
bottleAve = bottleAve.add(b3);
bottleAve = bottleAve.add(b4);
bottleAve.divide(4);
System.out.println("the 4 bottle average is: " + bottleAve);
if (b1.equals(b3))
{
System.out.println("bottle1 and bottle3 are equal");
}
else
{
System.out.println("bottle1 and bottle3 are not equal");
}
System.out.println("Enter an integer to add to bottle 1");
x = scan.nextInt();
b1.add(x);
System.out.println("adding your number " + x +
" to bottle1 gives " + b2);
b2 = b1.add(b3);
System.out.print("adding the number " + b3 + " to ");
System.out.println("bottle1 gives " + b2);
}
}
***************************************************************************************************
Bottle.java
import java.io.*;
import java.util.Scanner;
import java.io.*;
public class Bottle
{
private int bullets;
public Bottle()
{
}
public Bottle(int b)
{
bullets=b;
}
public void readBottle()
{
Scanner keyboard = new Scanner(System.in);
bullets=keyboard.nextInt();
}
public void set(int bull)
{
if(bull<0)
{
System.out.println("Bullets can't be negative");
bullets=0;
}
bullets=bull;
}
public void set(Bottle b1)
{
if(b1.bullets<0)
{
System.out.println("Bullets can't be negative");
bullets=0;
}
else
bullets=b1.bullets;
}
public Bottle add(Bottle b1)
{
Bottle new1=new Bottle();
new1.set(bullets+b1.bullets);
return new1;
}
public Bottle subtract(Bottle b1)
{
Bottle new1=new Bottle();
int sub;
sub=bullets-b1.bullets;
if(sub<0)
{
System.out.println("Bullets can't be negative");
new1.set(bullets);
}
else
new1.set(bullets-b1.bullets);
return new1;
}
public Bottle multiply(Bottle b1)
{
Bottle new1=new Bottle();
new1.set(bullets*b1.bullets);
return new1;
}
public Bottle divide(Bottle b1)
{
Bottle new1=new Bottle();
new1.set(bullets/b1.bullets);
return new1;
}
public void add(int value)
{
bullets+=value;
}
public void subtract(int value)
{
int sub;
sub=bullets-value;
if(sub<0)
System.out.println("Bullets can't be negative");
else
bullets-=value;
}
public void multiply(int value)
{
bullets*=value;
}
public void divide(int value)
{
bullets/=value;
}
public boolean equals(Bottle b1)
{
if (bullets==b1.bullets)
return true;
else
return false;
}
public String toString()
{
String str="Bullets:"+bullets;
return str;
}
}