0% found this document useful (0 votes)
2 views2 pages

Java Bit Final

The document outlines various Java programming tasks including arithmetic operations on command line arguments, base conversions, matrix operations, triangle classification, array merging, digit sum and reversal, HCF and LCM calculations, anagram checking, and complex number operations. Each task includes a brief description and code snippets illustrating the implementation. Additionally, it covers abstract classes and methods for geometric shapes and volume calculations.

Uploaded by

saikrd2617
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Bit Final

The document outlines various Java programming tasks including arithmetic operations on command line arguments, base conversions, matrix operations, triangle classification, array merging, digit sum and reversal, HCF and LCM calculations, anagram checking, and complex number operations. Each task includes a brief description and code snippets illustrating the implementation. Additionally, it covers abstract classes and methods for geometric shapes and volume calculations.

Uploaded by

saikrd2617
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Write a program to find the sum, difference, product, quotient and remainder of two numbers passed as command line 4. Write a program to perform base conversion
argument. a) Integer to binary b) Integer to Octal c) Integer to Hexadecimal 7. Write a program to find the trace and transpose of a matrix.
class CommandLine [Link]("Quotient = " + q); import [Link].*; [Link]("Octal equivalent : "+oct); import [Link]; }
{ public static void main(String[] args) [Link]("Remainder = " + r); public class BaseConv [Link]("Hexadecimal equivalent : "+hex); class TransposeAndTrace { [Link]();
{ int a, b, s, d, p, q, r; } } { } public static void main(String[] args) }
a = [Link](args[0]); 1-start public static void main(String args[]) } { [Link]("Transpose of A : ");
b = [Link](args[1]); 2- initiate and declare a,b, sum, different, product, quotient, { 1-start int s=0, i, j, n; for (i = 0; i < n; i++)
s = a + b; d = a - b; p = a * b; reminder int n; 2- initialise variables and declare num to state the input Scanner sc = new Scanner([Link]); {
q = a / b; r = a % b; 3- accept the command line argument as input a and b String bin,oct,hex; numbers. Declare strings (hex, bin,oct) to store [Link]("Order of the square matrix : "); for (j = 0; j < n; j++)
[Link]("Sum = " + s); 4- perform arithmetic operation Scanner sc=new Scanner([Link]); converted values n = [Link](); {
[Link]("Difference = " + d); 5- print output [Link]("Enter an integer : "); 3- accept an integer as input for the variable num int[][] a = new int[n][n]; [Link](" "+a[j][i]);
[Link]("Product = " + p); 6-stop n=[Link](); 4- perform base conversion convert Num to binary octal and [Link]("Read the Elements of Matrix : "); }
bin=[Link](n,2); hexa decimal for (i = 0; i < n; i++) [Link]();
2. Given the sides of a triangle, write a program to check whether the triangle is equilateral, isosceles or scalene and find its area. oct=[Link](n,8); 5- display the output { }
import [Link].*; else if(a==b||b==c||c==a) hex=[Link](n,16); 6-stop for (j = 0; j < n; j++) [Link]("Trace of the matrix A = "+s);
public class Triangle [Link]("Isosceles Triangle"); [Link]("Binary equivalent : "+bin); { }
{ else a[i][j] = [Link](); }
public static void main(String args[]) [Link]("Scalene Triangle"); 5. Write a program to merge two arrays. } 1- START
{ s=(a+b+c)/2; import [Link].*; k = 0; } 2-initialise variables,declare integer variables for Matrix
float a, b, c; double area,s; area=[Link](s*(s-a)*(s-b)*(s-c)); class ArrayMerge while (i<m && j <n) for (i = 0; i < n; i++) dimension and 2D array for the matrix
Scanner sc=new Scanner([Link]); [Link]("Area : "+area); { public static void main(String[] args) { if (a[i] < b[j]) s=s+a[i][i]; 2- Accept input for the matrix, specifying it’s dimension and
[Link]("First side = "); }}} { c[k++] = a[i++]; [Link]("Matrix A : "); populating the elements
a=[Link](); 1-start int m, n, i, j, k; else for (i = 0; i < n; i++) 3- Calculate trace
[Link]("Second side = "); 2- declare 3 sides of triangle. Input 3 sides at run time Scanner s = new Scanner([Link]); c[k++] = b[j++]; { 4- Calculate transpose,use nested loops to transpose the
b=[Link](); 3- if side 1 == side2 and Side 2 == side 3 go to step 6 [Link]("Enter the size of first array : "); } for (j = 0; j < n; j++) original matrix
[Link]("Third side = "); 4- if side 1 == side 2 || side 2 ==side 3 || side 3 ==side1 m = [Link](); while (i < m) { 5- Display the transposed matrix
c=[Link](); go to steps 7 int a[] = new int[m]; c[k++] = a[i++]; [Link](" "+a[i][j]); 7-stop
if(a>=(b+c)||b>=(c+a)||c>=(a+b)) 5- else go to step 8 [Link]("Read elements of first array : "); while (j < n)
[Link]("Cannot form a triangle"); 6- print triangle is equilateral for (i = 0; i < m; i++) c[k++] = b[j++]; 8. Write java program to find the sum of the digits and reverse of a given number using class and objects.
else 7- print triangle is isosceles { a[i] = [Link](); [Link]("Array after merging : "); import [Link].*; [Link]("Reverse of "+n+" = ");
{ if(a==b && b==c && a==c) 8- print triangle is scalene } for (i = 0; i < m+n; i++) class SumAndRev for(i=0;i<k;i++)
[Link]("Equilateral Triangle"); 9- stop [Link]("Enter the size of second array : "); { { { [Link](rev[i]);
n = [Link](); [Link](c[i]); int a,n,k,i,sum=0,x; } }
3. Read an array of 10 or more numbers and write a program to find the , int b[] = new int[n]; } } } int[] rev = new int[50]; }
a) Smallest element in the array b) Largest element in the array c) Second largest element in the array [Link]("Read elements of second array : "); 1-start public void sumrev() class SumAndRevMain
import [Link].*; a[j]=a[j+1]; for (i = 0; i < n; i++) { 2- initialise a array arr1,arr2 and an array before merged { {
public class NumArray a[j+1]=t; b[i] = [Link](); array [Link]("Enter the number : "); public static void main(String args[])
{ } } 3- use a loop to copy all elements of arr1 merged array Scanner sc = new Scanner([Link]); {
public static void main(String args[]) } [Link](a); 4- use another loop to copy all elements of arr2 to the n=[Link](); SumAndRev ob = new SumAndRev();
{ } [Link](b); remaining positions in merged array x=n; [Link]();
int n,i,j,t; [Link]("Smallest element : "+a[0]); int c[] = new int[m+n]; 5- display the merged array k=0; [Link]();
int a[]=new int[100]; [Link]("Largest element : "+a[n-1]); i = 0; j = 0; 6-stop while(x>0) [Link]();
Scanner sc=new Scanner([Link]); for(i=n-1;a[i]==a[i-1];i- -); { }
[Link]("Enter the size of the array : "); [Link]("Second largest element : "+a[i-1]); 6. Java Programming Code to Find HCF LCM of Two Numbers a=x%10; }
n=[Link](); } import [Link].*; break; sum=sum+a; [Link]
[Link]("Enter the elements : "); } public class HcfLcm } } x=x/10; [Link] a class with instance variable for the input number,
for(i=0;i<n;i++) 1- Start { public static void main(String args[]) lcm=(a*b)/hcf; rev[k]=a; sum and reversed.
{ a[i]=[Link](); 2- Read array of 10 or more numbers from the user { int a,b,c,i,hcf=1,lcm; [Link]("HCF of "+a+ " and "+b+" = "+hcf); k++; [Link] numbers for sum and reverse.
} 3- Initialise variable smallest, largest and second largest to Scanner sc=new Scanner([Link]); [Link]("LCM of "+a+ " and "+b+" = "+lcm); } [Link] sum of digits.
for(i=0;i<n-1;i++) the first 3 elements of array [Link]("Enter two numbers : "); } } [Link] reverse by using loops
{ 4- Traverse array from the 4th element to the end a=[Link](); } public void display() [Link] output.
for(j=0;j<n-1;j++) 5- For each element compare it with smallest, largest and b=[Link](); 1-start { [Link]
{ second largest and update the variable c=a<b?a:b; 2- initialise and declare variable num1,num2,HCF and LCM [Link]("Sum of the digits of "+n+" = "+sum);
if(a[j]>a[j+1]) accordingly for(i=c;i>=2;i- -) 3- calculate the HCF
{ 6- Display the smallest,largest and second largest { 4- calculate the LCM use the formula LCM =
t=a[j]; 7-stop if(a%i==0 && b%i==0) (num1*num2)/HCF
{ 5- display the output
hcf=i; 6-STOP

[Link]("Cube : "); h=[Link]();


9. Write a Java Programming Code to Check given string Anagram or Not. If the two strings are anagram to each other,then one [Link]("\tEnter the side : "); [Link](r,h);
string can be rearranged to form the other string. For Example: abc and cba are anagram. 12. Using class and objects, write a java program to find the sum of two complex numbers a=[Link](); }
import [Link].*;; char[] s2 = [Link]().toCharArray(); (Hint: Use object as parameter to function). [Link](a); }
class Anagram [Link](s1); [Link](s2); import [Link]; [Link]("Read first Complex number : "); [Link]("Rectangular box : "); 1. Start
{ public static void main(String[] input) flag = [Link](s1,s2); } public class Complex [Link](); [Link]("\tEnter length,breadth and height : "); 2. create a class named volumecalculator
{ String str1, str2; if (flag == true) { int real, img; public void read() [Link]("Read second Complex number : "); l=[Link](); 3. implemented three overloading functions for cubes
int len1, len2; { { Scanner sc=new Scanner([Link]); [Link](); b=[Link](); rectangular box and cylinder
boolean flag=false; [Link]("Strings are Anagram.."); [Link]("\tEnter the real part : "); [Link]("First Complex number : "); h=[Link](); 4. write the volume calculation formulas for each shape in
Scanner sc = new Scanner([Link]); } else real=[Link](); [Link](); [Link](l,b,h); their perspective overload methods
[Link]("Enter First String : "); { [Link]("Strings are not Anagram.."); [Link]("\tEnter the imaginary part : "); [Link]("Second Complex number : "); [Link]("Cylinder : "); 5. display the output
str1 = [Link](); } } } img=[Link](); [Link](); [Link]("\tEnter radius and height : "); 6. stop
[Link]("Enter Second String : "); 1. start } public Complex sum(Complex c1, Complex c2) [Link]("Sum of complex numbers : "); r=[Link]();
str2 = [Link](); 2. Input 2 strings { c3=[Link](c1, c2);
str1=[Link](" ",""); str2=[Link](" ", ""); 3. remove space and convert to lowercase Complex c3 = new Complex(); [Link](); 15. Create an abstract class shape and create TwoDim and ThreeDim as sub classes. Create classes square and triangle derived
len1 = [Link](); len2 = [Link](); 4. Sort the character in both string and compare them [Link] = [Link] + [Link]; [Link] = [Link] + [Link]; } } from TwoDim and sphere and cube derived from ThreeDim. Write a program to determinethe area of various shapes.
if (len1 == len2) 5. display output return c3; 1. Start import [Link].*; class SphereVol extends ThreeDim {
{ char[] s1 = [Link]().toCharArray(); 6. stop } 2. define a class complex number with real and imaginary abstract class Shape double r;
public void display() parts { void read()
10. Write a Java Program to remove all vowels from a string { [Link](real+" + "+img+ "i"); 3. initialise objects to represent 2 complex numbers trade abstract void read(); {
import [Link].*; } } } complex numbers abstract void calc(); [Link]("Sphere\n\tEnter radius : ");
public class RemoveVowels 1. Start public static void main(String args[]) 4. calculate sum Scanner sc=new Scanner([Link]); r=[Link]();
{ public static void main(String args[]) 2. Initialise variables str,r { Complex c1,c2,c3; 5. display result } }
{ String str, r; String[] v={"a"," 3. Iterate through the characters, use loops to iterate c1=new Complex(); c2=new Complex(); 6. stop abstract class TwoDim extends Shape{ void calc()
Scanner sc=new Scanner([Link]); through each character of input string c3=new Complex(); double ar; { ar=(4/3)*3.14*r*r*r;
[Link]("Enter the string : "); 4. Check the characters are vowels or not and create the void display() }
str=[Link](); r=str; modified string 13. Write a program to count and display total number of objects created to a class (Hint: static members). { }
for(int i=0;i<10;i++) 5. Display the output public class CountObject { [Link]("\tArea = "+ar); class Cube extends ThreeDim {
r=[Link](v[i],""); 6. stop { CountObject obj1 = new CountObject(); } } double a;
[Link]("String after removing vowels : "+r); static int count = 0; CountObject obj2 = new CountObject(9); abstract class ThreeDim extends Shape{ void read()
public CountObject() CountObject obj3 = new CountObject(); double ar; {
11. Create a class Student to read and display the student details. Create another class mark inherit from student to read marks { [Link]("Number of objects created : " + void display() [Link]("Cube\n\tEnter side : ");
of 5 subjects and find total and average. Write a Java program to display the result of a student. count = count + 1; [Link]); { [Link]("\tVolume = "+ar); a=[Link]();
import [Link].*; { } } } } } void calc()
class Student [Link]("Enter marks in 5 subjects : "); public CountObject(int n) } class Square extends TwoDim { { ar=a*a*a;
{ String name, course, regno; for(int i=0;i<5;i++) { 1. Start double a; } }
int sem; { m[i]=[Link](); CountObject[] ob=new CountObject[n]; 2. create a class with a static variable,count to track the void read() public class AbstractMain {
Scanner sc=new Scanner([Link]); total=total+m[i]; for(int i=0;i<n;i++) numbers of objective created { public static void main(String args[])
void read() } avg=total/5; { 3. initialise count to zero in the class [Link]("Square\n\tEnter side : "); {
{ } void dispmark() ob[i]=new CountObject(); 4. increment account in constructor a=[Link](); Square s=new Square();
[Link]("Enter the reg no : "); { } 5. create multiple objects of the class in the main program } Triangle t=new Triangle();
regno=[Link](); [Link]("Total : "+total+"\nAverage: "+avg); } 6. display total objects void calc() SphereVol sp=new SphereVol();
[Link]("Enter the name : "); } public static void main(String[] args) 7. stop { Cube c=new Cube();
name=[Link](); public static void main(String args[]) ar=a*a; [Link](); [Link]();
[Link]("Enter the course : "); { Mark ob=new Mark(); } } [Link](); [Link]();
course=[Link](); [Link](); [Link](); 14. Write a java program to find the volume of cube, rectangular box, cylinder using function overloading. class Triangle extends TwoDim { [Link](); [Link]();
[Link]("Enter the sem : "); [Link](); [Link](); import [Link].*; } double b,h; [Link](); [Link]();
sem=[Link](); } } class ShapeVolume void volume(double r,double h) void read() [Link](); [Link]();
} 1. Start { { { [Link](); [Link]();
void display() 2. define student class with methods to read and display double v; v=3.14*r*r*h; [Link]("Triangle\n\tEnter breadth : "); } }
{ student details void volume(double a) [Link]("\tThe volume of the cylinder = " + v); b=[Link](); 1. Start
[Link]("Student Details\n ---------------- "); 3. create mark class inheriting from [Link] { } [Link]("\tEnter height : "); 2. define abstract class shape for area calculation
[Link]("Reg No : "+regno+"\nName : "+name); methods to read 5 marks of 5 subjects, calculate v=a*a*a; } h=[Link](); 3. implement TwoDim and ThreeDim classes extending the
[Link]("Course : "+course+"\nSem : "+sem); total and find average [Link]("\tThe volume of the cube = " + v); public class ShapeVolumeMain } shape of classes
} } 4. Read student details using objects } { void calc() 4. override the area calculation methods
class Mark extends Student 5. read and calculate marks void volume(double l,double b,double h) public static void main(String args[]) { 5. invoke area calculation methods and display the result
{ 6. display result { { ar=0.5*b*h; 6. stop
int[] m=new int[5]; 7. stop v=l*b*h; double a,l,b,h,r; } }
float avg,total=0; [Link]("\tThe volume of the rectangular box = " ShapeVolume ob=new ShapeVolume();
void readmark() + v); Scanner sc=new Scanner([Link]);
16. Create an interface volume with member variable pi and methods readdata () and dispvolume (). Create two classes sphere
and cylinder to implement this interface. Write a Java program to find the volume. 18. Create a class Account to deposit and withdraw money from a bank. Create a user defined exception “MinBalExp‟ to be
import [Link].*; v = pi * [Link](r,2) * h; invoked when the withdrawal amount is greater than balance.
interface VolumeInterface }
{ double pi = 3.14; public void dispVolume() import [Link].*; void deposit()
public void readData(); { [Link]("\tVolume of Cylinder = " + v); class MinBalExp extends Exception {
public void dispVolume(); } } { [Link]("Enter the amount to deposit : ");
} public class VolumeInterMain MinBalExp(String str) x=[Link]();
class Sphere implements VolumeInterface { } { amt=amt+x;
{ double v, r; public static void main(String args[]) super(str); [Link]("Balance in your account : "+amt);
public void readData() { Sphere s=new Sphere(); } }
{ Scanner sc=new Scanner([Link]); Cylinder c=new Cylinder(); } public static void main(String args[])
[Link]("Sphere : "); [Link](); class Account { {
[Link]("\tEnter the radius : "); [Link](); int x,amt; Account ob=new Account();
r=[Link](); [Link](); Scanner sc=new Scanner([Link]); [Link]();
v = (4/3) * pi * [Link](r,3); [Link](); void read() while(true)
} } } { {
public void dispVolume() 1. Start [Link]("Enter the total amount : "); [Link]("\nTransaction\n1-Deposit\n2-
{ [Link]("\tVolume of Sphere = " + v); 2. create an interface with name volume with a member amt=[Link](); Withdraw\n3-Exit\nChoice : ");
} variable pi and abstract methods read data () } int ch=[Link]();
class Cylinder implements VolumeInterface and disp volume () void withdraw() if(ch==1)
{ 3. Implement the volume interface in classes sphere and { [Link]();
double v, r, h; cylinder try{ else if(ch==2)
public void readData() 4. override read data() to accept necessary parameter [Link]("Enter the amount to withdraw : "); [Link]();
{ Scanner sc=new Scanner([Link]); override disp volume() to calculate and display x=[Link](); else
[Link]("Cylinder : "); volume if(amt<x) return;
[Link]("\tEnter the radius : "); 5. in main program create objects to input length of the throw new MinBalExp("Withdrawal amount is greater than }
r=[Link](); shape balance..!");
[Link]("\tEnter the height : "); 6. Display output amt=amt-x;
h=[Link](); 7. stop [Link]("Balance in your account : "+amt); 1. start
} 2. define class account with attributes like account numbers,
catch(MinBalExp e) balance etc
17. Write a multi thread java program for displaying odd numbers and even numbers up to a limit (Hint: Implement thread using { 3. create user defined class MinBalExp extending exception
Runnable interface). [Link](e); 4. If withdrawal amount > balance, MinBalExp
import [Link].*; } 5. Display the output
class Oddthread implements Runnable } 6. stop
{ int n; } .
oddthread(int x) class MainThread
{ n=x; { public static void main(String args[])
} }
public void run() { Scanner sc=new Scanner([Link]);
{ [Link](" Enter the limit : ");
for(int i=1;i<=n;i+=2) int n=[Link]();
{ oddthread o=new oddthread(n);
[Link](" Odd thread : "+i); eventhread e=new eventhread(n);
}}} Thread t1=new Thread(o);Thread t2=new Thread(e);
class eventhread implements Runnable [Link](); [Link]();
{ int n; } }
eventhread(int x)
{ n=x; } 1. Start
public void run() 2. define runnable interface with a method to print numbers
{ for(int i=0;i<=n;i+=2) 3. implemente runnable classes, override the print
{ numbers() to display odd and even numbers
[Link](" Even thread : "+i); 4. create Threads and start them
} 5. display outputs
} 6. stop
[Link]();

You might also like