20251A3639 Sai Trisha

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

20251A3639 Sai Trisha

20251A3639 Sai Trisha

Week-1

1.write a java prgram to print the 10 terms of a Fibonacci series.


Code:

import java.io.*;
public class Fibonacci{
public static void main(String[]args){
int n1=0,n2=1,n3,i;

System.out.println(n1);
System.out.println(n2);
for(i=1;i<=8;i++)
{
n3=n1+n2;

System.out.println(n3);
n1=n2;
n2=n3;
}

}
}

Output:
0
20251A3639 Sai Trisha

1
1
2

3
5
8
13

21
34

2.write a java program to check whether a given number is prime or not.


Code:
import java.io.*;
public class IsPrime{
public static void main(String []args){
int p=36,f=2,i;
for(i=2;i<p;i++)
{
if(p%i==0)
{
f+=1;
}
}
if(f==2)
20251A3639 Sai Trisha

{
System.out.println(p+ "is not a prime number");
}
else
{
System.out.println(p+ "is not a prime number");
}
}
}

Output:
36 is not a prime number.

5 is a prime number.
20251A3639 Sai Trisha

3.write a java program that prints all real solutions to a quadratic equation
ax^2+bx+c=0.read in a,b,c and use quadratic formula.If the discriminant b^2-4ac
is negative, display message stating that there are no real solutions.
Code:
import java.io.*;
public class QuadRoots{
public static void main(String[]args){

int a=1,b=-5,c=6;
double r1,r2,d;
d=(b*b)-4*a*c;
if(d<0)

{
System.out.println("roots of a given equation are complex");
}
else

{
r1=(-b+Math.sqrt(d))/2*a;
r2=(-b-Math.sqrt(d))/2*a;
System.out.println("roots of the given quation are:");
System.out.println(r1+ " ");

System.out.println(r2+ " ");


}
20251A3639 Sai Trisha

}
}

Output:
roots of the given quation are:
3.0
2.0

4.write a java program to check whether a number is Armstrong or not.


Code:
import java.io.*;

public class armstrong{


public static void main(String []args){
int n=153;
int a,d;

int r=0;
a=n;
while(n>0)
{
d=n%10;

r=(d*d*d)+r;
n=n/10;
}
if(a==r)

System.out.println(a+ "is an Armstrong number");


else
System.out.println(a+" is not an armstrong number");
20251A3639 Sai Trisha

}
}

Output:
153is an Armstrong number.
375 is not an Armstrong number.

5.write a java program to print the reverse of a given number.


Code:
import java.io.*;

public class Reverse{


public static void main(String []args){
int n=234,a,d,r=0;
a=n;

while(n>0)
{
d=n%10;
r=(10*r)+d;
n=n/10;

}
System.out.println("reverse of" +a+ "is"+r);
}
}

Output:
20251A3639 Sai Trisha

Reverse of 234 is 432.


Reverse of 567 is 765.

6.write a java program for linear search.


Code:
import java.io.*;
public class LinearSearch{
public static int search(int arr[],int x)
{

int n=arr.length;
for(int i=0;i<n;i++)
{
if(arr[i]==x)

return i;
}
return -1;
}
public static void main(String args[]){

int arr[]={2,3,4,6,10,40};
int x=10;
int res=search(arr,x);
if(res==-1)

System.out.println("element"+x+"is present");
else
System.out.println("element is present at index"+res);
20251A3639 Sai Trisha

}
}

Output: element is present at index4.

7.write a program for bubble sort.


Code:
import java.io.*;
public class BubbleSort{

public static void main(String[]args){


int num=5,i,j,temp;
int arr[]={67,92,56,89,32};
for(i=0;i<(num-1);i++)

{
for(j=0;j<num-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;

}
}
}
System.out.println("Sorted list is:");
20251A3639 Sai Trisha

for(i=0;i<num;i++)
System.out.println(arr[i]);
}

Output:
Sorted list is:

32
56
67
89

92

8.write a java program for finding the min and max of an array.
Code:
import java.io.*;
public class MinAndMax{
public static int maxm(int[]arr){
int max=arr[0];
for(int i=0;i<arr.length;i++){
if(max<arr[i]){
max=arr[i];
}

}
return max;
}
20251A3639 Sai Trisha

public static int minm(int[]arr){


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

if(min>arr[i]){
min=arr[i];
}
}

return min;
}

public static void main(String[]args){

int min,max;
int[]array={24,89,92,78,56};
int n=array.length;
max=maxm(array);
min=minm(array);

System.out.println("maximum value in the array is" +max);


System.out.println("minimum value in the array is" +min);
}
}

Output:
maximum value in the array is92
minimum value in the array is24.

9.write a java program for creating a 3d array.


20251A3639 Sai Trisha

Code:
import java.io.*;
public class ThreeDarray
{
public static void main(String[] args)
{
int a[][][]={{{1,5,6},{80,5,6}},{{2,41,62},{1,52,7}}};
for(int i[][]:a)
{
for(int j[]:i)
{
for(int k:j)
{
System.out.print(k+" ");
}
System.out.println();
}
System.out.println();
}
}
}

Output:
1 5 6
80 5 6
2 41 62
1 52 7
20251A3639 Sai Trisha

10.write a java program to demonstrate constructor overloading.


Code:
import java.io.*;
class Subjects{

static int count=0;


private int sid;
private String subname;
Subjects()
{

this.sid=1;
this.subname="Mathematics";
count++;
}

Subjects(String s,int subid)


{
this.sid=subid;
this.subname=s;
count++;
}
Subjects(int r)
{

this.sid=r;
subname="english";
count++;
}
20251A3639 Sai Trisha

Subjects(Subjects obj)
{
this.sid=obj.sid;

this.subname=obj.subname;
count++;
}
public void printSub()

{
System.out.println("Subject id:"+sid);
System.out.println("subject name is"+subname);
}

}
public class TestSubjects{
public static void main(String[]args){
Subjects s1=new Subjects();
Subjects s2=new Subjects(12);

Subjects s3=new Subjects("evs",12);


Subjects s4=new Subjects(s1);
s1.printSub();
s2.printSub();
s3.printSub();
s4.printSub();
System.out.println("the total no of instances of subjects are:" +Subjects.count);
}

}
20251A3639 Sai Trisha

Output:
Subject id:1
subject name isMathematics
Subject id:12
subject name isenglish

Subject id:12
subject name isevs
Subject id:1
subject name isMathematics

the total no of instances of subjects are:4

11.write a java program to demonstrate the use of static variables,methods and


blocks.
Code:
import java.io.*;

public class Area{


static int l=25,b=20,h=32;
static{
int area=l*b;
System.out.println("The area of the rectangle is: "+area);
}
public static void volume(){
int volume=l*b*h;

System.out.println("The volume of the cuboid is: "+volume);


}

public static void main(String []args){


20251A3639 Sai Trisha

Area.volume();
}
}

Output:
The area of the rectangle is: 500
The volume of the cuboid is: 16000.

12.Write a java program to demonstrate Anonymous Inner Classes.


Code:
import java.io.*;
class Anony{

void show(){
System.out.println("in show method of outer/super class");
}
}

class TestAnony{
public static void main(String args[]){
Anony an=new Anony(){
void show(){
super.show();

System.out.println("in inner class");


}
};
an.show();
}
20251A3639 Sai Trisha

Output:
in show method of outer/super class
in inner class.

13.write a java program to demonstrate inner classes.


Code:
import java.io.*;

class Outer
{
class Inner
{

public void display()


{
System.out.println("In a nested class method");
class InnerLocal

{
public void InnerMethod()
{
System.out.println("In the Inner local class");
}

}
InnerLocal in=new InnerLocal();
in.InnerMethod();
}
}
20251A3639 Sai Trisha

public static void main(String []args)


{
Outer.Inner obj=new Outer().new Inner();

obj.display();
}
}

Output:
In a nested class method

In the Inner local class

14.write a program to demonstrate string methods.


Code:
import java.util.*;
import java.io.*;
class StrMeths

public static void main(String args[]){


double d=10.02;
String s1="GNITS";

String s2=" it department";


System.out.println("the concatenation of two strings is "+s1.concat(s2));
System.out.println("the character at 7th place of string 2 is "+s2.charAt(7));
System.out.println("the comparison of two strings is "+s1.compareTo(s2));
System.out.println( s1.equalsIgnoreCase(s2));
20251A3639 Sai Trisha

System.out.println("index of E in string 2 is "+s2.indexOf('e',3));


System.out.println("index of E in string 2 from last is "+s2.lastIndexOf('e',16));
System.out.println("length of string 2 is "+s2.length());

System.out.println("after replacing e with k in string 1, string 1 = "+s1.replace('E','K'));


System.out.println("substring of string 2 = "+s2.substring(4));
System.out.println("substring of string 2 from 4 to 9"+s2.substring(4,9));
System.out.println("lower case of string1 is ="+s1.toLowerCase());

System.out.println("UPPER case of string 2 = "+s2.toUpperCase());


System.out.println("after trimming, string 2 ="+s2.trim());
System.out.println(" to check if the string s2 starts with g"+s2.startsWith("i"));
System.out.println("to check if the string is empty or not"+s2.isEmpty());

System.out.println("to check if the string ends w t"+s2.endsWith("t"));


System.out.println("to get the value "+s2.valueOf(d));
System.out.println("contains a string"+s2.contains("it"));
}
}

Output:
the concatenation of two strings is GNITS it department
the character at 7th place of string 2 is a

the comparison of two strings is 39


false
index of E in string 2 is 5
index of E in string 2 from last is 11

length of string 2 is 14
after replacing e with k in string 1, string 1 = GNITS
substring of string 2 = department
20251A3639 Sai Trisha

substring of string 2 from 4 to 9depar


lower case of string1 is =gnits
UPPER case of string 2 = IT DEPARTMENT

after trimming, string 2 =it department


to check if the string s2 starts with gfalse
to check if the string is empty or notfalse
to check if the string ends w ttrue

to get the value 10.02


contains a string:true

15.write a java program to demopnstrate multilevel inheritance.


Code:
import java.io.*;
class Person{
public String name;

public String add;


Person(String n, String a){
name=n;
add=a;
}

void display(){
System.out.println("Name : "+name);
System.out.println("Address : "+add);
}

}
class Student extends Person{
public int rno;
20251A3639 Sai Trisha

Student(String n, String a, int r){


super(n,a);
rno=r;

}
void display(){
super.display();
System.out.println("roll no. : "+rno);

}
}
class Marks extends Student{
double tm,p;

Marks(String n, String a, int r, double m){


super(n,a,r);
tm=m;
}
void display(){

super.display();
p=((tm/500)*100);
System.out.println("Total marks out of 500: "+tm);
System.out.println("Percentage : "+p+"%");
}
}
class MultiLevelInheritance{
public static void main(String args[]){

Marks ob = new Marks("Trisha","VNR",3639,486.0);


ob.display();
}
20251A3639 Sai Trisha

Output:
Name :Trisha
Address :VNR

roll no. : 3639


Total marks out of 500: 486.0
Percentage : 97.02%

16.Write a java program to demonstrate method Overriding.


Code:
import java.io.*;
class Person{

private String name;


private int age;
Person(String nm,int ag){
name=nm;

age=ag;
}
public void show(){
System.out.println("Person's name is="+name+"Person's age is="+age);
}

}
class Employee extends Person{
private double salary;
Employee(String nm,int ag,double sal){
super(nm,ag);
20251A3639 Sai Trisha

salary=sal;
}
public void show(){

super.show();
System.out.println("Employee's salary is="+salary);
}
}

class MethOverride{
public static void main(String args[]){
Person p1=new Employee("Trisha",19,5000);
p1.show();

}
}

Output:
Person's name is=Trisha
Person's age is=19
Employee's salary is=5000.0

17.Write a java program to demonstrate Dynamic Method Dispatch


Code:
import java.io.*;

class Shape{
double l,h;
Shape(){
l=h=0;
20251A3639 Sai Trisha

}
Shape(double len,double ht){
l=len;

h=ht;
}
void area(){
System.out.println("This is super class method");

}
}
class Rectangle extends Shape{
double l,h;

Rectangle(){
l=h=0;
}
Rectangle(double len,double ht){
l=len;

h=ht;
}
void area(){
System.out.println("area of rectangle is: "+(l*h));
}
}
class Triangle extends Shape{
double l,h;

Triangle(double len,double ht){


l=len;
h=ht;
20251A3639 Sai Trisha

}
void area(){
System.out.println("area of triangle is: "+(l*h*0.5));

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

Rectangle r=new Rectangle(30,45);


Triangle t=new Triangle(15,6);
Shape s;
s=r;

s.area();
s=t;
s.area();
}
}

Output:
area of rectangle is: 1350.0

area of triangle is: 45.0

18.write a java program to demonstrate doubly linked list(DLL).


Code:
import java.util.Scanner;
public class DLL{
20251A3639 Sai Trisha

class Node{
int data;
Node previous;

Node next;
public Node(int data){
this.data=data;
}

}
Node head,tail=null;
public void addNode(int data){
Node newNode=new Node(data);

if(head==null){
head=tail=newNode;
head.previous=null;
tail.next=null;
}

else{
tail.next=newNode;
newNode.previous=tail;
tail=newNode;
tail.next=null;
}
}
void deleteNode(int del){

Node t,cur=null;
if(head==null){
System.out.println("List is empty");
20251A3639 Sai Trisha

return;
}
cur=head;

while(cur!=null){
if(cur.data==del){
if(cur==head){
head=cur.next;

head.previous=null;
}
else
if(cur==tail){

tail=cur.previous;
tail.next=null;
}
else{
t=cur.previous;

t.next=cur.next;
cur.next.previous=t;
}
}
cur=cur.next;
}
return;
}

public void display(){


Node current=head;
if(head== null){
20251A3639 Sai Trisha

System.out.println("List is empty");
return ;
}

System.out.println("Nodes of double linked list:");


while(current!=null){
System.out.println(current.data+" ");
current=current.next;

}
}
public static void main(String[] args){
DLL dList=new DLL();

dList.addNode(04);
dList.addNode(29);
dList.addNode(53);
dList.addNode(69);
dList.addNode(54);

dList.display();
Scanner inp=new Scanner(System.in);
System.out.println("Enter elemnt to be deleted:");
int ele=inp.nextInt();
dList.deleteNode(ele);
System.out.println("\n list after deleting the given node:");
dList.display();
}

Output:
20251A3639 Sai Trisha

Nodes of doubly linked list:


1
2

3
4
5
Enter the element to be deleted:

List after deleting the given node: Nodes of doubly linked list:
2

3
4
5
Nodes of doubly linked list:
1

2
3
4
5
Enter the element to be deleted:
8

List after deleting the given node: Nodes of doubly linked list:

1
2
3
20251A3639 Sai Trisha

4
5

Nodes of doubly linked list:


1
2
3

4
5
Enter the element to be deleted:
5

List after deleting the given node: Nodes of doubly linked list:
1
2
3

19.Write a program to demonstrate abstract classes.


Code:
import java.io.*;

abstract class Shape{


double dim1;
double dim2;
Shape(double d1,double d2){
dim1=d1;
20251A3639 Sai Trisha

dim2=d2;
}
abstract void printArea();

}
class Rectangle extends Shape{
Rectangle(double d1,double d2){
super(d1,d2);

}
void printArea(){
System.out.println("Area of rectangle="+(dim1*dim2));
}

}
class Triangle extends Shape{
Triangle(double d1,double d2){
super(d1,d2);
}

void printArea(){
System.out.println("Area of triangle="+(dim1*dim2/2));
}
}
class Circle extends Shape{
Circle(double d1,double d2){
super(d1,d2);
}

void printArea(){
System.out.println("Area of circle="+(dim1*dim1*3.142));
}
20251A3639 Sai Trisha

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

Rectangle r=new Rectangle(15,3);


Triangle t=new Triangle(5,2);
Circle c=new Circle(3,0);
Shape s;

s=r;
s.printArea();
s=t;
s.printArea();

s=c;
s.printArea();
}
}

Output:
Area of rectangle=45.0
Area of triangle=5.0
Area of circle=28.278

20.write a program to implement multiple inheritance(interfaces).


Code:
import java.io.*;
interface Car
{
int speed=60;
void distancetravelled();
20251A3639 Sai Trisha

}
interface Bus
{
int distance=100;
void speed();
}
class Vehicle implements Car,Bus
{
int distancetravelled;
double avgspeed;
public void distancetravelled()
{
distancetravelled=distance*speed;
System.out.println("total distance travelled: "+distancetravelled);
}
public void speed()
{
avgspeed=distancetravelled/speed;
System.out.println("Average speed : "+avgspeed);
}
}
class Interface{
public static void main(String args[])
{
Vehicle v1=new Vehicle();
v1.distancetravelled();
v1.speed();
}
}

Output:
total distance travelled: 6000

Average speed : 100.0

21. Write a program to list all files present in the directory and also the files
present in the sub directory.
Code:
20251A3639 Sai Trisha

import java.io.File;
public class Files
{

public static void main(String[] args)


{
File folder=new File("E:/3639/cycle 1");
String[] files=folder.list();

for(String file:files)
{
System.out.println(file);
}

File[] fobj=folder.listFiles();
for(File f:fobj)
{
if(f.isFile())
{

System.out.println(f.getName());
System.out.println(f.getPath()+"is a File");
System.out.println(f.getName()+"belongs to"+f.getParent());
System.out.println(f.getName()+"lastModifiedon"+f.lastModified());
System.out.println("length of"+f.getName()+"is:"+f.length());
}
else
{

System.out.println(f.getName());
System.out.println(f.getPath()+"is a directory");
System.out.println(f.getName()+"belongs to"+f.getParent());
20251A3639 Sai Trisha

File[] dirfiles=f.listFiles();
for(File file :dirfiles)
{

System.out.println(file);
}
}
}

}
}

Output:

cycle1_3639.docx
cycle1_3639.docx
E:\3639\cycle 1\cycle1_3639.docxis a File
cycle1_3639.docxbelongs toE:\3639\cycle 1

cycle1_3639.docxlastModifiedon1650965858218
length ofcycle1_3639.docxis:514

22.write a program for packages(Access specifiers)


Code:

package p1;
public class SuperClassP1
{
int n=1;
private int npriv=2;
protected int nprot=3;
20251A3639 Sai Trisha

public int npub=4;


public void meth1()
{

System.out.println("Inside superclassp1's method");


}
public SuperClassP1()
{

System.out.println("Super class constructor");


System.out.println("n = "+n);
System.out.println("nprivate= "+npriv);
System.out.println("nprotected = "+nprot);

System.out.println("npublic= "+npub);
}
}

package p1;

class SubClassP1 extends SuperClassP1


{
SubClassP1()
{
System.out.println("Subclass P1's constructor");
System.out.println("n= "+n);
//System.out.println("nprivate= "+npriv);

System.out.println("nprotected= "+nprot);
System.out.println("npublic= "+npub);
}
20251A3639 Sai Trisha

void meth2()
{
System.out.println("Inside subclassp1's method");

}
}
class SameClassP1
{

SameClassP1()
{
SuperClassP1 sp=new SuperClassP1();
System.out.println("Same class p1's constructor");

System.out.println("n= "+sp.n);
//System.out.println("nprivate= "+sp.npriv);
System.out.println("nprotected= "+sp.nprot);
System.out.println("npublic= "+sp.npub);
}

void meth3()
{
System.out.println("Inside sameclassp1's method");
}
}
public class Demo
{
public static void main(String[] args)

{
SuperClassP1 sp1=new SuperClassP1();
sp1.meth1();
20251A3639 Sai Trisha

SubClassP1 sp2=new SubClassP1();


sp2.meth2();
SameClassP1 sp3=new SameClassP1();

sp3.meth3();
}
}

package p2;
import p1.*;
class SubClassP2 extends SuperClassP1
{

SubClassP2()
{
//System.out.println("n= "+n);
//System.out.println("nprivate ="+npriv);
System.out.println("nprotected= "+nprot);

System.out.println("npublic= "+npub);
}
public void meth()
{
System.out.println("Inside subclassp2's method");
}
}
class OtherClassP2

{
OtherClassP2()
{
20251A3639 Sai Trisha

SuperClassP1 p=new SuperClassP1();


//System.out.println("n= "+p.n);
//System.out.println("nprivate= "+p.npriv);

//System.out.println("nprotected= "+p.nprot);
System.out.println("npublic= "+p.npub);
}
public void method()

{
System.out.println("Inside otherclassp2's method");
}
}

class Demo1
{
public static void main(String[] args)
{
SubClassP2 sp1=new SubClassP2();

sp1.meth();
OtherClassP2 sp2=new OtherClassP2();
sp2.method();
}
}
20251A3639 Sai Trisha

Output:

Super class constructor


n=1
nprivate= 2

nprotected = 3
npublic= 4
Inside superclass p1's method
Super class constructor

n=1
nprivate= 2
nprotected = 3
npublic= 4

Subclass P1's constructor


n= 1
nprotected= 3
npublic= 4
Inside subclassp1's method

Super class constructor


n=1
nprivate= 2
nprotected = 3

npublic= 4
Same class p1's constructor
n= 1
nprotected= 3

npublic= 4
20251A3639 Sai Trisha

Inside sameclassp1's method

Super class constructor

n=1
nprivate= 2
nprotected = 3
npublic= 4

nprotected= 3
npublic= 4
Inside subclassp2's method
Super class constructor

n=1
nprivate= 2
nprotected = 3
npublic= 4
npublic= 4

Inside otherclassp2's method

You might also like