Question

In: Computer Science

For this assignment, you will apply what you learned in analyzing a simple Java™ program by...

For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program that creates and accesses an array of integers. The Java™ program you write should do the following:

  • Create an array to hold 10 integers
  • Ask the user for an integer. Note: This code has already been written for you.
  • Populate the array. Note: The first element should be the integer input by the user. The second through tenth elements should each be the previous element + 100. For example, if the user inputs 10, the first array value should be 10, the second 110, the third 210, and so on.
  • Display the contents of the array on the screen in ascending index order.

Complete this assignment by doing the following:

  1. Download and unzip the linked Week 4 Coding Assignment Zip File.
  2. Read each line of the file carefully, including the detailed instructions at the top.
  3. Add comments to the code by typing your name and the date in the multi-line comment header.
  4. Replace the following lines with Java™ code as directed in the file:
  • LINE 1
  • LINE 2
  • LINE 3
  • LINE 4
  • LINE 5
  1. Comment each line of code you add to explain what you intend the code to do.
  2. Test and modify your Java™ program until it runs without errors and produces the results as described above.

Program Summary: This program demonstrates these basic Java concepts:
* - Creating an array based on user input
* - Accessing and displaying elements of the array
*
* The program should declare an array to hold 10 integers.
* The program should then ask the user for an integer.
* The program should populate the array by assigning the user-input integer
* to the first element of the array, the value of the first element + 100 to
* the second element of the array, the value of the second element + 100 to
* the third element of the array, the value of third element + 100 to
* the fourth element of the array, and so on until all 10 elements of the
* array are populated.
*
* Then the program should display the values of each of the array
* elements onscreen. For example, if the user inputs 4, the output
* should look like this:
*
* Enter an integer and hit Return: 4
* Element at index 0: 4
* Element at index 1: 104
* Element at index 2: 204
* Element at index 3: 304
* Element at index 4: 404
* Element at index 5: 504
* Element at index 6: 604
* Element at index 7: 704
* Element at index 8: 804
* Element at index 9: 904
***********************************************************************/
package prg420week4_codingassignment;

// We need to import the following library if we want to use the
// Scanner class to get user input.
import java.util.Scanner;

public class PRG420Week4_CodingAssignment {

public static void main(String[] args) {
  
// LINE 1. DECLARE AN ARRAY OF INTEGERS
  

// LINE 2. ALLOCATE MEMORY FOR 10 INTEGERS WITHIN THE ARRAY.
  
  
// Create a usable instance of an input device
Scanner myInputScannerInstance = new Scanner(System.in);
  
// We will ask a user to type in an integer. Note that in this practice
// code WE ARE NOT VERIFYING WHETHER THE USER ACTUALLY
// TYPES AN INTEGER OR NOT. In a production program, we would
// need to verify this; for example, by including
// exception handling code. (As-is, a user can type in XYZ
// and that will cause an exception.)
System.out.print("Enter an integer and hit Return: ");
  
// Convert the user input (which comes in as a string even
// though we ask the user for an integer) to an integer
int myFirstArrayElement = Integer.parseInt(myInputScannerInstance.next());
  
// LINE 3. INITIALIZE THE FIRST ARRAY ELEMENT WITH THE CONVERTED INTEGER myFirstArrayElement

  
// LINE 4. INITIALIZE THE SECOND THROUGH THE TENTH ELEMENTS BY ADDING 100 TO THE EACH PRECEDING VALUE.
// EXAMPLE: THE VALUE OF THE SECOND ELEMENT IS THE VALUE OF THE FIRST PLUS 100;
// THE VALUE OF THE THIRD ELEMENT IS THE VALUE OF THE SECOND PLUS 100; AND SO ON.

  
// LINE 5. DISPLAY THE VALUES OF EACH ELEMENT OF THE ARRAY IN ASCENDING ORDER BASED ON THE MODEL IN THE TOP-OF-CODE COMMENTS.

}
}

Solutions

Expert Solution

/*****************************PRG420Week4_CodingAssignment.java*******************/

package prg420week4_codingassignment;

import java.util.Scanner;

public class PRG420Week4_CodingAssignment {

   public static void main(String[] args) {

       int[] array;

       array = new int[10];

       // Create a usable instance of an input device
       Scanner myInputScannerInstance = new Scanner(System.in);

       System.out.print("Enter an integer and hit Return: ");
       // Convert the user input (which comes in as a string even
       // though we ask the user for an integer) to an integer
       int myFirstArrayElement = 0;
       try {
           myFirstArrayElement = Integer.parseInt(myInputScannerInstance.next());

       } catch (NumberFormatException e) {

           System.out.println("Please enter only integer!");
           System.out.print("Enter an integer and hit Return: ");
           myFirstArrayElement = Integer.parseInt(myInputScannerInstance.next());
       }
      
       for (int i = 0; i < array.length; i++) {

           array[i] = myFirstArrayElement + i * 100;

       }

       for (int i = 0; i < array.length; i++) {

           System.out.println("Element at index " + i + ":" + array[i]);

       }

       myInputScannerInstance.close();
   }
}

/********************output****************/

Enter an integer and hit Return: xyz
Please enter only integer!
Enter an integer and hit Return: 4
Element at index 0:4
Element at index 1:104
Element at index 2:204
Element at index 3:304
Element at index 4:404
Element at index 5:504
Element at index 6:604
Element at index 7:704
Element at index 8:804
Element at index 9:904

Please let me know if you have any doubt or modify the answer, Thanks:)


Related Solutions

For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program. The Java™ program you write should do the following: Display a prompt on the console asking the user to type in his or her first name Construct the greeting string "Hello, nameEntered!" Display the constructed greeting on the console Complete this assignment by doing the following: Download and unzip the linked zip file. Add comments to the code by...
For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze Assignment Zip File, and predicting the results. You will also examine both the code and the output for inconsistencies and clarity. This Java™ code includes examples of for, while, and do-while loops. Carefully read through the code line by line, then answer the following questions in a Microsoft® Word document: What is the output of the program as it is written? What improvement(s) could...
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java...
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java that incorporates the following: An abstract Bicycle class that contains private data relevant to all types of bicycles (cadence, speed, and gear) in addition to one new static variable: bicycleCount. The private data must be made visible via public getter and setter methods; the static variable must be set/manipulated in the Bicycle constructor and made visible via a public getter method. Two concrete classes...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug...
Overview In this assignment you will apply what you have learned about standard deviation and normal...
Overview In this assignment you will apply what you have learned about standard deviation and normal curves to solve a number of problems, including finding the area under selected sections of a normal curve using z-scores, then solve a problem using normal distributions. Part 1: The weights of all one hundred (100) 9th graders at a school are measured, and it is found that the mean of all the measurements is 100 lbs., with a standard deviation of 15 lbs.  ...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
Programmed In Java In this assignment you will create a simple game. You will put all...
Programmed In Java In this assignment you will create a simple game. You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y ou will start by welcoming the user. 1. You should print "Welcome! Your starting coordinates are (0, 0).” 2. On the next line, you will tell the user the acceptable list of commands. This should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT