Professional Documents
Culture Documents
Programs Assignment - 2: Parth Lakhani Java Roll No 146
Programs Assignment - 2: Parth Lakhani Java Roll No 146
Programs Assignment – 2
1. Write a Java program to show that private member of a super class cannot be
accessed from derived classes.
class room
{
private int l,b;
room(int x,int y)
{
l=x; b=y;}
int area()
{
return(l*b);}
}
class class_room extends room
{
int h;
class_room(int x,int y,int z)
{
super(x,y);
h=z;
}
int volume()
{
return(area()*h);
}
}
class Main
{
public static void main(String args[])
1
Parth Lakhani JAVA Roll no=146
{
class_room cr=new class_room(12,25,35);
int a1=cr.area();
int v1=cr.volume();
System.out.println("Area of Room : "+a1);
System.out.println("Volume of Room : "+v1);
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p1.java
2.Write a program which show the calling sequence of default constructor in multilevel
inheritance.
class Car
public Car()
System.out.println(" Car");
public Honda()
System.out.println("Honda");
2
Parth Lakhani JAVA Roll no=146
System.out.println("Brand: Honda");
System.out.println("Max: 110Kmph");
public Hondacity()
System.out.println("Max: 120Kmph");
obj.vehicleType();
obj.brand();
obj.speed();
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p2.java
3
Parth Lakhani JAVA Roll no=146
Car
Honda
Brand: Honda
Max: 120Kmph
3. Create a class Student and derive class Result. Student class has name and rollNo.
Result class has sub1, sub2, sub3 and total_marks. Student class and Result class has their
own display method to display their parameters. Display the result of 5 students. Take
input using parameterized constructors. Use super keyword to call parent class
constructor and parent class method.
import java.lang.*;
import java.io.*;
class Student
{
String name;
int roll_no;
int sub1,sub2,sub3;
void getdata() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println ("Enter Name of Student");
name = br.readLine();
System.out.println ("Enter Roll No. of Student");
roll_no = Integer.parseInt(br.readLine());
System.out.println ("Enter marks out of 100 of 1st subject");
sub1 = Integer.parseInt(br.readLine());
System.out.println ("Enter marks out of 100 of 2nd subject");
sub2 = Integer.parseInt(br.readLine());
System.out.println ("Enter marks out of 100 of 3rd subject");
sub3 = Integer.parseInt(br.readLine());
}
void show()
{
int total = sub1+sub2+sub3;
float per = (total * 100) / 300;
System.out.println ("Roll No. = "+roll_no);
System.out.println ("Name = "+name);
System.out.println ("Marks of 1st Subject = "+sub1);
System.out.println ("Marks of 2nd Subject = "+sub2);
System.out.println ("Marks of 3rd Subject = "+sub3);
System.out.println ("Total Marks = "+total);
System.out.println ("Percentage = "+per+"%");
}
4
Parth Lakhani JAVA Roll no=146
class main
{
5
Parth Lakhani JAVA Roll no=146
System.out.println("E_name : "+name);
}
}
class dailyworker extends worker
{
int rate;
dailyworker(int no,String n,int r)
{
super(no,n);
rate=r;
}
void compay(int h)
{
show();
System.out.println("Salary : "+rate*h);
}
}
class salariedworker extends worker
{
int rate;
salariedworker(int no,String n,int r)
{
super(no,n);
rate=r;
}
int hour=40;
void compay()
{
show();
System.out.println("Salary : "+rate*hour);
}
}
class Main
{
public static void main(String args[])
{
dailyworker d=new dailyworker(101,"Parth lakhani",60);
salariedworker s=new salariedworker(102,"krishna padsala",140);
d.compay(60);
s.compay();
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p4.java
E_number : 101
Salary : 3600
6
Parth Lakhani JAVA Roll no=146
E_number : 102
Salary : 5600
void p1()
System.out.println("Part_A");
class B extends A
void p1()
System.out.println("Part_B");
class C extends B
void p1()
System.out.println("Part_C");
class main
A a = new A();
a.p1();
7
Parth Lakhani JAVA Roll no=146
B b = new B();
b.p1();
C c = new C();
c.p1();
A a2;
a2 = b;
a2.p1();
a2 = c;
a2.p1();
B b2 = c;
b2.p1();
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p5.java
Part_A
Part_B
Part_C
Part_B
Part_C
Part_C
6. Create abstract class Figure and its child classes Rectangle and Triangle. Figure class has
abstract area method which is implemented by Rectangle and Triangle class to calculate
area. Use run time polymorphism to calculate area or Rectangle and Triangle objects.
abstract class Shape
abstract class Shape
{
private double height;
private double width;
public void setValues(double height, double width)
{
this.height = height;
this.width = width;
}
public double getHeight()
8
Parth Lakhani JAVA Roll no=146
{
return height;
}
public double getWidth()
{
return width;
}
public abstract double getArea();
}
class Rectangle extends Shape
{
public double getArea()
{
return getHeight() * getWidth();
}
}
class Triangle extends Shape
{
public double getArea()
{
return (getHeight() * getWidth()) / 2;
}
}
class main
{
public static void main(String[] args)
{
Shape shape;
Rectangle r = new Rectangle();
shape = r;
shape.setValues(40, 9);
System.out.println("Area of rectangle : " + shape.getArea());
Triangle t = new Triangle();
shape = t;
shape.setValues(75,16);
System.out.println("Area of triangle : " + shape.getArea());
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p6.java
7. Write a program which show the Dynamic method dispatch using interface.
interface A
9
Parth Lakhani JAVA Roll no=146
int x = 50;
void show();
class B implements A
class C implements A
class main
A ob = new B();
ob.show();
ob = new C();
ob.show();
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p7.java
B= 50
10
Parth Lakhani JAVA Roll no=146
C= 50
8. Write a program in Java to show the usefulness of Interfaces as a place to keep constant
value of the program.
interface area
{
static final float pi=3.142f;
float compute(float a,float b);
}
class rect implements area
{
public float compute(float a,float b)
{
return(a*b);
}
}
class circle implements area
{
public float compute(float a,float b)
{
return(pi*a*a);
}
}
class main
{
public static void main(String args[])
{
rect ob=new rect();
circle c=new circle();
area a;
a=ob;
System.out.println("Area of the rectangle= "+a.compute(40,80));
a=c;
System.out.println("Area of the circle= "+a.compute(90,20));
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p8.java
9. Create an Interface having two methods division and modules. Create a class, which
overrides these methods.
interface s1
{
void division(int a);
void semester(int b);
}
11
Parth Lakhani JAVA Roll no=146
Division :2
semester :4
10. Write a program in Java which show that interface can inherit another interface. Take
interface A which have input method and another interface B which have display method.
Create one child class C which implements the both methods.
import java.io.*;
interface s1
12
Parth Lakhani JAVA Roll no=146
void A();
interface s2
void B();
System.out.println("part-A");
System.out.println("part-B");
class main
ob1.A();
ob1.B();
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p10.java
part-A
13
Parth Lakhani JAVA Roll no=146
part-B
void java();
void WD();
System.out.println("SUB-JAVA");
class B extends A
System.out.println("SUB_WD");
class main
B L= new B();
L.java();
L.WD();
14
Parth Lakhani JAVA Roll no=146
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p11.java
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main
SUB-JAVA
SUB_WD
12. Write a program to accept names from the user, print all the names which have the
surname ͞Patel͟ in it
import java.util.*;
class main
{
public static void printInitials(String str)
{
int len = str.length();
str = str.trim();
String t = "";
for (int i = 0; i < len; i++)
{
char ch = str.charAt(i);
if (ch != ' ')
{
t = t + ch;
}
else
{
System.out.print(Character.toUpperCase(t.charAt(0))+ ". ");
t = "";
}
}
String temp = "";
for (int j = 0; j < t.length(); j++)
{
if (j == 0)
temp = temp + Character.toUpperCase(t.charAt(0));
else
temp = temp + Character.toLowerCase(t.charAt(j));
}
System.out.println(temp);
}
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter first name- ");
String str= sc.nextLine();
printInitials(str);
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p12.java
15
Parth Lakhani JAVA Roll no=146
class Reversestring
{
public static void main(String[] args)
{
String input = "Parth Lakhani";
StringBuilder inputstring = new StringBuilder();
inputstring.append(input);
inputstring.reverse();
System.out.println(inputstring);
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p14.java
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java Reversestring
16
Parth Lakhani JAVA Roll no=146
inahkaL htraP
15. Write a program in Java to create a String object. Initialize this object with your full
name. Find the length of your name using the appropriate String method. Find whether
the character ͚a͛ is in your name or not; if yes find the number of times ͚a͛ appears in your
name. Print locations of occurrences of ͚a͛ .
class A
{
String name;
A(String n)
{
name=n;
}
void disp()
{
System.out.println("Name:"+name);
int c=0;
int len=name.length();
for(int i=0;i<len;i++)
{
if(name.charAt(i)=='A'||name.charAt(i)=='a')
{
c++;
System.out.println("number of occurance :"+c);
System.out.println("Possition :"+(i+1));
}
if(c==0)
System.out.println("there is no 'A' available in the string");
}
}
}
class main
{
public static void main(String ar[])
{
A ob1=new A("jay");
ob1.disp();
A ob2=new A("parth");
ob2.disp();
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p15.java
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main
Name:jay
there is no 'A' available in the string
number of occurance :1
Possition :2
Name:parth
there is no 'A' available in the string
number of occurance :1
Possition :2
17
Parth Lakhani JAVA Roll no=146
16. Write a program in Java for String handling which performs the following using StringBuffer
class:
i) Checks the capacity of StringBuffer objects.
ii) Reverses the contents of a string given on console and converts the resultant string in upper
case. iii) Reads a string from console and appends it to the resultant string of ii.
import java.io.*;
class main
{
public static void main(String s[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1,s2,s3,s4,s5;
int i,l;
s2=" ";
System.out.println("\nEnter the string : ");
s1=br.readLine();
System.out.println("\nEntered string is : "+s1);
System.out.println("\nlength of the string : "+s1.length());
StringBuffer sb=new StringBuffer(s1);
System.out.println("\nCapacity of string buffer : "+sb.capacity());
l=s1.length();
if(l==0)
System.out.println("\nEmpty String");
else
{
for(i=l-1;i>=0;i--)
{
s2=s2+s1.charAt(i);
}
System.out.println("\nThe reversed string :"+s2);
s3=s2.toUpperCase();
System.out.println("\nUpper case of reverse string :"+s3);
System.out.println("\nEnter a new string : \t");
s4=br.readLine();
System.out.println("\nThe entered new string : "+s4);
StringBuffer sb1=new StringBuffer(s4);
s5=sb1.append(s3).toString();
System.out.println("\nThe appended string :"+s5);
}
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p16.java
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main
Enter the string :
parth kamleshbhai lakhani
Entered string is : parth kamleshbhai lakhani
length of the string : 25
Capacity of string buffer : 41
The reversed string : inahkal iahbhselmak htrap
18
Parth Lakhani JAVA Roll no=146
17. Write a program for searching a given sub string from the given sentence. Also
calculate number of times given sub string occur in given sentence.
import java.io.*;
class main
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p17.java
19
Parth Lakhani JAVA Roll no=146
18. Write a program to make a package Balance in which has Account class with
Display_Balance method in it. Import Balance package in another program to access
Display_Balance method of Account class3
package p1;
import java.io.*;
class account
long acc,bal;
String name;
name=in.readLine();
acc=Long.parseLong(in.readLine());
bal=Long.parseLong(in.readLine());
System.out.println("Name :"+name);
class main
20
Parth Lakhani JAVA Roll no=146
try
ob.read();
ob.disp();
catch(Exception e)
System.out.println(e);
19. Write a program which show the different package non sub class concept. Check the
result of different access specifiers – private, public, protected and default.
package p1;
public class A
{
int a=5;
public int b=10;
private int c=15;
protected int d=20;
}
package p2;
import p1.A;
public class B
{
public static void main(String args[])
{
A ob = new A();
System.out.println(ob.a);
System.out.println(ob.b);
System.out.println(ob.c);
System.out.println(ob.d);
}
}
20. Write a program which show the different package sub class concept. Check the result
of different access specifiers -– private, public, protected and default.
21
Parth Lakhani JAVA Roll no=146
package p1;
public class A
{
int a=5;
public int b=10;
private int c=15;
protected int d=20;
}
package p3;
import p1.A;
public class C extends A
{
public static void main(String args[])
{
C ob = new C();
System.out.println(ob.a);
System.out.println(ob.b);
System.out.println(ob.c);
System.out.println(ob.d);
}
}
22