Question

This is the third time I'm posting this request, its a very confusing lab. Please help...

This is the third time I'm posting this request, its a very confusing lab. Please help

My class has lab this week following these guidelines. If anyone could construct a code in JAVA with helpful comments, I would greatly appreciate it! :)

1. Create a class level method that takes an ArrayList of doubles and returns the average value using a wrapper class correspondent to double?

2. Create a class level method that takes an ArrayList of integers, store its reverse in new ArrayList and return the answer.

3. Create a class level method that takes an ArrayList of characters and swaps the first element for last element. (No need to return the ArrayList, why?)

4. Create a class level method that takes an ArrayList of bytes and returns the max value, returns the maximum value using wrapper classes correspondence for bytes?

5. Create a class level method takes an Arraylist of doubles and an ArrayList of integers and return an ArrayList that includes all the double and integers values inserted into it alternatively. 6. Create class level method that takes ArrayList and prints its elements with a tap between them?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.Arrays;

class AL{
    //Method to find average of the ArrayList
    public double getAvg(ArrayList<Double> list){
        double ans = 0;
        //Find sum of all elements in the list
        for(double x : list){
            ans += x;
        }
        //Return average
        return ans/list.size();
    }

    //Method to return the reverse ArrayList
    public ArrayList<Integer> getReverse(ArrayList<Integer> list){
        //Initialize ans
        ArrayList<Integer> ans = new ArrayList<>();
        //Iterate in reverse direction and add to ans
        for(int i=list.size()-1;i>=0;i--){
            ans.add(list.get(i));
        }
        return ans;
    }

    //Method to swap first and last char of the ArrayList
    public void swap(ArrayList<Character> list){
        Character t = list.get(0);
        list.set(0, list.get(list.size()-1));
        list.set(list.size()-1,t);
    }

    //Method to return the maximum byte in the arraylist
    public byte getMax(ArrayList<Byte> bytes){
        //Initialize ans
        Byte ans = bytes.get(0);
        //Iterate through al elements and update the answer
        for(Byte x : bytes){
            if(x>ans){
                ans = x;
            }
        }
        return ans;
    }

    //Method to alternatively add elements from 2 lists into a single list
    public ArrayList alternate(ArrayList<Double> doubles, ArrayList<Integer> ints){
        ArrayList ans = new ArrayList();
        int i=0,j=0;
        //Add double and int arrays to ans
        for(;i<doubles.size()&&j<doubles.size();j++,i++){
            ans.add(doubles.get(i));
            ans.add(ints.get(j));
        }
        //Add remaining elements 
        for(;i<doubles.size();i++){
            ans.add(doubles.get(i));
        }
        for(;j<ints.size();j++){
            ans.add(ints.get(j));
        }
        return ans;
    }

    //Method to print arraylist elements
    public void print(ArrayList list){
        for(int i=0;i<list.size();i++){
            System.out.print(list.get(i));
            if(i!=list.size()-1){
                System.out.print(" | ");
            }
        }
        System.out.println();
    }
}

class Main{
    public static void main(String[] args) {
        AL al = new AL();
        ArrayList<Double> dl= new ArrayList<Double>(Arrays.asList(1.38, 2.56, 4.3));
        System.out.println("Average of "+dl+" is "+al.getAvg(dl));
        ArrayList<Integer> il= new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
        System.out.println("Reverse of "+il+" is "+al.getReverse(il));
        ArrayList<Character> cl= new ArrayList<Character>(Arrays.asList('a','b','c','d'));
        al.swap(cl);
        System.out.println("Swapping first and last of "+cl+" is "+cl);
        ArrayList<Byte> bl= new ArrayList<Byte>(Arrays.asList(new Byte((byte)2),new Byte((byte)6),new Byte((byte)3),new Byte((byte)9),new Byte((byte)5)));
        System.out.println("Maximum value of "+bl+" is "+al.getMax(bl));
        System.out.println("Alternating "+dl+" "+il+" is "+al.alternate(dl,il));
        System.out.println("Integer list is ");
        al.print(il);
    }
}

OUTPUT :

Run: → Main x Maximum value of [2, 6, 3, 9, 5] is 9 Alternating (1.38, 2.56, 4.3] [1, 2, 3, 4, 5] is [1.38, 1, 2.56, 2, 4.3,

Add a comment
Know the answer?
Add Answer to:
This is the third time I'm posting this request, its a very confusing lab. Please help...
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 PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • This is code for c++. Any help would be great. #include <iostream> using namespace std; /*...

    This is code for c++. Any help would be great. #include <iostream> using namespace std; /* 1. Create a method that prints your name */ /* 2. Create an overloaded method that takes an integer and prints it out multiplied * by itself * Print out the argument inside the method and then call the method from main * and print it out * What was the typed of passing used inhere ? */ /* 3. Create an overloaded method...

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

  • How to Submit Please submit your answers to the lab instructor once you have completed. Failure...

    How to Submit Please submit your answers to the lab instructor once you have completed. Failure to submit will result in a ZERO FOR THIS LAB. NO EXCEPTIONS. 1. Implement a method factorial(int n), that takes a number (int) and returns its factorial. Factorial of an integer is the product of all positive integers less than or equal to it. Method needs to be implemented using recursion. 2. Implement a method fibonacci(int n), that takes a number (int) and prints...

  • Please write a C# program For this part of the lab, you'll be writing methods to...

    Please write a C# program For this part of the lab, you'll be writing methods to work with 1D arrays. We will expect you to know how to create an array, and store and retrieve information from them. Continue working in the same project and “Program.cs” file. You will be generating random numbers, but the class Random is part of the standard library (don’t need any additional using statements). You should consider developing the methods for this project incrementally. In...

  • Create a new project in BlueJ and name it LastName-lab8-appstore, e.g., Smith-lab8-appstore. If your App class from Lab 4 is fully functional, copy it into the project. (You can drag a file from File...

    Create a new project in BlueJ and name it LastName-lab8-appstore, e.g., Smith-lab8-appstore. If your App class from Lab 4 is fully functional, copy it into the project. (You can drag a file from File Explorer onto the BlueJ project window.) Otherwise, use the instructor's App class: Create a new class using the "New Class..." button and name it App. Open the App class to edit the source code. Select and delete all the source code so that the file is...

  • Please solve the following, please type out the code, and do not hand write because its...

    Please solve the following, please type out the code, and do not hand write because its hard to read. I would greatly appreciate it. (JAVA) Problem 1 In a class called H1P1, write a method called bmiOne that takes as arguments a mass in kilograms (a double) and a height in meters (also a double), and returns the body mass index (or BMI) for the given data. If we have mass -m kg and height-h meters, then BMI = Write...

  • Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public...

    Please help me to solve this source code. Use the following file: InterfaceRunner.java import java.util.ArrayList; public class InterfaceRunner { public static void main(String[] args) { ArrayList<GeometricSolid> shapes = new ArrayList<>(); shapes.add(new Sphere(10)); shapes.add(new Sphere(1)); shapes.add(new Cylinder(1, 5)); shapes.add(new Cylinder(10, 20)); shapes.add(new RightCircularCone(1, 5)); shapes.add(new RightCircularCone(10, 20)); /* * Notice that the array list holds different kinds of objects * but each one is a GeometricSolid because it implements * the interface. * */ for (GeometricSolid shape : shapes) { System.out.printf("%.2f%n",shape.volume());...

  • c++ Error after oveloading, please help? LAB #8- CLASSES REVISITED &&    Lab #8.5 …      Jayasinghe...

    c++ Error after oveloading, please help? LAB #8- CLASSES REVISITED &&    Lab #8.5 …      Jayasinghe De Silva Design and Implement a Program that will allow all aspects of the Class BookType to work properly as defined below: class bookType { public:    void setBookTitle(string s);            //sets the bookTitle to s    void setBookISBN(string ISBN);            //sets the private member bookISBN to the parameter    void setBookPrice(double cost);            //sets the private member bookPrice to cost    void setCopiesInStock(int noOfCopies);            //sets the private member copiesInStock to...

  • LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing...

    LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing if statements, and improve your skills with for loops. The methods you will be writing have nothing to do with each other but allow you to use the skills you have gained so far to solve a series of small problems. Power For this method you will practice using methods from the Math class. The first step you must take is to write the...

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