daniel_lawson9999

Daniel Lawson Chapter 4 Projects(Roman Numerals/Pascal's tr

Apr 26th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.17 KB | None | 0 0
  1. /*
  2. Pseudo Code:
  3. Pascals's triangle:
  4.  
  5. first create a method that will calculate factorials:
  6. The number will accept an integer as a parameter and return a factorial. It will do this by using a for loop which will
  7. run equal to the size of the number itself, and every iteration it will multiply the total number by the number of the current value of the for loop
  8.  
  9. Create a method which will create Pascals Triangle
  10. In the method it will have two for loops, one for the row, and one for the number in the row. In the row loop:
  11. if it is the first number in the row it will print a certain amount of spaces based on the row, if it is not the first number, it will just print uses a 4 padded printf
  12. It will print the number itself by calling the factorial number and plugging in values depending on its place in the for loop
  13.  
  14. In the main:
  15. It will prompt the user for a number of rows, and then call the pascals triangle method.
  16. */
  17. /**
  18.  * Created by Daniel on 4/22/2016.
  19.  */
  20. import java.util.*;
  21. public class Project7PascalsTriangle {
  22.     public static void main(String[] args){
  23.         Scanner c = new Scanner(System.in);
  24.         System.out.println("Create Pascal's Triangle!");
  25.         System.out.print("Enter a number of rows : ");
  26.         int n = c.nextInt();
  27.         System.out.println();
  28.         triangle(n);
  29.     }
  30.     public static void triangle(int row){
  31.         int num = 0;
  32.         row--;
  33.         for(int r = 0; r < row; r++){
  34.             for(int n = 0; n < r+1;n++){
  35.                 num = (int) ((factorial(r))/(factorial(n)*factorial(r-n)));
  36.                 if (n == 0) {
  37.                     for (int i = 1; i <= r * -2 + row * 2 -2; i++)System.out.print(" ");
  38.                     System.out.print(num);
  39.                 }
  40.                 else System.out.printf("%4d", num);
  41.  
  42.             }
  43.             System.out.println();
  44.         }
  45.     }
  46.     public static double factorial(int n){
  47.         double num = 1;
  48.         for(int i = 1; i <= n;i++)num *= i;
  49.         return num;
  50.     }
  51. }
  52. /*
  53. Pseudo code for roman numerals!
  54.  
  55. Create a method that will accept an integer value. The method will take the integer value, and print it in Roman Numerals.
  56. It will convert the number to roman numerals by doing:
  57. There will be a while loop that will run as long as the inputed integer is not 0.
  58. In the while loop it will follow a sequence of if/else/if statements which will test to see if the remaining integer value is above a certain point,
  59. and will then subtract that much from the number, and add the quantity it subtracted to the end of the string.
  60. This will follow a general sequence of 1000,500,100,50,10,5,1 but will have numbers in between like 900, 400, 90, 40, 9, 4 to satisfy the rule that numbers before subtract. After
  61. the while loop is done, it will print the final number in roman numerals.
  62.  
  63.  
  64. There will be a second method which will prompt the user for values. It will run convert numbers into roman numerals by calling the previous method over and over again in
  65. a while loop, but it will tell the user that they can end the while loop by entering an invalid roman numeral (not between 1-3999).
  66.  
  67. In the main it will call the 2nd method.
  68. */
  69. /*
  70. /**
  71.  * Created by Daniel on 4/26/2016.
  72.  */
  73. import java.util.*;
  74. public class Project1RomanNumerals {
  75.     public static void main(String[] args){
  76.         Scanner c = new Scanner(System.in);
  77.         con(c);
  78.     }
  79.     public static void num(int n){
  80.         String r = "";
  81.         while(n != 0) {
  82.             if (n>= 1000) {
  83.                 r += "M";
  84.                 n -= 1000;
  85.             }
  86.             else if(n >= 900){
  87.                 r+= "CM";
  88.                 n-= 900;
  89.             }
  90.             else if (n >= 500) {
  91.                 r += "D";
  92.                 n -= 500;
  93.             }else if(n >= 400){
  94.                 r += "CD";
  95.                 n -= 400;
  96.             }
  97.             else if (n >= 100) {
  98.                 r += "C";
  99.                 n -= 100;
  100.             }
  101.             else if(n>= 90){
  102.                 r+= "XC";
  103.                 n-= 90;
  104.             }
  105.             else if (n >= 50) {
  106.                 r += "L";
  107.                 n -= 50;
  108.             }
  109.             else if(n >= 40){
  110.                 r += "XL";
  111.                 n -= 40;
  112.             }
  113.             else if (n >= 10) {
  114.                 r += "X";
  115.                 n -= 10;
  116.             }
  117.             else if(n >= 9){
  118.                 r += "IX";
  119.                 n -=9;
  120.             }
  121.             else if (n >= 5) {
  122.                 r += "V";
  123.                 n -= 5;
  124.             }
  125.             else if(n>= 4){
  126.                 r += "IV";
  127.                 n-= 4;
  128.             }
  129.             else if (n >= 1){
  130.                 r += "I";
  131.                 n -= 1;
  132.             }
  133.         }
  134.         if(n == 0){
  135.             System.out.println("in roman numerals : " + r);
  136.         }
  137.     }
  138.     public static void con(Scanner c){
  139.         int x = -1   ;
  140.         System.out.println("Convert ints to roman numerals!");
  141.         while(x == -1){
  142.             System.out.print("Enter a value between 1-3999 (enter a valid not between 1-3999 to quit) :");
  143.             int n = c.nextInt();
  144.             if(n >3999 || n<1)x=1;
  145.             else num(n);
  146.         }
  147.     }
  148. }
  149. */
Advertisement
Add Comment
Please, Sign In to add comment