Question

Valentine’s Day Question(JAVA) Suppose you had a list with 30 valentines and you stored their names...

Valentine’s Day Question(JAVA)

Suppose you had a list with 30 valentines and you stored their names in an array (you hopeless romantic). Then if you had 5 people ask to be your valentine, you would need to make a new array of size 35 (30 + 5) and copy over the original valentines before being able to add the new valentines to the array. This question uses the same logic.

Write a program that has a method called doubleSize. The method takes in an array of integers called values. The method will create a new array of integers that will be double (two times) the size of the passed in array. It will copy all the integers contained in values to the new array and return the new array.

In the main method:

  • Use a Scanner object to read in one int (n) that represents the size of an array of integers. Create an array of integers that uses this first number as the size.
  • You will then read in (n) ints all on the same line and store them in the array.
  • Call the method doubleSize and pass in the array of integers. This will return a new array with copied values.
  • Print the elements of the new array in the main method.

The method header looks as follows:

public static int [] doubleSize (int[] values)

You must use a method.

Input

In integer (n), followed by a list of n integers.

Example Input: 5 12 34 12 9 -7

Output

A single double representing the smallest number.

Example Output: 12 34 12 9 -7 0 0 0 0 0

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

import java.util.Scanner;

public class ValentinesDay {

   public static int[] doubleSize(int[] values) {
       // reading the size of the values array
       int initialsize = values.length;
       // multiplying the values array size with 2 so that we get the
       // double size for creation of new array
       int upgradedsize = initialsize * 2;
       // declaring the new array by name "resizedvalues" with length as
       // upgradedsize
       int[] resizedvalues = new int[upgradedsize];
       // running the for loop to copy the "values" array elements and to
       // assign value 0 for the resized locations in the array
       for (int inc = 0; inc < upgradedsize; inc++) {
           if (inc < initialsize) {
               // copying the values array to resizedvalues array
               resizedvalues[inc] = values[inc];
           } else {
               // assigning value zero to the remaining locations
               resizedvalues[inc] = 0;
           }
       }

       return resizedvalues;

   }

   public static void main(String args[]) {
       // initializing the scanner to read the values from the console.
       Scanner in = new Scanner(System.in);
       System.out.println("input the intial size of the array : ");
       // reading the size from the console and assigning it to a size
       // variable.
       int size = Integer.parseInt(in.next());
       // declaring the int array by name "values" with the size given as input
       int[] values = new int[size];
       System.out.println("input the integers one by one ");
       // for loop to read the values into the int array "values" one by one.
       for (int counter = 0; counter < size; counter++) {
           values[counter] = in.nextInt();
       }
       // calling the doubleSize method by passing the values array as
       // parameter
       // and assigning the returned array from the doublesize method to
       // upgradedarray
       int[] upgradedarray = doubleSize(values);
       // running a for loop to print the values in the resized array by name
       // "upgradedarray"
       for (int inc = 0; inc < upgradedarray.length; inc++) {
           // using if condition for spacing the output
           if (inc != 0)
               System.out.print(" ");
           else
               System.out.println("The resized array is :");
           System.out.print(upgradedarray[inc]);
       }
       // closing the scanner
       in.close();
   }

}

Add a comment
Know the answer?
Add Answer to:
Valentine’s Day Question(JAVA) Suppose you had a list with 30 valentines and you stored their names...
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
  • Write a java program that has a method called sameArrayBackwards. The method takes an array of...

    Write a java program that has a method called sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...

  • 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N  (that is not more than 50) from standard input and then reads N  integers from a file named...

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

  • pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the...

    pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the last assignment by providing the following additional features: (The additional features are listed in bold below) Class Statistics In the class Statistics, create the following static methods (in addition to the instance methods already provided). • A public static method for computing sorted data. • A public static method for computing min value. • A public static method for computing max value. • A...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • java estion7 For this question, assume all input comes from the keyboard, and al output goes...

    java estion7 For this question, assume all input comes from the keyboard, and al output goes to the screen. Include method prototypes and comments. The array should have room for 100 integers Write a complete Java program, including at least one comment in the main propram and one in e to do the following: Write a main program which will call the methods described below (a) First the main program will read an integer (this integer is a parameter or...

  • last question : Don't be negative and source Code : of don't be negative Repeat the...

    last question : Don't be negative and source Code : of don't be negative Repeat the assignment "don't be so negative" except solve the problem using recursion. Specifically, create an array of 10 random integers between -10 and +10 then create a method say "findNegatives" that takes as input parameters the array, and possibly other parameters, then calls itself (direct recursion); method find Negatives( ...method parameters here...) ...some stopping criteria... find Negatives(...method parameters here...) Output: Array position #0 #1 Value...

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