/ 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.
Expand |
Embed | Plain Text
////////////////////////////////////////////////////////// /** * HelloWorld.java * The first program anyone should write in any language. */ public class HelloWorld { } } ////////////////////////////////////////////////////////// /** * HelloFromCommandLine.java * The second program anyone should write in any language. * This teaches you to accept input to your program as parameters * at run-time. The Program will say your name when you provide * an input. * args[0] is the program name * args[1] is the first parameter * args[n] is the Nth parameter * EXAMPLE * Call the program as follows (from the command-line): * java HelloFromCommandLine Michael * For special characters and spaces, enclose the parameter "in quotes": * java HelloFromCommandLine "Michael Jackson" */ public class HelloFromCommandLine { //check that the input is valid (i.e. not missing or empty space) if (args[1] == null || args[1].equals("")) { } else { name = args[1]; } } } ////////////////////////////////////////////////////////// /** * HelloFromConsole.java * The first program anyone writes in any language. */ public class HelloFromConsole { } } ////////////////////////////////////////////////////////// /** * Multiples.java * Used to calculate N multiples of 2. */ public class Multiples() { public static void printMultsOf2(int multiples) { int runningTotal = 2; for (int i = 0; i < multiples; i++) { runningTotal += 2; } } printMultsOf2(10); } } ////////////////////////////////////////////////////////// /** * NumberToBinary.java * Convert a Decimal Number to a Binary Number (i.e. "2" becomes "10"). */ import java.io.*; import java.lang.*; public class NumberToBinary { } } ////////////////////////////////////////////////////////// /** * Grade.java * Given a student score, calculate their Letter Grade. */ ////////////////////////////////////////////////////////// /** * GPA.java * Given a set of student scores, calculate their GPA. */ ////////////////////////////////////////////////////////// /** * Lottery.java * Generate lotto numbers and/or given a player's lotto * ticket, check whether they are winners. */ ////////////////////////////////////////////////////////// /** * ConnectToURL.java * Connect to & display data from the web at a specific site's URL. */ import java.net.*; import java.io.*; public class ConnectToURL { public ConnectToURL() { } try { String str; while ((str = in.readLine()) != null) { } in.close(); } } } ////////////////////////////////////////////////////////// // THE END // //////////////////////////////////////////////////////////
You need to login to post a comment.
