Question

In: Computer Science

Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...

Java Problem: Please answer both parts of the question fully:

(a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4. Any sequence containing only two values is Fibonacci. If the list has <=1 value, then it is NOT Fibonacci. Make sure to test the code for correctness before submission and you don’t need to submit test results.

public boolean isFibonacci(LinkedList<Long> li) {

//

}
Estimate in big-oh notation the run-time complexity of the method as a function of the size of the list n.

(b). Write Java code for extending the ArrayList<E> class of java.util.* to ExtArraayList<E> that includes the following one method:

public boolean equals(ExtArrayList<E> eal)

// returns true if the values in this list is the same as in eal. For instance, if E is Integer and this has {2,1,3,1,6} and eal has {1,2,3,1,6} then you should return true. If eal has {1,2,3,6}, you should return false. Clearly the size of each should be the same but obviously there is more to it than that. You can assume the existence of equals()method for testing equality of E. Do not use specific types like String or Integer. Estimate the run-time complexity of the methods assuming the size of the list is n.

Solutions

Expert Solution

(a)

/* method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise.*/

public boolean isFibonacci(LinkedList<Long> li) {

       // check if size is atleast 2   

if(li.size() >= 2)

             {

                    // get the first 2 values of the linked list

                    long f1 = li.get(0);

                    long f2 = li.get(1);

                    /* loop over the linked list, starting from index 2

                    and check if this element is sum of the previous 2, if not return false, else assign 2nd element to 1st element and assign the current element to 2nd element*/

                    for(int i=2;i<li.size();i++)

                    {

                           if(li.get(i) != (f2+f1))

                                 return false;

                           f1= f2;

                           f2 = li.get(i);

                    }

                   

                    return true;

             }

            

             return false;

}

//end of method

The time complexity of the method is O(n) since the for loop runs for a maximum of n times, where n is the size of the linked list. The worst time complexity will be of O(n).

(b)

public class ExtArrayList<E> extends ArrayList<E> {

             

       // returns true if the values in this list is the same as in eal

public boolean equals(ExtArrayList<E> eal)

       {

             // check if size is same

             if(size() == eal.size())

             {

                    int count1, count2;

                    // loop over the calling ArrayList

                    for(int i=0;i<size();i++)

                    {

                           count1 = 0;

                           count2 = 0;

             // loop to count the number of times ith element occurs in this list

                           for(int j=0;j<size();j++)

                                 if(get(i).equals(get(j)))

                                        count1++;

       // loop to count the number of times ith element occurs in this passed list

                           for(int j=0;j<eal.size();j++)

                                 if(get(i).equals(eal.get(j)))

                                        count2++;

       // check if both counts are same or not, if not then return false              

                           if(count1 != count2)

                                 return false;

                    }

                   

                    return true;

             }

            

             return false; // if size is not the same

       }

}

The time complexity of the method is O(n2) because the outer loop runs n times, where n is the size of the list and to count the number of times each element occurs in both lists, the for loop runs n times. So the inner loop also runs for n times giving the complexity of O(n2)


Related Solutions

Please answer both parts of this question. Please answer both parts of this question. Question: a)...
Please answer both parts of this question. Please answer both parts of this question. Question: a) The channel in question 2 now has a 0.3m high smooth extended shelf built across its base to cover a submerged pipeline but still carries 50m3/s. Plot the specific energy diagram for 0 < y < 4m and calculate the critical depth and minimum specific energy. What are now the two possible flow depths for a specific energy of 4m upstream of the obstructions?...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better...
This is an entry to Java Question. Please answer with Pseudocode and Java code for better understanding. We are mainly using basic in and out declarations and are focusing on API for method invocations. Any help would be very appreciated :) Problem 7: Distance (10 points) Use API (Data Science) Data Science is an emergent field from Computer Science with applications in almost every domain including finances, medical research, entertainment, retail, advertising, and insurance. The role of a data analyst...
This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
Please answer with a fully detailed solution with examples. In Java, write a method to determine...
Please answer with a fully detailed solution with examples. In Java, write a method to determine if a string contains only digit characters. In Java, write a method on how to find each occurrence of a letter given a string. In Java, given a list of numbers, write an algorithm to find all the triples and pairs of numbers in the list that equal exactly 13. In Java, write a function to find the total number of each character in...
Please answer all parts of this question for both parts!! Prompt: In our study of the...
Please answer all parts of this question for both parts!! Prompt: In our study of the skeletal system this week it should be readily apparent to you how important synovial joints are in the normal function of the human body. This week you will pick two synovial joints to discuss. Pick one from each category below. For each selected joint you should provide the following information: the movements that are possible at that joint (use appropriate anatomical terminology), identification of...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep...
Write in Java. Separate code for each question. The answer to the first question should NOT...
Write in Java. Separate code for each question. The answer to the first question should NOT be comparable. 1. Write a class to represent a AlternativeEnergyCar. Select the fields and methods that fit the modeling of an alternative energy car. Make sure to include code for the constructors, set/get methods, a toString() method. 2. Inheritance – Create two abstract subclasses of AECar, one for Electric cars and one for Altfuel cars. Next create four additional subclasses., two for types of...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Please answer all the parts of this question Please answer all the parts of this question...
Please answer all the parts of this question Please answer all the parts of this question Question: a) Where is the flow accelerating in the control volume? b) Will the pressure be greater or less than hydrostatic in a region of accelerating flow? c) Is the hydrostatic estimate of force on the gate; larger, smaller, or the same, as that obtained from the Momentum Equation? Explain. d) Where does the change in flow momentum go in this control volume? Explain.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT