Saturday, September 12, 2015

Calculator using java

/*
* Write a C program for performing as calculator which allows all Arithmetic Operators with operands from
* terminal
 */
package cb_pr176;
import java.util.Scanner;
/**
 **
 * @author Arif
 **/
public class Cb_pr176 {
    /*
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner inp=new Scanner(System.in);
    int a,b;
    float c = 0;
    char ch;
    System.out.println("Enter Your 1st number:");
    a=inp.nextInt();
    System.out.println("Enter your 2nd number:");
    b=inp.nextInt();
    System.out.println("Enter your choice, example: +, -, *, /");
    ch=inp.next().charAt(0);
    if (ch=='+')
        c=a+b;
    else if(ch=='-')
        c=a-b;
    else if(ch=='/')
        c=(float)a/(float)b;
    else if(ch=='*')
        c=a*b;
    System.out.println("The Result is:" + c);
         
    }
  }

Calcualate Simple Interest using java

/*
*Write a C program to find the Simple Interest. Take input for principle amount, rate of interest and time
*from terminal.
*Simple Interest is the money paid by the borrower to the lender, based on the Principle Amount, Interest
*Rate and the Time Period.
*Simple Interest is calculated by, SI= P * T * R / 100 formula.
*Where, P is the Principle Amount.
*T is the Time Period.
*R is the Interest Rate.
*/
package cb_pr175;
import java.util.Scanner;

public class Cb_pr175 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner inp=new Scanner(System.in);
        int pa,ye;
        float rate,si;
        System.out.println("Enter your principal amount");
        pa=inp.nextInt();
        System.out.println("Enter your number of year");
        ye=inp.nextInt();
        System.out.println("Enter your rate of interest");
        rate=inp.nextFloat();
        si=(rate*pa)*ye;
        System.out.println("Your total interest is " + si);
       
    }

}

Sunday, March 22, 2015

Swap Digits from an Integer Number Using Constructor with parameter

Write a Java Program to swap digits from a given integer number. Use Constructor with parameter. Use necessary method and instance, if required.

Reference:

1.
Course Book(The Complete Reference Java j2se) ; Page 119

2.
http://c-programming-sourcecode.blogspot.com/2014/12/c-program-to-swap-numbers.html?updated-min=2014-12-01T00:00:00-08:00&updated-max=2015-01-01T00:00:00-08:00&max-results=41

Source Code:

package Swap;
import java.util.Scanner;

class Swapnum
{
int number;
Swapnum(int digit)
{
number=digit;
}
int get_Swap(){
int swa = 0;
int remainder = 0;
do{
remainder = number%10;
swa = swa*10 + remainder;
number = number/10;
}while(number > 0);
return swa;
}
}
public class Swap {

public static void main(String args[]) {
Scanner input =new Scanner(System.in);
int number;
System.out.println("Please enter number to be Swap using Parameterized Constructor: ");
number = input.nextInt();
Swapnum r =new Swapnum(number);
System.out.println("After Swap : "+r.get_Swap());
}
}

Count Vowels from a String, Use Constructor

Write a Java Program to Count the Number of Vowels in a given string. Use Constructor, Instances and Method, if necessary. Do not use any parameter with Constructor.

Reference: 
1.  Course Book(The Complete Reference Java j2se) ; Page 118

2. http://c-programming-sourcecode.blogspot.com/2014/12/count-number-of-vowels.html?updated-min=2014-12-01T00:00:00-08:00&updated-max=2015-01-01T00:00:00-08:00&max-results=41

Java Source Code:

package vowel;
import java.util.Scanner;
class CountVowels
{
int count =0;
String str;
CountVowels()
{
str="Bangladesh" ;
}
void get()
{
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' || ch == 'A' ||
ch == 'E' || ch == 'I' || ch == 'O' ||
ch == 'U')
count++;

}
System.out.println("Enter String is " + str);
System.out.println("Number of vowel of "+str+ " is : " + count);
}
}

public class vowel{
public static void main(String[]args){
CountVowels vol = new CountVowels();
vol.get();
}
}

Sum and Count of Prime Number With Method and Parameter

Write a Java program to calculate the SUM and COUNT of Primes from a given range of numbers. Formatting required for the Input and Output statements. Use Method with Parameter and return value to the Main function.

Reference:
(1) Course Book(The Complete Reference Java j2se) ; Page 116
(2) http://c-programming-sourcecode.blogspot.com/2012/08/prime-number.html?updated-min=2012-01-01T00:00:00-08:00&updated-max=2013-01-01T00:00:00-08:00&max-results=14


Source Code:

package Prime;
import java.util.Scanner;
class PrimeCheeck
{
int sum=0;
int get_Prime_Sum(int range)
{
for(int i=1;i<=range;i++)
{
int flag=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
flag=1;
}
if(flag==0)
{
System.out.print(i+" ");
sum=sum+i;
}

}
System.out.println();
return sum;
}
}
public class Prime {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

PrimeCheeck pr =new PrimeCheeck();
int range,result;
System.out.print("Enter your last Range : ");
range = in.nextInt();
System.out.println("Sum of all prime number giving in range = " +pr.get_Prime_Sum(range));
}
}

Reverse an Integer Number


Write a Java program to reverse an Integer number. After print the output, make the sum of all digits. Use Method. Do not use parameter. Return value from Method to the Main function.


Reference:
(1) Course Book (The Complete Reference Java j2se) ; Page 114
(2) http://c-programming-sourcecode.blogspot.com/2012/08/integer-number.html?updated-min=2012-01-01T00:00:00-08:00&updated-max=2013-01-01T00:00:00-08:00&max-results=14

Source Code:

package ReverseNumber;
import java.util.Scanner;

class Reversnum
{
int number;
int sum=0;
int reverse(){
int reverse = 0;
int remainder = 0;
do{
remainder = number%10;
reverse = reverse*10 + remainder;
number = number/10;
sum=sum+reverse;
}while(number > 0);

return reverse;
}
int getsum()
{
return sum;
}
}

public class ReverseNumber
{
public static void main(String args[]) {
Scanner input =new Scanner(System.in);
Reversnum r =new Reversnum();
int number;
System.out.println("Please enter number to be reversed using Java program: ");
r.number = input.nextInt();
int a = r.reverse();
System.out.println("After Reverse : "+a);
System.out.println("Sum of : "+r.getsum());
}
}

Calculate Simple Interest using method in java

Write a Java Program to Solve/Calculate of a Simple Interest by given condition. Use Method to solve this issue. Do not use parameter and return value from Method.

Reference:
1. http://c-programming-sourcecode.blogspot.com/2012/07/write-c-program-to-find-simple-interest.html?q=simple+interest
2. Course Book(The Complete Reference Java j2se) ; Page 112

Source Code: 
package calculate;
import java.util.Scanner;
class Interest
{
float SI,t,y,r;
void showValue()
{
System.out.println("The result is :" +((t*y*r)/100));
}
}
public class Calculate
{
public static void main(String[] args)
{
Interest i= new Interest();
Scanner inp=new Scanner(System.in);
System.out.print("Enter the value of t,y,r :");
i.t=inp.nextFloat();
i.y=inp.nextFloat();
i.r=inp.nextFloat();
i.showValue();
}
}

Java Calculator using instance variable

Design and Write  a simple java class program which can perform all basic operation of a Calculator. Define necessary instance variable but do not use method.

Reference:
1. http://c-programming-sourcecode.blogspot.com/2012/06/write-program-performing-as-calculator.html?q=simple+interest

2. Course Book(The Complete Reference Java j2se) ; Page 106

Source Code:

package calculator_shohan;
import java.util.Scanner;
import java.io.*;
public class Calculator_shohan {
public static void main(String[] args) {
calculator_ c=new calculator_();
int num1=0,num2=0;
Scanner input=new Scanner(System.in);
System.out.println("Enter the first number:");
num1=input.nextInt();
System.out.println("Enter the second number:");
num2=input.nextInt();
c.cal(num1, num2);
}

}
class calculator_
{
int add=0,sub=0,div=0,mul=0,mod=0;
public void cal(int x,int y)
{
Scanner input=new Scanner(System.in);

char ch;
System.out.println("Enter your choice:");
System.out.println("Addition for + :");
System.out.println("Subtraction for - :");
System.out.println("Multiplication for * :");
System.out.println("Divition for /:");
System.out.println("Modulation for %:");
ch=input.next().charAt(0);
switch(ch)
{
case '+':
System.out.println(x+y);
break;

case '-':
System.out.println(x-y);
break;

case '*':
System.out.println(x*y);
break;
case '/':
System.out.println(x/y);
break;
case '%':
System.out.println(x%y);
break;
default:
System.out.println("no choice");
}
}
}

Wednesday, March 4, 2015

Class with Instance and Method

Write a program where you have to assign a class called Circle is to be defined as illustrated in the class diagram below. It contains two variables: radius (of type double) and color (of type String); and three methods: getRadius(), getColor(), and getArea().
Three instances of Circles called c1, c2, and c3 shall then be constructed with their respective data members, as shown in the instance diagrams.

Source Code:

package crl;
class circle
{
double radius;
String colour;
circle(double r,String c)
{
radius=r;
colour=c;
}
double getRadius()
{
return radius;
}
String getColour()
{
return colour;
}
double getArea()
{
return 3.1416*radius*radius;
}
}
public class CRL
{
public static void main(String[] args)
{
circle c1=new circle(3.4,"Pink");
circle c2=new circle(1.1,"white");
circle c3=new circle(1.1,"blue");
System.out.println("Radius :"+c1.getRadius() + "Colour :"+c1.getColour()+"Area:"+c1.getArea());
System.out.println("Radius :"+c2.getRadius() + "Colour :"+c2.getColour()+"Area:"+c2.getArea());
System.out.println("Radius :"+c3.getRadius() + "Colour :"+c3.getColour()+"Area:"+c3.getArea());
}
}

Thursday, December 19, 2013

Constructor with Overloading in JAVA : Day 8

          a) How to Overload a Constructor? Demonstrate with example.
    
      b) How to use Object as Parameter? Give example.

Tuesday, December 17, 2013

Stack Programming using consturctor in JAVA : Day 7


 
 Write a program to see a practical application of the JAVA CLASS, let’s develop the Stack. A stack stores data using first-in, last-out ordering. That is, a stack is like a stack of plates on a table. The first plate put down on the table is the last plate to be used. Stacks are controlled through two operations traditionally called push and pop. Use the Class name for this program as Stack, Stack() as constructor, push() and pop() as method.

Keyword and Constructor in JAVA : Day 6


a) What is this Keyword?

b) Write any program using Parameter with Constructor.

Tuesday, December 10, 2013

Constructor in JAVA : Day 5



 a) What happened when a constructor initialized in an Object?


b) Write a program to find the volume of a Box. Use Constructor to initialize the dimensions of a Box. Compute and Return the result using Method.

Method with return type in JAVA : Day 4



a) How Method return a value? Write necessary Syntax.

b) Write any JAVA program that takes parameters in a Method.

Method in JAVA : Day 3

a) How to access the instance variable using Method?


b) Write a program to calculate the Volume of a Box using Method.

Sunday, December 8, 2013

Object in JAVA : Day 2


a) What is Object? How to declare an Object in a JAVA program? Explain the Syntax of Object Declaration.

b) Write Syntax to assign an Object Reference Variable.