Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSIS 1410 Exercise B: At this point package final1410 should include a file call

ID: 3576631 • Letter: C

Question

CSIS 1410

Exercise B: At this point package final1410 should include a file called MyFile.txt

If you missed that step go to package explorer and add the file now. So far no text has been added to the file. This needs to be done now.

Add the following three numbers to the file - each number in a separate line
2.345
6.4
8.7654321

Create a generic list called numbers and read in all the numbers from MyFile.txt
At this point, MyFile.txt includes 3 numbers. However, there could be more or less. If added or removed numbers from MyFile.txt your program should still read in all the numbers available in the file. When reading numbers from the file you need to be prepared that a checked exception could occur. In this exercise checked exceptions should be caught and in such situations the stack trace should be printed.

Print the list

Each element should be rounded to 2 digits after the decimal point and the elements should be separated by a space (see output) Make sure to include a label.

Create a generic method called duplicateElements It has one parameter, which is a generic List, and it returns a generic list. The list that is returned should include each element of the original list twice (see example)
e.g. If the original list included 2.345, 6.4, and 8.7654321
then the modified list should include 2.345, 2.345, 6.4, 6.4, 8.7654321, and 8.7654321
Notice the order of the list elements Also, notice that the list elements are not rounded; only the output is rounded.

In the main method call duplicateElements in order to duplicate the elements of list numbers

Print the list that was returned by the method duplicateElements

Make your output look like the output provided

Sample Output:
list : 2.35 6.40 8.77
double list: 2.35 2.34 6.40 6.40 8.77 8.77

Explanation / Answer

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.*;

public class DataInputStreamDemo {
   public static void main(String[] args) throws IOException {
      Scanner scan;
      float inputlist[]=new float[3];//declaration and instantiation.
      float outputlist[]=new float[6];//declaration and instantiation.
    File file = new File("MyFile.txt");
    try {
        scan = new Scanner(file);
       int i=0,j=0;
        while(scan.hasNextFloat())
        {
          
           inputlist[i]=scan.nextFloat();
           outputlist[j]=inputlist[i];
           j++;
           outputlist[j]=inputlist[i];
           j++;
           i++;
          
          
        }
       System.out.print("List :");
       for(i=0;i<inputlist.length;i++)
       {
           System.out.printf("%.2f     ",inputlist[i]);
       }
       System.out.print(" double List :");
       for(i=0;i<outputlist.length;i++)
       {
           System.out.printf("%.2f      ",outputlist[i]);
       }

    } catch (FileNotFoundException e1) {
            e1.printStackTrace();
    }
   }
}

Output :

List :2.35     6.40     8.77
double List :2.35      2.35      6.40      6.40      8.77      8.77