Advertisement
Guest User

Java Basics

a guest
Oct 1st, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Point of this is to cover all the basics you will need to remember for the competition.
  3.  *  Nothing challenging, just all the core stuff.
  4.  */
  5.  
  6. import java.util.Arrays;
  7. import java.util.Collections;
  8. import java.util.Comparator;
  9. import java.util.Scanner;
  10.  
  11.  
  12.  
  13. public class template {
  14.     public static void main(String args[]){
  15.        
  16.         // Type syso then press ctrl+space to add the output line
  17.         System.out.println("Hello world!");
  18.        
  19.         int x = 3;
  20.        
  21.         String s = "String";
  22.         char c = 'c';
  23.         float f = 1.2345f; // Need the f to explicitly create a float (32-bits precision)
  24.         double d = 1.2345; // 64-bits
  25.         boolean b = true;
  26.        
  27.         System.out.println("Output with variable concatenated: " + d);
  28.         System.out.print("Output without new line added by default.\nNew line\n");
  29.        
  30.         // Object used to read input
  31.         Scanner in = new Scanner(System.in);
  32.        
  33.         // Get an int
  34.         System.out.print("Type in a number: ");
  35.         int y = in.nextInt();
  36.         // This clears the line. Essential this is used after reading anything in other than the whole line.
  37.         in.nextLine();
  38.        
  39.         System.out.print("Type in a string: ");
  40.         String str = in.nextLine(); // Use this to get the whole line.
  41.        
  42.        
  43.         System.out.println("Number entered: " + y + ", string entered: \"" + str + "\"");
  44.        
  45.         System.out.print("Press enter to continue.");
  46.         in.nextLine();
  47.        
  48.        
  49.         // Arrays- type name[] = new type[size]
  50.         // When you know the size
  51.         int xs[] = new int[10];
  52.         // When you don't know the size
  53.         int ys[][];
  54.         // Re-assigning a variable for multiple arrays of different sizes
  55.         ys = new int[3][4];
  56.         ys = new int[5][7];
  57.        
  58.         System.out.println("Filling array xs with values:");
  59.         // Fill it with descending values
  60.         for(int i=0;i<xs.length;i++){
  61.             xs[i] = xs.length - i;
  62.             System.out.print(xs[i] + " ");
  63.         }
  64.         System.out.println();
  65.        
  66.         // Sort the array in ascending order
  67.         Arrays.sort(xs);
  68.         // Output array
  69.         System.out.println("\nSorted array: ");
  70.         for(int i=0; i<xs.length;i++){
  71.             System.out.print(xs[i] + " ");
  72.         }
  73.         System.out.println();
  74.        
  75.  
  76.         // Sorting in any other order is more difficult.
  77.         // One way is to copy our int array to an Integer array
  78.         Integer[] is = new Integer[xs.length];
  79.         for(int i=0; i<is.length; i++){
  80.             is[i] = Integer.valueOf(xs[i]);
  81.         }
  82.         // Now sort in descending order
  83.         Arrays.sort(is, Collections.reverseOrder());
  84.         // Output array
  85.         System.out.println("\nSorted array: ");
  86.         for(int i=0; i<is.length;i++){
  87.             System.out.print(is[i] + " ");
  88.         }
  89.        
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement