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}}
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...
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.
Create a function to output a one dimensional double array Mwith n elements where the...
Create a function to output a one dimensional double array M with n elements where the first three elements are 1 and each subsequent element is the sum of previous three elements before it. Name the function myArray. Write the function in the correct format to be used to create a Matlab function. Call the function in correct format to output the array with 7 elements.
Write a method that, given an array of grades stored as double values, computes and return...
Write a method that, given an array of grades stored as double values, computes and return their mean.    Write another method that will return an array of the mean grade obtained by an arbitrary number of student.    This method will take a 2-dimensional array of double values in which each row corresponds to the grade vector    for a different student. You can compute the mean for each row as you did before...    Once you are done,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT