Question: Problem # 1 public static void pyramidInFile ( int num, String fileName ) throws IOException, IllegalArgumentException What to do In the TwoMethods.java file Work on
Problem #
public static void pyramidInFileint num, String fileName throws IOException, IllegalArgumentException
What to do
In the TwoMethods.java file
Work on the pyramidInFile method
Goal
In the method
outputs a pyramid pattern of any given size into a file with a given name
See the samples below in the section
Method calls and patterns they produceEven and odd sized patterns are differentThe method takes two parameters
Pattern size as an integerFile name as a String that specifies the name of the file to output the pyramid into
The method must handle types of Exceptions
IllegalArgumentExceptionIOExceptionBoth exceptions
are listed in the Throws Clause as part of the method's signatureNeither Exception is caught in the method but are instead thrown
TheIllegalArgumentException is thrown by your code by the throw statement
when the value of parameter size is negative or a
The IOException is not thrown by your code
but instead, will naturally propagate into the calling code without you programming the throw statement
It may be helpful to see examples of how Exceptions are handled by these methods provided in the code
buildTestFileareEqualFiles
Approach
Code in the pyramidInFile method of the TwoMethods.java file
Note
that testing naturally occurs during development as
the main method calls the testPyramidInFile method which
then calls your pyramidInFile method
comment out the call to the method testFactorsOfTwoInFile in the main method
unless you have the factorsOfTwoInFile method already working
When you feel comfortable with the testing done in the submit directory
copy TwoMethods.java to the Test Directory to do formal testingunless you have the testFactorsOfTwoInFile method working in the TwoMethods.java file
In TestAll.java
comment out the call to the method
testFactorsOfTwoInFileout;
Coding constructs to consider
if statement
throw statement
PrintWriter based on a file
For Loops possibly nested
Method calls and patterns they produce
Pattern of size written in file testtxt
pyramidInFile "testtxt;
Pattern of size written in file testtxt
pyramidInFile "testtxt;
Pattern of size written in file testtxt
pyramidInFile "testtxt;
Pattern of size written in file testtxt
pyramidInFile "testtxt;
Pattern of size written in file testtxt
pyramidInFile "testtxt;
Pattern of size written in file testtxt
pyramidInFile "testtxt;
This is my code so far:
public static void pyramidInFileint num, String fileName throws IOException, IllegalArgumentException
if num
throw new IllegalArgumentExceptionSize must be positive.";
try PrintWriter writer new PrintWriterfileName
for int i ; i num; i
Calculate number of hyphens and stars for the current row
int hyphens num i; Decreasing hyphens as we go down the rows
int stars i ; Number of stars is i for row i
Print leading hyphens
for int j ; j hyphens; j
writer.print;
Print stars with a space between them
for int j ; j stars; j
writer.print;
if j stars Avoid extra space after the last star
writer.print;
Print trailing hyphens same number as leading hyphens
for int j ; j hyphens; j
writer.print;
Move to the next line after each row
writer.println;
But when it gets tested this is the results:
pyramidInFile Tests
pyramidInFile TEST pattern of size PASSED
pyramidInFile TEST pattern of size FAILED
pyramidInFile TEST pattern of size FAILED
pyramidInFile TEST pattern of size FAILED
pyramidInFile TEST pattern of size FAILED
pyramidInFile TEST pattern of size FAILED
pyramidInFile TEST IllegalArgumentException, negative parameter PASSED
pyramidInFile TEST IOException PASSED
I don't understand what Im doing wrong.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
