Java Program
Java Program
class Main {
public static void main(String[] args) {
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;
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);
}
}
class J_3 {
int id;
String name;
J_3(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name);
}
s1.display();
s2.display();
}
}
//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());
}
}
//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);
//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).
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);
}
}
}
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);
}
}