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 #1
public static void pyramidInFile(int 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 2 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 0
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
buildTestFile()areEqualFiles()
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
testFactorsOfTwoInFile(out);
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 1 written in file test01.txt
pyramidInFile(1, "test01.txt");
*
Pattern of size 2 written in file test02.txt
pyramidInFile(2, "test02.txt");
**
Pattern of size 3 written in file test03.txt
pyramidInFile(3, "test03.txt");
-*-
***
Pattern of size 4 written in file test04.txt
pyramidInFile(4, "test04.txt");
-**-
****
Pattern of size 5 written in file test05.txt
pyramidInFile(5, "test05.txt");
--*--
-***-
*****
Pattern of size 6 written in file test06.txt
pyramidInFile(6, "test06.txt");
--**--
-****-
******
This is my code so far:
public static void pyramidInFile(int num, String fileName) throws IOException, IllegalArgumentException {
if (num <=0){
throw new IllegalArgumentException("Size must be positive.");
}
try (PrintWriter writer = new PrintWriter(fileName)){
for (int i =1; 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 =2* i -1; // Number of stars is 2*i -1 for row i
// Print leading hyphens
for (int j =0; j < hyphens; j++){
writer.print("-");
}
// Print stars with a space between them
for (int j =0; j < stars; j++){
writer.print("*");
if (j != stars -1){// Avoid extra space after the last star
writer.print("");
}
}
// Print trailing hyphens (same number as leading hyphens)
for (int j =0; 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 01- pattern of size 1 PASSED
pyramidInFile() TEST 02- pattern of size 2 FAILED
pyramidInFile() TEST 03- pattern of size 3 FAILED
pyramidInFile() TEST 04- pattern of size 4 FAILED
pyramidInFile() TEST 05- pattern of size 5 FAILED
pyramidInFile() TEST 06- pattern of size 8 FAILED
pyramidInFile() TEST 07- IllegalArgumentException, negative parameter PASSED
pyramidInFile() TEST 08- IOException PASSED
I don't understand what I'm doing wrong.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!