mah17

Untitled

Oct 16th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. /*
  2.  * Class:      CSC190
  3.  * Project:    hw7
  4.  * Date:       2013, Oct 16
  5.  * Author:     Austin Holbrook <[email protected]>
  6.  * Purpose:    This program takes an input (n) between 1 and 13 and prints out 3
  7.  *             triangles of size n
  8.  */
  9.  
  10. import java.util.Scanner;
  11.  
  12.  
  13. class Triangle {
  14.     int lines;
  15.  
  16.     public void setLines(int lines) {
  17.         this.lines = lines;
  18.     }
  19.    
  20.     int getTriangularNumber(int n) {
  21.         return (n * (n+1)) / 2;
  22.     }
  23.    
  24.     void plotLeftAlignedHorizontal() {
  25.         for (int i = 1; i <= lines; i++) {
  26.             for (int j = getTriangularNumber(i)-i+1; j <= getTriangularNumber(i); j++) {
  27.                 System.out.printf("%-3d", j); //the dash left aligns the values
  28.             }
  29.             System.out.println();
  30.         }
  31.         System.out.println();
  32.     }
  33.    
  34.     void plotLeftAlignedVertical() {
  35.         int lastNum = 0;
  36.         for (int i = 1; i <= lines; i++) {
  37.             for (int j = lines; j > lines-i; j--) {
  38.                 if (j == lines) {
  39.                     lastNum = i;
  40.                     System.out.printf("%-3d", lastNum);
  41.                 }
  42.                 else {
  43.                     lastNum += j;
  44.                     System.out.printf("%-3d", lastNum);
  45.                 }
  46.             }
  47.             System.out.println();
  48.         }
  49.         System.out.println();
  50.     }
  51.    
  52.     void plotRightAlignedDiag() {
  53.         int lastNum = 0;
  54.         int times = 0;
  55.         for (int i = lines; i >= 1; i--) {
  56.             times++;
  57.             //Print the leading space
  58.             for (int j = 1; j <= lines-times; j++) {
  59.                 System.out.print("   ");
  60.             }
  61.            
  62.             for (int j = lines; j > lines-times; j--) {
  63.                 if (j == lines) {
  64.                     lastNum = i;
  65.                     System.out.printf("%3d", lastNum);
  66.                 }
  67.                 else {
  68.                     lastNum += j+1;
  69.                     System.out.printf("%3d", lastNum);
  70.                 }
  71.             }
  72.             System.out.println();
  73.         }
  74.     }
  75.    
  76. }
  77.  
  78. public class Runner {
  79.     public static void main(String[] args) {
  80.         Scanner sc = new Scanner(System.in);
  81.         Triangle myTriangle = new Triangle();
  82.        
  83.         int lines; //Lines the triangle has
  84.        
  85.         //Validate input for lines
  86.         while (true) {
  87.             System.out.print("Enter lines (1 - 13): ");
  88.             lines = sc.nextInt();
  89.            
  90.             if (lines >= 1 && lines <= 13) {
  91.                 myTriangle.setLines(lines);
  92.                 break;
  93.             }
  94.            
  95.             System.out.println("You must enter a value between 1 and 13!");
  96.         }
  97.        
  98.         myTriangle.plotLeftAlignedHorizontal();
  99.         myTriangle.plotLeftAlignedVertical();
  100.         myTriangle.plotRightAlignedDiag();
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment