Posted By

bcmoney on 08/17/12


Tagged

Shell java output batch system standalone


Versions (?)

Learning JAVA (with basic standalone code snippets)


 / Published in: Java
 

Just some random stuff I worked through myself and used a long time ago to get up to speed with Java in University.

If you can understand these basic samples by actually running them and experimenting, then you'll be set to do more in-depth programming.

  1. //////////////////////////////////////////////////////////
  2. /**
  3.  * HelloWorld.java
  4.  * The first program anyone should write in any language.
  5.  */
  6. public class HelloWorld {
  7.  
  8. public static void main(String[] args) {
  9. System.out.println("Hello World");
  10. }
  11. }
  12.  
  13.  
  14. //////////////////////////////////////////////////////////
  15. /**
  16.  * HelloFromCommandLine.java
  17.  * The second program anyone should write in any language.
  18.  * This teaches you to accept input to your program as parameters
  19.  * at run-time. The Program will say your name when you provide
  20.  * an input.
  21.  * args[0] is the program name
  22.  * args[1] is the first parameter
  23.  * args[n] is the Nth parameter
  24.  * EXAMPLE
  25.  * Call the program as follows (from the command-line):
  26.  * java HelloFromCommandLine Michael
  27.  * For special characters and spaces, enclose the parameter "in quotes":
  28.  * java HelloFromCommandLine "Michael Jackson"
  29.  */
  30. public class HelloFromCommandLine {
  31. public static void main(String[] args) {
  32. String name = "";
  33. //check that the input is valid (i.e. not missing or empty space)
  34. if (args[1] == null || args[1].equals("")) {
  35. System.exit(1);
  36. }
  37. else {
  38. name = args[1];
  39. }
  40. System.out.println("Hello " + name);
  41. }
  42. }
  43.  
  44.  
  45.  
  46. //////////////////////////////////////////////////////////
  47. /**
  48.  * HelloFromConsole.java
  49.  * The first program anyone writes in any language.
  50.  */
  51. public class HelloFromConsole {
  52.  
  53. public static void main(String[] args) {
  54. System.out.print("Please enter username: ");
  55. System.out.println(" ");
  56. ConsoleReader console = new ConsoleReader(System.in);
  57. String username = console.readLine();
  58. System.out.println("Hello: " + username);
  59. }
  60. }
  61.  
  62. //////////////////////////////////////////////////////////
  63. /**
  64.  * Multiples.java
  65.  * Used to calculate N multiples of 2.
  66.  */
  67. public class Multiples() {
  68.  
  69. public static void printMultsOf2(int multiples) {
  70. int runningTotal = 2;
  71. for (int i = 0; i < multiples; i++) {
  72. System.out.print(runningTotal + " ");
  73. runningTotal += 2;
  74. }
  75. }
  76.  
  77. public static void main(String[] args) {
  78. printMultsOf2(10);
  79. }
  80. }
  81.  
  82.  
  83. //////////////////////////////////////////////////////////
  84. /**
  85.  * NumberToBinary.java
  86.  * Convert a Decimal Number to a Binary Number (i.e. "2" becomes "10").
  87.  */
  88. import java.io.*;
  89. import java.lang.*;
  90.  
  91. public class NumberToBinary {
  92. public static void main(String[] args) throws IOException {
  93. System.out.println("Enter any number:");
  94. String sn = bf.readLine();
  95. int i = Integer.parseInt(sn);
  96. String s = Integer.toBinaryString(i);
  97. System.out.println("Binary number is:" + s);
  98. }
  99. }
  100.  
  101.  
  102. //////////////////////////////////////////////////////////
  103. /**
  104.  * Grade.java
  105.  * Given a student score, calculate their Letter Grade.
  106.  */
  107.  
  108.  
  109.  
  110. //////////////////////////////////////////////////////////
  111. /**
  112.  * GPA.java
  113.  * Given a set of student scores, calculate their GPA.
  114.  */
  115.  
  116.  
  117.  
  118. //////////////////////////////////////////////////////////
  119. /**
  120.  * Lottery.java
  121.  * Generate lotto numbers and/or given a player's lotto
  122.  * ticket, check whether they are winners.
  123.  */
  124.  
  125.  
  126.  
  127.  
  128. //////////////////////////////////////////////////////////
  129. /**
  130.  * ConnectToURL.java
  131.  * Connect to & display data from the web at a specific site's URL.
  132.  */
  133. import java.net.*;
  134. import java.io.*;
  135.  
  136. public class ConnectToURL {
  137. public ConnectToURL() { }
  138.  
  139. public static void main (String[] args) throws IOException {
  140. try {
  141. URL url = new URL("http://www.bryancopeland.com");
  142. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  143. String str;
  144.  
  145. while ((str = in.readLine()) != null) {
  146. System.out.println(str);
  147. }
  148.  
  149. in.close();
  150. }
  151. catch (MalformedURLException e) {}
  152. catch (IOException e) {}
  153. }
  154. }
  155.  
  156. //////////////////////////////////////////////////////////
  157. // THE END //
  158. //////////////////////////////////////////////////////////

Report this snippet  

You need to login to post a comment.