Question

Programming in javaIn this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple ma

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

JAVA program:

 class ArrayShiftMult {  // Method for shiftOne  public static int[] shiftOne(int[] inArray)     {               int len_array;          len_array = inArray.length;             int flag = 0;           int[] result = new int[len_array];                              // Checking for negetive integers               for(int i = 0; i < len_array; i++)           {                       if(inArray[i] < 0)                   {                               flag = 1;                               break;                  }               }                               // Shift for non negetive integers              if(flag == 0)           {                       result[0] = -1;                         for(int i = 1; i < len_array; i++)                   {                               result[i] = inArray[i-1];                       }               }               return result;                  }               // Method for mult      public static int[] mult(int[] array1, int[] array2)    {               int len1 = array1.length;               int len2 = array2.length;               int minLen;             int[] longArray;                                // Finding the minLen of the arrays and set the longArray               if(len1 <= len2)             {                       minLen = len1;                  longArray = array2;             }               else            {                       minLen = len2;                  longArray = array1;             }                               // Define outArray with length of longArray             int[] outArray = new int[longArray.length];             for(int i = 0; i < minLen; i++)              {                       outArray[i] = array1[i] * array2[i];            }               for(int j = minLen; j < longArray.length; j++)               {                       outArray[j] = longArray[j];             }                               return outArray;        }                       // Main method to initialize array and call the methods         public static void main(String args[])  {               int[] arr1 = {4, 7, 21, 3, 4, 5, 18, 4, 6, 2};          int[] arr2 = {13, 10, 1, 3, 6, 9};                              System.out.println("\nArray1: ");               for(int i = 0; i < arr1.length; i++)                 {                       System.out.print(arr1[i]+" ");          }                               System.out.println("\n\nArray2: ");             for(int i = 0; i < arr2.length; i++)                 {                       System.out.print(arr2[i]+" ");          }                               System.out.println("\n\nInvoking shiftOne method using second array:");                 int[] shift_arr = shiftOne(arr2);                               for(int i = 0; i < shift_arr.length; i++)            {                       System.out.print(shift_arr[i]+" ");             }                               System.out.println("\n\nInvoking mult method using the two arrays:");           int[] mult_arr = mult(arr1, arr2);                              for(int i = 0; i < mult_arr.length; i++)             {                       System.out.print(mult_arr[i]+" ");              }                               System.out.println("");                 }        } 

The output of the above code:

Array1: may using the for loop. 4 7 21 3 4 5 18 4 6 2 Array2: 13 10 1 3 6 9 Invoking shiftOne method using second array: -1 1

Thank you !!!

Add a comment
Know the answer?
Add Answer to:
Programming in java In this part of this Lab exercise, you will need to create a...
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
  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Need help with java code, this is all in a method 1) 3 different arrays are...

    Need help with java code, this is all in a method 1) 3 different arrays are brought in 2) I create local array called arrayWithLargestValues which is the size of the largest array from the 3 brought in 3) I need to somehow fill the local 'arrayWithLargestValues' with the all different largest values from the 3 different arrays hopefully I made sense sample input/output int [ ] arrayWithLargestValues = new int [ ] // this has to be set to...

  • Please answer all the questions 2. Design two programs named BaseClass and SubClass. In BaseClass, define...

    Please answer all the questions 2. Design two programs named BaseClass and SubClass. In BaseClass, define a variable xVar (type: char, value: 65), and a method myPrint to print xVar. SubClass is a subclass of BaseClass. In SubClass, define a variable yVar (type: int, value: 16) and another variable strVar (type: String, value: "java program!"). There is also a method myPrint to print the value of yVar and strVar in SubClass, as well as an additional method printAll, in which...

  • This is for JAVA programming. Create a test class that contains two arrays and two methods:...

    This is for JAVA programming. Create a test class that contains two arrays and two methods: - The first array has 3 rows and 3 columns and is initialized with type double data - The second array has 4 rows and 4 columns and is also initialized with type double data - The first method ArraysRows will receive an array of type double as an argument and then print out the sum and average of all elements in each ROW....

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...

  • Java Project Name: IC19_WinningTheLottery n this assignment, we're going to attempt to win the lottery! (**If...

    Java Project Name: IC19_WinningTheLottery n this assignment, we're going to attempt to win the lottery! (**If you do actually win the lottery, please be sure to give back to your alma mater, Orange Coast College. We will be happy to put your name on the building!**) Ok, time to get real. The Fantasy 5 (TM) lottery is a game in which 5 numbers from 1 to 36 are randomly drawn by the State of California. If you correctly guess all...

  • Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal...

    Hello, In need of help with this Java pa homework. Thank you! 2. Create a normal (POJo, JavaBean) class to represent, i. e. model, varieties of green beans (also known as string beans or snap beans). Following the steps shown in "Assignment: Tutorial on Creating Classes in IntelliJ", create the class in a file of its own in the same package as class Main. Name the class an appropriate name. The class must have (a) private field variables of appropriate...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • This is for my Computer Programming class using Visual Studio C++ This is a home work...

    This is for my Computer Programming class using Visual Studio C++ This is a home work assignment and is for a grade so answer seriously and if you don't know know or unsure don't answer I need the coding so i can copy and paste and need a screen shot of the result i can copy as well Thank You. P1 – Merge two sorter arrays (80 points) (a) Write a function that receive two sorted arrays of integers and...

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
ADVERTISEMENT