Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Java 13

Uploaded by

pranavbadhe20
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java 13

Uploaded by

pranavbadhe20
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1. Write a program in Java to print first fifty prime numbers.

import java.util.*;
public class primeNumber
{
public static void main(String args[])
{
int j,flag=1;
Scanner in=new Scanner(System.in);
System.out.print("Enter starting and ending number to print prime number: ");
int start=in.nextInt();
int end=in.nextInt();
while(start<end)
{
for(j=2;j<start;j++)
{
if((start%j)==0)
{
flag=0;
}
}
if(flag==1)
{
System.out.print(" "+start);
}
start++;
flag=1;
}
}
}

/**************************** OUTPUT ***************************

Enter starting and ending number to print prime number: 11 20


11 13 17 19 Press any key to continue . . .

******************************************************************/
2. Write a Program in Java to print factorial of given number using recursion.

import java.util.*;
class factoNumber
{
public static void main(String args[])
{
double f,facto;
factoNumber n=new factoNumber();
System.out.println("Enter factorial number :");
Scanner in=new Scanner(System.in);
f=in.nextDouble();
facto=n.factorial(f); //facto=factorial(f);
System.out.println("Factorial of given number is : "+facto);
}
double factorial(double d) //static double factorial(double d)
{
if(d==0)
return(1);
else
return(d*factorial(d-1));
}
}

/*************************** OUTPUT ***************************


Enter factorial number :
4
Factorial of given number is : 24.0
Press any key to continue . . .

*************************************************************/
3. Write a program in Java to print Fibonacci series in given series.

import java.util.*;
public class fibonacciNumber
{
public static void main(String args[])
{
int a=0,b=1,c,i=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter up to Fibonnacci Series :");
int n=in.nextInt();
while(i<n)
{
System.out.print(" "+a);
c=a;
a=b;
b=c+b;
i++;
}
}
}

/************************ OUTPUT ******************


Enter up to Fibonnacci Series :
10
0 1 1 2 3 5 8 13 21 34

***************************************************/
4. Write a Program in Java to demonstrate Command Line Arguments.

public class CommandArgs {


public static void main(String[] args) {
int sum = 0;
for(int i=0;i<args.length;i++)
{
sum = sum + Integer.parseInt(args[i]);
}

System.out.println("The sum is : "+sum);


}
}

/**********************OUTPUT***********************
S:\GobLiN\JAVA>javac CommandArgs.java

S:\GobLiN\JAVA>java CommandArgs 10 20 30 40 50
The sum is : 150

****************************************************
5. Write a Program in Java to create student information using array.

class StudentInfo
{
String name ;
int age ;
double CGPA ;

public StudentInfo(String n , int a , double d)


{
name = n;
age = a;
CGPA = d;
}
public void print()
{
System.out.println("NAME="+name+"\tAGE="+ age+"\tCGPA="+CGPA);
}
}
public class StudentArray
{
public static void main(String[] args)
{
StudentInfo Student[] = new StudentInfo[3];
Student[0] = new StudentInfo("preet", 21, 8.99);
Student[1] = new StudentInfo("Sak", 19, 7.86);
Student[2] = new StudentInfo("Praj", 20, 9.5);

int i;
for(i=0 ;i<3;i++)
{
Student[i].print();
}

}
}
*****************************OUTPUT***************************
NAME=Preet AGE=21 CGPA=8.99
NAME=Sak AGE=19 CGPA=7.86
NAME=Praj AGE=20 CGPA=9.5
6. Write a program in Java to implement user defined package.
User Defined Package.

//save this in mypack folder

package mypack;
public class Arithmatic {

public int addition(int a,int b)


{
int sum = a+b;
return sum;
}
public int subtraction(int a,int b)
{
int sub = a-b;
return sub;
}

public int multiplication(int a,int b)


{
int mult = a*b;
return mult;
}

// save this in mypackage folder


import mypack.Arithmatic;
public class demoPack {
public static void main(String[] args) {

Arithmetic A1 = new Arithmatic();


int add = A1.addition(10,20);
int sub = A1.subtraction(30,10);
int mult = A1.multiplication(10,20);

System.out.println("The Addition is : "+add);


System.out.println("The Subtraction is : "+sub);
System.out.println("The Multipliction : "+mult);

}
}

********************************OUTPUT**************************
The Addition is : 30
The Subtraction is : 20
The Multipliction : 200
Press any key to continue . . .

*******************************************************************
7. Write a program in Java to implement default & parameterized constructor.

class AddDemo{

int a ,b,c,sum;

public AddDemo()
{
System.out.println("I am Default constructor ");
}

public AddDemo(int a , int b)


{
this.a = a;
this.b =b ;
sum = a+b;
System.out.println("I am Parameterized Constructor");
System.out.println("Addition="+sum);
}

public AddDemo(int a , int b,int c)


{
this.a = a;
this.b =b ;
this.c = c;
sum = a+b+c;
System.out.println("I am also Parameterized Constructor");
System.out.println("Addition="+sum);
}

}
public class ContructorsDemo {
public static void main(String[] args) {

AddDemo a1 = new AddDemo();


System.out.println("\n");
AddDemo a2 = new AddDemo(10, 20);
System.out.println("\n");
AddDemo a3 = new AddDemo(10,20,30);

}
}
**************************OUTPUT*****************************
I am Default constructor

I am Parameterized Constructor
Addition=30

I am also Parameterized Constructor


Addition=60
9. Write a Program in java To demonstrate Wrapper Class.

public class WrapperDemo


{
public static void main(String[] args)
{

int i=30;
float f=50.0F;
double d=60.0D;
char c='a';
boolean b2=true;

//Autoboxing: Converting primitives into objects

Integer intobj=i;
Float floatobj=f;
Double doubleobj=d;
Character charobj=c;
Boolean boolobj=b2;

//Printing objects
System.out.println("---Printing object values---");
System.out.println("Integer object: "+intobj);
System.out.println("Float object: "+floatobj);
System.out.println("Double object: "+doubleobj);
System.out.println("Character object: "+charobj);
System.out.println("Boolean object: "+boolobj);

//Unboxing: Converting Objects to Primitives

int intvalue=intobj;
float floatvalue=floatobj;
double doublevalue=doubleobj;
char charvalue=charobj;
boolean boolvalue=boolobj;

//Printing primitives
System.out.println("---Printing primitive values---");
System.out.println("int value: "+intvalue);
System.out.println("float value: "+floatvalue);
System.out.println("double value: "+doublevalue);
System.out.println("char value: "+charvalue);
System.out.println("boolean value: "+boolvalue);
}
}
***************************OUTPUT*****************************
---Printing object values---
Integer object: 30
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true
---Printing primitive values---
int value: 30
float value: 50.0
double value: 60.0
char value: a
boolean value: true
Press any key to continue . . .
*************************************************************

12. Write a Program in Java to demonstrate Inner Class.


class Outer{
int outerx =1001;
public void test(){
Inner in = new Inner();
in.display();
}
class Inner{
public void display(){
System.out.println("I am from Inner Class");
System.out.println(" Display:OuterX= \t"+outerx);
}
}
}
public class InnerDemo{
public static void main(String[] args) {
Outer O1 = new Outer();
O1.test();
}

}
*************************OUTPUT***************************
I am from Inner Class
Display:OuterX= 1001
**********************************************************

10. Write a Program to Demonstrate Class in Java.


class Emp
{
int Id;
String name;
double salary;

public Emp(int i , String n , double d)


{
name = n;
Id = i;
salary = d;
}
public void Emp_display()
{
System.out.println("Name :-" + name );
System.out.println("Id :-" +Id);
System.out.println("Salary :-"+ salary);
}
}
public class ClassDemo {
public static void main(String[] args) {
Emp e1 = new Emp(101,"Vivek", 45560.78);
Emp e2 = new Emp(102, "Rahul", 57856.76);
Emp e3 = new Emp(103, "Sohil", 27458.77);

e1.Emp_display();
e2.Emp_display();
e3.Emp_display();
}

***************************OUTPUT****************************
Name :-Vivek
Id :-101
Salary :-45560.78
Name :-Pranav
Id :-102
Salary :-57856.76
Name :-Sahil
Id :-103
Salary :-27458.77
Press any key to continue . . .

14. Write a program in java to demonstrate exception handling.


import java.util.Scanner;

public class demoException {


public static void main(String[] args) {
int num1 = 10,num2 = 0;

int arr[] = new int[5];


System.out.println("Enter 5 element for array");
Scanner sc = new Scanner(System.in);

String s=null;

try{

for(int i = 0;i<6;i++)
{
arr[i] = sc.nextInt();
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception is : "+e.getMessage());
}
try{
int ans = num1 / num2;
}
catch(ArithmeticException e)
{
System.out.println("Exception is "+e.getMessage());
}
try{
s.length();
}
catch(NullPointerException e)
{
System.out.println("NullPointerException Caught");
}
System.out.println("Last line");

}
}

/*********************************OUTPUT******************************

Enter 5 element for array


123456
Exception is : Index 5 out of bounds for length 5
Exception is / by zero
NullPointerException Caught
Last line

***********************************************************************/
15. Write a program in java to demonstrate text strean object that take input from user &
write it into text file.

import java.util.*;
import java.io.*;
public class FileWrite
{
public static void main(String args[])throws IOException
{
String s1,s2,s3,fn;
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter the file name to write : ");
fn=br.readLine();
System.out.println("Enter the text to writen in file:");
System.out.print("Enter the name : ");
s1=br.readLine();
System.out.print("Enter the roll no : ");
s2=br.readLine();
System.out.print("Enter the address : ");
s3=br.readLine();
s1=s1+s2+s3;
try
{
byte buf[]=s1.getBytes();
FileOutputStream f1=new FileOutputStream(fn);
f1.write(buf);
f1.close();
}
catch(Exception e){}
}
}
/************************************OUTPUT*************************************
Enter the file name to write : demofile.txt
Enter the text to writen in file:
Enter the name : xyz
Enter the roll no : 31
Enter the address : Muktainagar
Press any key to continue . . .
********************************************************************************/
11. Write a program in java to implement inheritance.

class person
{
String name;
int age;
int weight;

}
class student extends person
{
long StudentId;
int RollNO;

public void display()


{
System.out.println("Name "+name);
System.out.println("Age "+age);
System.out.println("Weight "+weight);
System.out.println("StudentId "+StudentId);
System.out.println("RollNo :"+RollNO);
}

public class Sample {


public static void main(String[] args) {
student s1 = new student();
s1.name = "Swastik";
s1.age = 20;
s1.weight = 50;
s1.RollNO = 04;
s1.StudentId = 2020015400141095L;

s1.display();

}
}

/********************************OUTPUT*************************************

Name Swastik
Age 20
Weight 50
StudentId 2020015400141095
RollNo :4

*****************************************************************************/
8. Write a program in java to demonstrate various operations on String Functions.

import java.util.Scanner;
public class demoString {
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a 1st Sample String :");


String s1 = sc.nextLine();

System.out.println("Enter a 2nd Sample String :");


String s2 = sc.nextLine();

//1.Concatination
String s3 = s1.concat(s2);
System.out.println("Concatination of two string : "+s3);

//2.substring
s3 = s1.substring(0,3);
System.out.println("Substring of string :"+s3);

//3.Length
int len = s2.length();
System.out.println("Length of string : "+len);

//4.Upper Case
s3 = s1.toUpperCase();
System.out.println("Upper case of String is: "+s3);

//5.Lower Case
s3 = s1.toLowerCase();
System.out.println("Lower Case of String is : "+s3);

/************************************OUTPUT*********************************
Enter a 1st Sample String :
welcome to learn
Enter a 2nd Sample String :
java programming
Concatination of two string : welcome to learnjava programming
Substring of string :wel
Length of string : 16
Upper case of String is: WELCOME TO LEARN
Lower Case of String is : welcome to learn
*****************************************************************************/
13. Write a program in java to demonstrate reflection.

import java.lang.Class;
import java.lang.reflect.*;
class Dog
{
public void display()
{
System.out.println("I am a Dog:");
}

}
class Reflection
{
public static void main(String args[])
{
try
{
Dog d1=new Dog();
class obj=d1.getClass();
String nam3=obj.getName();
System.out.println("Name:"+name);
class superClass=obj.getSuperclass();
System.out.println("Superclass:"+superClass.getName());
{
catch(Exception e)
}
e.printStackTerace();
}
}

/********************************OUTPUT*************************************

Name:Dog
Superclass:java.lang.Object

*****************************************************************************/

You might also like