Nothing Special   »   [go: up one dir, main page]

0% found this document useful (0 votes)
21 views32 pages

Java Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 32

S.

No INDEX PAGE
.
1. WAP that implements the
Concept of Encapsulation
2. WAP to demonstrate concept
of function overloading of
Polymorphism.
3. WAP to demonstrate concept
of construction overloading of
Polymorphism.
4. WAP the use boolean data
type and print the Prime
number Series up to 50.
5. WAP to print first 10 number
of the following Series using
Do-While Loops 0, 1, 1, 2, 3, 5,
8, 11...
6. WAP to check the given
number is Armstrong or not.
7. WAP to find the factorial of
any given number.
8. WAP to sort the element of
One Dimensional Array in
Ascending order.
9. WAP for matrix multiplication
using input/output Stream.
10. WAP for matrix addition using
input/output stream class.
11. WAP for matrix transposes
using input/output stream
class.
12. WAP to add the elements of
Vector as arguments of main
method (Run time) and
rearrange them, and copy it
into an Array.
13. WAP to check that the given
String is palindrome or not.
14. WAP to arrange the String in
alphabetical order.
15. WAP for StringBuffer class
which perform the all methods
of that class.
16. WAP to calculate Simple
Interest using the Wrapper
Class.
17. WAP to calculate Area of
various geometrical figures
using the abstract class.
18. WAP where Single class
implements more than one
interfaces and with help of
interface reference variable
user call the methods.
19. WAP that use the multiple
catch statements within the
try-catch mechanism.
20. WAP where user will create a
self-Exception using the
"throw" keyword.
21. WAP for multithread using the
isAlive(), join() and
synchronized() methods of
Thread class.
22. WAP to create a package using
command and one package
will import another package.
23. WAP for JDBC to insert the
values into the existing table
by using prepared Statement.
24. WAP for JDBC to display the
records from the existing
table.
25. WAP for demonstration of
switch statement, continue
and break.
PROGRAMS
Program 1: WAP that implements the Concept of Encapsulation.
import java.util.Scanner;
class Greetings{
Scanner sc = new Scanner(System.in);
void greet(){
System.out.print("Enter Your Name : ");
String name = sc.nextLine();
System.out.println(" Hello, " + name+ " Have A Nice Day! ");
sc.close();
}
}
public class EncapsulationClassProgram1 {
public static void main(String[] args) {
Greetings gs = new Greetings();
gs.greet();
}
}

OUTPUT:
E:\JavaAssignment>javac EncapsulationClassProgram1.java
E:\JavaAssignment>java EncapsulationClassProgram1
Enter Your Name : Animesh Singh
Hello, Animesh Singh Have A Nice Day!
E:\JavaAssignment>

Program 2: WAP to demonstrate concept of function overloading of


Polymorphism.

class Program{
int add(int a,int b){
return a+b;
}
int add(int a,int b, int c){
return a+b+c;
}
int add(int a,int b,int c,int d){
return a+b+c+d;
}
}

public class OverloadingMethodsProgram2{


public static void main(String[] args) {
int a=10,b=20,c=30,d=40;
Program oM = new Program();
System.out.println("Function Overloading of Polymorphism\n");
System.out.println(" Sum Of Two Digits : " + oM.add(a,b));
System.out.println(" Sum Of Three Digits : " + oM.add(a,b,c));
System.out.println(" Sum Of Four Digits : " + oM.add(a,b,c,d));
}
}

OUTPUT:
E:\JavaAssignment>javac OverloadingMethodsProgram2.java

E:\JavaAssignment>java OverloadingMethodsProgram2

Function Overloading of Polymorphism

Sum Of Two Digits : 30

Sum Of Three Digits : 60

Sum Of Four Digits : 100

E:\JavaAssignment>
Program 3: WAP to demonstrate concept of construction overloading of
Polymorphism.
class Program{

int id;

String name;

Program(){

System.out.println(" This is Default Constructor");

Program(int i,String n)

id = i;

name = n;

public class ConstructionOverloadingProgram3 {

public static void main(String[] args) {

System.out.println("\t\t Construction Overloading of Polymorphism\n");

Program s = new Program();

System.out.println(" Student Id : " + s.id + " \n Student Name : "+ s.name);

System.out.println();

System.out.println(" This is Parameterized Constructor");

Program student = new Program(101, "Manish");

System.out.println(" Student Id : " + student.id + " \n Student Name : "+


student.name);

}
OUTPUT:

E:\JavaAssignment>javac ConstructionOverloadingProgram3.java

E:\JavaAssignment>java ConstructionOverloadingProgram3

Construction Overloading of Polymorphism

This is Default Constructor

Student Id : 0

Student Name : null

This is Parameterized Constructor

Student Id : 101

Student Name : Manish

E:\JavaAssignment>

Program 4: WAP the use boolean data type and print the Prime number Series
up to 50.
public class PrimeSeriesProgram4{

static boolean isPrime(int num){

if(num == 0 || num ==1) return false;

for(int i = 2; i <num ; i++){

if(num % i == 0) return false;

return true;
}

public static void main(String[] args) {

System.out.println("\t\t Prime Number Series Upto 50 ");

int N = 50;

for(int i = 1 ; i < N ; i++){

if(isPrime(i))

System.out.print(" " + i);

OUTPUT:

E:\JavaAssignment>javac PrimeSeriesProgram4.java

E:\JavaAssignment>java PrimeSeriesProgram4

Prime Number Series Upto 50

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

E:\JavaAssignment>
Program 5: WAP to print first 10 number of the following Series using Do-While
Loops 0, 1, 1, 2, 3, 5, 8, 11...

public class FibonacciSeriesProgram5 {

public static void main(String[] args) {

System.out.println("\t\t Fibonacci Series Upto 10 Terms \n");

int x = 0,y=1,z;

System.out.print(" "+x+ " " +y);

int term = 10;

int i = 2;

do{

z = x +y;

System.out.print(" "+z);

x = y;

y = z;

i++;

}while(i < term);

OUTPUT:
E:\JavaAssignment>javac FibonacciSeriesProgram5.java

E:\JavaAssignment>java FibonacciSeriesProgram5

Fibonacci Series Upto 10 Terms

0 1 1 2 3 5 8 13 21 34
Program 6: WAP to check the given number is Armstrong or not.

import java.util.Scanner;
public class ArmstrongProgram6{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(" Enter a Number To Check The Number is Armstrong Or Not :
");
int n = sc.nextInt();
int term = n;
int r, sum=0;
while(n!=0){
r = n%10;
sum = sum + (r*r*r);
n = n/10;
}
if(term == sum){
System.out.println(" \n\nNumber Is Armstrong");
}
else{System.out.println(" Number Is Not Armstrong");}
sc.close();
}
}
OUTPUT:

E:\JavaAssignment>javac ArmstrongProgram6.java

E:\JavaAssignment>java ArmstrongProgram6

Enter a Number To Check The Number is Armstrong Or Not : 371

Number Is Armstrong

E:\JavaAssignment>java ArmstrongProgram6

Enter a Number To Check The Number is Armstrong Or Not : 567

Number Is Not Armstrong

E:\JavaAssignment>

Program 7: WAP to find the factorial of any given number.

import java.util.Scanner;
public class FactorialProgram7{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter A Number To Find Factorial : ");
int num = sc.nextInt();
int factorial=1;
for(int i = 1;i<=num;i++)
{
factorial *= i;
}
System.out.println("Factorial ("+num +") is : "+ factorial);
sc.close();
}
}

OUTPUT:
E:\JavaAssignment>javac FactorialProgram7.java

E:\JavaAssignment>java FactorialProgram7

Enter A Number To Find Factorial : 6

Factorial (6) is : 720

E:\JavaAssignment>

Program 8: WAP to sort the element of One Dimensional Array in Ascending


order.

class Sorting{
void printArray(int[] arr) {
for (int i : arr) {
System.out.print(" " + i);
}
}
void bubbleSort(int[] arr,int n) {
boolean isSorted = false;
for (int i = 0; i < n-1; i++) {
isSorted = true;
System.out.println("\n Working of pass number " + (i + 1));
for (int j = 0; j < n-1-i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSorted = false;
}
}
if (isSorted) {
break;
}
}
}
}
public class SortingArrayProgram8{
public static void main(String[] args) {
Sorting s = new Sorting();
int arr[] = {10,8,2,5,1,25};
int n = arr.length;
System.out.print("\t\t Sort The Element Using BubbleSort \n\n");
System.out.print(" Before Sorting an Array : ");
s.printArray(arr);
s.bubbleSort(arr,n);
System.out.print(" Before Sorting an Array : ");
s.printArray(arr);
}
}

OUTPUT:
E:\JavaAssignment>javac SortingArrayProgram8.java

E:\JavaAssignment>java SortingArrayProgram8

Sort The Element Using BubbleSort

Before Sorting an Array : 10 8 2 5 1 25

Working of pass number 1

Working of pass number 2

Working of pass number 3

Working of pass number 4

Working of pass number 5

Before Sorting an Array : 1 2 5 8 10 25

E:\JavaAssignment>
Program 9: WAP for matrix multiplication using input/output Stream.

import java.util.*;

public class MatricesProgram9{

static void multiplicationArray(int[][] arr1,int[][] arr2){

int arr3[][] = new int[3][3];

for (int i = 0; i < arr3.length; i++) {

for (int j = 0; j < arr3.length; j++) {

arr3[i][j] = 0;

for (int k = 0; k < arr3.length; k++) {

arr3[i][j] += arr1[i][k] * arr2[k][j];

System.out.print(" " + arr3[i][j]);

System.out.println();

static void printArray(int[][] arr) {

System.out.println(" \nHere is Matrix \n ");

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr.length; j++) {

System.out.print(" " + arr[i][j]);

System.out.println();

}
static void inputArray(int[][] arr) {

Scanner sc = new Scanner(System.in);

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr.length; j++) {

arr[i][j] = sc.nextInt();

public static void main(String[] args) {

int arr1[][] = new int[3][3];

int arr2[][] = new int[3][3];

// int arr3[][] = new int[3][3];

System.out.println("Enter Values of Matrix -1");

inputArray(arr1);

printArray(arr1);

System.out.println("Enter Values of Matrix -2");

inputArray(arr2);

printArray(arr2);

System.out.println("Multiplication of Two Matrices");

multiplicationArray(arr1,arr2);

}
OUTPUT:

E:\JavaAssignment>javac MatricesProgram9.java

E:\JavaAssignment>java MatricesProgram9

Enter Values of Matrix -1

123456789

Here is Matrix

123

456

789

Enter Values of Matrix -2

123456789

Here is Matrix

123

456

789

Multiplication of Two Matrices

30 36 42

66 81 96

102 126 150

E:\JavaAssignment>
Program 10: WAP for matrix addition using input/output stream class.

import java.util.*;

public class AdditionArrayProgram10{

static void additionArray(int[][] arr1,int[][] arr2){

for (int i = 0; i < arr1.length; i++) {

for (int j = 0; j < arr1.length; j++) {

System.out.print(" " + (arr1[i][j] + arr2[i][j]));

System.out.println();

static void printArray(int[][] arr) {

System.out.println(" \nHere is Matrix \n ");

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr.length; j++) {

System.out.print(" " + arr[i][j]);

System.out.println();

static void inputArray(int[][] arr) {

Scanner sc = new Scanner(System.in);

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr.length; j++) {


arr[i][j] = sc.nextInt();

public static void main(String[] args) {

int arr1[][] = new int[3][3];

int arr2[][] = new int[3][3];

System.out.println("Enter Values of Matrix -1");

inputArray(arr1);

printArray(arr1);

System.out.println("Enter Values of Matrix -2");

inputArray(arr2);

printArray(arr2);

System.out.println("Addition of Two Matrices");

additionArray(arr1,arr2);

OUTPUT:

E:\JavaAssignment>javac AdditionArrayProgram10.java

E:\JavaAssignment>java AdditionArrayProgram10

Enter Values of Matrix -1

123456789
Here is Matrix

123

456

789

Enter Values of Matrix -2

123456789

Here is Matrix

123

456

789

Addition of Two Matrices

246

8 10 12

14 16 18

E:\JavaAssignment>

Program 11: WAP for matrix transposes using input/output stream class.

import java.util.*;
public class TransposeMatrixProgram11 {
static void printArray(int[][] arr) {
System.out.println(" \nHere is Matrix \n ");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(" " + arr[i][j]);
}
System.out.println();
}
}
static void inputArray(int[][] arr) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
arr[i][j] = sc.nextInt();
}
}
sc.close();
}
static void transposeArray(int[][] arr) {
System.out.println(" \nHere is Matrix \n ");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(" " + arr[j][i]);
}
System.out.println();
}
}
public static void main(String[] args) {
int arr[][] = new int[3][3];
System.out.println("Enter Values of Matrix \n");
inputArray(arr);
printArray(arr);
System.out.println("Transpose of Matrix : \n");
transposeArray(arr);
}
}

OUTPUT:

E:\JavaAssignment>javac TransposeMatrixProgram11.java

E:\JavaAssignment>java TransposeMatrixProgram11

Enter Values of Matrix

123456789

Here is Matrix

123

456

789

Transpose of Matrix :

Here is Matrix

147

258

369

E:\JavaAssignment>
Program 12: WAP to add the elements of Vector as arguments of main method
(Run time) and rearrange them, and copy it into an Array.

import java.util.*;
class VectorAddElementsProgram12{
public static void main(String args[]){
Vector<String> vectorAddElem = new Vector<String>();
String[] vectorArray = new String[5];
vectorAddElem.add(args[0]);
vectorAddElem.add(args[1]);
vectorAddElem.add(args[2]);
vectorAddElem.add(args[3]);
vectorAddElem.add(args[4]);
System.out.println("\t\t Program To Add Element In Vector And Copy To Array \
n");
System.out.println("Vector Elements Without Rearrange\n");
for(String elem : vectorAddElem){
System.out.println(elem);
}
System.out.println("Vector Elements With Ascending Order \n");
Collections.sort(vectorAddElem);
for(int i = 0;i < vectorAddElem.size(); i++){
//Copy Vector Elements To Array
vectorArray[i] = vectorAddElem.get(i);
}
for(String elem : vectorArray){
System.out.println(elem);
}
}
}

OUTPUT:

E:\JavaAssignment>javac VectorAddElementsProgram12.java

E:\JavaAssignment>java VectorAddElementsProgram12 Java C++ Python MATLAB DSA

Program To Add Element In Vector And Copy To Array

Vector Elements Without Rearrange

Java

C++

Python

MATLAB

DSA

Vector Elements With Ascending Order

C++

DSA

Java

MATLAB

Python

E:\JavaAssignment>
Program 13: WAP to check that the given String is palindrome or not.
import java.util.*;
class StringPalindromeProgram13{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = new String();
String reverseStr = new String();
System.out.println("\t\t Check String Palindrome Or Not \n");
System.out.print("Input String To Check : ");
str = sc.nextLine();
for(int i = str.length()-1; i>=0; i--){
reverseStr = reverseStr+str.charAt(i);
}
if(str.equals(reverseStr)){
System.out.println("\t String Is Palindrome ");
}
else{
System.out.println("\t String Is Not Palindrome ");
}
sc.close();
}

OUTPUT:
E:\JavaAssignment>javac StringPalindromeProgram13.java
E:\JavaAssignment>java StringPalindromeProgram13

Check String Palindrome Or Not

Input String To Check : 121

String Is Palindrome

E:\JavaAssignment>java StringPalindromeProgram13

Check String Palindrome Or Not

Input String To Check : 234

String Is Not Palindrome

E:\JavaAssignment>

Program 14: WAP to arrange the String in alphabetical order.


import java.util.*;

class CharToStrProgram14 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String str = new String();

System.out.print("Enter String To Arrange Alphabetical Order : ");

str = sc.nextLine();

char[] ch = new char[str.length()];

for(int i = 0 ; i < ch.length ; i++){

ch[i] = str.charAt(i);

Arrays.sort(ch);//Using Arrays.sort() method

//it is a method from Collections Framework

for(int k = 0 ; k < ch.length; k++){


System.out.println(" "+ch[k]);
}

sc.close();

OUTPUT:

E:\JavaAssignment>javac CharToStrProgram14.java

E:\JavaAssignment>java CharToStrProgram14

Enter String To Arrange Alphabetical Order : java

aajv

E:\JavaAssignment>

Program 15 : WAP for StringBuffer class which perform the all methods of that
class
public class StringBufferMethodsProgram15{

public static void main(String[] args) {

StringBuffer sb = new StringBuffer("Hello");

System.out.println("\t\t StringBuffer All Methods\n");

System.out.println(" Given String : Hello");

System.out.println(" String Buffer Capacity : "+sb.capacity());

System.out.println(" Append Method : "+ sb.append(" Java"));

System.out.println(" Insert Method : "+ sb.insert(10, " Programming "));

System.out.println(" Replace Method : "+sb.replace(0,6, "Good Morning "));

System.out.println(" Delete Method : "+sb.delete(5,13));

System.out.println(" Reverse Method : "+sb.reverse());


System.out.println(" String Buffer Capacity : "+sb.capacity());

OUTPUT:
E:\JavaAssignment>javac StringBufferMethodsProgram15.java

E:\JavaAssignment>java StringBufferMethodsProgram15

StringBuffer All Methods

Given String : Hello

String Buffer Capacity : 21

Append Method : Hello Java

Insert Method : Hello Java Programming

Replace Method : Good Morning Java Programming

Delete Method : Good Java Programming

Reverse Method : gnimmargorP avaJ dooG

String Buffer Capacity : 44

E:\JavaAssignment>

Program 16: WAP to calculate Simple Interest using the Wrapper Class.
import java.util.Scanner;
public class SI_UsingWrapperClassProgram16{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("\t\t Simple Interest Using Wrapper Class \n");
System.out.print(" Enter The Principle Amount : ");
int p = sc.nextInt();
System.out.print(" Enter The Rate Of Interest : ");
int r = sc.nextInt();
System.out.print(" Enter The Tenure : ");
int t = sc.nextInt();
//Autoboxing process
Integer objP = p;
Integer objR = r;
Integer objT = t;
Integer objSI;
objSI = (objP * objR * objT)/100;
System.out.println(" Simple Interest : " + objSI);
}
}

OUTPUT:

E:\JavaAssignment>javac SI_UsingWrapperClassProgram16.java

E:\JavaAssignment>java SI_UsingWrapperClassProgram16
Simple Interest Using Wrapper Class
Enter The Principle Amount : 5000
Enter The Rate Of Interest : 7
Enter The Tenure : 2
Simple Interest : 700

E:\JavaAssignment>

You might also like