1.
Write a program in Java to perform reverse of entered number and check
whether entered number is odd or even using switch case.
package com.practice;
class Main {
public static void main(String[] args) {
int num = 2344, rev = 0;
System.out.println("Entered no: "+num);
while(num != 0){
int digit = num %10;
rev = (rev + digit )*10;
num = num/10;
}
rev=rev/10;
System.out.println("Reversed no: "+rev);
int temp = rev % 2;
switch(temp){
case 0:
System.out.println("even");
break;
case 1:
System.out.println("odd");
break;
}
}
}
2. Write a program in Java to create class Employee with methods getdata() and
putdata() and instantiate its object.
import java.util.*;
class Employee {
int emp_id;
String name;
String Dept;
public void getdata(){
Scanner s = new Scanner(System.in);
System.out.print("Enter Employee Id : ");
emp_id = s.nextInt();
System.out.print("Enter Employee Name : ");
name = s.next();
System.out.print("Enter Employee Department : ");
Dept = s.next();
s.close();
}
public void putdata(){
System.out.println("Employee Id is : "+emp_id);
System.out.println("Employee Name is : "+name);
System.out.println("Employee Department is : "+Dept);
}
public static void main(String args[])
{
Employee a = new Employee();
a.getdata();
a.putdata();
}
}
3. Write a program for Factorial using Function.
import java.util.Scanner;
public class Factorial {
static void fact() {
Scanner s = new Scanner(System.in);
int fact = 1, a;
System.out.println("enter no of which you want factorial : ");
a = s.nextInt();
for (int i = 1; i <= a; i++) {
fact = fact * i;
}
System.out.println("FActorial of " + a + " is : " + fact);
s.close();
}
public static void main(String[] args) {
fact();
}
}
4. Write a program for Wrapper Class.(Autoboxing)
import java.util.Vector;
//primitive to object auto boxing
public class J_10 {
public static void main(String[] args) {
Vector v = new Vector<>(5, 2);
int a = 2;
double b = 3.32456789;
float c = 4.65f;
// v.add(a);
Integer x = new Integer(a);
Double y = new Double(b);
Float z = new Float(c);
v.add(x);
v.add(y);
v.add(z);
System.out.println(v);
5. Write a program for Wrapper Class.(Unboxing)
//object to primitive unboxing
public class J_11 {
public static void main(String[] args) {
Integer x = new Integer(100);
Double y = new Double(5.765455);
Float z = new Float(9.324f);
int p = Integer.valueOf(x);
double q = Double.valueOf(y);
float r = Float.valueOf(z);
System.out.println(p);
System.out.println(q);
System.out.println(r);
}
}
6. Write a program for Parameterised Constructor
//Parameterized constructor.
class J_3 {
int id;
String name;
J_3(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
J_3 s1 = new J_3(111, "Karan");
J_3 s2 = new J_3(222, "Aryan");
s1.display();
s2.display();
}
}
7. Write a program in Java to apply string methods : equals(), compareTo(),
charAt(), toUpperCase() over entered strings from user.
//String methods
public class j_12 {
public static void main(String[] args) {
String s = "sujal";
//equal
System.out.println(s.equals("sujal"));//returns true or false
//compare
System.out.println(s.compareTo("suja"));// returns zero if equal,1(size) if
less size,-1(size) if big size
//charat
System.out.println(s.charAt(3));
//uppercase
System.out.println(s.toUpperCase());
}
}
8. Write a program in Java to apply String methods : equalsIgnoreCase(),
compareTo(), indexOf(), toLowerCase() over entered strings from user.
//String methods
public class j_12 {
public static void main(String[] args) {
String s = "sujal";
//equal
System.out.println(s.equals("sujal"));//returns true or false
//compare
System.out.println(s.compareTo("suja"));// returns zero if equal,1(size) if
less size,-1(size) if big size
//charat
System.out.println(s.charAt(3));
//uppercase
System.out.println(s.toUpperCase());
//compare
System.out.println(s.compareTo("Prasad"));// returns zero if equal,1(size)
if less size,-1(size) if big size
//charat
System.out.println(s.charAt(3));
//uppercase
System.out.println(s.toUpperCase());
//equalegnorcase
System.out.println(s.equalsIgnoreCase("Prasad"));
//indexof
System.out.println(s.indexOf("P"));
//tolowercase
System.out.println(s.toLowerCase());
}
}
9. Write a program in Java to implement a vector and add five elements of type
Integer, Character, Boolean, Long, Float into that vector. Also display vector
elements.
import java.util.Vector;
public class J_10 {
public static void main(String[] args) {
Vector v = new Vector<>(5, 2);
int a = 2;
double b = 3.32456789;
float c = 4.65f;
char d = 'a';
long e = 2312414;
// v.add(a);
Integer t = new Integer(a);
Double w = new Double(b);
Float x = new Float(c);
Character y = new Character(d);
Long z = new Long(e);
v.add(t);
v.add(w);
v.add(x);
v.add(y);
v.add(z);
System.out.println(v);
10. Write a program in Java to apply Vector methods : addElement(), elementAt(),
firstElement(), removeElementAt() over vector v.
import java.util.Vector;
public class J_13 {
public static void main(String[] args) {
Vector v = new Vector<>(5, 2);
int a = 2;
double b = 3.32456789;
float c = 4.65f;
// v.add(a);
Integer x = new Integer(a);
Double y = new Double(b);
Float z = new Float(c);
String V = "Sujal";
v.add(x);
v.add(y);
v.add(z);
v.add(V);
System.out.println(v);
//add element
v.addElement("ThisWillBeAdded");
//element at
System.out.println(v.elementAt(3));
//first element
System.out.println(v.firstElement());
//remove element at
v.removeElementAt(1);
System.out.println(v);
}
}
11. Write a program in Java to implement single inheritance with super class
Student and sub class Marks.
class Student {
String name="Abhinav Ingle";
}
class Displayname extends Student
{
void display(){
String name="Rupesh Bhadane";
System.out.println(name);
System.out.print(super.name);
}
}
class Tester extends Student
{
public static void main(String args[])
{
Displayname D = new Displayname();
D.display();
}
}
12. Write a program in Java to create package with class student. Write another
program to import created package(class and methods).
13. Write a program in Java to handle Arithmetic Exception.
import java.util.*;
import java.io.*;
public class DivideException {
public static void main(String args[]){
int a,b,c;
Scanner s = new Scanner(System.in);
System.out.println("Enter first no: ");
a = s.nextInt();
System.out.println("Enter Second no: ");
b = s.nextInt();
try{
c = a/b;
System.out.println("Division is"+c);
}catch(ArithmeticException obj){
System.out.println(obj);
System.out.println("Invalid Input");
}
}
}
14. Write a program in Java to throw user defined exception if entered number is
Negative.
import java.io.*;
import java.util.*;
class NegativeException extends Exception{
NegativeException(String str)
{
super(str);
}
}
public class Negexc {
public static void main(String args[])
{
int num;
Scanner s = new Scanner(System.in);
System.out.println("Enter a Number: ");
num = s.nextInt();
try{
if(num<0)
{
throw new NegativeException("Number is Negative");
}
else
{
System.out.println("Number is Positive");
}
}catch(NegativeException N)
{
System.out.println(N);
}
}
}
15. Write a program in Java to create two threads: one will print even numbers
and other odd number from 1 to 20.
import java.lang.*;
class Even extends Thread
{
public void run()
{
for(int i=2;i<=20;i=i+2)
{
System.out.println("\t Even thread :"+i);
}
}
}
class Odd extends Thread
{
public void run()
{
for(int i=1;i<20;i=i+2)
{
System.out.println("\t Odd thread :"+i);
}
}
}
class EvenOdd
{
public static void main(String args[])
{
new Even().start();
new Odd().start();
}
}
16. Write a program in Java to draw smiley shape using applet.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Smiley extends Applet {
@Override
public void paint(Graphics g) {
g.setColor(Color.yellow);
g.fillOval(50, 100, 100,100);
g.setColor(Color.black);
g.fillOval(65, 120, 20, 20);
g.fillOval(115, 120, 20, 20);
g.drawArc(75, 145, 50, 40, 0, -180);
}
}
// <Applet code = "Smiley" width = "480" height = "300"></Applet>
17. Write a program in java to draw three concentric circle.
import java.applet.*;
import java.awt.*;
public class ConcentricCircle extends Applet{
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(50, 100, 400, 400);
g.setColor(Color.yellow);
g.fillOval(100, 150, 300, 300);
g.setColor(Color.CYAN);
g.fillOval(150, 200, 200, 200);
}
}
18. Write a program to copy contents of one File into another using Byte stream.
1. import java.io.*;
2. import java.util.*;
3. class Copyfile {
4. public static void main(String arg[]) throws Exception {
5. Scanner sc = new Scanner(System.in);
6. System.out.print("Provide source file name :");
7. String sfile = sc.next();
8. System.out.print("Provide destination file name :");
9. String dfile = sc.next();
10. FileReader fin = new FileReader(sfile);
11. FileWriter fout = new FileWriter(dfile, true);
12. int c;
13. while ((c = fin.read()) != -1) {
14. fout.write(c);
15. }
16. System.out.println("Copy finish...");
17. fin.close();
18. fout.close();
19. }
20. }
19. Write a program in Java to create class Student with methods getdata() and
putdata() and instantiate its object.
import java.util.*;
public class StudentEx {
String name;
int rollno;
String Class;
Scanner s = new Scanner(System.in);
void getdata()
{
System.out.println("Enter Name of Student: ");
name = s.nextLine();
System.out.println("Enter Class Of Student: ");
Class = s.nextLine();
System.out.println("Enter Roll No of Student: ");
rollno = s.nextInt();
}
void putdata()
{
System.out.println("Name of Student: "+name);
System.out.println("Roll No of Student: "+rollno);
System.out.println("Class Of Student: "+Class);
}
public static void main(String args[])
{
StudentEx obj = new StudentEx();
obj.getdata();
obj.putdata();
}
}
20. Write a program in Java to create two threads: one will print numbers 1 to 20
and other reverse number from 20 to 1.
import java.util.*;
import java.io.*;
class Orginal extends Thread{
public void run(){
for(int i=1; i<=20;i++)
{
System.out.println(i);
}
}
}
class Reverse extends Thread{
public void run(){
for(int i=20; i>=1;i--)
{
System.out.println(i);
}
}
}
class RevThread {
public static void main(String args[])
{
new Orginal().start();
new Reverse().start();
}
}
21. Write a program in Java to perform reverse of entered number and check
whether entered number is positive or negative using switch case.
import java.util.Scanner;
class Poscheck{
public static int positive(int num){
if(num>0){
return 1;
}
else if(num<0){
return 0;
}
else{
return -1;
}
}
public static void main(String args[]){
int rev=0;
int n;
Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("\nEnter the integer number: ");
int num=scan.nextInt();//get input from the user for num
int result=positive(num);
switch(result){
case 0://check num is negative
System.out.print(num+" is negative"+"\n");
break;
case 1://check num is positive
System.out.print(num+" is positive"+"\n");
break;
default:
System.out.print("the given number is zero");
break;
while(num!=0)
{
n = num%10;
rev = rev *10+n;
num = num/10;
}
System.out.println("Reverse Number is:"+rev);
}
}
22. Write a program in Java to implement a vector and add five elements of type
Integer, Character, Boolean, Long, Float into that vector. Also display vector
elements.
import java.util.*;
import java.lang.*;
public class VectorOP {
public static void main(String args[])
{
Vector V = new Vector();
Integer a = new Integer("10");
Character b = new Character('j');
Boolean c = new Boolean("True");
Long d = new Long("1223456");
Float e = new Float("23.22");
V.add(a);
V.add(b);
V.add(c);
V.add(d);
V.add(e);
System.out.println(V);
}
}
19.Write a program to implement following interface with multiple
inheritance.