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

Want the solutions in Netbeans Your assignment is to use the Array List code (pr

ID: 3919487 • Letter: W

Question

Want the solutions in Netbeans

Your assignment is to use the Array List code (provided in code examples) to manage the produce files that was begun in class.

Array list code

package pkg2100hwrk;

import java.util.ArrayList;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author b.craven
*/


public class ArrayListTest {

public static void main(String args[]) {

/*add String elements so make it of string type */
// take note that this is an "ArrayList" and not a simple array
ArrayList<String> nme = new ArrayList<String>();

/*This is how the intital elements should be added to the array list*/
nme.add("Dallas");
nme.add("Boston");
nme.add("Hartford");
nme.add("Cheyenne");
nme.add("Portland");

/* Displaying array list elements as initialized */
System.out.println("The array list has following names: " + nme + " ");
/*Add element at the given array index*/

nme.add(0, "Boise");
nme.add(0, "Cincinnati");
nme.add(6, "Northeastern");
nme.add(0, "University");

/*elements can be directly removed like this*/
System.out.println("The modified array list has the following names:" + nme + " ");
/*elements can be removed by the index reference*/

nme.remove("Boise");
nme.remove("Dallas");
System.out.println("The array list now has the following names:" + nme + " ");

String remove; // this will be the index value 1 which is Hartford
remove = nme.remove(0b11);

System.out.println("The array list now has the following names:" + nme + " ");

int ln = nme.size();
  
System.out.println("The size of the array is now " + ln + " elements ");
  
  
}

}

Produce.Java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2100hwrk;

/**
*
* @author bryan
*/

class Produce {
  
String []apples={"apples","Macintosh","Empire","Golden Delicious","Fiji","Banana"};
String []pears={"pears","Macintosh","Empire","Golden Delicious"};
String []onions={"onions","Macintosh","Empire","Golden Delicious","hd","hagsjg","uwhdiq"};
  
}

Produce Class exercise.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg2100hwrk;

/**
*
* @author bryan
*/
public class ProduceClassExer extends Produce {

public static void main(String[] args) {
Produce Items = new Produce();
System.out.println("The number of " + Items.apples[0] + " varieties is: " + (Items.apples.length - 1));
System.out.println("The number of pear varieties is: " + (Items.pears.length - 1));
System.out.println("The number of onion varieties is: " + (Items.onions.length - 1));
}

}

Explanation / Answer

ProduceClassExer.java

import java.util.ArrayList;

class Produce {
private ArrayList<String> apples = new ArrayList<String>();
private ArrayList<String> pears = new ArrayList<String>();
private ArrayList<String> ArrayList<String>();

public Produce() {

/*Elements are added to the array list*/
apples.add("apples");
apples.add("Macintosh");
apples.add("Empire");
apples.add("Golden Delicious");
apples.add("Fiji");
apples.add("Banana");

/*Elements are added to the array list*/
pears.add("pears");
pears.add("Macintosh");
pears.add("Empire");
pears.add("Golden Delicious");

/*Elements are added to the array list*/
onions.add("onions");
onions.add("Macintosh");
onions.add("Empire");
onions.add("Golden Delicious");
onions.add("hd");
onions.add("hagsjg");
onions.add("uwhdiq");

}

/* function to return the apple list */
public ArrayList<String> getAppleList()
{
return apples;
}

/* function to return the pears list */
public ArrayList<String> getPearsList()
{
return pears;
}

/* function to return the onion list */
public ArrayList<String> getOnionList()
{
return onions;
}

  
}

public class ProduceClassExer extends Produce {

public static void main(String[] args) {
Produce Items = new Produce();
ArrayList<String> apples = Items.getAppleList();
ArrayList<String> pears = Items.getPearsList();
ArrayList<String>> System.out.println("The number of " + apples.get(0) + " varieties is: " + (apples.size()));
System.out.println("The number of pear varieties is: " + (pears.size()));
System.out.println("The number of onion varieties is: " + (onions.size()));
}

}

Output:

The number of apples varieties is: 6                                                                                           

The number of pear varieties is: 4                                                                                             

The number of onion varieties is: 7