Question
This program is supposed to an array of 800 integers 100 to 999. Then it is supposed to prompt the user to enter a number,
This program is supposed to an array of 800 integers 100 to 999. Then it is supposed to prompt the user to enter a number, and then print out the returned value. However, when ever I try and run it it says it has errors and does not compile.
My code
Main class
package main;
import lab.Lab3;
/** * @This program prints a clone of another array * @author jgemerick0 * * @version 2020.20.2 */ public class Main { public static void main(String[] args) { Lab3.test(); } }
MyArrayList2 class
/* * Made some changes to your code. * decrementing intialsize after removing an element. * printing numbers in set of 20. * File Name : MyArrayList.java */ package collection;
/* * @This program clones an array * @author jgemerick0 * @version 2020.20.2 */
public class MyArrayList2 implements Cloneable {
public Object[] obj; public int maxsize = 800, initialsize = 0;
public MyArrayList2() { obj = new Object[maxsize]; }
public void append(Object element) { if (initialsize < maxsize) obj[initialsize++] = element; else System.out.println("Full"); }
public void clear() { obj = new Object[maxsize]; }
public void contains(Object element) { int flag = 1; for (int i = 0; i < initialsize; i++) { if (obj[i] == element) { System.out.println("Found"); flag = 1; break; } } if (flag == 0) System.out.println("Not Found"); }
public Object elementAt(int index) { return obj[index]; }
public int indexOf(Object element) { for (int i = 0; i < initialsize; i++) { if (obj[i] == element) return i; } return -1; }
public void insertAt(int index, Object element) { for (int i = initialsize; i > index; i--) { obj[i] = obj[i - 1]; } obj[index] = element; }
public boolean isEmpty() { if (initialsize == 0) return true; return false; }
public void removedAt(int index) { for (int i = index; i < initialsize - 1; i++) { obj[i] = obj[i + 1]; } initialsize--; }
public void remove(Object element) { for (int i = 0; i < initialsize; i++) { if (obj[i] == element) { removedAt(i); break; } } }
public void replace(int index, Object element) { obj[index] = element; }
public int size() { return initialsize; }
public boolean ensureCapacity(int minCapacity) { if (initialsize >= minCapacity) return true; return false; }
public Object clone() throws CloneNotSupportedException { return super.clone(); }
public void removeRange(int fromIndex, int toIndex) { for (int i = fromIndex; i < toIndex; i++) { removedAt(i); } }
public String toString() { String res = " "; for (int i = 0; i < initialsize; i++) { res += obj[i]; } return res; }
public void reverse() { int last = initialsize - 1; for (int i = 0; i < (initialsize - 1) / 2; i++) { Object temp = obj[i]; obj[i] = obj[last]; obj[last] = temp; last--; } }
public void merge(MyArrayList arraylist2) { for (int i = 0; i < arraylist2.size(); i++) { obj[initialsize++] = arraylist2.elementAt(i); } }
public void print() { for (int i = 0; i < initialsize; i++) { if(i > 0 && (i%10 == 0)) { System.out.println(); } System.out.print(obj[i] + " "); } } }
The MySearch class
/* * File Name : MySearch.java */ package collection;
public class MySearch { public Object linearSearchSorted (MyArrayList arraylist, Comparable target) { for(int i = 0;i
}
The MySort class
/* * File Name : MySort.java */ package collection;
public class MySort { void bubbleSort(MyArrayList arraylist) throws Exception
{ for(int i =0;i The lab3 class. /* * 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 lab; import collection.MyArrayList2; import collection.MySearch; import collection.MySort; import java.util.Random; import java.util.Scanner; /** * * @author JD117 */ public class Lab3 { public static void test() { { MyArrayList2 numArraylist = new MyArrayList2(); Random rand = new Random(); rand.setSeed(java.lang.System.nanoTime()); Integer randNum; int high = 999; int low = 100; for(int i = 0;i<900;i++) { randNum = rand.nextInt(high-low) + low; numArraylist.append(randNum); } MySort sort = new MySort(); System.out.println(" Array Before Sort "); numArraylist.print(); sort.bubbleSort(numArraylist); System.out.println(" Array After Sort "); numArraylist.print(); Scanner in = new Scanner(System.in); System.out.print(" Enter Number to Search : "); Integer num = Integer.parseInt(in.nextLine()); MySearch search = new MySearch(); MyArrayList2 numArraylist2 = null; Object linearSearchSorted = search.linearSearchSorted(numArraylist2, num); System.out.print(" Is "+num + " in array List: " + (linearSearchSorted != null ? "Yes" : "No")); numArraylist.removeRange(50, 650); numArraylist.reverse(); System.out.println(" Array Before Sort "); numArraylist.print(); sort.selectionSort(numArraylist); System.out.println(" Array After Sort "); numArraylist.print(); System.out.print(" Enter Number to Search : "); num = Integer.parseInt(in.nextLine()); linearSearchSorted = search.binarySearch(numArraylist, num); System.out.print(" Is "+num + " in array List: " + (linearSearchSorted != null ? "Yes" : "No")); } }}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started