Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

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

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java Main

Area of Room : 300

Volume of Room : 10500

2.Write a program which show the calling sequence of default constructor in multilevel
inheritance.
class Car

public Car()

System.out.println(" Car");

public void vehicleType()

System.out.println("Vehicle Type: Car");

class Honda extends Car

public Honda()

System.out.println("Honda");

2
Parth Lakhani JAVA Roll no=146

public void brand()

System.out.println("Brand: Honda");

public void speed()

System.out.println("Max: 110Kmph");

class Hondacity extends Honda

public Hondacity()

System.out.println("Honda Model: 800");

public void speed()

System.out.println("Max: 120Kmph");

public static void main(String args[])

Hondacity obj=new Hondacity();

obj.vehicleType();

obj.brand();

obj.speed();

OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p2.java

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java Hondacity

3
Parth Lakhani JAVA Roll no=146

Car

Honda

Honda Model: 800

Vehicle Type: Car

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
{

public static void main(String[] args) throws IOException


{
Student s=new Student();
s.getdata();
s.show();
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac pbaki.java
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main
Enter Name of Student
parth lakhani
Enter Roll No. of Student
146
Enter marks out of 100 of 1st subject
90
Enter marks out of 100 of 2nd subject
80
Enter marks out of 100 of 3rd subject
75
Roll No. = 146
Name = parth lakhani
Marks of 1st Subject = 90
Marks of 2nd Subject = 80
Marks of 3rd Subject = 75
Total Marks = 245
Percentage = 81.0%
4. Write a class Worker and derive classes DailyWorker and SalariedWorker from it. Every
worker has a name and a salary rate. Write method ComPay (int hours) to compute the
week pay of every worker. A Daily Worker is paid on the basis of the number of days s/he
works. The Salaried Worker gets paid the wage for 40 hours a week no matter what the
actual hours are. Test this program to calculate the pay of workers. You are expected to
use the concept of polymorphism to write this program.
class worker
{
String name;
int empno;
worker(int no,String n)
{
empno=no;
name=n;
}
void show()
{
System.out.println("E_number : "+empno);

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

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java Main

E_number : 101

E_name : Parth lakhani

Salary : 3600

6
Parth Lakhani JAVA Roll no=146

E_number : 102

E_name : krishna padsala

Salary : 5600

5. Write a program which show the Dynamic method dispatch.


class A

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

public static void main(String[] args)

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

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main

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

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main

Area of rectangle : 360.0

Area of triangle : 600.0

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

public void show()

System.out.println("B= " + x);

class C implements A

public void show()

System.out.println("C= " + x);

class main

public static void main(String args[])

A ob = new B();

ob.show();

ob = new C();

ob.show();

OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p7.java

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main

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

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main

Area of the rectangle= 3200.0

Area of the circle= 25450.2

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

class student implements s1


{
String name;
int div,sem;
void name(String n)
{
name=n;
}
public void division(int a)
{
div=a;
}
public void semester(int b)
{
sem=b;
}
void disp()
{
System.out.println("Name :"+name);
System.out.println("Division :"+div);
System.out.println("semester :"+sem);
}
}
class main
{
public static void main(String args[])
{
student s=new student();
s.name("Parth lakhani");
s.division(2);
s.semester(4);
s.disp();
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p9.java

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main

Name :Parth lakhani

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();

class c implements s1,s2

public void A()

System.out.println("part-A");

public void B()

System.out.println("part-B");

class main

public static void main (String[] args)

c ob1 = new c();

ob1.A();

ob1.B();

OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p10.java

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main

part-A

13
Parth Lakhani JAVA Roll no=146

part-B

11. Write a program which show partial implementation concept in interface.


interface Sub

void java();

void WD();

abstract class A implements Sub

public void java()

System.out.println("SUB-JAVA");

class B extends A

public void WD()

System.out.println("SUB_WD");

class main

public static void main(String []a)

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

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main


Enter first name-
parth patel
P. Patel
13. Write a java application which accept two strings. Merge both the string using
alternate characters Of each strings.
class mergeString
{
public static String merge(String s1, String s2)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < s1.length() || i < s2.length(); i++)
{
if (i < s1.length())
result.append(s1.charAt(i));
if (i < s2.length())
result.append(s2.charAt(i));
}
return result.toString();
}
public static void main(String[] args)
{
String s1 = "parth";
String s2 = "Lakhani";
System.out.println(merge(s1, s2));
}
}
OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p13.java
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java mergeString
pLaarkthhani
14. Write a java code which accept a string and display the string in reverse order.
import java.lang.*;
import java.io.*;
import java.util.*;

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

Upper case of reverse string : INAHKAL IAHBHSELMAK HTRAP


Enter a new string :
harshad kantilal maheta
The entered new string : harshad kantilal maheta
The appended string :harshad kantilal maheta INAHKAL IAHBHSELMAK HTRAP

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

public static void main (String[] args)

String str = "You Are Very Special For Me";

int firstIndex = str.indexOf('s');

System.out.println("First occurrence of char 's'" +" is found at : " + firstIndex);

int lastIndex = str.lastIndexOf('s');

System.out.println("Last occurrence of char 's' is" +" found at : " + lastIndex);

int first_in = str.indexOf('s', 10);

System.out.println("First occurrence of char 's'" +" after index 10 : " + first_in);

int last_in = str.lastIndexOf('s', 20);

System.out.println("Last occurrence of char 's'" +" after index 20 is : " + last_in);

int char_at = str.charAt(20);

System.out.println("Character at location 20: " + char_at);

OUTPUT
D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>javac p17.java

D:\PARTH\SYBCA SEM-4\JAVA\java Ass2>java main

First occurrence of char 's' is found at : -1

Last occurrence of char 's' is found at : -1

First occurrence of char 's' after index 10 : -1

Last occurrence of char 's' after index 20 is : -1

19
Parth Lakhani JAVA Roll no=146

Character at location 20: 32

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;

public void read()throws Exception

DataInputStream in=new DataInputStream(System.in);

System.out.println("Enter the name:");

name=in.readLine();

System.out.println("Enter the account number :");

acc=Long.parseLong(in.readLine());

System.out.println("Enter the account balance :");

bal=Long.parseLong(in.readLine());

public void disp()

System.out.println("**** Account Details ****");

System.out.println("Name :"+name);

System.out.println("Account number :"+acc);

System.out.println("Account Balance :"+bal);

class main

20
Parth Lakhani JAVA Roll no=146

public static void main(String ar[])

try

balance.account ob=new balance.account();

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

You might also like