NetBeans IDE Source Code

Source code of the projects and programs developed using NetBeans IDE.

Passing String in Switch Case in Java

Objectives:

  • How to use switch case in Java?
  • How to use switch case with string in Java?
  • How to pass string literals in switch case in Java?

 

Source Code:

[sourcecode lang=”java”]

public class SwitchWithStringExample

{
public static void main(String[] args)
{
String str = "two";
switch(str)
{
case "one":
System.out.println("one");
break;
case "two":
System.out.println("two");
break;
case "three":
System.out.println("three");
break;
default:
System.out.println("no match");
} // switch close
} // main close
} // class close

[/sourcecode]

 

Steps to run above Java code:

Consider that we have saved code at C drive (c:\>) and the file is saved as SwitchWithStringExample.java

  1. Compilation : c:\> javac SwitchWithStringExample.java
  2. Interpretation : c:\> java SwitchWithStringExample

 

Output:

two

 

We conduct Java Training and Workshop for Corporates and College Students, if interested, please write to us on kloudsancyber@gmail.com

 

Aatul Palandurkar
International Trainer, and Author, NetBeans Dream Team Member

https://www.facebook.com/aatulpalandurkar
https://www.instagram.com/aatulpalandurkar/

Java Program to convert Decimal values to Hexadecimal values

Objectives :

  • How to convert Decimal to Hexadecimal in Java?

 

Java Program to convert Decimal values to Hexadecimal values : 

[sourcecode lang=”java”]

import java.util.Scanner;

public class DecimalToHexadecimal {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter decimal number you like: ");
int deci = input.nextInt();

System.out.println("The hexadecimal number for decimal "
+ deci + " is " + convert(deci));
}

public static String convert(int decimal) {
String hex = "";

while (decimal != 0) {
int hexValue = decimal % 16;
hex = toHexadecimal(hexValue) + hex;
decimal = decimal / 16;
}

return hex;
}

public static char toHexadecimal(int hexValue) {
if (hexValue <= 9 && hexValue >= 0) {
return (char) (hexValue + ‘0’);
} else {
return (char) (hexValue – 10 + ‘A’);
}
}
}

[/sourcecode]

Output :
Enter decimal number you like: 1234
The hexadecimal number for decimal 1234 is 4D2

Java Program to Convert Decimal Number to Roman Number

Objectives :

  • Convert Decimal number to Roman number
  • Convert Decimal numeral to Roman numeral
  • How to convert a decimal number to roman number
  • Write a program that converts a decimal number to Roman number.
  • Write a program that converts a decimal number to Roman number. Decimal Number is accepted using Scanner class at the time of execution.
  • Write a program that converts a decimal number to Roman number. Decimal Number is accepted as command line input at the time of execution.

 

Program : 

Java program that converts a decimal number to Roman number. Decimal Number is accepted using Scanner class at the time of execution.

[sourcecode lang=”java”]

&nbsp;

import java.util.Scanner;

public class DecimalToRoman {

private static String toRoman(int num) {
String[] romanCharacters = { "M", "CM", "D", "C", "XC", "L", "X", "IX", "V", "I" };
int[] romanValues = { 1000, 900, 500, 100, 90, 50, 10, 9, 5, 1 };
String result = "";

for (int i = 0; i < romanValues.length; i++) {
int numberInPlace = num / romanValues[i];
if (numberInPlace == 0) continue;
result += numberInPlace == 4 && i > 0? romanCharacters[i] + romanCharacters[i – 1]:
new String(new char[numberInPlace]).replace("\0",romanCharacters[i]);
num = num % romanValues[i];
}
return result;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number : ");
int decimal = scanner.nextInt();
System.out.println(toRoman(decimal));
}

}

[/sourcecode]

Output :

Enter a number : 1234

MCCXXXIV

Java program that converts a decimal number to Roman number. Decimal Number is accepted as command line input at the time of execution.

[sourcecode lang=”java”]

public class DecimalToRoman {

private static String toRoman(int num) {
String[] romanCharacters = { "M", "CM", "D", "C", "XC", "L", "X", "IX", "V", "I" };
int[] romanValues = { 1000, 900, 500, 100, 90, 50, 10, 9, 5, 1 };
String result = "";

for (int i = 0; i < romanValues.length; i++) {
int numberInPlace = num / romanValues[i];
if (numberInPlace == 0) continue;
result += numberInPlace == 4 && i > 0? romanCharacters[i] + romanCharacters[i – 1]:
new String(new char[numberInPlace]).replace("\0",romanCharacters[i]);
num = num % romanValues[i];
}
return result;
}

public static void main(String[] args) {
if(args.length<1 || args.length>1){
System.out.println("Wrong input");
}else{
String number = args[0];
int decimal = Integer.parseInt(number);
System.out.println(toRoman(decimal));
}
}

}

[/sourcecode]

Book : NetBeans IDE How-to Instant

NetBeans IDE How-to : Atul Palandurkar : Packt Publishing, UK

NetBeans IDE How-to : Atul Palandurkar : Packt Publishing, UK

I was asked to write a book for Java Application Development using NetBeans IDE by PACKT Publishing, UK which is one of the leading technology books publishers in world. It will be a great experience to work with the great publishing house and their team. Thanks a lot Packt & Team.

I know, Java and NetBeans community will definitely accept the book with a grand welcome. The book is intended for Java Developers who are not aware of NetBeans IDE or those who have experience of Java development but have not used NetBeans for development of Java applications. This book can be referred by novice also to learn Java and Java application development using NetBeans IDE very easily.

My outline (contents) was selected out of few other authors and then after we signed the contract, now the book is complete and very soon the book will hit the market in different formats by the publisher in March 2013.

I just love it, I have tried my best in writing this book and to make it easier for novice to experts. It was a great pleasure working with Packt Team.

You will learn many things from this book such as :

  • Use NetBeans IDE for Java technologies
  • Develop a HelloWorld application with NetBeans IDE
  • Explore the layout of NetBeans IDE
  • Develop different Java applications in NetBeans
  • Use the Visual Designer
  • Design Java projects
  • Develop web applications using NetBeans IDE
  • Add and handle images in your application
  • Write interfaces in NetBeans IDE
  • Handle exceptions in NetBeans IDE
  • Deploy UI applications

You can order the book to avail huge discount. Instead of Packt Publishing the NetBeans IDE How-to book is also available on Amazon, Amazon UK, Barnes & Noble, Safari Books Online too.

You can buy and download the book in different formats such as e-book (pdf), e-pub, kindle book, etc.

Buy NetBeans IDE How-to Book

I hope you will like the book. After reading, please mail me your reviews.

You can book your copy from following links :

Get your copy now and start developing professional applications within few minutes.