Assignment-2 1. Write A Java Program To Show That Private Member of A Super Class Cannot Be Accessed From Derivedclasses
Assignment-2 1. Write A Java Program To Show That Private Member of A Super Class Cannot Be Accessed From Derivedclasses
ASSIGNMENT-2
1. Write a Java program to show that private member of a super class cannot be accessed
from derivedclasses.
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[])
{
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);
}
}
1
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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");
}
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[])
2
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
Hondacity obj=new Hondacity();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
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();
3
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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+"%");
}
}
class main
{
public static void main(String[] args) throws IOException
{
Student s=new Student();
s.getdata();
s.show();
}
}
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()
{
4
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
System.out.println("E_number : "+empno);
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();
5
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
}
}
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();
B b = new B();
b.p1();
C c = new C();
c.p1();
A a2;
a2 = b;
a2.p1();
a2 = c;
a2.p1();
6
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
B b2 = c;
b2.p1();
}
}
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.
7
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
}
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());
}
}
7. Write a program which show the Dynamic method dispatch using interface.
interface A
{
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[])
8
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
A ob = new B();
ob.show();
ob = new C();
ob.show();
}
}
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));
9
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
}
}
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);
}
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("Avani Joshi");
s.division(2);
10
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
s.semester(4);
s.disp();
}
}
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
{
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();
}
}
11
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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();
}
}
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++)
12
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
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);
}
}
13. Write a java application which accept two strings. Merge both the string using
alternate characters Of each strings.
Eg. "Hello" and "Good".
Result should be, "HGeololdo".
class mergeString
{
public static String merge(String s1, String s2)
{
StringBuilder result = new StringBuilder();
13
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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 = "Avani Joshi";
StringBuilder inputstring = new StringBuilder();
inputstring.append(input);
inputstring.reverse();
System.out.println(inputstring);
}
}
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' .
14
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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("karan");
ob1.disp();
A ob2=new A("avani");
ob2.disp();
}
}
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.
15
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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);
}
}
}
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.
16
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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);
}
}
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 class.
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()
17
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
System.out.println("**** Account Details ****");
System.out.println("Name :"+name);
System.out.println("Account number :"+acc);
System.out.println("Account Balance :"+bal);
}
}
class main
{
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[])
18
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
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.
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;
19