Limited Offer Get 25% off — use code BESTW25
No AI No Plagiarism On-Time Delivery Free Revisions
Claim Now

CPW 142 Object-Oriented Programming

CPW 142 Object-Oriented Programming I, Winter 2017
Programming Assignment #6, Due Wednesday, March 1, 11:30 PM
Write a method named justifyJava that takes three parameters: an input file name, an output file name,
and a tab size. Use the provided code, JavaJustifier.java, and add your code to the body of
justifyJava. Be sure to put the provided test data in the same folder as your java code. The test data
consists of input files and reference files that contain the expected results.
public static void
justifyJava( String inputFileName, String outputFileName, int tabSize )
throws FileNotFoundException /* your code goes here */
The method should assume that the input file contains a Java program. The method opens the input file and
reads the input file line by line and prints each line to System.out and to the output file with -blocks
properly indented.
First Step:
In the method justifyJava, write code to open the input file for reading, then loop through the file line by
line, to print out the entire input file.
Second Step:
In the method justifyJava, write code to open the output file for writing, then add code to the loop you
created in the first step to write, line by line, to the output file.
Third Step:
In the method justifyJava, write code to implement proper indentation.
Fourth Step:
Uncomment the code in main that will verify your work. You should get Success for file n for each of
the test files.
Proper indentation is as follows:

The first line of a program must have no spaces before the first non-white space character.
See Note 1 below.
Whenever a line has a }, that line must be indented one “tab” less than the previous line.
See Note 2 below.
Whenever a line has a {, the next line must be indented one “tab” more than the current line.
See Note 3 below.
Between the first non-white space character and the last non-white space character, the characters in
the line should not be changed.

Note 1: The class String has a method named trim that removes leading and trailing white space.
So if a String named str was ” abc “, str = str.trim(); would change str to “abc”.
Note 2: Example: line = line.substring( tabSize );
The above code will remove tabSize spaces from the beginning of the String named line.
Note 3: We can add one tab to the beginning of a line as follows:
Example: line = one_tab + line;
A tab is a type of magic number in that the number of spaces that make up a tab is completely arbitrary, so
when we add a tab to our output, the number of spaces that appears is as if by magic.
This means the program needs a constant to represent a tab:
public static final int TAB_SIZE = 4;
When we call justifyJava, we send the value of TAB_SIZE as a parameter, tabSize.
The above example assumes that a String variable one_tab has the right number of spaces in it. You can
build one_tab with a for loop:
String one_tab = “”;
for ( int i = 0; i < tabSize; i++ )
one_tab += ” “;

To get the indent for a line correct, remove all the spaces at the beginning and end of the string, then add the
right number of tabs at the beginning of the line.
Test files are provided on Canvas. Reference files containing the expected output are also posted to Canvas.
One of your jobs is to determine what the proper output for these files is. Another is to verify that your
program produces that output. Starter code is also posted to Canvas.
Expected Output:
The following examples assume that a tab is 4 spaces. When I test your code, I may use a different value. I may
also use a different file as input.
public class Test1

public static void main( String[] args )

System.out.println( “This is Test 1.” );

Success for file 1
public class Test2

public static void main( String[] args )

System.out.println( “This is Test 2.” );

Success for file 2
class Test3
public static void main( String[ ] args )
for( int i = 1; i < 10; i ++ )
System.out.println( “Hello World!” );

for( int i = 1; i < 10; i ++ )
for( int j = 1; j < 10; j ++ )
for( int k = 1; k < 10; k ++ )
System.out.println( “Hello World!” );

if( 3 < 5 )
System.out.println( “Hello World!” );

else if ( 4 < 5 )
System.out.println( “Hello World!” );
if( 5 > 3 )
System.out.println( “Hello World!” );

Success for file 3
public class Test4

public static void main( String[] args )

System.out.println( “This is Test 4.” );

Success for file 4
class Test5

public static void main( String[ ] args )

for( int i = 1; i < 10; i ++ )

System.out.println( “Hello World!” );

for( int i = 1; i < 10; i ++ )

for( int j = 1; j < 10; j ++ )

for( int k = 1; k < 10; k ++ )

System.out.println( “Hello World!” );

if( 3 < 5 )

System.out.println( “Hello World!” );

else if ( 4 < 5 )

System.out.println( “Hello World!” );
if( 5 > 3 )

System.out.println( “Hello World!” );

Success for file 5

The post CPW 142 Object-Oriented Programming appeared first on My Assignment Online.

Plagiarism Free Assignment Help

Expert Help With This Assignment — On Your Terms

Native UK, USA & Australia writers Deadline from 3 hours 100% Plagiarism-Free — Turnitin included Unlimited free revisions Free to submit — compare quotes
Scroll to Top