♠ Posted by
Java Tutorials
at
12:31 AM
Page 1|2|3|4
11.
Using two classes in Java program
class Computer {
Computer() {
System.out.println("Constructor
of Computer class.");
}
void computer_method() {
System.out.println("Power
gone! Shut down your PC soon...");
}
public static void main(String[] args) {
Computer my = new Computer();
Laptop your = new Laptop();
my.computer_method();
your.laptop_method();
}
}
class Laptop {
Laptop() {
System.out.println("Constructor
of Laptop class.");
}
void laptop_method() {
System.out.println("99%
Battery available.");
}
}
Output of program:
You can also create objects
in method of Laptop class. When you compile above code two .class files will be
created which are Computer.class and Laptop.class, this has the advantage that
you can reuse your .class file somewhere in other projects without compiling
the code again. In short number of .class files created will be equal to number
of classes in code. You can create as many classes as you want but writing many
classes in a single file is not recommended as it makes code difficult to read
rather you can create single file for every class. You can also group classes
in packages for easily managing your code.
12.
Java program to swap two numbers
This java program swaps two
numbers using a temporary variable. To swap numbers without using extra
variable see another code below.
Swapping using temporary or third variable
import java.util.Scanner;
class SwapNumbers
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter x
and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before
Swapping\nx = "+x+"\ny = "+y);
temp = x;
x = y;
y = temp;
System.out.println("After
Swapping\nx = "+x+"\ny = "+y);
}
}
Output of program:
Swapping without temporary variable
import java.util.Scanner;
class SwapNumbers
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x
and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before
Swapping\nx = "+x+"\ny = "+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After
Swapping\nx = "+x+"\ny = "+y);
}
}
13.
Java program to find largest of three numbers
This java program finds
largest of three numbers and then prints it. If the entered numbers are unequal
then "numbers are not distinct" is printed.
import java.util.Scanner;
class LargestOfThreeNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter
three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if ( x > y && x > z )
System.out.println("First
number is largest.");
else if ( y > x && y > z )
System.out.println("Second
number is largest.");
else if ( z > x && z > y )
System.out.println("Third
number is largest.");
else
System.out.println("Entered
numbers are not distinct.");
}
}
Output of program:
If you want to find out
largest of a list of numbers say 10 integers then using above approach is not
easy, instead you can use array data structure.
14.
Java program to find factorial
This java program finds
factorial of a number. Entered number is checked first if its negative then an
error message is printed.
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;
System.out.println("Enter an
integer to calculate it's factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if ( n < 0 )
System.out.println("Number
should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial
of "+n+" is = "+fact);
}
}
}
Output of program:
You can also find factorial
using recursion, in the code fact is an integer variable so only factorial of
small numbers will be correctly displayed or which fits in 4 bytes. For large
numbers you can use long data type.
15.
Java program print prime numbers
This java program prints
prime numbers, number of prime numbers required is asked from the user.
Remember that smallest prime number is 2.
import java.util.*;
class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;
Scanner in = new Scanner(System.in);
System.out.println("Enter the
number of prime numbers you want");
n = in.nextInt();
if (n >= 1)
{
System.out.println("First
"+n+" prime
numbers are :-");
System.out.println(2);
}
for ( int count = 2 ; count <=n ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
count++;
}
status = 1;
num++;
}
}
}
Output of program:
We have used sqrt method of
Math package which find square root of a number. To check if an integer(say n)
is prime you can check if it is divisible by any integer from 2 to (n-1) or
check from 2 to sqrt(n), first one is less efficient and will take more time.
16.
Java program to check armstrong number
This java program checks if
a number is armstrong or not.
import java.util.*;
class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, r;
Scanner in = new Scanner(System.in);
System.out.println("Enter a
number to check if it is an armstrong number");
n = in.nextInt();
temp = n;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( n == sum )
System.out.println("Entered
number is an armstrong number.");
else
System.out.println("Entered
number is not an armstrong number.");
}
}
Output of program:
Using one more loop in the
above code you can generate armstrong numbers from 1 to n(say) or between two
integers (a to b).
17.
Java program to print Floyd's triangle
This java program prints
Floyd's triangle.
import java.util.Scanner;
class FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Enter the
number of rows of floyd's triangle you want");
n = in.nextInt();
System.out.println("Floyd's
triangle :-");
for ( c = 1 ; c <= n ; c++ )
{
for ( d = 1 ; d <= c ; d++ )
{
System.out.print(num+" ");
num++;
}
System.out.println();
}
}
}
Output of program:
In Floyd triangle there are
n integers in the nth row and a total of (n(n+1))/2 integers in n rows. This is
a simple pattern to print but helpful in learning how to create other patterns.
Key to develop pattern is using nested loops appropriately.
18.
Java program to reverse a string
This java program reverses
a string entered by the user. We use charAt method to extract characters from
the string and append them in reverse order to reverse the entered string.
import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a
string to reverse");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of
entered string is: "+reverse);
}
}
Output of program:
0 comments :
Post a Comment