Question

In: Computer Science

1) Design and implement a method to double all elements of an array. Important: You don’t...

1) Design and implement a method to double all elements of an array.

Important: You don’t need to test test method in main(). You don’t need to initialize the array.

pubilc static void doubleElements(int [] arr)

{

}

2) Start with the code below and complete the getInt method. The method should prompt the user to enter an integer. Scan the input the user types. If the input is not an int, throw an IOException; otherwise, return the int. In the main program, invoke the getInt method, use try-catch block to catch the IOException.

import java.util.*;

import java.io.*;

public class ReadInteger

{

pubilc static void main()

{

// your code goes here

}

public static int getInt() throws IOException

{

// your code goes here

}

}

3) Create a class called Car (Car.java).

It should have the following private data members:

• String make

• String model

• int year

Provide the following methods:

• default constructor (set make and model to an empty string, and set year to 0)

• non-default constructor Car(String make, String model, int year)

• getters and setters for the three data members

• method print() prints the Car object’s information, formatted as follows:

Make: Toyota

Model: 4Runner

Year: 2010

public class Car

{

}

4) Complete the following unit test for the class Car described above. Make sure to test each public constructor and each public method, print the expected and actual results.

// Start of the unit test of class Car

public class CarTester

{

public static void main()

{

// Your source codes are placed here

return;

}

}

Solutions

Expert Solution

I will answer the first question here.

What we have to do is just iterate through the array and multiply each element by 2.

Here's the code.

import java.util.*;
import java.lang.*;
import java.io.*;

class Problem
{
   public static void main (String[] args){
   int[] arr = {3,4,5,6,6};
   doubleElements(arr);
     
   System.out.println(Arrays.toString(arr));
   }
  
   public static void doubleElements(int[] arr){
   for(int i = 0; i < arr.length; i++){
   arr[i] = arr[i] * 2;
   }
  
  
   }
}

Here are the screenshots of the code and the output.

Let me know if you have any doubts in the comments.


Related Solutions

Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array A 2) an int number x to search for (b) determines whether x exists in A, and prints a message indicating the result (c) has the best worst case Big Oh complexity you can manage, using only your own thinking and the materials (the worst case growth rate for the number of items searched should be as low as possible, given that A contains...
1. Implement a public method named initialize. It takes a twodimensional square array of integers...
1. Implement a public method named initialize. It takes a two dimensional square array of integers namedarray as a parameter. It initializes all of the elements of the array to the sum of their indices except for themajor diagonal (upper left to lower right) where each element is initialized to -1. (For testing use a 4X4 or5X5 array and have the application print out the array in 2 dimension format.2. Implement a method named totals that takes a two dimensional...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of...
Recursion java: 1. Write a recursive algorithm to add all the elements of an array of n elements 2. Write a recursive algorithm to get the minimum element of an array of n elements 3. Write a recursive algorithm to add the corresponding elements of two arrays (A and B) of n elements. Store the results in a third array C .4. Write a recursive algorithm to get the maximum element of a binary tree 5. Write a recursive algorithm...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a...
Create a program “Fib.java” and a method called “double[] getFib)” that creates an array with a length of 15 that contains the first 15 numbers in the Fibonacci sequence and returns it. Set the first element to 0 and the second element to 1, then use a for loop to fill out the rest of the array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT