Advertisements

Aatul Palandurkar

Posts Tagged ‘Core Java

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 : 

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);
 }
}

Output :

1110

 

Java Program to convert Decimal to Binary using Scanner class :


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);

}
}

Output :

Enter a number: 14

1110

Advertisements

Starting a series of some Java programs as many students were asking to share the programs. Here is the first program of the series.

Objectives:

  • Write a Program that calculates and prints the simple interest using the formula : Simple Interest = PTR/100
  • How to calculate Simple Interest in Java?
  • Write a program to calculate Simple Interest in Java.
  • Write a Java program that calculates and prints the simple interest using the formula : “SimpleInterest = PTR/100” and input values P, T, R should be accepted as command line.
  • How to use Command Line Arguments in Java?
  • How to pass values from command line in Java?
  • How to take input in Java from console?

 

This particular program can be written in various ways, we will try to write the solution program in different ways here and one can try to understand the difference between them.

Method 1 :

import java.util.Scanner;

public class SimpleInterestExample {

    public static void main(String[] args) {

        int p,t,r, result;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the Value of P : ");
        p = sc.nextInt();

        System.out.println("Enter the Value of T : ");
        t = sc.nextInt();

        System.out.println("Enter the Value of R : ");
        r = sc.nextInt();

        result = (p*t*r)/100;
        System.out.println("Interest is : " + result);
    }
}

This one is the simplest way to write the program to find simple interest in Java. Here we have simply created the object of Scanner class for taking input. Scanner class was introduced in Java 6. To use Scanner class we will have to import the class from java.util package.

Method 2:

Now we will write a Java program that calculates and prints the simple interest using the Scanner class again but with object oriented programming approach.

import java.util.Scanner;

public class SimpleInterestExample
{
		double principalAmount = 0;
        double interestRate = 0;
        double term = 0;
		double simpleInterest = 0; 

    public void calculateSimpleInterest()
	{
		Scanner input = new Scanner(System.in);

		System.out.print("Enter the Principal amount : ");
		principalAmount = input.nextDouble();

		System.out.print("Enter the Rate As a decimal : ");
		interestRate = input.nextDouble();

		System.out.print("Enter the amount of time in years : ");
		term = input.nextDouble();

		simpleInterest = (principalAmount * interestRate * term) / 100;
	}

    public void displaySimpleInterest()
	{
		System.out.println("The Simple Interest is : " + simpleInterest);
    }
}

Mock Test Program for above code so that we can test the code. keep both the classes in same package and run the Mock Test Program or MockTestProgram.java

public class MockTestProgram{

    public static void main(String[]args);
	{
		SimpleInterestExample simpleInt = new SimpleInterestExample();
		simpleInt.calculateSimpleInterest();
		simpleInt.displaySimpleInterest();
	}
}

Method 3:

Now we will write a Java program that calculates and prints the simple interest using the formula : “SimpleInterest = PTR/100” and input values P, T, R should be accepted as command line.

import java.util.Scanner;

public class SimpleInterestExample {

    public static void main(String[] args) {
		double p,t,r, result;

		p = Double.parseDouble(args[0]);
		t = Double.parseDouble(args[1]);
		r = Double.parseDouble(args[2]);

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter the Value of P : ");
		p = sc.nextDouble();

		System.out.println("Enter the Value of T : ");
		t = sc.nextDouble();

		System.out.println("Enter the Value of R : ");
		r = sc.nextDouble();

		result = (p*t*r)/100;
		System.out.println("Interest is : " + result);
    }
}

 

I will be sharing more programs soon as many students were asking for programs which were carried out during Java training and were asked in assignments after Java training. By the time enjoy coding in Java and use NetBeans, it will ease your life.

Conducted a Workshop on Core Java at Tulsiramji Gaikwad Patil College of Engineering & Technology, Nagpur on 25 Mar. 2013 & 26 Mar. 2013. It was nice to provide Classroom Training & Workshop on Core Java to students of TGPCET.

Have a look at glimpse of workshop here :

Watch this as You Tube video:

Objectives :

  • Why Java is platform independent?
  • How java program executes?
  • What is execution flow of Java Program?
  • What are the steps to execute a Java Program?
  • What are the steps to run a Java Program?

Objective :

  • How to write a simple Java program?
  • How to write Hello in Java?
  • How to write HelloWorld in Java?
  • How to write & execute a simple Java program?
  • How to write Java program using Notepad & execute using Command Prompt?
[youtube:http://www.youtube.com/watch?v=WF0vl4x8Dm0%5D

Posted from WordPress for Android


Advertisements

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 8,346 other followers

Tweets

%d bloggers like this: