Create a solution using all of the following. 1. call a method that will print a
ID: 3641064 • Letter: C
Question
Create a solution using all of the following.1. call a method that will print a string that contains your explanation of at least three differences between an array and an arraylist
2. call a method to create an array using these numbers
{50,48,43,50,43,50,46,49,38,47,46,36,45,50,49,50,37,46,50,46,49} sort them and send them to the printArray() method for printing to the screen
3. call a method that creates 4 arrays array x1 array x2 array y1 array y2). Populate each array with 1000 random numbers between 1 and 100. (your choice of doubles or ints). Use a loop to calculate using the distance formula
* http://en.wikipedia.org/wiki/Distance - save all the calculated distances to a grandtotal variable and print the average of all the distances calculated
4. call a method that takes this list {50,48,43,50,18,43,50,46,49,38,47,46,36,45,50,49,50,37,46,50,46,49} create an array and then loop through the array and assign all the values to an arraylist using a loop and the .add() method. Use the .indexOf() (see java 7 API) function of the arraylist to print out the location of the number "18".
5. call a method that declares an arrayList - use the .add() method to add two elements (18,34) (Watch the data type!) - then send the arrayList to the printArrayList() method for printing.
Explanation / Answer
public class Practice {
public static void difference() {
System.out.println("1. ArrayList support dynamic resizing unlike arrays.");
System.out.println("2. ArrayList is part of java Collections class.");
}
public static void createArray() {
int[] array = {50,48,43,50,43,50,46,49,38,47,46,36,45,50,49,50,37,46,50,46,49};
printArray(array);
}
public static void printArray(int[] array) {
for(int i=0;i<array.length;i++) {
System.out.print(array[i]+ " ");
}
System.out.println();
}
public static void createArrayList() {
int[] array = {50,48,43,50,18,43,50,46,49,38,47,46,36,45,50,49,50,37,46,50,46,49};
java.util.ArrayList arrayList = new java.util.ArrayList();
for(int i=0;i<array.length; i++) {
arrayList.add(new Integer(array[i]));
}
printArrayList(arrayList);
System.out.println();
System.out.println("After adding 18 and 34 : ");
arrayList.add(new Integer(18));
arrayList.add(new Integer(34));
printArrayList(arrayList);
}
public static void printArrayList(java.util.ArrayList arrayList) {
for(int i=0;i<arrayList.size(); i++){
System.out.print(arrayList.get(i)+" ");
}
System.out.println();
System.out.println("Index of ArrayList 18: "+arrayList.indexOf(18));
}
public static void createFourArrays() {
double[] x1 = new double[1000];
double[] x2 = new double[1000];
double[] y1 = new double[1000];
double[] y2 = new double[1000];
double grandTotal=0;
java.util.Random rand = new java.util.Random();
for(int i=0;i<x1.length;i++) {
x1[i] = (rand.nextInt(100)+1);
x2[i] = (rand.nextInt(100)+1);
y1[i] = (rand.nextInt(100)+1);
y2[i] = (rand.nextInt(100)+1);
grandTotal = grandTotal+ Math.sqrt(Math.pow(x2[i]-x1[i], 2) + Math.pow(y2[i]-y1[i], 2));
}
System.out.println("Average Of All distances : "+(grandTotal/1000));
}
public static void main(String[] args) {
Practice.createArray();
Practice.createArrayList();
Practice.createFourArrays();
}
}
public class Practice {
public static void difference() {
System.out.println("1. ArrayList support dynamic resizing unlike arrays.");
System.out.println("2. ArrayList is part of java Collections class.");
}
public static void createArray() {
int[] array = {50,48,43,50,43,50,46,49,38,47,46,36,45,50,49,50,37,46,50,46,49};
printArray(array);
}
public static void printArray(int[] array) {
for(int i=0;i<array.length;i++) {
System.out.print(array[i]+ " ");
}
System.out.println();
}
public static void createArrayList() {
int[] array = {50,48,43,50,18,43,50,46,49,38,47,46,36,45,50,49,50,37,46,50,46,49};
java.util.ArrayList arrayList = new java.util.ArrayList();
for(int i=0;i<array.length; i++) {
arrayList.add(new Integer(array[i]));
}
printArrayList(arrayList);
System.out.println();
System.out.println("After adding 18 and 34 : ");
arrayList.add(new Integer(18));
arrayList.add(new Integer(34));
printArrayList(arrayList);
}
public static void printArrayList(java.util.ArrayList arrayList) {
for(int i=0;i<arrayList.size(); i++){
System.out.print(arrayList.get(i)+" ");
}
System.out.println();
System.out.println("Index of ArrayList 18: "+arrayList.indexOf(18));
}
public static void createFourArrays() {
double[] x1 = new double[1000];
double[] x2 = new double[1000];
double[] y1 = new double[1000];
double[] y2 = new double[1000];
double grandTotal=0;
java.util.Random rand = new java.util.Random();
for(int i=0;i<x1.length;i++) {
x1[i] = (rand.nextInt(100)+1);
x2[i] = (rand.nextInt(100)+1);
y1[i] = (rand.nextInt(100)+1);
y2[i] = (rand.nextInt(100)+1);
grandTotal = grandTotal+ Math.sqrt(Math.pow(x2[i]-x1[i], 2) + Math.pow(y2[i]-y1[i], 2));
}
System.out.println("Average Of All distances : "+(grandTotal/1000));
}
public static void main(String[] args) {
Practice.createArray();
Practice.createArrayList();
Practice.createFourArrays();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.