Question

Unit 7 Worksheet #2                                        &

Unit 7 Worksheet #2                                            Name:

  1. Declare and initialize a 3 x 3 two-‐dimensional array that has the numbers 1-‐9 in it using an initializer list.
  1. On the array below:

int[][] values = new int[4][10];

for(int r = 0; r < values.length; r++)

{

for(int c = 0; c < values[r].length; c++)

{

values[r][c] = (int)(Math.random()*51 + 50);

}

}

write the code to count and print the number of elements that are greater than 75.

  1. What is the syntax for declaring and constructing an ArrayList?

  1. What must be imported in order to be able to use an ArrayList?
  1. Do you have to specify the type of data that will be stored in the ArrayList? Is it necessary to specify the amount of data that will be stored in the ArrayList? Explain.

  1. What do the following Arraylist methodsreturn? size(), get(), remove(), set()

size()

get()

remove()

set()

  1. Write the loop to print each of the ArrayLists:

ArrayList<String> list1 = new ArrayList<String>();

list1.add(“hello”);

list1.add(“there!”);

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

thanks for the question.


Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.If you are satisfied with the solution,

please rate the answer.

========================================================================================

import java.util.ArrayList;

public class Unit7 {

    public static void main(String[] args) {

        //Declare and initialize a 3 x 3 two-‐dimensional array that has the numbers 1-‐9 in it using an initializer list.
        int[][] digits = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

        //On the array below:
        int[][] values = new int[4][10];
        for (int r = 0; r < values.length; r++) {
            for (int c = 0; c < values[r].length; c++) {
                values[r][c] = (int) (Math.random() * 51 + 50);
            }
        }
        //write the code to count and print the number of elements that are greater than 75.
        int greaterThan75Count = 0;
        for (int r = 0; r < values.length; r++) {
            for (int c = 0; c < values[r].length; c++) {
                greaterThan75Count += values[r][c] > 75 ? 1 : 0;
            }
        }
        System.out.println("There are "+greaterThan75Count+" numbers greater than 75 in the array");

        ArrayList<String> list1 = new ArrayList<String>();
        list1.add("hello");
        list1.add("there!");
        for(String item:list1){
            System.out.println(item);
        }
    }

    //What is the syntax for declaring and constructing an ArrayList?
    // We can replace Object by any class name
    ArrayList<Object> arrayList = new ArrayList();

    //What must be imported in order to be able to use an ArrayList?
    // answer : import java.util.ArrayList;

    //Do you have to specify the type of data that will be stored in the ArrayList?
    // answer : By typing in the class name inside the angle brackets. e.g String
    ArrayList<String> stringList = new ArrayList<>();
    // Is it necessary to specify the amount of data that will be stored in the ArrayList? Explain.
    // answer: arraylist are resizable, we dont need to declare the size
    // it automatically resizes to accomodate new objects

    //What do the following Arraylist methodsreturn? size(), get(), remove(), set()
    // size() : returns the total elements in the arraylist
    // get() : returns the element at a particular index, it takes an int value
    // remove() : removes an object if the equals() matches for that object
    // set() : set a value at a particular index



}

===================================================================================

Add a comment
Know the answer?
Add Answer to:
Unit 7 Worksheet #2                                        &
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
  • Array lists are objects that, like arrays, provide you the ability to store items sequentially and...

    Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that...

  • If I have a class named classroom, a classroom class consists of five methods: 1. void...

    If I have a class named classroom, a classroom class consists of five methods: 1. void add(T newStudent) //add a student 2. void set(int pos, T student) //changes the student stored at position pos to be the student parameter 3. T get(int pos)//returns the student at position pos. 4. int size() //num of student in a classroom 5. String toString() ------------------------------------------------------------------------------------------------------------------------------- In another class recursionWS static <T> boolean studentInList (classroom<T> list1, classroom<T> list2){ //this method returns true if students in...

  • Write a generic array list class. Task Description Your goal for this lab is to write...

    Write a generic array list class. Task Description Your goal for this lab is to write a generic ArrayList class similar to the one in the given lecture notes on array lists. The ArrayList Class The public constructors and methods required for the ArrayList class are listed here. The type E is the generic type of an element of the list. ArrayList() Construct an empty ArrayList object. int size() Return the size (number of items) in this ArrayList. boolean isEmpty()...

  • (This program is to be done on Java) 7.5 ArrayLists Part 1 A significant limitation of...

    (This program is to be done on Java) 7.5 ArrayLists Part 1 A significant limitation of the array is you cannot add or delete elements from the array. The size is fixed and you can only do workarounds. An example is the following: public class Ex_7_5_Prep { public static void main(String[] args) { int[] intArray = { 5, 10, 15, 20 }; printArray(intArray); intArray = addNewElement(intArray, 25); printArray(intArray); } public static int[] addNewElement(int[] originalArray, int newInt) { // Create new...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is,...

    PART 1 Modify the class ArrayList given in Exercise 1 by using expandable arrays. That is, if the list is full when an item is being added to this list, the elements will be moved to a larger array. The new array should have twice the size of the original array. Using the new class ArrayList, write a program to store 1,000 random numbers, each in the interval [0, 500]. The initial size of the array in the class should...

  • Note: Please answer the question in clear and legible steps. Nothing too advanced, this is a...

    Note: Please answer the question in clear and legible steps. Nothing too advanced, this is a question for introductory C++ class. Thank you. QUESTION: Write C++ statements to declare the following arrays. The name of the array is up to you. Use a const int variable for the array size. a) An array that will hold 7 integers, using an initializer list to set all values to 0. b) An array that will hold 100 doubles. c) An array that...

  • Add the following methods to the ArrayList class that we wrote during lecture. You may call...

    Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...

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