Knowledge in Java

Funny code comments

// sometimes I believe compiler ignores all my comments Exception up = new Exception(“Something is really wrong.”); throw up; //ha ha //When I wrote this, only God and I understood what I was doing //Now, God only knows // I dedicate all this code, all my work, to my wife, Darlene, who will // have to support me and our three children and the dog once it gets // released into the public. // drunk, fix later // Magic. Do not touch. return 1; # returns 1 double penetration; // ouch /////////////////////////////////////// this is a well commented line // I am not sure if we need this, but too scared to delete. // I am not responsible of this code. // They made me write it, against my will. //Dear future me. Please forgive me. //I can’t even begin to express how sorry I am. options.BatchSize = 300; //Madness? THIS IS SPARTA! // I have to find a better job // hack for ie browser (assuming that ie is a browser) } catch (PartInitException pie) { // Mmm… pie } // John! If you’ll svn remove this once more, // I’ll shut you, for God’s sake! // That piece of code is not “something strange”! // That is THE AUTH VALIDATION. try { } catch (SQLException ex) { // Basically, without saying too much, you’re screwed. Royally and totally. } catch(Exception ex) { //If you thought you were screwed before, boy have I news for you!!! } // Catching exceptions is for communists // If you’re reading this, that means you have been put in charge of my previous project. // I am so, so sorry for you. God speed. // if i ever see this again i’m going to start bringing guns to work // The magnitude of this hack compares favorably with that of the national debt. //ALL YOUR BASE ARE BELONG TO US // If this code works, it was written by Paul. If not, I don’t know who wrote it //You are not expected to understand this /** * If you don’t understand this code, you should be flipping burgers instead. */ ‘NO COMMENT //Abandon all hope yea who enter beyond this point //Mr. Compiler, please do not read this. catch (Ex as Exception) { // oh crap, we should do something. } // TODO make this work // If you delete the credits, I will fucking kill you. // This is crap code but it’s 3 a.m. and I need to get this working. // For the sins I am about to commit, may James Gosling forgive me // Houston, we have a problem // If I from the future read this I’ll back in time and kill myself.

Java programs to print integers from 1 to 10

class Integers {   public static void main(String[] arguments) {     int c; //declaring a variable   /* Using for loop to repeat instruction execution */     for (c = 1; c <= 10; c++) {       System.out.println(c);     }   } }

Java If Else Condition

class Condition {   public static void main(String[] args) {     boolean learning = true;     if (learning) {       System.out.println("Java programmer");     }     else {       System.out.println("What are you doing here?");     }   } }

Java print command line arguments

class Arguments {   public static void main(String[] args) {     for (String t: args) {       System.out.println(t);     }   } }

Java program to compare strings

import java.util.Scanner; class CompareStrings {    public static void main(String args[])    {       String s1, s2;       Scanner in = new Scanner(System.in);             System.out.println("Enter the first string");       s1 = in.nextLine();             System.out.println("Enter the second string");       s2 = in.nextLine();             if (s1.compareTo(s2) > 0)          System.out.println("The first string is greater than the second.");       else if (s1.compareTo(s2) < 0)          System.out.println("The first string is smaller than the second.");       else            System.out.println("Both the strings are equal.");    } }

Java program to print alphabets

class Alphabets {    public static void main(String args[])    {       char ch;       for (ch = 'a'; ch <= 'z'; ch++)          System.out.println(ch);    } }

Java program to print multiplication table

import java.util.Scanner; class MultiplicationTable {   public static void main(String args[])   {     int n, c;     System.out.println("Enter an integer to print it's multiplication table");     Scanner in = new Scanner(System.in);     n = in.nextInt();     System.out.println("Multiplication table of " + n);     for (c = 1; c <= 10; c++)       System.out.println(n + "*" + c + " = " + (n*c));   } }

Java program to check if number is even or odd

import java.util.Scanner; class OddOrEven {    public static void main(String args[])    {       int x;       System.out.println("Enter an integer to check if it is odd or even");       Scanner in = new Scanner(System.in);       x = in.nextInt();             if (x % 2 == 0)          System.out.println("The number is even.");       else          System.out.println("The number is odd.");    } }

Java program to convert Celsius to Fahrenheit

import java.util.*; class FahrenheitToCelsius {   public static void main(String[] args) {     float temperature;     Scanner in = new Scanner(System.in);     System.out.println("Enter temperature in Fahrenheit");     temperature = in.nextInt();     temperature = ((temperature - 32)*5)/9;     System.out.println("temperature in Celsius = " + temperature);   } }

Java program to print the largest of the 3 numbers

import java.util.Scanner; class Largest {   public static void main(String args[])   {     int x, y, z;     System.out.println("Enter three integers");     Scanner in = new Scanner(System.in);     x = in.nextInt();     y = in.nextInt();     z = in.nextInt();     if (x > y && x > z)       System.out.println("First number is largest.");     else if (y > x && y > z)       System.out.println("Second number is largest.");     else if (z > x && z > y)       System.out.println("Third number is largest.");     else       System.out.println("The numbers are not distinct.");   } }

Java program for enhanced for loop

class EnhancedForLoop {   public static void main(String[] args) {     int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};     for (int t: primes) {       System.out.println(t);     }   } }

Java program to reverse string

import java.util.*; class ReverseString {    public static void main(String args[])    {       String original, reverse = "";       Scanner in = new Scanner(System.in);             System.out.println("Enter a string to reverse");       original = in.nextLine();             int length = original.length();             for (int i = length - 1 ; i >= 0 ; i--)          reverse = reverse + original.charAt(i);                 System.out.println("Reverse of the string: " + reverse);    }