Question

In: Computer Science

This task has the following elements that are relevant to the learning outcomes of this module:...

This task has the following elements that are relevant to the learning outcomes of this module: - use ArrayList to save, find, insert items - convert from an array to an ArrayList - shuffle a list using your own method, sort a list You should write a simple Java class that will perform some operations on an ArrayList Create an Integer array of ten objects that you initialize with values ​​1 to 10. Convert it to an ArrayList object. Add a number Remove one of the numbers in the list. Ask the ArrayList object if it contains added value and the value that was removed. Create a method (do not use built-in shuffle methods) to put the values ​​in a random order Arrange the array list in descending order and print it. Example of printing is:

Does list contain 42? Yes 
Does list contain 9? No 

4 2 5 1 0 3 8 7 6 42 
42 8 7 6 5 4 3 2 1 0

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;

public class Test {

    public static void main(String[] args) {
        //Create an Integer array of ten objects that you initialize with values ​​1 to 10.
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        //Convert it to an ArrayList object.
        ArrayList<Integer> integers = new ArrayList<>(10);
        for (int num : numbers) {
            integers.add(num);
        }

        // Add a number; adding number 42
        integers.add(42);
        //Remove one of the numbers in the list. Removing number 10
        integers.remove(integers.indexOf(10));
        //Ask the ArrayList object if it contains added value
        System.out.println("Does list contain 42? " + (integers.indexOf(42) >= 0 ? "Yes" : "No"));
        //and the value that was removed.
        System.out.println("Does list contain 10? " + (integers.indexOf(10) >= 0 ? "Yes" : "No"));
        shuffleArrayElements(integers);
        // printing after shuffling the array
        System.out.println("After shuffle:" + integers);
        //Arrange the array list in descending order and print it.
        integers.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer t2, Integer t1) {
                return t1-t2;
            }
        });
        System.out.println("After sorting:" + integers);
    }

    //Create a method (do not use built-in shuffle methods) to put the values ​​in a random order
    public static void shuffleArrayElements(ArrayList<Integer> integers) {
        Random random = new Random();
        for (int i = 0; i < integers.size(); i++) {
            int randomNumber = random.nextInt(10);
            int temp = integers.get(i);
            integers.set(i, integers.get(randomNumber));
            integers.set(randomNumber, temp);
        }


    }
}

___________________________________________________________________________________________


Related Solutions

Maths of finance This task assesses the following learning outcomes: Time value for money and the...
Maths of finance This task assesses the following learning outcomes: Time value for money and the rate of return Assess the simple interest and compound interest Net Present value in Capital Budgeting (Internal rate of return, Payback period) Annuities (PV, FV, Growth Annuities, types of Annuities) Perpetuities (PV, Growth Perpetuities) 5. You can invest in to projects: PROJECT A A five-year scope project that consists on an initial investment of 110,000€ and a set of 5 yearly revenues of 25.000€...
This task assesses the following learning outcomes: Critically understand the differences between the methods of valuation...
This task assesses the following learning outcomes: Critically understand the differences between the methods of valuation of the inventory Knowing how to properly elaborate an income statement and determine the ending inventory balance. Submission file format: Excel document with all the answers, clearly identifying all steps, results, journals and including comments besides each answer. BUSINESS CASE (100 points) Jim has recently opened a dry fruits wholesale company dedicated to the sale of peanuts, almonds and pistachios. During its first month...
Finance and Banking Module Learning Outcomes: Identify the functions & characteristics of money. Summarize how the...
Finance and Banking Module Learning Outcomes: Identify the functions & characteristics of money. Summarize how the Federal Reserve System regulates money supply to keep economy healthy. Describe the organizations involved and the services they supply in the banking and financial institution industry. Explain how deposit insurance (FDIC) protects customers. Discuss personal credit management. Identify a firm’s short- and long-term financial needs. Evaluate the advantages and disadvantages of equity financing. Summarize the process of planning for financial management. Explain how you...
Introduction to Business & Economics Module Learning Outcomes: Define Business and Economics while exploring economic systems....
Introduction to Business & Economics Module Learning Outcomes: Define Business and Economics while exploring economic systems. Identify the ways to measure economic performance. Outline the different phases in a business cycle and the types of competition. Summarize the factors affecting the business environment and future challenges. Describe the advantages/disadvantages of sole proprietorships, partnerships, and corporations. Examine special types of corporations: LLC, S-corporations, non-for-profits Key Terms/Concepts to Learn: Free enterprise system           federal deficit                         4 types of competition Business                                  national debt                           equilibrium...
Fuling Plastics: A Fork in the Road This discussion addresses the following Module Outcomes: Evaluate a...
Fuling Plastics: A Fork in the Road This discussion addresses the following Module Outcomes: Evaluate a firm’s use of leverage using financial statement analysis. Evaluate trends in the firm’s pattern of operations using financial statement analysis. Evaluate the financial position of the firm using time and trend or peer-group analysis. Fuling Plastics USA is a subsidiary of Fuling Global, Inc. (https://ir.fulingglobal.com) Fuling manufactures environmentally-friendly plastic food-service disposable products. Their historic strength has been in the production of disposable cutlery, the...
In each of the following cases, identify whether the task required is supervised or unsupervised learning,...
In each of the following cases, identify whether the task required is supervised or unsupervised learning, and then identify the appropriate technique—i.e., prediction, classification, affinity or clustering analysis—that you would use. Assume that an appropriate dataset is available for your algorithm to learn from. a. Deciding whether to issue a loan to an applicant based on demographic and financial data using a database of similar data on prior customers. b. In an online bookstore, making recommendations to customers concerning additional...
Mathemitcs of finance following learning outcomes: Time value for money and the rate of return Assess...
Mathemitcs of finance following learning outcomes: Time value for money and the rate of return Assess the simple interest and compound interest Net Present value in Capital Budgeting (Internal rate of return, Payback period) Clearly identifying all steps, results, and including comments besides each answer. You can invest in to projects: PROJECT A A five-year scope project that consists on an initial investment of 110,000€ and a set of 5 yearly revenues of 25.000€ from year 1 to year 5...
Mathemitcs of finance following learning outcomes: Time value for money and the rate of return Assess...
Mathemitcs of finance following learning outcomes: Time value for money and the rate of return Assess the simple interest and compound interest Net Present value in Capital Budgeting (Internal rate of return, Payback period) Clearly identifying all steps, results, and including comments besides each answer. You have saved 120,000€ and you want to know what is the better option for your money, It is better to spend the money today or invested and receive 122,000 in 3 years with annual...
This assignment covers practical aspects of this course. This assessment covers the following course learning outcomes:...
This assignment covers practical aspects of this course. This assessment covers the following course learning outcomes: CLO 2 - Apply programming concepts to computing problems CLO 3 - Examine Code for its syntax and semantic validity Case Study The assignment is based on the monthly income of Anna, a coffee shop owner in Suva. Anna’s monthly income for 2019 is given below, and you are required to write a program that will perform some manipulation actions on her monthly income...
Assignment title or task: Identify which study design is applicable for studying the following health outcomes...
Assignment title or task: Identify which study design is applicable for studying the following health outcomes and why? 1. Number of new cases of obesity among college students in Saudi Arabia in 2019. 2. The number of students with obesity who are physically active and the number of students with obesity who are physically inactive. 3. Give two difference between descriptive and analytic cross-sectional study. * Answer should be ( 300 - 350) Words.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT