Question

[JAVA] help

——————
ListArrayMain.java
——————-
public class ListArrayMain {
public static void main (String[] args) {
ListArray L1 = new ListArray(); // constructs the list L1
L1.InsertBegin(2);
L1.InsertBegin(3);
L1.InsertBegin(7);
L1.InsertBegin(15);
L1.InsertBegin(5);
L1.InsertEnd(666);
L1.InsertAfter(1000,7);
// Now, let's print out the array to verify the insertions worked.
System.out.println("The items in the list are:");
for (int i = 0; i<= L1.lastCell; i++) {
System.out.println(L1.a[i]);
}
System.out.println("The last cell number is: " + L1.lastCell);
}// end main
}
———————————-
ListArray.java
————————————
public class ListArray {
//data fields
int a[]; // holds the list’s elements
int lastCell; //index of the last item in the list (array);
//cannot be greater than what the constructor allows
// Constructor
public ListArray() {
a = new int[10]; // sets up array of 10 cells, numbered 0 to 9,
//to hold the elements
lastCell = -1; // indicates an empty “list”
}
// Methods:
public boolean IsFull() {
if(lastCell==9) {
return true;
} else {
return false;
}
}
public boolean IsEmpty() {
/*if(lastCell==-1) {
return true;
} else {
return false;
}*/
return (lastCell==-1);
}
public void InsertBegin(int item) {
if (IsFull() == false) {
for (int i=lastCell;i>=0;i--) {
a[i+1] = a[i];
}
a[0]=item;
lastCell++;
} else {
System.out.println("list is full");
}
}
public void InsertEnd(int item) {
if (IsFull() == false) {
a[lastCell+1] = item;
lastCell++;
}
}
public void InsertAfter(int item, int datum) {
// insert an item after a specifed datum
int locationOfDatum = Find(datum);
// move list items to the right
for (int i=lastCell;i>=locationOfDatum+1;i--) {
a[i+1]=a[i];
}
a[locationOfDatum+1]=item;
lastCell++;
}
public int Find(int item) {
for (int i=0; i<=lastCell;i++) {
if (a[i]==item) {
return i;
}
};
return -1;
}
}//end class
————————————————————-
My question:
——————-

My question:

——————-—————————————————

1. Do the following two tasks. Submit the code in ListArrayMain.java that does the following: . In ListArrayMain, construct an array and create the array which is the reversal of the given array and print it out. In ListArrayMain, construct two arrays of the same size and produce an array that interlaces them, and prints out the interlaced array. Example if array a consists of data 2, 5, 3, 6 and array b consists of 12,7, 4, 2, then the interlaced array should be 2, 12, 5, 7, 3, 4, 6, 2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the updated ListArrayMain class which includes solution for both problems.

---------------------------

ListArrayMain.java

----------------------------

public class ListArrayMain {

public static void main(String[] args) {

ListArray L1 = new ListArray(); // constructs the list L1

L1.InsertBegin(2);

L1.InsertBegin(3);

L1.InsertBegin(7);

L1.InsertBegin(15);

L1.InsertBegin(5);

L1.InsertEnd(666);

L1.InsertAfter(1000, 7);

// Task1

System.out.println("---Task1---");

// Now, let's print out the array to verify the insertions worked.

System.out.println("The items in the list are:");

for (int i = 0; i <= L1.lastCell; i++) {

System.out.println(L1.a[i]);

}

System.out.println("The last cell number is: " + L1.lastCell);

ListArray reverseArray = new ListArray();

for (int i = L1.lastCell; i >= 0; i--) {

if (i == L1.lastCell) {

reverseArray.InsertBegin(L1.a[i]);

} else if (i == 0) {

reverseArray.InsertEnd(L1.a[i]);

} else {

reverseArray.InsertAfter(L1.a[i], L1.a[i + 1]);

}

}

System.out.println("The items in the reverse list of ListArray L1 are:");

for (int i = 0; i <= reverseArray.lastCell; i++) {

System.out.println(reverseArray.a[i]);

}

// Task2- Interlaced Array

System.out.println("---Task2---");

ListArray L2 = new ListArray(); // constructs the list L2

L2.InsertBegin(2);

L2.InsertAfter(5, 2);

L2.InsertAfter(3, 5);

L2.InsertEnd(6);

System.out.println("The items in the list L2 are:");

for (int i = 0; i <= L2.lastCell; i++) {

System.out.println(L2.a[i]);

}

ListArray L3 = new ListArray(); // constructs the list L3

L3.InsertBegin(12);

L3.InsertAfter(7, 12);

L3.InsertAfter(4, 7);

L3.InsertEnd(2);

System.out.println("The items in the list L3 are:");

for (int i = 0; i <= L3.lastCell; i++) {

System.out.println(L3.a[i]);

}

ListArray interlacedListArray = new ListArray();

for (int i = 0; i <= L2.lastCell; i++) {

if (i == 0) {

interlacedListArray.InsertBegin(L2.a[i]);

interlacedListArray.InsertAfter(L3.a[i], L2.a[i]);

}else if (i == L2.lastCell) {

interlacedListArray.InsertAfter(L2.a[i], L3.a[i - 1]);

interlacedListArray.InsertEnd(L3.a[i]);

} else {

interlacedListArray.InsertAfter(L2.a[i], L3.a[i - 1]);

interlacedListArray.InsertAfter(L3.a[i], L2.a[i]);

}

}

System.out.println("Interlaced Array of ListArrays L2 and L3");

for (int i = 0; i <= interlacedListArray.lastCell; i++) {

System.out.println(interlacedListArray.a[i]);

}

}// end main

}

Add a comment
Know the answer?
Add Answer to:
[JAVA] help —————— ListArrayMain.java ——————- public class ListArrayMain { public static void main (String[] args) {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])...

    Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])    {        Scanner s=new Scanner(System.in);        ScoresCircularDoubleLL score=new ScoresCircularDoubleLL();        while(true)        {            System.out.println("1--->Enter a number\n-1--->exit");            System.out.print("Enter your choice:");            int choice=s.nextInt();            if(choice!=-1)            {                System.out.print("Enter the score:");                int number=s.nextInt();                GameEntry entry=new GameEntry(number);   ...

  • import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) {...

    import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • What is the output of following program: public class Test{ public static void main(String[] args) {...

    What is the output of following program: public class Test{ public static void main(String[] args) { A a = new A(): a method B(): } } class A{ public A(){ System out println("A's constructor is executed"): } public void method(){ System out printin ("methodA is executed"): } public void methodAB(){ System out printin ("As methodAB is executed"): } } class B extends A { private int num = 0: public B (){ super(): System out printin ("B's constructor is executed"):...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {...

    import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 {    public static void main(String[] args)    {        String name;        String answer;        int correct = 0;        int incorrect = 0;        Scanner phantom = new Scanner(System.in);        System.out.println("Hello, What is your name?");        name = phantom.nextLine();        System.out.println("Welcome " + name + "!\n");        System.out.println("My name is Danielle Brandt. "            +"This is a quiz program that...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT