Document From Chhaya Shah
Document From Chhaya Shah
Write Java Program to define a class, describe its constructor, overload the constructors and
instantiate its object.
import java.lang.*;
class student {
String name;
int reg_no;
int marks1, marks2, marks3;
// Null constructor
student() {
name = "Raju";
reg_no = 123456;
marks1 = 34;
marks2 = 46;
marks3 = 85;
}
// Parameterized constructor
student (String n, int r, int m1, int m2, int m3) {
name = n;
reg_no = r;
marks1 = m1;
marks2 = m2;
marks3 = m3;
}
// Copy constructor
student (student s) {
name = s.name;
reg_no = s.reg_no;
marks1 = s.marks1;
marks2 = s.marks2;
marks3 = s.marks3;
}
void display() {
System.out.println(name + "\t" + reg_no + "\t" + marks1 + "\t" + marks2 + "\t" + marks3);
}
}
21-B-CSE-029 1
public static void main(String[] args) {
student s1 = new student();
student s2 = new student("john", 253345, 45, 46, 87);
student s3 = new student(s1);
s1.display();
s2.display();
s3.display();
}
}
OUTPUT
Raju 123456 34 46 85
john 253345 45 46 87
Raju 123456 34 46 85
21-B-CSE-029 2
PROGRAM 2
Write a Java Program to define a class, define instance, methods for setting and Retrieving
values of instance variable and instantiate its object.
class emp {
String name;
int id;
String address;
void putData () {
System.out.println("Employee details are: ");
System.out.println("Name: " + name);
System.out.println("ID: " + id);
System.out.println("Address: " + address);
}
}
OUTPUT
21-B-CSE-029 3
PROGRAM 3
Write a Java Program to define a class, define instance, methods and overload them for
dynamic method invocation.
class add {
void display(int a, int b) {
int c = a + b;
System.out.println("The sum of " + a + " & " + b + " is " + c);
}
OUTPUT
21-B-CSE-029 4
PROGRAM 4
Write a Java Program to demonstrate use of sub class.
class parent {
int m;
void get_m(int m) {
this.m = m;
}
void display_m() {
System.out.println("This is from parent: m = " + m);
}
}
void get_n(int n) {
this.n = n;
}
void display_n(){
System.out.println("This is from child: n = " + n);
}
}
OUTPUT
21-B-CSE-029 5
PROGRAM 5
Write a Java Program to demonstrate use of nested class.
class outer {
int m = 10;
class inner {
int n = 20;
void display () {
System.out.println("m = " + m);
System.out.println("n = " + n);
}
}
}
OUTPUT
m = 10
n = 20
21-B-CSE-029 6
PROGRAM 6
Write a Java Program to implement array of objects.
public class ArrayOfObject {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
class Employee {
private String name;
private double salary;
OUTPUT
Harry 3424.0
Vishal 87966.0
Karan 16565.0
21-B-CSE-029 7
PROGRAM 7(A)
Write a java program to practice using string, class and its methods.
import java.lang.String;
public class StringAndClass {
public static void main(String[] args) {
String s1 = new String("gptgulbarga");
String s2 = "GPT GULBARGA";
21-B-CSE-029 8
}
}
OUTPUT
21-B-CSE-029 9
PROGRAM 7(B)
Write a java program to practice stringbuffer class and its methods.
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("This is my college");
sb.setCharAt(3, 'x');
System.out.println("After setting char 'x' at position 3: " + sb);
System.out.println("After appending: " + sb.append("ingulbarga"));
System.out.println("After inserting 'gpt ': " + sb.insert(19, "gpt "));
System.out.println("After deleting characters from index 19 to 22: " + sb.delete(19, 22));
}
}
OUTPUT
21-B-CSE-029 10
PROGRAM 8
Write a java program to implement vector class and its methods.
import java.util.Vector;
import java.util.Enumeration;
v.removeElement("oops");
v.removeElementAt(1);
21-B-CSE-029 11
OUTPUT
Vector Size: 6
Vector Capacity: 10
The elements of the vector are:
zero
one
two
oops
three
four
The first element is: zero
The last element is: four
The object 'oops' is found at position: 3
After removing 2 elements
Vector Size: 4
The elements of the vector are:
zero
two
three
four
21-B-CSE-029 12
PROGRAM 9
write a java program to implement wrapper classes and their methods.
import java.io.*;
import java.lang.Float;
try {
DataInputStream ds = new DataInputStream(System.in);
System.out.println("ENTER THE PRINCIPAL AMOUNT");
System.out.flush();
String sp = ds.readLine();
P = Float.valueOf(sp);
21-B-CSE-029 13
return sum;
}
}
OUTPUT
21-B-CSE-029 14
PROGRAM 10
Write a java program to implement inheritance and demonstrate use of method overriding.
class A {
void display() {
System.out.println("This is from class A");
}
}
class B extends A {
void display() {
System.out.println("This is from class B");
}
}
OUTPUT
15
21-B-CSE-029
PROGRAM 11
Write a java program to implement multilevel inheritance by applying various access controls
to its data members and methods.
import java.io.DataInputStream;
class Student {
private int rollno;
private String name;
DataInputStream dis = new DataInputStream(System.in);
void putrollno() {
System.out.println("Roll No = " + rollno);
System.out.println("Name = " + name);
}
}
void getmarks() {
try {
System.out.println("Enter marks:");
m1 = Integer.parseInt(dis.readLine());
m2 = Integer.parseInt(dis.readLine());
m3 = Integer.parseInt(dis.readLine());
} catch (Exception e) {}
}
void putmarks() {
System.out.println("m1 = " + m1);
System.out.println("m2 = " + m2);
System.out.println("m3 = " + m3);
}
}
21-B-CSE-029 16
class Result extends Marks {
private float total;
void compute_display() {
total = m1 + m2 + m3;
System.out.println("Total marks: " + total);
}
}
OUTPUT
Enter rollno:
246
Enter name:
Vishal
Enter marks:
45
65
31
Roll No = 246
Name = Vishal
m1 = 45
m2 = 65
m3 = 31
Total marks: 141.0
21-B-CSE-029 17
PROGRAM 12(A)
Write a program to demonstrate use of implementing interfaces.
import java.lang.*;
interface Area {
final static float pi = 3.14F;
float compute(float x, float y);
}
A = rect;
System.out.println("Area of rectangle = " + A.compute(10, 20));
A = cir;
System.out.println("Area of circle = " + A.compute(30, 0));
}
}
OUTPUT
21-B-CSE-029 18
PROGRAM 12(B)
Write a program to demonstrate use of extending interfaces.
import java.lang.*;
interface Area {
final static float pi = 3.14F;
float compute(float x, float y);
}
A = (Area) rect;
System.out.println("Area of rectangle = " + A.compute(10, 20));
A = cir;
System.out.println("Area of circle = " + A.compute(30, 0));
}
}
OUTPUT
21-B-CSE-029 19
PROGRAM 13
Write a Java program to implement the concept of importing classes from user defined
package and creating packages.
// creating package for importing class
package p1;
public class Student {
int regno;
String name;
class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.getdata(123, "xyz");
s.putdata();
}
}
OUTPUT
Regno = 123
Name = xyz
21-B-CSE-029 20
PROGRAM 14(A)
Write a program to implement the concept of threading by extending Thread Class.
import java.lang.Thread;
class A_1 extends Thread {
public void run() {
System.out.println("Thread A_1 is started:");
for (int i = 1; i <= 5; i++) {
System.out.println("\tFrom Thread A_1: i = " + i);
}
System.out.println("Exit from Thread A_1:");
}
}
21-B-CSE-029 21
OUTPUT
21-B-CSE-029 22
PROGRAM 14(B)
Write a program to implement the concept of threading by implementing Runnable Interface.
import java.lang.Runnable;
class X implements Runnable {
public void run() {
for (int i = 1; i < 10; i++) {
System.out.println("\tThread X: " + i);
}
System.out.println("End of Thread X");
}
}
Thread X: 1
Thread X: 2
Thread X: 3
Thread X: 4
Thread X: 5
Thread X: 6
Thread X: 7
Thread X: 8
Thread X: 9
End of Thread X
21-B-CSE-029 23
PROGRAM 15(A)
Write a program to implement the concept of Exception Handling using predefined
exception.
import java.lang.*;
class ExceptionHandle {
public static void main(String argv[]) {
int a = 10, b = 5, c = 5, x, y;
try {
x = a / (b - c);
} catch (ArithmeticException e) {
System.out.println("DIVISION BY ZERO");
}
y = a / (b + c);
System.out.println("y = " + y);
}
}
OUTPUT
DIVISION BY ZERO
y=1
21-B-CSE-029 24
PROGRAM 15(B)
Write a program to implement the concept of Exception Handling by creating user defined
exceptions.
import java.io.DataInputStream;
import java.lang.Exception;
class UserDefinedException {
public static void main(String a[]) {
int age;
DataInputStream ds = new DataInputStream(System.in);
try {
System.out.println("Enter the age (above 15 and below 25):");
age = Integer.parseInt(ds.readLine());
if (age < 15 || age > 25) {
throw new MyException("Age not in range");
}
System.out.println("The age is: " + age);
} catch (MyException e) {
System.out.println("Caught MyException");
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e);
}
}
}
OUTPUT
21-B-CSE-029 25