Question

In: Computer Science

public class AddValueNewArray { // You must define the addValueNew method, which behaves // similarly to...

public class AddValueNewArray {
    // You must define the addValueNew method, which behaves
    // similarly to the addValueTo method in AddValueToArray.java.
    // However, instead of adding the given value to the given
    // array, it will return a _new_ array, where the new array
    // is the result of adding the value to each element of the
    // given array. To be clear, the given array NEVER CHANGES.
    //
    // TODO - define your code below this comment
    //

    // DO NOT MODIFY printArray!
    public static void printArray(int[] array) {
        for (int index = 0; index < array.length; index++) {
            System.out.println(array[index]);
        }
    }
    
    public static void main(String[] args) {
        int[] array = new int[]{3, 8, 6, 4};
        int valueToAdd = Integer.parseInt(args[0]);
        int[] newArray = addValueNew(valueToAdd, array);
        System.out.println("Original array:");
        printArray(array);
        System.out.println("New array:");
        printArray(newArray);
    }
}

Solutions

Expert Solution

Since this program takes input from CLI it would be better if you run this code in command prompt

File name is AddValueNewArray.java

I have explained what the code does in comments in the code

public class AddValueNewArray {
    // You must define the addValueNew method, which behaves
    // similarly to the addValueTo method in AddValueToArray.java.
    // However, instead of adding the given value to the given
    // array, it will return a _new_ array, where the new array
    // is the result of adding the value to each element of the
    // given array. To be clear, the given array NEVER CHANGES.
    //
    // TODO - define your code below this comment
    //
    public static int[] addValueNew(int value, int[] old_array){
        
        // length of old array
        int l = old_array.length;
        // creating new array of size l
        int[] new_array = new int[l]; 
        // for loop for adding each element
        // old array with value and storing it in new_array
        for (int i=0; i<l; i++){
            new_array[i] = old_array[i]+value;
        }
        //returning the new array
        return new_array;

    }
    

    // DO NOT MODIFY printArray!
    public static void printArray(int[] array) {
        for (int index = 0; index < array.length; index++) {
            System.out.println(array[index]);
        }
    }
    
    public static void main(String[] args) {
        int[] array = new int[]{3, 8, 6, 4};
        int valueToAdd = Integer.parseInt(args[0]);
        int[] newArray = addValueNew(valueToAdd, array);
        System.out.println("Original array:");
        printArray(array);
        System.out.println("New array:");
        printArray(newArray);
    }
}

Hope this answers your questions, please leave a upvote if you find this helpful.


Related Solutions

public class AddValueToArray { // You must define the addValueTo method, which will add // a...
public class AddValueToArray { // You must define the addValueTo method, which will add // a given value to each element of the given array. // // TODO - define your code below this comment // // DO NOT MODIFY main! public static void main(String[] args) { int[] array = new int[]{3, 8, 6, 4}; int valueToAdd = Integer.parseInt(args[0]); addValueTo(valueToAdd, array); for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } }
public class AllEqual { // You must define the allEqual method, which will return // true...
public class AllEqual { // You must define the allEqual method, which will return // true if either: // 1.) The given array contains fewer than two elements, or... // 2.) All elements of the array are equal to each other. // As a hint, you only need to compare the first element // to all subsequent elements for this check. // // TODO - define your code below this comment // // DO NOT MODIFY parseStrings! public static int[]...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else. Examples % java CountInts...
The Date Class This class will function similarly in spirit to the Date class in the...
The Date Class This class will function similarly in spirit to the Date class in the text, and behaves very much like a “standard” class should. It should store a month, day, and year, in addition to providing useful functions like date reporting (toString()), date setting (constructors and getters/setters), as well as some basic error detection (for example, all month values should be between 1-12, if represented as an integer). Start this by defining a new class called “Date” or...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes...
import java.util.Scanner; public class MonthsOnAndAfter { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // As a hint, you should not have to use the name of each // month more than once. // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month...
import java.util.Scanner; public class Months { // You will need to write a method that makes...
import java.util.Scanner; public class Months { // You will need to write a method that makes this // code compile and produce the correct output. // YOU MUST USE switch! // TODO - write your code below this comment. // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter month (0-11): "); int month = input.nextInt(); String output = monthAsString(month); System.out.println(output); } }
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT