Followers

Sunday, October 11, 2009

Static Inner Class Example in Java

Hello and welcome back to Java Code Online, there has been a request from many of my users to come up with an example for static inner class. So the topic of today is to to illustrate Static Inner Class in action.

You may any time check my previous article on Static Inner Class in Java for an illustration of what it is and what are the details for the other type of Inner classes in Java. today I will straight move to the example for it.

------------------------------------
class Cover
{
         static class InnerCover
             {
              void go()
                  {
                       System.out.println("I am the first Static Inner Class");
                  }
            }
 }

class Broom
{
         static class InnerBroom
        {
                  void go1()
                   {
                         System.out.println("I am second Static Inner Class");
                    }
         }

         public static void main(String[] args) {
         // You have to use the names of both the classes
         Cover.InnerCover demo = new Cover.InnerCover();
         demo.go();
        // Here we are accessing the enclosed class
        InnerBroom innerBroom = new InnerBroom ();
        innerBroom .go1();
 }
}
------------------------------------

When the above code is executed, the output is something like as displayed below:-
------------------------------------
 I am the first Static Inner Class
 I am second Static Inner Class
------------------------------------

We know that a Static inner class is just a class which is a static  member of the main class. So the same concept is applied here. Here we have two outer classes, one is called Cover, and the second is called Broom. Both of these outer classes have one static class within them.

The method of accessing both the inner classes is shown in the main method of the Broom class. I hope the example will be helpful to all of you. Do post a comment if you have any query or if you liked the article. Java Code Online will soon be back with yet another informative article.

Wednesday, September 23, 2009

Convert Celsius to Fahrenheit Java Code

The Java Code discussed today at Java Code Online deals with conversion of temperature from Celsius or Centigrade to Fahrenheit. The temperature conversions are required normally many times in our daily purposes. Like the body temperature in degree Fahrenheit and degree Celsius.

The only meeting point of these two temperature scales is -40 degree. At this temperature both the Celsius and the Fahrenheit scales are equal.

The Java code for this temperature conversion from Celsius to Fahrenheit is given below. The code requests the user to enter a temperature value in Celsius, and then displays the equivalent Fahrenheit Temperature.

/////////////////////////////////
package developer;

import java.util.Scanner;

public class CelsiusToFahrenheit {

    public static void main(String[] args) {

        System.out.println("Enter a temperature in Celsius: ");
        Scanner scanCelsius = new Scanner(System.in);
        double Fahrenheit = 0;

        if (scanCelsius.hasNextDouble())
        {
            Fahrenheit = (scanCelsius.nextDouble()*9) / 5 + 32;
        }
        System.out.println("The temperature in Fahrenheit is: " + Fahrenheit);
    }
}
/////////////////////////////////

When this Java Code was executed on my JVM, then the output was as shown below.

/////////////////////////////////
Enter a temperature in Celsius:
102.87
The temperature in Fahrenheit is: 217.166
/////////////////////////////////

Hope that the Java code provide for conversion from Celsius to Fahrenheit was beneficial to all. Keep checking Java Code Online for more Java updates and codes.

Related Java Code
Java Code to convert Fahrenheit to Celsius

Tuesday, September 22, 2009

Convert Fahrenheit to Celsius Java Code

Java Code Online has come up with a new Java Program, and that is for converting a temperature from Fahrenheit to Celsius or Centigrade. Fahrenheit and Celsius are the two main scales used for temperature measurement. The temperature of the normal human body is 98.4 degree Fahrenheit or 36.88 degree Celsius.

So temperature conversion is used normally in daily life. The only place where Fahrenheit and Celsius scales meet is at the temperature of -40 degree. That is to say that -40 degree Fahrenheit is equal to -40 degree Celsius.

The Java code for this temperature conversion from Fahrenheit to Celsius is given below. the code starts by asking the user to enter a temperature in Fahrenheit scale and then displays the equivalent Celsius Temperature.

/////////////////////////////////
package developer;

import java.util.Scanner;

public class FahrenheitToCelsius {

    public static void main(String[] args) {
        System.out.println("Enter a temperature in Fahrenheit: ");
        Scanner scanFaren = new Scanner(System.in);
        double Celsius = 0;
      
        if(scanFaren.hasNextDouble())
        {
            Celsius = (scanFaren.nextDouble() - 32)*5/9;
        }
        System.out.println("The temperature in Celsius is: "+Celsius);
      
    }
}
/////////////////////////////////

When this Java Code was executed on my JVM, then the output was as shown below.

/////////////////////////////////
Enter a temperature in Fahrenheit:
56
The temperature in Celsius is: 13.333333333333334
/////////////////////////////////

Hope that the Java code provide for conversion from Fahrenheit to Celsius was useful to you all. Java Code Online will soon be back with another important Java Code.

Related Java Code:-
Java Code to convert Celsius to Fahrenheit

Java Code to Convert Binary to Decimal

Java has deal with many different situations. So Java Code Online is going to discuss a case when a number entered as binary is to be converted to a decimal number. A binary number which consists of only 1 and 0, while a decimal number is a number which consists of numbers between 0-9.

We normally use decimal numbers in our daily purposes. Binary numbers on the other hand are very important and used for computers data transfer.

The Java code for converting a binary number to a decimal number, starts by asking the user to enter a binary number. The result is a decimal number which is displayed to the user. In case the number entered is not a binary number, i.e. it contains numbers other then 0 and 1. In that a NumberFormatException will be thrown. I have catch this exception, so that the developer can use there custom message in that place.

The Java Code is displayed below:-

/////////////////////////////////////

package developer;

import java.util.Scanner;

public class BinaryToDecimal {

    public static void main(String[] args) {
      
        System.out.println("Enter a binary number: ");
        Scanner scanStr = new Scanner(System.in);
              
        int decimalNum = 0;      
              
        if(scanStr.hasNext())
        {   
            try
            {
                decimalNum = Integer.parseInt(scanStr.next(),2);
                System.out.println("Binary number in decimal is: "+decimalNum);  
            }
            catch(NumberFormatException nfe)
            {
                System.out.println("The number is not a binary number.");
            }
        }  
    }
}


/////////////////////////////////////

When this code is executed on my system, the output is as shown below.
/////////////////////////////////////

Enter a binary number:
10100011
The binary number in decimal is: 163

/////////////////////////////////////

In case the number entered is not a binary number, then the output is as shown below:-

/////////////////////////////////////

Enter a binary number:
123456
The number is not a binary number.

/////////////////////////////////////

Hope that I was able to make my point clear. If you liked the article then do post a comment. Kepp checking Java Code Online for more Java Codes.

Related Java Codes
Java Code to Convert Decimal to Binary

Java Code to Convert Decimal to Binary

The Java Code Online is today going to discuss the Java code for converting a decimal number to binary number. A decimal number is known to all of us. It is number whose base value is 10. All the numbers we use in normal practice are usually decimal numbers. It implies that decimal numbers can consist of numbers from 0-9.

A binary number is a number whose base value is 2. It means that a binary number system consists of only 0 and 1. In computers all the data is transferred in the form of binary numbers.

The Java Code presented today, asks the user to enter a decimal number, and displays the binary equivalent of that number.

The Java code is displayed below:-

/////////////////////////////////////////////

package developer;

import java.util.Scanner;

public class DecimalToBinary {

    public static void main(String[] args) {
  
        System.out.println("Enter a decimal number: ");
        Scanner scanStr = new Scanner(System.in);
              
        String num = null;      
      
        //Check for an integer number entered by the user
        if(scanStr.hasNextInt())
        {
            //Convert the decimal number to binary String
            num = Integer.toBinaryString(scanStr.nextInt());
        }          
      
        System.out.println("The decimal number in binary is: "+num);          
    }
}

/////////////////////////////////////////////

When the code is executed, the output is as shown below:-

/////////////////////////////////////////////

Enter a decimal number:
23
The decimal number in binary is: 10111

/////////////////////////////////////////////

Hope that the code was useful to all of you. Keep buzzing Java Code Online.

Related Java Codes
Java Code to Convert Binary to Decimal

Monday, September 21, 2009

Java Code to write to CSV File


Hello guys, Java Code Online is back again with an important code. The code for today is for making a CSV (Comma Separated File) file. The operation to make a CSV file in Java is similar to the Java Code to write to a file. The difference between these two is that a comma is inserted within the text, where ever we want the columns in MS EXCEL to change to new column.

The beauty of the .csv file is that, when this file is opened in MS Excel, then all the values which are separated by comma, are displayed in a new column. This is required many times, if we want to send data to different columns. Even there is an option to switch rows while writing.

The Java Code displayed today, makes a .csv file at a particular location on your hard disk. You need to change the path, according to your requirements. The code writes some data to the .csv file. It generates 3 columns and 2 rows. You can modify it, according to your own requirements.

The Java Code is provided below:-

/////////////////////////////////////

/** The below Java Code writes
 * some data to a file in CSV
 * (Comma Separated Value) File
 */

package developer;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MakeCSVFile {

    public static void main(String[] args) throws IOException {

        //Note the "\\" used in the path of the file
        //instead of "\", this is required to read
        //the path in the String format.
        FileWriter fw = new FileWriter("D:\\JavaCodeFile\\WriteTest.csv");
        PrintWriter pw = new PrintWriter(fw);
       
        //Write to file for the first row
        pw.print("Hello guys");
        pw.print(",");
        pw.print("Java Code Online is maing");
        pw.print(",");
        pw.println("a csv file and now a new line is going to come");
       
        //Write to file for the second row
        pw.print("Hey");
        pw.print(",");
        pw.print("It's a");
        pw.print(",");
        pw.print("New Line");
       
        //Flush the output to the file
        pw.flush();
       
        //Close the Print Writer
        pw.close();
       
        //Close the File Writer
        fw.close();       
    }
}

/////////////////////////////////////

The code when run on my system a produces a file called WriteTest.csv, when opened with MS EXCEL, the output is shown below:-




I hoe that the code was useful to all of you. Java Code Online is going to be back soon.

Check out the tutorial at the Sun site

 Java is everywhere. Of the most  authenticate sources, that could be used for learning Java, I trust sun the most. The tutorial provided by java.sun.com normally called as the Java Tutorial at java.sun.com/docs/books/tutorial , is a very good one. I have scanned the tutorial many times earlier also, and every-time found it to be the most updated and authentic resource on the net.

The Java technology always keep coming up with new changes and features. Therefore the need to continuously update the link and site is very important. Sun is the provider of Java, and so they have to maintain the most updated links and material. The best part I found with this site was that the tutorial was concise and up to the point.

The site does not go in unnecessary examples or extra elaboration. It sticks to the basics and explains them well. The section for Trail Covering the basics, consists of many great Java technology articles. It does not deal only with basic Java, but the tutorial for Java Collections and Swing is also provided. This is an extra bonus for all those who are interested in getting to the roots of Java.

The section of specialized trails and lessons consists of many advanced Java related tutorials and articles. To name a few, the trail consists of articles and tutorials on Networking, Generics, JavaBeans, JDBC, JMX, JNDI, RMI, Reflection, Java Security, Graphics, Socket Programming in Java, etc.

All these are very important fields of Java for an advanced programmer. Like JDBC which is used for Database connection establishment, and retrieval and updation of records. The process involves many commands and is a separate field altogether.

JNDI and RMI, are used for Remote Method Invocation. This is a very large field in itself. The part of RMI is important for a distributed architecture. Like your server is at a particular place, and the application is running at a different place. There need to be interaction between this application and the server. RMI along with JNDI are used here.

Like this there are many fields from which the person can choose depending upon his or her intrest. It is vast ocean of resources for Java. Go ahead and give it a look, Hope that you will also find something very interesting and useful from this site.

That's it for now. Keep buzzing Java Code Online.