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

Java - Isabel has an interesting way of summing up the values in an array A of \

ID: 3667217 • Letter: J

Question

Java - Isabel has an interesting way of summing up the values in an array A of "n" integers, where "n" is a power of two. She creates an array B of half the size of A and sets B[i] = A[2i] +A[2i+1], for i = 0, 1,...,(n/2) - 1. If B has size 1, then she outputs B[0]. Otherwise, she replaces A with B, and repeats the process. Implement the code Isabel used in Java.

For Isabel’s technique the parameter should be an integer array.

This integer array must be read in for a text file.

You will need a method or segment of your main program that asks the user for the path and filename of the text file that contains a comma separated list of integer values that will make up the array.

This method or segment of code should then read the file and create the integer array.

This method or segment of code should then call Isabel’s technique with appropriate integer array parameter.

When you read in the values from the file you should skip any values that are not integers.

If the user enters a file that does not exist your program should inform the user that the file does not exist and ask the user to enter a correct filename or quit back to algorithm selection.

Explanation / Answer

import java.io.*;
import java.util.Scanner;

public class CopyFile

{

public static void main(String args[])throws IOException

{

  

Scanner s=new Scanner(System.in);

System.out.println("enter the file name along with its path seperated by /");

String fname;
  

fname=s.next();

try{

FileInputStream in;
in = new FileInputStream(fname);
DataInputStream din = new DataInputStream(in);

int c;
c = (in.read());

int a[]=new int[100];

int i=0;

while((c=din.read())!=-1)

{
int x = din.readInt();
a[i]=x;
i++;

}

int size=i;
int n=size;
for(i=0;i<n;i++)
System.out.println(a[i]);
  
int b[]=new int[100];
int size2 = 0;

if((n/2)==0)

{

size2=n/2;

while(size2==1)

{

for(int j=0;j<size2;j++)

{

b[j]= a[2*j]+a[2*j+1];

}

a=b;

size2=size/2;

}

}

if(size2==1)

{

System.out.println("the element is "+b[0]);

}

}

catch(Exception e)

{

System.out.println("file not found");

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote