Details: This time you are given the freedom to design your program on your own
ID: 3624634 • Letter: D
Question
Details:This time you are given the freedom to design your program on your own under the constraints given below. There will be no UML to follow.
Your program must
•query the user about which two files the user wants to copy into a new one,
•query the user about what file name the user wants to use as the output the program to.
•take the first line of the first input file and copy it into the output file
•then take the first line of the second input file and copy it into the output file
•contiue copying a line from each the first file and then a line from the second file till you have copied all of the lines from both files.
?if one of the files has fewer lines than the other simply copy the remaining lines of the longer file into the output file.
Additinal restrictions:
To help you get in the habit of good program writing, I am imposing the following additional restrictions on your program.
•You may not have more than three lines of code in the main method
•You must comment your code. We are starting to get to the point where you are writing longer programs and doing more on your own. you will need to start commenting your code so that both you and I know what you are trying to do. To this end credit in the assignment will be reservedfor grading your comments.
•Add a readme.txt file as a text file in your submission. Your readme file should contain
?your name
?a brief (one paragraph) description of how you implemented the project,
Explanation / Answer
please rate - thanks
import java.util.*;
import java.io.*;
public class mergefile
{public static Scanner in,in1;
public static PrintStream out;
public static void main(String[] args)throws FileNotFoundException
{openfiles();
merge();
}
public static boolean openfiles()throws FileNotFoundException
{String filename;
Scanner kb=new Scanner(System.in);
System.out.print("Enter input file 1 name: ");
filename=kb.nextLine();
in=new Scanner(new File(filename));
System.out.print("Enter input file 2 name: ");
filename=kb.nextLine();
in1=new Scanner(new File(filename));
System.out.print("Enter output file name: ");
filename=kb.nextLine();
out=new PrintStream(new File(filename));
return true;
}
public static void merge()
{
boolean> String n,m;
int i=1;
n=in.nextLine();
m=in1.nextLine();
while(one&&two)
{if(i==1)
{out.println(n);
if(in.hasNextLine())
n=in.nextLine();
else
> i=0;
}
else
{out.println(m);
if(in1.hasNextLine())
m=in1.nextLine();
else
two=false;
i=1;
}
}
if(two)
{ out.println(m);
while(in1.hasNextLine())
{m=in1.nextLine();
out.println(m);
}
}
else
{ out.println(n);
while(in.hasNextLine())
{n=in.nextLine();
out.println(n);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.