Showing posts with label parameter in method. Show all posts
Showing posts with label parameter in method. Show all posts

Sunday, March 22, 2015

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

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

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.