Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- public class Template {
- public static void main(String args[]) {
- BufferedWriter writer = null;
- try {
- // create a temporary file
- System.out.println("dir:" + System.getProperty("user.dir"));
- File logFile = new File(System.getProperty("user.dir") + "\\lab3.txt");
- writer = new BufferedWriter(new FileWriter(logFile));
- writer.write(shape3(9) + System.lineSeparator() + shape4(9) + System.lineSeparator() + shape5(9)
- + System.lineSeparator() + shape6(9));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- // Close the writer regardless of what happens...
- writer.close();
- } catch (Exception e) {
- }
- }
- // append following patterns in a file e.g. "c:\\lab3.txt" one by one
- // read the pattern File e.g. "c://lab3.txt" and display to the screen
- }
- // sample pattern printing to the screen
- public static void shape1(int j) {
- for (int i = 1; i <= j; i++) {
- for (int k = 1; k <= i; k++) {
- System.out.print(i);
- }
- System.out.println();
- }
- }
- // sample pattern printing to the screen
- public static void shape2(int j) {
- for (int i = 1; i <= j; i++) {
- for (int k = 1; k <= (j * 2 + 1); k++) {
- System.out.print(i);
- }
- System.out.println();
- }
- }
- // save the pattern in a file
- // need to handle the exceptions
- public static String shape3(int j) {
- int width = j * 2;
- int height = j;
- String stringToPrint = "";
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- if (row == 0) {
- stringToPrint += "" + (row + 1);
- } else {
- if (row == (height - 1)) {
- stringToPrint += "" + (row + 1);
- } else {
- if ((col == 0) || (col == width / 2) || (col == (width - 1))) {
- stringToPrint += "" + (row + 1);
- } else {
- stringToPrint += ".";
- }
- }
- }
- }
- stringToPrint += System.getProperty("line.separator");
- }
- return stringToPrint;
- }
- // save the pattern in a file
- // need to handle the exceptions
- public static String shape4(int j) {
- int width = j * 2 + 1;
- int height = j;
- String stringToPrint = "";
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- if (row == 0) {
- stringToPrint += "" + (row + 1);
- } else {
- if (row == (height - 1)) {
- stringToPrint += "" + (row + 1);
- } else {
- if (row == (height / 2)) {
- stringToPrint += "" + (row + 1);
- } else {
- if ((col == 0) || (col == width / 2) || (col == (width - 1))) {
- stringToPrint += "" + (row + 1);
- } else {
- stringToPrint += ".";
- }
- }
- }
- }
- }
- stringToPrint += System.getProperty("line.separator");
- }
- return stringToPrint;
- }
- // save the pattern in a file
- // need to handle the exceptions
- public static String shape5(int j) {
- int width = j * 2 + 1;
- int height = j;
- String stringToPrint = "";
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- if (row == 0) {
- stringToPrint += "" + (row + 1);
- } else {
- if (row == (height - 1)) {
- stringToPrint += "" + (row + 1);
- } else {
- if (row == (height / 2)) {
- stringToPrint += "" + (row + 1);
- } else {
- if ((col == 0) || (col == width / 2) || (col == (width - 1))) {
- stringToPrint += "" + (1);
- } else {
- if ((col == (width / 6)) || (col == (width / 3)) || (col == (width * 2 / 3))
- || (col == (width * 5 / 6))) {
- stringToPrint += "x";
- } else {
- stringToPrint += ".";
- }
- }
- }
- }
- }
- }
- stringToPrint += System.getProperty("line.separator");
- }
- return stringToPrint;
- }
- // save the pattern in a file
- // need to handle the exceptions
- public static String shape6(int j) {
- int width = j * 2 + 1;
- int height = j;
- int middle = width / 2;
- String stringToPrint = "";
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- if ((col < (middle - row)) || (col > (middle + row))) {
- stringToPrint += ".";
- } else {
- stringToPrint += "" + (row + 1);
- }
- }
- stringToPrint += System.getProperty("line.separator");
- }
- return stringToPrint;
- }
- }
Add Comment
Please, Sign In to add comment