Question

In: Computer Science

Java programming language Array - Single identifier which can store multiple values Example initialized with values:...

Java programming language

Array - Single identifier which can store multiple values

Example initialized with values: int[] mySimpleArray = {4, 17, 99};

Example with fixed length but no values: int[] myFixedArray = new int[11];

The brackets identify the index. Each value has its own index number.
NOTE: First element is the zeroth element: mySimpleArray[0] is 4, [1] is 17

Make a new Netbeans project called ArraysAndLoops

Create the simple array with 4, 17, 99.

Use System.out.println with the variable with index literals to print out each value.

MORE LOOPS - Computing factorials

Show how factorial algorithm works, use FOR loop for factorials 0 to 10

Use i as index to store each factorial in an array

Use method Array.toString(myFactorials) to print out array at each step to check

          NOTE: requires import java.util.*

WRITING A FILE - requires import java.io.*

Step 1: Make an object for file output

Step 2: Use object, method println(...)

In-Class Active: Print the factorials array to a .txt file

          PrintWriter objectName = new PrintWriter(file path.filename);

Apply that structure to the array already made; use objectName factorialsTest

          Make a header statement for the first line of the file:

                   factorialsTest.println("Factorials 0 to 10");

Make the next line the array with all its values:

          factorialsTest.println(Arrays.toString(myFactorials));

These println method put the Strings in a buffer. Write to actual file by closing object.

          factorialsTest.close();

If time, print out all the array values using a FOR loop, System.out

Solutions

Expert Solution

Java Code:

package arrayprog;

//Imports
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;

public class ArraysAndLoops {
   //Main method
   public static void main(String[] args) throws FileNotFoundException {
       //Initializing an array
       int arr[] = {4, 17, 99};
       //Printing the array using the index
       System.out.println(arr[0]);
       System.out.println(arr[1]);
       System.out.println(arr[2]);
      
       //Declaring an array to store factorials
       int myFactorials[] = new int[11];
       myFactorials[0] = 1; //0! is 1
       System.out.println("Factorial values: ");
       //Factorial algorithm
       for(int i = 1 ; i <= 10 ; i++) {
           myFactorials[i] = i * myFactorials[i-1]; //factorial(i) = i * factorial(i-1)
           System.out.println(Arrays.toString(myFactorials)); //printing the array at each iteration
       }
       //Writing to a file using a PrintWriter
       PrintWriter factorialsTest = new PrintWriter("C:\\Users\\vkanchan\\pro\\Ques\\src\\arrayprog\\factorials.txt");
       factorialsTest.println("Factorials 0 to 10");
       factorialsTest.println(Arrays.toString(myFactorials));
       //Closing the file
       factorialsTest.close();
   }

}

Sample Output:

4
17
99
Factorial values:
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 2, 6, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 2, 6, 24, 0, 0, 0, 0, 0, 0]
[1, 1, 2, 6, 24, 120, 0, 0, 0, 0, 0]
[1, 1, 2, 6, 24, 120, 720, 0, 0, 0, 0]
[1, 1, 2, 6, 24, 120, 720, 5040, 0, 0, 0]
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 0, 0]
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 0]
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]

factorials.txt:

Factorials 0 to 10
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]


Related Solutions

Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
PHP Programming What is an array? Select one: a. a variable that stores multiple values b....
PHP Programming What is an array? Select one: a. a variable that stores multiple values b. a constant that stores a list of items c. a list that is stored in multiple variables d. an index of values Which of the following is the correct syntax for creating an array? Select one: a. $basket = array(“Watermelon”, “Orange”, “Grape”) b. $basket = array(Watermelon, Orange, Grape); c. $basket = array($“Watermelon”, $“Orange”, $“Grape”); d. $basket = array(“Watermelon”, “Orange”, “Grape”); What types of items...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
In java language how would I write the following? Create an array called “boyNames” and store...
In java language how would I write the following? Create an array called “boyNames” and store all names from the BoyNames.txt file Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file Create a text menu that allowing users to:          1. Enter a name and the application must display a message indicating if the name is   among the most popular (found on either array) If the name is found, tell the user if it is a boy’s...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Write an assembly language program to define an array of 5 double words initialized to 33,44,25,72,23,11...
Write an assembly language program to define an array of 5 double words initialized to 33,44,25,72,23,11 (all decimal). Add the first three numbers together and subtract the last two numbers from the sum. Store the sum in EAX register. Display the sum by using Irvine32 library procedures or by dumping registers to the display and taking a screenshot.
[jAVA] Assume the following array is defined and initialized (it will already contain numbers before your...
[jAVA] Assume the following array is defined and initialized (it will already contain numbers before your code runs): public static void main( String [ ] args ) { int [ ] numbers = . . . ; // An array of integers 1) Provide code that will print the entire contents of numbers in reverse order (from the end to the beginning) 2) Provide code that will print true if the numbers are in strictly ascending order (that is, if...
Write a program that uses an array of doubles initialized to whatever values you wish. Write...
Write a program that uses an array of doubles initialized to whatever values you wish. Write methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same...
Java Programming: Can I get an example of a program that will allow 4 options? Example...
Java Programming: Can I get an example of a program that will allow 4 options? Example Output: Choose on of the options below: 1. Display all items in CSV file. (CSV file includes for example brand of phone, size, price) 2. Pick an item by linear searching 3. Pick an item by bineary searching 4. Quit Program
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT