Java Programs for Basic Calculations
Java Programs for Basic Calculations
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int m;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter Your Marks:");
m=[Link]([Link]());
if((m<=100)&&(m>=80))
{
[Link]("Distinction class acquired");
}
else if((m<=79)&&(m>=60))
{
[Link]("First class acquired");
}
else if((m<=59)&&(m>=50))
{
[Link]("Second class acquired");
}
else if((m<=49)&&(m>40))
{
[Link]("Pass class acquired");
}
else if(m<=40)
{
[Link]("You have Failed");
}
else
{
[Link]("Invalid Input");
}
}
}
3. WAP to check if the given number is divisible by 7 and 9: -
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter A number:");
n=[Link]([Link]());
if((n%9==0)&&(n%7==0))
{
[Link]("The number is divisible by 7 and 9");
}
else
{
[Link]("The number is not divisible by 7 or 9");
}
}
}
4. WAP to find greatest and smallest of two numbers using ternary operator: -
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int n,m,s,g;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter Two Numbers:");
n=[Link]([Link]());
m=[Link]([Link]());
g=(n>m)?n:m;
s=(n<m)?n:m;
[Link]("The Largest Number:"+g);
[Link]("The Smallest Number:"+s);
}
}
5. WAP to display month name from the month number using switch statement: -
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter Number to check to check the month:");
n=[Link]([Link]());
switch(n)
{
case 1:
[Link]("Janaury");
break;
case 2:
[Link]("February");
break;
case 3:
[Link]("March");
break;
case 4:
[Link]("April");
break;
case 5:
[Link]("May");
break;
case 6:
[Link]("June");
break;
case 7:
[Link]("July");
break;
case 8:
[Link]("August");
break;
case 9:
[Link]("September");
break;
case 10:
[Link]("October");
break;
case 11:
[Link]("November");
break;
case 12:
[Link]("December");
break;
default:
[Link]("Invalid Input");
}
}
}
6. WAP to print prime numbers from 1 to 99 using do loop: -
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int i=1;
[Link]("Prime numbers from 1 to 100:");
do
{
int k=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
k++;
}
}
if(k==2)
{
[Link](i+" is a prime number");
}
i++;
}while(i<=99);
}
}
7. WAP to check if the given number is palindrome or not:-
import [Link].*;
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int temp,rev=0,n,r;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a number");
n=[Link]([Link]());
temp=n;
while(n>0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
if(temp==rev)
{
[Link]("Number is a palindrome");
}
else
{
[Link]("Number is not a palindrome");
}
}
}
8. WAP to check if the given number is divisible by 7 and 9: -
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter A number:");
n=[Link]([Link]());
if((n%9==0)&&(n%7==0))
{
[Link]("The number is divisible by 7 and 9");
}
else
{
[Link]("The number is not divisible by 7 or 9");
}
}
}
9. WAP to print following pyramid using for loop: -
*
***
*****
*******
*********
import [Link].*;
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows-i; j++) {
[Link](" ");
}
for (int k = 1; k <= 2*i-1; k++) {
[Link]("*");
}
[Link]();
}
}
}
WAP to find the factorial of a number using while loop:-
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int n,i=1,fact=1;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a number to find its factorial");
n=[Link]([Link]());
while(i<=n)
{
fact=fact*i;
i++;
}
[Link]("The factorial of the number is:"+fact);
}
}
10. WAP to find reverse of a number using do loop: -
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int rev=0,r,n;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a number to find its reverse");
n=[Link]([Link]());
do
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}while(n>0);
[Link]("The reverse of the number is:"+rev);
}
}
11. WAP to find sum of digits of a number using for loop: -
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int rev=0,r,n;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a number to find its reverse");
n=[Link]([Link]());
for(rev=0;n!=0;n=n/10)
{
rev=rev+n%10;
}
[Link]("The Sum of the digits of the number is:"+rev);
}
}
12. WAP to check whether the given number is prime or not using while loop: -
import [Link].*;
class Main2
{
public static void main(String args[]) throws IOException
{
int i=1,n,p=0;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a number:");
n=[Link]([Link]());
while(i<=n)
{
if(n%i==0)
{
p++;
}
i++;
}
if(p==2)
{
[Link]("It is a prime number");
}
else
{
[Link]("It is not a prime number");
}
}
}
13. Define the class cube having data members length, breath and height. Initialize three objects
using different constructors and display its volume: -
import [Link].*;
class cube
{
int length,breadth,height;
cube()
{
length=10;
breadth=10;
height=10;
}
cube(int l,int b,int h)
{
length=l;
breadth=b;
height=h;
}
cube(cube obj)
{
length=[Link];
breadth=[Link];
height=[Link];
}
void volume()
{
[Link]("The volume of the cube is:"+(length*breadth*height));
}
}
class Main2
{
public static void main(String args[]) throws IOException
{
cube a=new cube();
cube b=new cube(20,20,20);
cube c=new cube(a);
[Link]("Default Constructor");
[Link]();
[Link]("Parameterized Constructor");
[Link]();
[Link]("Copy Constructor");
[Link]();
}
}
14. Define a class employee with data members as empid, name and salary. Accept data for five
objects and print it. (use parameterized constructor): -
import [Link].*;
class empolyee
{
int empid,salary;
String name;
empolyee(String n,int i,int s)
{
name=n;
empid=i;
salary=s;
}
void display()
{
[Link]("Name:"+name+" Empid:"+empid+" Salary:"+salary);
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
empolyee a=new empolyee("noob",1,2000);
empolyee b=new empolyee("noob1",2,3000);
empolyee c=new empolyee("noob2",3,4000);
empolyee d=new empolyee("noob3",4,5000);
empolyee e=new empolyee("noob4",5,6000);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
15. WAP to calculate gross salary of five employees if basic, HRA and DA are given. Use
parameterized constructor: -
import [Link].*;
class empolyee
{
int ba,ha,da;
empolyee(int b,int h,int d)
{
ba=b;
ha=h;
d=da;
}
void display()
{
[Link]("Gross Salary:"+(ba+ha+da));
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
empolyee a=new empolyee(1000,1000,1000);
empolyee b=new empolyee(2000,2000,2000);
empolyee c=new empolyee(3000,3000,3000);
empolyee d=new empolyee(4000,4000,4000);
empolyee e=new empolyee(5000,5000,5000);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
16. WAP to define class Employee with members as id and salary. Accept data for five employees
and display details of employees getting highest salary: -
import [Link].*;
class empolyee
{
int empid,salary;
empolyee(int i,int s)
{
empid=i;
salary=s;
}
void display()
{
[Link]("Empid:"+empid+" Salary:"+salary);
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
empolyee a=new empolyee(1,2000);
empolyee b=new empolyee(2,3000);
empolyee c=new empolyee(3,4000);
empolyee d=new empolyee(4,5000);
empolyee e=new empolyee(5,6000);
if(([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link]))
{
[Link]();
}
else if(([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link]))
{
[Link]();
}
else if(([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link]))
{
[Link]();
}
else if(([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link]))
{
[Link]();
}
else if(([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link])&&([Link]>[Link]))
{
[Link]();
}
}
}
17. WAP to check whether the given string is a palindrome or not: -
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
String s,r="";
char s1;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter a string:");
s=[Link]();
for(int i=([Link]()-1);i>=0;--i)
{
r=r+[Link](i);
}
if([Link](r))
{
[Link]("The String is a Palindrome");
}
else
{
[Link]("The String is not a Palindrome");
}
}
}
18. WAP to show the use of following methods of String class on a String:-
compareTo()
charAt()
replace()
substring()
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
String s="noob",s1="noob2",s2="noob";
[Link]("Compare to: "+[Link](s1));
[Link]("Compare to: "+[Link](s2));
[Link]("The character at index 3: "+[Link](3));
String s3=[Link]('o','n');
[Link]("Replacing o with b: "+s3);
[Link]("Substring: "+[Link](2));
}
}
19. WAP to show the use of following methods of StringBuffer class on a String:-
append()
insert()
reverse()
setCharAt()
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
StringBuffer sb=new StringBuffer("How are you");
StringBuffer sb1=new StringBuffer("Welcome");
[Link](" to appending of string");
[Link]("After appending: "+sb1);
[Link](sb);
[Link](11," with them");
[Link]("After insertion: "+sb);
StringBuffer sb2=[Link]();
[Link]("Reversing of string: "+sb2);
[Link](2,'h');
[Link]("After setcharat: "+sb);
}
}
20. Write a program to create and sort an integer array: -
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
int[] numbers = {8, 2, 7, 3, 1, 5};
[Link](numbers);
[Link]("Sorted Numbers:");
for (int number : numbers) {
[Link](number);
}
}
}
21. Write a Java program to test if an array contains a specific value: -
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
int[] numbers = {8, 2, 7, 3, 2, 5};
boolean a=false;
[Link]("If the array contains 1:");
for (int i=0;i<[Link];i++)
{
if(numbers[i]==1)
{
a=true;
break;
}
}
if(a==true)
{
[Link]("The array contains the value 1");
}
else
{
[Link]("The array does not contains the value 1");
}
}
}
22. Define the class mobile with data members ‘company_name’ & ‘screen_size’. Initialize and
display values of data members for five mobiles using array of objects: -
import [Link].*;
import [Link].*;
class mobile
{
int screensize;
String companyname;
mobile(String c,int s)
{
companyname=c;
screensize=s;
}
void display()
{
[Link]("Company name: "+companyname+" ScreenSize: "+screensize);
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
String c1;
int s1;
mobile a[]=new mobile[5];
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
for(int i=0;i<5;i++)
{
[Link]("Enter the mobile's company name and screen size");
c1=[Link]();
s1=[Link]([Link]());
a[i]=new mobile(c1,s1);
}
for(int i=0;i<5;i++)
{
a[i].display();
}
}
}
23. Implement program to accomplished the following task using Vector class :-
To add two integers, two float, two characters, two string objects
To search a particular object of the vector.
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
Vector v=new Vector();
int a=1,b=2;
boolean j=false;
char e='a',f='b';
String h="HOW",g="Are You";
[Link](a);
[Link](b);
[Link](5.5);
[Link](0.25);
[Link](e);
[Link](f);
[Link](h);
[Link](g);
for(int i=0;i<[Link]();i++)
{
[Link]([Link](i));
}
for(int i=0;i<[Link]();i++)
{
if([Link](i)=='b')
{
j=true;
break;
}
}
if(j==true)
{
[Link]("It does contain object 'b'");
}
else
{
[Link]("It does not contain object 'b'");
}
}
}
24. Implement program to accomplished the following task using Vector class: -
To display all objects along with their position
To delete the object mentioned by the user from vector.
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
Vector v=new Vector();
int a=1,b=2,k;
boolean j=false;
char e='a',f='b';
String h="HOW",g="Are You";
[Link](a);
[Link](b);
[Link](5.5);
[Link](0.25);
[Link](e);
[Link](f);
[Link](h);
[Link](g);
for(int i=0;i<[Link]();i++)
{
[Link]("Position: "+i+" Element: "+[Link](i));
}
[Link]("Enter the object you want to delete:");
k=[Link]([Link]());
[Link](k);
for(int i=0;i<[Link]();i++)
{
[Link]("Position: "+i+"Element: "+[Link](i));
}
}
}
25. WAP to convert String value into Integer Wrapper class object: -
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
String a;
[Link]("Enter numbers:");
a=[Link]();
int b=[Link](a);
[Link]("String to Integer wrapper class object: "+b);
}
}
26. WAP to convert Integer object into primitive dada type, short and double value: -
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
Integer intObj = new Integer(10);
short shortValue = [Link]();
double doubleValue = [Link]();
[Link]("Short value: " + shortValue);
[Link]("Double value: " + doubleValue);
}
}
27. WAP to create class employee with data members name and id and display() method. Create
a subclass salary with data members bas_salary. Override display() method and display the
net salary.(Assume additional data if necessary): -
import [Link].*;
import [Link].*;
class employee
{
int id=1;
String name="Nobody";
void display()
{
[Link]("Name: "+name+" ID:"+id);
}
}
class salary extends employee
{
int salary=30000;
void display()
{
[Link]("Salary: "+salary);
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
employee a=new employee();
salary b=new salary();
employee r;
r=b;
[Link]();
}
}
28. WAP to extend class dog from class animal to override move() method: -
import [Link].*;
import [Link].*;
class animal
{
void move()
{
[Link]("There are many animals ");
}
}
class dog extends animal
{
void move()
{
[Link]("Dog is an animal out of many animals");
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
animal a=new animal();
dog b=new dog();
animal r;
r=a;
[Link]();
r=b;
[Link]();
}
}
29. WAP to calculate the room area and volume using single inheritance: -
import [Link].*;
import [Link].*;
class area
{
int l=20,b=20,h=20;
void a()
{
[Link]("The area of the room in metres: "+(2*h*(l+b)));
}
}
class volume extends area
{
void b()
{
[Link]("The volume of the room in metres: "+(l*b*h));
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
volume c=new volume();
c.a();
c.b();
}
}
30. WAP to calculate the room area and volume using multilevel inheritance: -
import [Link].*;
import [Link].*;
class area
{
int l=20,b=20,h=20;
void a()
{
[Link]("The area of the room in metres: "+(2*h*(l+b)));
}
}
class volume extends area
{
void b()
{
[Link]("The volume of the room in metres: "+(l*b*h));
}
}
class sarea extends volume
{
void c()
{
[Link]("The surface area of the room in metres: "+((l*b)+(b*h)+(h*l)));
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
sarea d=new sarea();
d.a();
d.b();
d.c();
}
}
31. WAP to create a class Student with data members sid, sname and display() method. Create a
subclass Marks with data members m1, m2 (Marks in subjects 1 and 2) and override display()
method. Create a subclass of class marks having data member spmarks and override
display() method. Calculate total marks and display: -
import [Link].*;
import [Link].*;
class student
{
int sid=11;
String sname="Nobody";
void display()
{
[Link]("Name: "+sname+" sid: "+sid);
}
}
class marks extends student
{
int m1=20,m2=19;
void display()
{
[Link]("Marks of the subject 1: "+m1+"/20");
[Link]("Marks of the subject 2: "+m2+"/20");
}
}
class total extends marks
{
int spmks=m1+m2;
void display()
{
[Link]("Total marks of the subjects: "+spmks+"/40");
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
student a=new student();
marks b=new marks();
total c=new total();
student r;
r=a;
[Link]();
r=b;
[Link]();
r=c;
[Link]();
}
}
32. WAP to implement the following: -
import [Link].*;
import [Link].*;
interface loader
{
public void loadOS();
}
class device
{
int ram=64;
String companyname="HP(Hewlett-Packard)";
}
class mobile extends device implements loader
{
public void loadOS()
{
[Link]("Company name: "+companyname);
[Link]("RAM of the computer: "+ram);
[Link]("System now loading....");
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
mobile a=new mobile();
[Link]();
}
}
33. WAP to implement the following: -Percentage is not being shows for some reason
import [Link].*;
import [Link].*;
interface Exam
{
public void percent_cal();
}
class Student
{
int rollno=11,m1=20,m2=19;
String name="Nobody";
}
class Result extends Student implements Exam
{
public void percent_cal()
{
[Link]("Percentage: "+(((m1+m2)/40)*100));
}
public void display()
{
[Link]("Name of the student: "+name);
[Link]("RollNo of the student: "+rollno);
[Link]("Marks of Subject 1: "+m1);
[Link]("Marks of Subject 2: "+m2);
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
Result a=new Result();
[Link]();
a.percent_cal();
}
}
34. WAP to implement the following: -
import [Link].*;
import [Link].*;
interface gross
{
int ta=2000,da=2000;
public void gross_sal();
}
class employee
{
int sal=30000;
String name="Nobody";
}
class salary extends employee implements gross
{
int hra=2000;
public void gross_sal()
{
[Link]("Gross Salary: "+(sal+hra+ta+da));
}
public void display_net()
{
[Link]("Name of the employee: "+name);
[Link]("Salary of the employee: "+sal);
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
salary a=new salary();
a.display_net();
a.gross_sal();
}
}
35. Define a package named ‘useful’ with the class name ‘UseMe’ having a method to calculate
the salary. Import the above package ‘useFul’ and make use of salary() method: -
Package code: -
package useful;
public class UseMe
{
public int h,t,g;
public UseMe(int h1,int t1,int g1)
{
h=h1;
t=t1;
g=g1;
}
public void salary()
{
int sal;
sal=g-h-t;
[Link]("The salary of after calculating: "+sal);
}
}
Import package code: -
import [Link].*;
import [Link].*;
import useful.*;
class Main3
{
public static void main(String args[]) throws IOException
{
UseMe a=new UseMe(2000,3000,40000);
[Link]();
}
}
36. Define a package named ‘useful’ with the class names ‘UseMe’ having a method to calculate
the area of two different shapes. Import the above package ‘useFul’ and make use of
methods to calculate area: -
Package code: -
package useful;
public class UseMe
{
public void atraingle(int b,int h)
{
double tarea;
tarea= 0.5*b*h;
[Link]("The area of the traingle: "+tarea);
}
public void acube(int s)
{
int carea;
carea=s*s*s;
[Link]("The area of the cube: "+carea);
}
}
Importing package code: -
import [Link].*;
import [Link].*;
import useful.*;
class Main3
{
public static void main(String args[]) throws IOException
{
UseMe a=new UseMe();
[Link](20,20);
[Link](10);
}
}
37. Define a package named ‘useful’ with the class name ‘UseMe’ having a method to calculate
the percentage. Import the above package ‘useFul’ and make use of method to calculate
percentage: -
Don’t know why percentage is showing 0
Package code: -
package useful;
public class UseMe
{
public void percentage(int obtainedvalue,int totalvalue)
{
double per,per1;
per1=(obtainedvalue/totalvalue);
per=per1*100;
[Link]("The percenatge of the student: "+per);
}
}
Importing package code: -
import [Link].*;
import [Link].*;
import useful.*;
class Main3
{
public static void main(String args[]) throws IOException
{
UseMe a=new UseMe();
[Link](38,40);
}
}
38. Develop a program to create two threads. One thread print number from 1 to 10 & the other
thread prints numbers from 10 to 1. Use Thread class: -
import [Link].*;
import [Link].*;
class first extends Thread
{
public void run()
{
[Link]("Number from 0 to 10:");
for(int i=0;i<=10;i++)
{
[Link](i);
}
}
}
class second extends Thread
{
public void run()
{
[Link]("Number from 10 to 0:");
for(int i=10;i>=0;i--)
{
[Link](i);
}
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
first a=new first();
second b=new second();
[Link]();
[Link]();
}
}
39. Develop a program to create two threads. One thread print number from 1 to 10 & the other
thread prints prime numbers between 50 to 100. Use Thread class: -
import [Link].*;
import [Link].*;
class first extends Thread
{
public void run()
{
[Link]("Number from 0 to 10:");
for(int i=0;i<=10;i++)
{
[Link](i);
}
}
}
class second extends Thread
{
public void run()
{
for(int i=50;i<=100;i++)
{
int k=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
k++;
}
}
if(k==2)
{
[Link](i+" is a prime number");
}
}
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
first a=new first();
second b=new second();
[Link]();
[Link]();
}
}
40. Develop a program to create two threads. One thread print multiples of 5 & the other thread
prints multiples of 7. Use Thread class. Introduce a delay of 1000ms after printing every
number: -
import [Link].*;
import [Link].*;
class first extends Thread
{
public void run()
{
[Link]("Multiples of 5:");
try
{
for(int i=1;i<=10;i++)
{
[Link]("5 * "+i+" = "+(5*i));
[Link](1000);
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class second extends Thread
{
public void run()
{
[Link]("Multiples of 7:");
try
{
for(int i=1;i<=10;i++)
{
[Link]("7 * "+i+" = "+(7*i));
[Link](1000);
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
try
{
first a=new first();
[Link]();
[Link]();
second b=new second();
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
41. Develop a program to create two threads. One thread prints even numbers from 1 to 100 &
the other thread prints odd numbers from 200 to 300. Use Runnable interface: -
import [Link].*;
import [Link].*;
class first implements Runnable
{
public void run()
{
[Link]("Even numbers from 1 to 100:");
for(int i=0;i<=100;i++)
{
if(i%2==0)
{
[Link](i);
}
}
}
}
class second implements Runnable
{
public void run()
{
[Link]("Odd numbers from 200 to 300:");
for(int i=200;i<=300;i++)
{
if(i%2!=0)
{
[Link](i);
}
}
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
first a=new first();
Thread b=new Thread(a);
second c=new second();
Thread d=new Thread(c);
[Link]();
[Link]();
}
}
42. Develop a program to create two threads. One thread print prime numbers between 1 to
100 & the other thread prints non prime numbers from 200 to 250. Use Runnable interface: -
import [Link].*;
import [Link].*;
class first implements Runnable
{
public void run()
{
[Link]("Prime numbers from 1 to 100:");
for(int i=1;i<=100;i++)
{
int k=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
k++;
}
}
if(k==2)
{
[Link](i+" is a prime number");
}
}
}
}
class second implements Runnable
{
public void run()
{
[Link]("Non-prime numbers from 200 to 250:");
for(int i=200;i<=250;i++)
{
int l=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
l++;
}
}
if(l!=2)
{
[Link](i+" is a non prime number");
}
}
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
try
{
first a=new first();
Thread b=new Thread(a);
[Link]();
[Link]();
second c=new second();
Thread d=new Thread(c);
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
43. Develop a program to create two threads. One thread print numbers from 1 to 10 & the
other thread prints odd numbers from 20 to 30. Use Runnable interface. Introduce a delay of
1000ms after printing every number: -
import [Link].*;
import [Link].*;
class first implements Runnable
{
public void run()
{
[Link]("Number from 0 to 10:");
try
{
for(int i=0;i<=10;i++)
{
[Link](i);
[Link](1000);
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class second implements Runnable
{
public void run()
{
[Link]("Odd number from 20 to 30:");
try
{
for(int i=20;i<=30;i++)
{
if(i%2!=0)
{
[Link](i);
[Link](1000);
}
}
}
catch(Exception e)
{
[Link](e);
}
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
try
{
first a=new first();
Thread b=new Thread(a);
[Link]();
[Link]();
second c=new second();
Thread d=new Thread(c);
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
44. WAP to throw ArrayIndexOutOfBoundsException: -
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
try
{
int[] numbers = {8, 2, 7, 3, 1, 5};
int a=numbers[7];
}
catch(Exception e)
{
[Link](e);
}
}
}
45. WAP to throw ArithmaticException: -
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
try
{
int a=10;
[Link]("Division by zero: "+(10/0));
}
catch(Exception e)
{
[Link](e);
}
}
}
46. WAP to accept a password rom the user and throw an “Authentication Failure” exception if
the password does not match: -
import [Link].*;
import [Link].*;
class my extends Exception
{
my(String msg)
{
super(msg);
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
String p,p1;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
try
{
[Link]("Enter New Password:");
p=[Link]();
[Link]("Re-Enter Password:");
p1=[Link]();
boolean c=[Link](p1);
if(c==true)
{
throw new my("Authentication succesfull");
}
else
{
throw new my("Authenticatio failure");
}
}
catch(my e)
{
[Link](e);
}
catch(Exception e1)
{
[Link](e1);
}
}
}
47. WAP to accept a number from the user and throw “Not a prime number “ exception if the
number is not a prime number: -
import [Link].*;
import [Link].*;
class my extends Exception
{
my(String msg)
{
super(msg);
}
}
class Main3
{
public static void main(String args[]) throws IOException
{
int i=1,n,p=0;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
try
{
[Link]("Enter a number:");
n=[Link]([Link]());
while(i<=n)
{
if(n%i==0)
{
p++;
}
i++;
}
if(p==2)
{
throw new my("It is a prime number");
}
else
{
throw new my("It is not a prime number");
}
}
catch(my e)
{
[Link](e);
}
catch(Exception e1)
{
[Link](e1);
}
}
}
48. WAP to handle IOException using throws clause: -
FileNotFoundException, UnsupportedEncodingException, DirectoryNotFoundException are
some of the subclasses of IOException class.
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws IOException
{
FileReader fileReader = new FileReader("[Link]");
[Link]([Link]());
[Link]();
}
}
49. WAP to handle StringIndexOutOfBoundsException using throws clause: -
import [Link].*;
import [Link].*;
class Main3
{
public static void main(String args[]) throws StringIndexOutOfBoundsException
{
String name="Nobody";
[Link]([Link](7));
}
}
50. Design an applet to draw three concentric circles in different colors. Also draw four arcs
around the circles: -
import [Link].*;
import [Link].*;
/*<applet code="[Link]" width=300 height=300></applet>*/
public class Main3 extends Applet
{
public void paint(Graphics g) {
// Set the background color
setBackground([Link]);
// Draw a cylinder
[Link]([Link]);
int x1 = 100;
int y1 = 100;
int width = 50;
int height = 100;
int arcWidth = 50;
int arcHeight = 50;
[Link](x1, y1, width, height, arcWidth, arcHeight);
[Link](x1, y1, width, height, arcWidth, arcHeight);
}
}
55. Design an applet to draw a cone and circle inside a square in different colors: -
import [Link].*;
import [Link].*;
/*<applet code="[Link]" width=500 height=500></applet>*/
public class Main3 extends Applet
{
public void paint(Graphics g)
{
// Draw a square
[Link]([Link]);
[Link](50, 50, 260, 260); // Square
// Draw a cone
[Link]([Link]);
int[] xPoints = {150, 200, 100}; // Triangle points
int[] yPoints = {150, 250, 250};
[Link](xPoints, yPoints, 3); // Triangle
[Link](100, 250, 100, 50); // Rectangle
}
}
56. WAP to copy characters from one file to another: -
import [Link].*;
class cpy
{
public static void main(String args[]) throws IOException
{
FileInputStream f = new FileInputStream("C:\\Users\\chauk\\OneDrive\\Desktop\\[Link]");
FileOutputStream r = new FileOutputStream("C:\\Users\\chauk\\OneDrive\\Desktop\\
[Link]");
int i;
while((i=[Link]())!=-1)
{
[Link]((char)i);
}
[Link]("Data copied successfully");
}
}
57. WAP to write bytes from one file to another: -