Professional Documents
Culture Documents
20251A3639 Sai Trisha
20251A3639 Sai Trisha
20251A3639 Sai Trisha
Week-1
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
{
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+ " ");
}
}
Output:
roots of the given quation are:
3.0
2.0
int r=0;
a=n;
while(n>0)
{
d=n%10;
r=(d*d*d)+r;
n=n/10;
}
if(a==r)
}
}
Output:
153is an Armstrong number.
375 is not an Armstrong number.
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
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
}
}
{
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
if(min>arr[i]){
min=arr[i];
}
}
return min;
}
int min,max;
int[]array={24,89,92,78,56};
int n=array.length;
max=maxm(array);
min=minm(array);
Output:
maximum value in the array is92
minimum value in the array is24.
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
this.sid=1;
this.subname="Mathematics";
count++;
}
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);
}
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
Area.volume();
}
}
Output:
The area of the rectangle is: 500
The volume of the cuboid is: 16000.
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();
Output:
in show method of outer/super class
in inner class.
class Outer
{
class Inner
{
{
public void InnerMethod()
{
System.out.println("In the Inner local class");
}
}
InnerLocal in=new InnerLocal();
in.InnerMethod();
}
}
20251A3639 Sai Trisha
obj.display();
}
}
Output:
In a nested class method
Output:
the concatenation of two strings is GNITS it department
the character at 7th place of string 2 is a
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
void display(){
System.out.println("Name : "+name);
System.out.println("Address : "+add);
}
}
class Student extends Person{
public int rno;
20251A3639 Sai Trisha
}
void display(){
super.display();
System.out.println("roll no. : "+rno);
}
}
class Marks extends Student{
double tm,p;
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[]){
Output:
Name :Trisha
Address :VNR
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
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;
}
void area(){
System.out.println("area of triangle is: "+(l*h*0.5));
}
}
class DynamicMethodDispatch{
public static void main(String []args){
s.area();
s=t;
s.area();
}
}
Output:
area of rectangle is: 1350.0
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;
}
System.out.println("List is empty");
return ;
}
}
}
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
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
4
5
Enter the element to be deleted:
5
List after deleting the given node: Nodes of doubly linked list:
1
2
3
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[]){
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
}
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
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
{
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
package p1;
public class SuperClassP1
{
int n=1;
private int npriv=2;
protected int nprot=3;
20251A3639 Sai Trisha
System.out.println("npublic= "+npub);
}
}
package p1;
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
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
//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:
nprotected = 3
npublic= 4
Inside superclass p1's method
Super class constructor
n=1
nprivate= 2
nprotected = 3
npublic= 4
npublic= 4
Same class p1's constructor
n= 1
nprotected= 3
npublic= 4
20251A3639 Sai Trisha
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