Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 6
- HILLS
- AP
- COMPUTERS
- GOSLING
- INT
- A
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: FancyWord
- // Spec: Read in one word at a time from the file and output the word as an hourglass as shown – words connect along diagonal
- // and form complete words from one point to another – either forwards or backwards as shown.
- import java.util.Arrays;
- public class FancyWord
- {
- private String[][] mat;
- public FancyWord()
- {
- mat = new String[0][0];
- }
- public FancyWord(String s)
- {
- // size the matrix
- mat = new String[s.length()][s.length()];
- // use Arrays.fill() to fill in the matrix with spaces
- for (int r = 0; r < mat.length; r++)
- {
- Arrays.fill(mat[r], " ");
- }
- // use a for loop to load in the letters into the matrix
- for(int r = 0; r < mat.length; r++)
- {
- for(int c = 0; c < mat.length; c++)
- {
- if(r == c)
- {
- mat[r][c] = s.substring(c, c + 1);
- }
- else if(r == 0 || r == s.length() - 1)
- {
- mat[r][c] = s.substring(c, c + 1);
- }
- else if((r + c) == s.length() - 1)
- {
- mat[r][c] = s.substring(c, c + 1);
- }
- }
- }
- }
- public String toString()
- {
- String output = "";
- // use nested for loops to build a String from the matrix
- for (int r = 0; r < mat.length; r++)
- {
- output += Arrays.toString(mat[r]) + "\n";
- }
- return output.replace(",", "").replace("[", "").replace("]", "") + "\n";
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Name: Sammy Samkough
- // Prog: FancyWord
- // Spec: Read in one word at a time from the file and output the word as an hourglass as shown – words connect along diagonal
- // and form complete words from one point to another – either forwards or backwards as shown.
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- public class FancyWordRunner
- {
- public static void main(String args[]) throws IOException
- {
- Scanner file = new Scanner(new File ("fancyword.dat"));
- String word;
- FancyWord fw;
- int size = file.nextInt();
- file.nextLine();
- for(int i = 0; i < size; i++)
- {
- word = file.nextLine();
- fw = new FancyWord(word);
- System.out.println(fw);
- }
- }
- }
- /*
- H I L L S
- I L
- L
- I L
- H I L L S
- A P
- A P
- C O M P U T E R S
- O R
- M E
- P T
- U
- P T
- M E
- O R
- C O M P U T E R S
- G O S L I N G
- O N
- S I
- L
- S I
- O N
- G O S L I N G
- I N T
- N
- I N T
- A
- Press any key to continue . . .
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement