Advertisement
wingman007

Java2014MethodsRecursionObjects

Nov 16th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.42 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package datatypes;
  8.  
  9. import java.io.IOException;
  10. import java.util.*;
  11.  
  12. /**
  13.  *
  14.  * @author fmi
  15.  */
  16. public class DataTypes {
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.     public static void main(String[] args) throws IOException {
  22.  
  23.         // TODO code application logic here
  24.         byte myByte = 22;
  25.         short myShort = 257;
  26.         int myInt = -123;
  27.         long myLong = 123L; // 077 0x077
  28.        
  29.         float myFloat = 2.34F;
  30.         double myDouble = 2.34; //
  31.        
  32.         char myChar = 'c'; //'\u0000'
  33.         boolean myBoolean = false; // true
  34.        
  35.         // -------------------- Referent -----
  36.         String myName = "Stoyan\\ "; // \n
  37.         Object myOtherByte = 3;
  38.        
  39.         System.out.print(myName);
  40.         System.out.println(myName);
  41.        
  42.         // int ch;
  43.         // 1.
  44.         // ch = System.in.read();
  45.        // System.out.print((char) ch);
  46.        
  47.         // while ((ch = System.in.read()) != '\n') {
  48.         //    System.out.print((char) ch);
  49.         // }
  50.        
  51.         // 2.
  52.         // java.util.Scanner input = new java.util.Scanner(System.in);
  53.         Scanner input = new Scanner(System.in);
  54.        
  55.         System.out.print("Please enter your first name: ");
  56.         String firstName = input.nextLine();
  57.         System.out.println(firstName);
  58.        
  59.         double number1 = 3.14;
  60.         double number2 = 2.71;
  61.        
  62.         System.out.println(number1 + number2);
  63.        
  64.         System.out.printf("%.3f \n", number1 + number2);
  65.        
  66. //  Old way for reading symbols
  67. //        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  68. //        String firstString = br.readLine();
  69. //        String lastString = br.readLine();
  70. //        System.out.printf("Hello, %s %s!\n", firstString, lastString);
  71.  
  72.  
  73.  
  74.       for(int i = 0; i < 10; i++)
  75.       {
  76.           for (int j = 0; j < i; j ++)
  77.           {
  78.             System.out.print('*');
  79.           }
  80.           System.out.print("\n");
  81.       }
  82.      
  83.       Operations(2.32, 45.6);
  84.      
  85.       // Excersise 2
  86.       // ############################################## 2 ###################################
  87.       // 1. Type conversions
  88.         long minNumber = 1;
  89.         long maxNumber = 1000000;
  90.  
  91.         // Generates a random number between 1 and 1000000
  92.         long uniqueID = minNumber + (long)(Math.random() * ((maxNumber - minNumber) + 1));
  93.  
  94.         // You can cast from one primitive value into another by putting what you want between ( )
  95.         // (byte) (short) (long) (double)
  96.         // (float), (boolean) & (char) don't work.
  97.         // (char) stays as a number instead of a character
  98.  
  99.         // You convert from a primitive to a string like this
  100.         String stringNumber = Long.toString(maxNumber);
  101.  
  102.         // Byte.toString(bigByte); Short.toString(bigShort); Integer.toString(bigInt);
  103.         // Float.toString(bigFloat); Double.toString(bigDouble); Boolean.toString(trueOrFalse);
  104.  
  105.         // You convert from a String to a primitive like this
  106.         int numberString = Integer.parseInt(stringNumber);
  107.  
  108.         // parseShort, parseLong, parseByte, parseFloat, parseDouble, parseBoolean
  109.  
  110.         System.out.println("Unique ID set to: " + uniqueID);
  111.      
  112.        
  113.         // 2) If
  114.       String name = "";
  115.      
  116.     // userInput.hasNextLine() returns true if a String was entered in the keyboard
  117.        if(input.hasNextLine()){
  118.  
  119.            // userInput.nextLine() returns the value that was entered at the keyboard
  120.            System.out.print("Enter your family name; \n");
  121.            name = input.nextLine();
  122.  
  123.            // hasNextInt, hasNextFloat, hasNextDouble, hasNextBoolean, hasNextByte,
  124.            // hasNextLong, nextInt, nextDouble, nextFloat, nextBoolean, etc.
  125.  
  126.        }
  127.        
  128.       System.out.printf("%s \n", name);
  129.      
  130.       // 3. if-else
  131.         int randomNumber = (int) (Math.random() * 126) + 1;
  132.              
  133.     char favoriteChar = (char) randomNumber;
  134.              
  135.     // if then else statement
  136.     // > < == != >= <=
  137.     if(randomNumber == 32){
  138.  
  139.             System.out.println("Favorite character set to: Space");
  140.  
  141.         } else if(randomNumber == 10){
  142.  
  143.             System.out.println("Favorite character set to: New Line");
  144.  
  145.         } else {
  146.  
  147.             System.out.println("Favorite character set to: " + favoriteChar);
  148.  
  149.         }
  150.      
  151.    
  152.         // Logical operators
  153.         // ! Logical operators : Converts the boolean value to its right to its opposite form ie. true to false
  154.         // & : Returns true if boolean value on the right and left are both true (Always evaluates both boolean values)
  155.         // && : Returns true if boolean value on the right and left are both true (Stops evaluating after first false)
  156.         // | : Returns true if either boolean value on the right or left are true (Always evaluates both boolean values)
  157.         // || : Returns true if either boolean value on the right or left are true (Stops evaluating after first true)
  158.         // ^ : Returns true if there is 1 true and 1 false boolean value on the right or left
  159.  
  160.         if((randomNumber > 97) && (randomNumber < 122)){
  161.  
  162.             System.out.println("Favorite character is a lowercase letter");
  163.  
  164.         }
  165.  
  166.         if(((randomNumber > 97) && (randomNumber < 122)) || ((randomNumber > 64) && (randomNumber < 91))){
  167.  
  168.             System.out.println("Favorite character is a letter");
  169.  
  170.         }
  171.  
  172.         if(!false){
  173.  
  174.             System.out.println("I turned false to " + !false);
  175.  
  176.         }
  177.  
  178.         // The ternary operator assigns one or another value based on a condition
  179.         int whichIsBigger = (50 > randomNumber) ? 50 : randomNumber;
  180.  
  181.         System.out.println("The biggest number is " + whichIsBigger);
  182.        
  183.        
  184.       // 4. switch
  185.     // The switch statement is great for when you have a limited number of values
  186.         // and the values are int, byte, or char unless you have Java 7 which allows Strings
  187.         switch(randomNumber){
  188.  
  189.         case 8 :
  190.             System.out.println("Favorite character set to: Backspace");
  191.             break;
  192.  
  193.         case 9 :
  194.             System.out.println("Favorite character set to: Horizontal Tab");
  195.             break;
  196.  
  197.         case 10 :
  198.         case 11 :
  199.         case 12 :
  200.             System.out.println("Favorite character set to: Something else weird");
  201.             break;
  202.  
  203.         default :
  204.             System.out.println("Favorite character set to: " + favoriteChar);
  205.             break;
  206.  
  207.         }        
  208.        
  209.        
  210.         // 5. loops
  211.          // System.out.println(1);
  212.          // System.out.println(2);
  213.          // System.out.println(3);
  214.        
  215.         // 5.1 while
  216.         int i = 1;
  217.         while(i < (maxNumber / 2000)){
  218.  
  219.             System.out.println(i);
  220.             i++;
  221.  
  222.             // This isn't needed, but if you want to jump out of a loop use break
  223.             if(i == (maxNumber/20000)) break;
  224.         }
  225.        
  226.         // 5.2 do - while
  227.         int number = 50;
  228.         // Do while loops are used when you want to execute the code in the braces at least once
  229.         do {
  230.  
  231.             System.out.println("Guess my number up to 100");
  232.  
  233.             // If what they entered isn't a number send a warning
  234.             while(!input.hasNextInt()){
  235.  
  236.                 String numberEntered = input.next();
  237.                 System.out.printf("%s is not a number\n", numberEntered);
  238.  
  239.             }
  240.             number = input.nextInt();
  241.  
  242.         }while(number != 50);
  243.  
  244.         System.out.println("Yes the number was 50");   
  245.        
  246.  
  247.      // 5.3 for      
  248.         for(int i1 = 0; i1 <= 100; i1++){
  249.  
  250.             // continue is used to skip 1 iteration of the loop
  251.             if(i1 == 90) continue;
  252.  
  253.             System.out.println(i1);
  254.  
  255.         }
  256.         // 5.4
  257.         int[] numbers = {2, 3, 5, 7, 11, 13, 17, 19};
  258.         for (int i2 : numbers){
  259.             System.out.println(i2);
  260.         }
  261.        
  262.          // An array is a fixed series of boxes that contain multiple values of the same data type
  263.      // How you create arrays
  264.      // int[] favoriteNumbers;
  265.      // favoriteNumbers = new int[20];
  266.      
  267.          int[] favoriteNumbers1 = new int[20];
  268.              
  269.         favoriteNumbers1[0] = 100;
  270.  
  271.         String[] stringArray = {"Random", "Words", "Here"};
  272.  
  273.         // for(dataType[] varForRow : arrayName)
  274.         for(String word : stringArray)
  275.         {
  276.             System.out.println(word);
  277.         }
  278.      
  279.     // This is a multidimensional array
  280.         String[][][] arrayName = {
  281.             {
  282.                 { "000", "more1" },
  283.                 { "100", "more2" },
  284.                 { "200", "more3" },
  285.                 { "300", "more4" }
  286.             },
  287.             {
  288.                 { "010", "more5" },
  289.                 { "110", "more6" },
  290.                 { "210", "more7" },
  291.                 { "310", "more8" }
  292.             },
  293.             {
  294.                 { "020", "more9" },
  295.                 { "120", "more10" },
  296.                 { "220", "more11" },
  297.                 { "320", "more12" }
  298.             }
  299.         };
  300.         System.out.println(arrayName[2][3][1]);
  301.  /*              
  302.         for(int i = 0; i < arrayName.length; i++)
  303.         {
  304.             for(int j = 0; j < arrayName[i].length; j++)
  305.             {
  306.  
  307.                 for(int k = 0; k < arrayName[i][j].length; k++)
  308.                 {
  309.                     System.out.print("| " + arrayName[i][j][k] + " ");
  310.  
  311.                 }
  312.             }
  313.  
  314.             System.out.println("|");
  315.  
  316.         }        
  317.  */  
  318.          // You can copy an array (stringToCopy, indexes to copy)
  319.         String[] cloneOfArray = Arrays.copyOf(stringArray, 3);
  320.  
  321.         // You can print out the whole array
  322.         System.out.println(Arrays.toString(cloneOfArray));
  323.  
  324.         // Returns the index or a negative number
  325.         System.out.println(Arrays.binarySearch(cloneOfArray, "Random"));
  326.  
  327.        Operations(1.0, 2.0);
  328.         Operations(4.5, 1, 2, 3, 4);
  329.        
  330.         System.out.println(factoriel(10));
  331.        
  332.         Random randomGen = new Random();
  333.        
  334.         randomGen = null;
  335.         // coolcsn.System.myMethod myClass = new coolcsn.System.myMethod();
  336.         // 1. non static        
  337.         // datatypes.Test test = new datatypes.Test();
  338.         // test.getAge();
  339.        
  340.         datatypes.Test.getAge();
  341.        
  342.     }
  343.    
  344.     static void Operations(double par1, double par2)
  345.     {
  346.         System.out.printf("%.2f + %.2f = %.2f", par1, par2, par1 + par2);
  347.     }
  348.  
  349.     static void Operations(double par1, int... elements)
  350.     {
  351.         System.out.printf("\n %.2f ", par1);
  352.         for (int number : elements) {
  353.             System.out.println(number);
  354.         }
  355.     }
  356.    
  357.     public static int factoriel(int n)
  358.     {
  359.         if(n <= 1) return 1;
  360.         return factoriel(n - 1) * n;
  361.     }
  362. }
  363.  
  364. /*
  365. import java.util.*;
  366. public class ReadingStringsNewStyle {
  367. public static void main(String[] args) {
  368. Scanner input = new Scanner(System.in);
  369. System.out.print("Please enter your first name: ");
  370. String firstName = input.nextLine();
  371. System.out.print("Please enter your last name: ");
  372. String lastName = input.nextLine();
  373. System.out.printf("Hello, %s %s!\n", firstName, lastName);
  374. // input.close(); - Don't close Scanner reading System.in!
  375. }
  376. }
  377. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement