Function Name: detention Inputs: 1. ( char ) A string with a sentence to be copi
ID: 3674755 • Letter: F
Question
Function Name: detention
Inputs: 1. ( char ) A string with a sentence to be copied
2. ( double ) The number of lines to print
3. ( double ) The number of times per line the sentence should appear
Outputs: (none)
File Outputs: 1. A text file of lines written in detention
Function Description: Throwback to oldschool detention where you are told to copy down a sentence over and over. However, that's way too tedious so you decide to use MATLAB instead! Write a function that takes in an input of a sentence along with the number of lines to write and the number of times per line the sentence should appear.. Then, write a file with all of the copied sentences. Each line should begin with 'Line . ' and each sentence should have a space between it and the next sentence. The output file name should be the string you are supposed to copy, but converted to camelcase (feel free to use your camelCase function from Homework 5 for this). *camelCase is a function that is already written and works that should be implemented into this code. For example, if the input string is 'MATLAB is the best!' and it was to be written 4 times with 2 sentences per line, the output file should be named matlabIsTheBest.txt and should look like this: Line 1. MATLAB is the best! MATLAB is the best! Line 2. MATLAB is the best! MATLAB is the best!
Notes: There should be no extra spaces at the end of each line
There should be no extra blank lines in your file (this would occur if your last line has a new line character at the end of it).
Explanation / Answer
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
public class Files {
public static void main(String args[])
{
String text= “MATLAB is the best”;
Scanner in = new Scanner(System.in);
System.out.print("Enter Number of times Sentence should be written: ");
int NoOfTimes = in.nextInt();
System.out.print("Enter Number of times Sentence per Line: ");
int SenPerLine = in.nextInt();
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("MATLABIsTheBest.txt");
writer = new BufferedWriter(fWriter);
for (int i=0; i<math.ceil(NoOfTimes/2); i++)
{
writer.write(“Line”);
writer.write(i);
writer.write(“.”);
for( int j=0; j<SenPerLine; j++)
{
writer.write(text);
writer.write(“ “);
}
writer.newLine();
writer.close();
} catch (Exception e) {
System.out.println("Error!");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.