Advertisement
wingman007

Java2014ConditionLoopsArrays

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