Question

COMP Help (Java): Can someone please help me with this problem! Someone answered it but it...

COMP Help (Java): Can someone please help me with this problem! Someone answered it but it was wrong.

1. Create an ExtArrayList<E> class by extending the ArrayListE> class to include the following methods and estimate the run-time complexity of each method.

  1. public ExtArrayList<E> append(ExtArrayList<E> ea)
    // appends the values in ea to the end of this ExtArrayList and returns the new ExtArrayList //example: this: {1,2,3} parameter : {4,5}, result {1,2,3,4,5}
  2. public boolean consecutivePair(E e1, E e2)
    // checks if this ExtArrayList has two consecutive elements that are equal to e1 and e2 from the //beginning to the end of the list in that order and returns true, otherwise returns false. You can //assume the existence of an appropriately defined equals() method.
    // you need to handle errors for different situations.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ExtArrayList.java

import java.util.ArrayList;

/**
*
* @author owner
* @param <E>
*/
public class ExtArrayList<E> extends ArrayList<E> {
  
// appends the values in ea to the end of this ExtArrayList and returns the new ExtArrayList
//example: this: {1,2,3} parameter : {4,5}, result {1,2,3,4,5}
public ExtArrayList<E> append(ExtArrayList<E> ea){
ExtArrayList<E> result;
result = new ExtArrayList<>();

///append all elements of this(calling objects) list into result list
for (E obj:this)
{   
result.add(obj);
}   
///append all elements of list ea(passed as argument)into result list
for (E obj:ea)
{   
result.add(obj);
}   
return result;
}////ends append

  
//checks if this ExtArrayList has two consecutive elements that are equal to e1 and e2 from the
//beginning to the end of the list in that order and returns true, otherwise returns false.
public boolean consecutivePair(E e1, E e2){
///iterate from first element to second last element of this calling objects list
for(int i=0;i<this.size()-1;i++){
//if the current i element is matched with e1
//then check if i+1 th element is matches with e2
if(this.get(i).equals(e1)){
if(this.get(i+1).equals(e2)){
return true;
}
}
//if the current i element is matched with e2
//then check if i+1 th element is matches with e1
else if(this.get(i).equals(e2)){
if(this.get(i+1).equals(e1)){
return true;
}
}
}
//no match found
return false;
}

///function to print this list in format --> { 1 2 3 }
public void print(){
System.out.print("{ ");
for (int i=0;i<this.size();i++)
{
System.out.print(this.get(i)+" ");
}
System.out.println("}");

}
public static void main(String[] args) {

  
ExtArrayList<Integer> list1=new ExtArrayList<>();
ExtArrayList<Integer> list2=new ExtArrayList<>();
list1.add(1);
list1.add(2);
list1.add(2);
list1.add(3);
System.out.print("list1 =");
list1.print();
list2.add(4);
list2.add(5);
System.out.print("list2 =");
list2.print();
ExtArrayList<Integer> result=list1.append(list2);
System.out.print("result =");
result.print();
  
System.out.println("<2,3> in list1 ="+list1.consecutivePair(2,3));
System.out.println("<1,2> in list1 ="+list1.consecutivePair(1,2));
System.out.println("<2,1> in list1 ="+list1.consecutivePair(2,1));
System.out.println("<3,2> in list1 ="+list1.consecutivePair(3,2));
System.out.println("<1,3> in list1 ="+list1.consecutivePair(1,3));
System.out.println("<3,1> in list1 ="+list1.consecutivePair(3,1));
}

}

OUTPUT


Add a comment
Know the answer?
Add Answer to:
COMP Help (Java): Can someone please help me with this problem! Someone answered it but it...
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
  • JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out...

    JAVA HELP FOR COMP: Can someone please modify the code to create LowestGPA.java that prints out the name of the student with the lowestgpa. In the attached zip file, you will find two files HighestGPA.java and students.dat. Students.dat file contains names of students and their gpa. Check if the code runs correctly in BlueJ or any other IDE by running the code. Modify the data to include few more students and their gpa. import java.util.*; // for the Scanner class...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • Can someone please help with this in JAVA? Write one application program by using the following...

    Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think...

    I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item)       Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

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
Active Questions
ADVERTISEMENT