March 2016

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”]

 

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]

Raspberry Pi 3 VS Raspberry Pi 2

Particulars Raspberry Pi 3 Model B Raspberry Pi 2 Model B
Processor Chipset Broadcom BCM2837 64Bit Quad Core Processor powered Single Board Computer running at 1.2GHz Broadcom BCM2836 32Bit Quad Core Processor powered Single Board Computer running at 900MHz
Processor Speed QUAD Core @1.2 GHz QUAD Core @900 MHz
RAM 1GB SDRAM @ 400 MHz 1GB SDRAM @ 400 MHz
Storage MicroSD MicroSD
USB 2.0 4x USB Ports 4x USB Ports
Max Power Draw/voltage 2.5A @ 5V 1.8A @ 5V
GPIO 40 pin 40 pin
Ethernet Port Yes Yes
WiFi Built in No
Bluetooth LE Built in No

Visit here for Raspberry Pi Training

Passing data between activities in Android

Objective :

  • Passing data between activities in Android
  • Passing multiple data between activities in Android
  • Passing array between activities in Android
  • Passing ArrayList between activities in Android
  • Passing ArrayList to another activity in Android
  • Sending data via Intent in Android
  • Sending multiple data via Intent in Android
  • Sending array between activities in Android
  • Sending ArrayList between activities in Android
  • Sending ArrayList to another activity in Android
  • Sending data via Intent and Bundle in Android
  • Sending multiple data via Intent and Bundle in Android
  • Passing array between activities in Android using Intent and Bundle
  • How to pass array to another activity in Android?
  • How to pass array between activities in Android?
  • How to pass data to another activity in Android?
  • How to pass data to activity in Android?
  • How to pass data to between activities in Android?
  • How to pass ArrayList to another activity in Android?
  • How to pass ArrayList between activities in Android?

 

Code for passing data between activities in Android :

ActivityOne.java

[sourcecode lang=”java”]

String value = "Hello!";
Intent in = new Intent(this,ActivityTwo.class);
in.putExtra("Key", value);
startActivity(in);

[/sourcecode]

ActivityTwo.java

[sourcecode lang=”java”]

Bundle bundle = getIntent().getExtras();
String valueReceived = bundle .getString("Key");

[/sourcecode]

 

Code for passing multiple data or values between activities in Android :

Method 1 : Using Intent to pass data and Bundle to extract data between activities in Android

ActivityOne.java

[sourcecode lang=”java”]

String value1 = "Hello!";
String value2 = "Hi!";
Intent in = new Intent(this,ActivityTwo.class);
in.putExtra("Key1", value1);
in.putExtra("Key2", value2);
startActivity(in);
[/sourcecode]

ActivityTwo.java

[sourcecode lang=”java”]
Bundle bundle = getIntent().getExtras();
String valueReceived1 = bundle .getString("Key1");
String valueReceived2 = bundle .getString("Key2");
[/sourcecode]

 

Method 2 : Using Bundle to pass and to extract data between activities in Android

ActivityOne.java

[sourcecode lang=”java”]

String value1 = "Hello!";
String value2 = "Hi!";
Intent in = new Intent(this,ActivityTwo.class);
Bundle bundle = new Bundle();
bundle.putString("Key1", value1);
bundle.putString("Key2", value2);
in.putExtras(bundle);
startActivity(in);
[/sourcecode]

ActivityTwo.java

[sourcecode lang=”java”]
Bundle bundle = getIntent().getExtras();
String valueReceived1 = bundle .getString("Key1");
String valueReceived2 = bundle .getString("Key2");
[/sourcecode]

 

Code for passing array between activities in Android :

ActivityOne.java

[sourcecode lang=”java”]
String[] array = new String[]{"Item1", "Item2", "item3", "Item4", "item5"};
Intent in = new Intent(this,ActivityTwo.class);
Bundle bundle = new Bundle();
bundle.putStringArray("MyArray", array);
in.putExtras(bundle);
startActivity(in);
[/sourcecode]

ActivityTwo.java

[sourcecode lang=”java”]
Bundle bundle = getIntent().getExtras();
String arrayReceived[] = bundle.getStringArray("MyArray");
[/sourcecode]

 

Code for passing ArrayList between activities in Android :

ActivityOne.java

[sourcecode lang=”java”]
ArrayList<String> array = new ArrayList<String>();
array.add("Hello");
array.add("Hi");
array.add("Bye");
Intent intent = new Intent(this, ActivityTwo.class);
intent.putExtra("array_list", array);
startActivity(intent);
[/sourcecode]

ActivityTwo.java

[sourcecode lang=”java”]
Bundle bundle = getIntent().getExtras();
ArrayList<String> array = (ArrayList<String>) bundle.getStringArrayList("array_list");
[/sourcecode]


Have fun with Intent.

Java Program to Convert Binary to Decimal

Objectives :

  • Binary to Decimal Conversion
  • Converting Binary to Decimal
  • Write a program to convert binary to decimal
  • Write a program to convert binary number to decimal format
  • Write a program to convert binary to decimal using Scanner class
  • Write a program to convert binary to decimal, take input using Scanner class

Java Program :

[sourcecode lang=”java”]

import java.util.Scanner;

public class BinaryToDecimal {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(&quot;Enter binary number: &quot;);
String binary = scanner.nextLine();
int decimal = binaryToDecimal(binary);
System.out.println(&quot;Decimal equivalent of &quot;+ binary +&quot; is &quot;+ decimal);
}

private static int binaryToDecimal(String binary) {
final int base = 2;
int decimal = 0;
for (int i = 0; i &lt; binary.length(); i++) {
if (binary.charAt(i) == ‘0’) {
decimal += 0 * Math.pow(base, binary.length() – i – 1);
} else if (binary.charAt(i) == ‘1’) {
decimal += 1 * Math.pow(base, binary.length() – i – 1);
} else {
System.out.println(&quot;Invalid Binary Number&quot;);
System.out.println(&quot;Binary Number Contains only 0’s or 1’s&quot;);
System.exit(0);
}
}
return decimal;
}
}

[/sourcecode]

Output :
Enter binary number: 1110
Decimal equivalent of 1110 is 14

Java Program to convert Decimal to Binary

Objectives :

  • Decimal to Binary Conversion
  • Converting Decimal to Binary
  • Converting Decimal Number to Binary format
  • Write a program to convert decimal to binary
  • Write a program to convert decimal number to binary format
  • Write a program to convert positive decimal number to binary format
  • Write a program to convert decimal to binary using Scanner class
  • Write a program to convert decimal to binary, take input using Scanner class

 

Java Program to convert Decimal to Binary : 

[sourcecode lang=”java”]
public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int[25];
int n = 0;
// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}
// Print Binary number
for(int i = n-1;i >= 0;i–){
System.out.print(binary[i]);
}
}

public static void main(String[] args){
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(14);
}
}
[/sourcecode]

Output :

1110

 

Java Program to convert Decimal to Binary using Scanner class :

[sourcecode lang=”java”]

import java.util.Scanner;

public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int[14];
int n = 0;

// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}

// Print Binary Number
for(int i = n-1;i >= 0;i–){
System.out.print(binary[i]);
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num= scanner.nextInt();
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(num);

}
}

[/sourcecode]

Output :

Enter a number: 14

1110

Java Program to find prime number upto N number

Objectives :

  • Write a Java program to find prime number upto N number
  • Write a Java program to print prime number upto N number
  • Write a Java program to find prime number upto N number using Command Line Arguments
  • Write a Java Program to find prime number upto N number via Command Line Arguments
  • Write a Java program to find prime number upto N number using Scanner class

 

Java Program / Code :

Method 1 : Java Program to find Prime Number upto N number using Scanner class

[sourcecode lang=”Java”]

import java.util.Scanner;
class PrimeNumber
{
public static void main(String[] args)
{
int n,p;
Scanner s=new Scanner(System.in);
System.out.println(“Enter number : ”);
n=s.nextInt();
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0){
System.out.println(i);
}
}
}
}
[/sourcecode]

Method 2 : Java Program to find Prime Number upto N number using Scanner class and writing function to find prime number

Method isPrime() for checking if a number is prime or not

[sourcecode lang=”java”]
public class PrimeNumber{
public boolean isPrime(int num) {
if ( num < 2 ){
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if ( num % i == 0 ) {
return false;
}
}
return true;
}
}

// Mock Test Class to test above code
import java.util.Scanner;

public class Demo
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Please enter a number: ");
int num = scanner.nextInt();

if ( num < 2 ) {
System.out.println("\n There are no Prime Numbers available");
System.exit(0);
}
System.out.println("\n Prime Numbers from 1 to "+ num);
PrimeNumber primeNum = new PrimeNumber();

for (int i = 3; i <= num; i++) {
if ( primeNum.isPrime(i) ) {
System.out.print(", " + i);
}
}
}
}
[/sourcecode]

Method 3 : Java Program to find Prime Number upto N number using Command line input

[sourcecode lang=”Java”]

class PrimeNumber
{
public static void main(String[] args)
{
int n,p;
n=Integer.parseint(args[0]);
for(int i=2;i&lt;n;i++)
{
p=0;
for(int j=2;j&lt;i;j++)
{
if(i%j==0)
p=1;
}
if(p==0){
System.out.println(i);
}
}
}
}
[/sourcecode]

Steps to run above program via command line :

  1. Compilation : C:\JavaPrograms>javac PrimeNumber.java
  2. Interpretation : C:\JavaPrograms>java PrimeNumber 20

Output :

2

3

5

7

11

13

17

19