Java Journal Program Unit - 2

//PROGRAM - 1


/*

Create class box and box3d. box3d is extended class of box. The above

two classes going to full fill following requirement


-> Include constructor.

-> set value of length, breadth, height

-> Find out area and volume.


Note: Base class and sub classes have respective methods and instance

variables.

*/


class Box

{

   private int  length;

   private int breadth;

  

   public Box()

  {

    length=0;

    breadth=0;

  }


  public Box(int x,int y)

  {

    length=x;

    breadth=y;

  }

  public void setval(int x,int y)

  {

     length=x;

     breadth=y;

  }


  public int area()

  {

    return(length * breadth);

  }

}


class Box3d extends Box

{

   private int height;

 

   public Box3d()

  {

     // it's parent length and breadth 

     // super keyword use to that parent

     // class accession

  super();

    height=0;

  }

 public Box3d(int x,int y,int z)

  {

    super(x,y);

    height=z;

  }

  public void setval(int x,int y,int z)

  {

   super.setval(x,y);

   height=z;

  }

  public int volume()

  {

   return (super.area() * height);

  }

}


class program1

{

   public static void main(String arg[])

  {

      Box b1=new Box();

      Box3d b2=new Box3d(12,34,18);

      b1.setval(25,30);


     System.out.println("The Area of B1 is=" +b1.area());

     System.out.println("The Volume of B2 is=" +b2.volume());

 }

}

/*

OUTPUT:

The Area of B1 is=750

The Volume of B2 is=7344

*/

___________________________________

//PROGRAM - 3

/*

Create a class Circle that subclasses from Shape. This class should define

a private double field to store the radius of the circle

*/


class circle

  {

    private double radius;

    double red,red1;


    

    void getRadius(int r)

    {

        radius=r;

        if(r<0)

         {

           System.out.println("Circle is zero");

         }

         else

          {

            red=r*r/2;

           }

          System.out.println("Radius=" +red);

    }

   void peremeter(int y)

   {

     radius=y;

     if(y!=0)

     {

       red1=2*3.14*y;

     }

    System.out.println("Radius=" +red1);

 }

 }


class Store extends circle

{

 

}


class program3

{

   public static void main(String arg[])

   {

      Store obj=new Store();

      obj.getRadius(9);

      obj.peremeter(4);

   }

}

/*

OUTPUT:

Radius=40.0

Radius=25.12

*/                 

___________________________________


//PROGRAM - 4

/*

Write a java program to create an abstract class named Shape that

contains two integers and an empty method named print Area (). Provide

three classes named Rectangle, Triangle and Circle such that each one of

the classes extends the class Shape. Each one of the classes contains

only the method print Area () that prints the area of the given shape.

*/


import java.util.*;


abstract class shape

{

   int length,height;

   abstract void printArea();

   Scanner obj=new Scanner(System.in);

}


class Rectangle extends shape

{

  void printArea()

  {

    System.out.println("Finding the Area of Rectangle:");

    System.out.print("Enter length of rectangle=");

    length=obj.nextInt();

    System.out.print("Enter height of rectangle=");

    height=obj.nextInt();

    System.out.println("The Area of Rectangle is= "+length * height + "\n");

  }

}


class Triangle extends shape

{

   void printArea()

  {

    System.out.println("Finding the Area of Triangle:");

    System.out.print("Enter length of Triangle=");

    length=obj.nextInt();

    System.out.print("Enter height of Triangle=");

    height=obj.nextInt();

    System.out.println("The Area of Triangle is= "+length * height/2 + "\n");

  }

}


class circle extends shape

{

    void printArea()

  {

    System.out.println("Finding the Area of Circle:");

    System.out.print("Enter length of Circle=");

    length=obj.nextInt();

    System.out.print("Enter height of Circle=");

    height=obj.nextInt();

    System.out.println("The Area of Circle is= "+3.14f+length * height + "\n");

  }

}


class program4

{

  public static void main(String arg[])

  {

    Rectangle obj=new Rectangle();

    obj.printArea();

    

    Triangle obj1=new Triangle();

    obj1.printArea();

  

    circle obj2=new circle();

    obj2.printArea();

  }

}

/*

OUTPUT:

Finding the Area of Rectangle:

Enter length of rectangle=3

Enter height of rectangle=3

The Area of Rectangle is= 9


Finding the Area of Triangle:

Enter length of Triangle=2

Enter height of Triangle=3

The Area of Triangle is= 3


Finding the Area of Circle:

Enter length of Circle=4

Enter height of Circle=5

The Area of Circle is= 3.1420

*/ 

___________________________________

//PROGRAM - 5


/*

Write a program to give example for multiple 

inheritance in Java using interface.

*/


interface A

{

  void callA();

}


interface B

{

  void callB();

}


class c implements A,B

{

  public void callA()

  {

   System.out.println("Parent class A is called");

  }

  public void callB()

  {

   System.out.println("Parent class B is called");

  }

  void callc()

  { 

   System.out.println("Child class C is called");

  }

}


class program5

{

  public static void main(String arg[])

  {

     c obj=new c();

     obj.callA();

     obj.callB();

     obj.callc();

  }

}

/*

OUTPUT:

Parent class A is called

Parent class B is called

Child class C is called

*/

___________________________________

//PROGRAM - 6

/*

Write a java program that implements 

educational hierarchy using inheritance.


    IMAGE

*/


import java.util.*;

class Office

{

  int empno,salary;

  String empnm;


  Scanner obj =new Scanner(System.in);

  

  void getvalue()

  {

    System.out.print("Enter the Emp No=");

    empno=obj.nextInt();

    System.out.print("Enter the Emp Name:-");

    empnm=obj.next();

    System.out.print("Enter the Emp Salary=");

    salary=obj.nextInt();

  }

}


class Teaching extends Office

{

  String Designation;

  

  void setvalue()

  {

    getvalue();

    System.out.print("Enter your Designation here:-");  

    Designation=obj.next();


    System.out.println("This is the Data of Teaching Employees:");

    System.out.println("Employess No="+empno);

    System.out.println("Employess Name="+empnm);

    System.out.println("Employess Designation="+Designation);

    System.out.println("Employess Salary="+salary);

  }

}


class NonTeaching extends Office

{

  String Designation;

  

  void setvalue()

  {

    getvalue();

    System.out.print("Enter your Designation here:-");  

    Designation=obj.next();

    System.out.println("This is the Data of Non-Teaching Employees:");

    System.out.println("Employess No="+empno);

    System.out.println("Employess Name="+empnm);

    System.out.println("Employess Designation="+Designation);

    System.out.println("Employess Salary="+salary);

  }

}


class program6

{

  public static void main(String arg[])

  {

    Teaching obj=new Teaching();    

    obj.setvalue();


    NonTeaching obj2=new NonTeaching();

    obj2.setvalue();



 }

/*

OUTPUT:

Enter the Emp No=7

Enter the Emp Name:-milan

Enter the Emp Salary=1000000

Enter your Designation here:-ok

This is the Data of Teaching Employees:

Employess No=7

Employess Name=milan

Employess Designation=ok

Employess Salary=1000000

Enter the Emp No=8

Enter the Emp Name:-bakotra

Enter the Emp Salary=1000000

Enter your Designation here:-ok

This is the Data of Non-Teaching Employees:

Employess No=8

Employess Name=bakotra

Employess Designation=ok

Employess Salary=1000000

*/  

___________________________________

//PROGRAM - 7


/*

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

DailyWorker is paid on the basis of number of days s/he work. The

SalariedWorker gets paid the wage for 40 hours a week no matter what

actual hours is. Test this program to calculate the pay of workers. You

are expected to use concept of polymorphism to write this program.

*/





class worker

{

  String name;

  int empno;


  worker(int no,String n)  //parameterized constuctor is called

  { 

    empno=no;

    name=n;

  }


  void show()

  {

   System.out.println("\n---Employess Basic Data is below---");

   System.out.println("Employee number : "+empno);

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

 }


}



class dailyworker extends worker

{

 int rate;

 dailyworker(int no,String n,int r)

 {

  super(no,n);  //goes to parent/super class set empno and name

  rate=r;

}



void compay(int h)

{

   show();  //parent method show is called emp data is display

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

 }


}

//--------- main -----------

class program7

{

 public static void main(String arg[])

 {

   dailyworker d=new dailyworker(24,"abc",60);

   salariedworker s=new salariedworker(26,"xyz",40);

   d.compay(45);

   s.compay();

 }

}

/*

OUTPUT:

---Employess Basic Data is below---

Employee number : 24

Employee name : abc

Salary : 2700


---Employess Basic Data is below---

Employee number : 26

Employee name : xyz

Salary : 1600

*/

___________________________________

//PROGRAM - 8

/*

Write a Java Program to implement inheritance and demonstrate use of

method overriding.

*/



class Human

{

   //Overridden method

   public void eat()

   {

      System.out.println("Human is eating");

   }

}


class Boy extends Human

{

   //Overriding method

    public void eat()

    {

      System.out.println("Boy is eating");

    }

}


class Overridden

{

    public static void main( String args[])

   {

      Boy obj = new Boy();

      //This will call the child class version of eat()

      obj.eat();

   }

}

/*

OUTPUT:

The square of 64 is 4096

*/

___________________________________

//PROGRAM - 9

/*

Write a program to identify the accessibility of a variable by means of

different access specifiers within and outside package.

*/


class Data

 {

    private String name;


    // getter method

    public String getName()

    {

        return this.name;

    }


    // setter method

    public void setName(String name)

    {

        this.name= name;

    }

}

class program9

  {

     public static void main(String arg[])

    {

        Data d = new Data();


        // access the private variable using the getter and setter

        d.setName("Programiz");

        System.out.println(d.getName());

    }

}

/*OUTPUT

Programiz

*/

___________________________________

//PROGRAM - 10

/*

Create an outer class with a function display, 

again create another class inside the outer 

class named inner with a function called 

display and call the two functions in the 

main class.

*/



class Outer

 {

  String so = ("This is Outer Class");


  void display()

  {

    System.out.println(so);

  }


  void test()

 {

  Inner inner = new Inner();

  inner.display();

 }


//this is an inner class


class Inner

 {

  String si =("This is inner Class");

  void display()

 {

   System.out.println(si);

  }

 }


}



class program10

{

  public static void main(String arg[])

 {

  Outer outer = new Outer();

  outer.display();

  outer.test();

 }

}

/*

OUTPUT:

This is Outer Class

This is inner Class

*/

__________________________________

//PROGRAM - 11

/*

Consider trunk calls of a telephone exchange. A trunk call can be

ordinary, urgent or lightning call. The charges depend on the duration

and the type of the call. Writ a program-using concept of polymorphism

in Java to calculate the charges.

*/

import java.util.*;


class Calls

{

 float dur;

 String type;


 float rate()

 {

   if(type.equals("urgent"))

     return 4.5f;


   else if(type=="lightning")

     return 3.5f;

   

   else

     return 3f;

 }


}



class Bill extends Calls

{

  float amount;

  void read()

  {

    Scanner input=new Scanner(System.in);

    System.out.print("Enter Call Type(urgent,lightning,ordinary): ");

    type=input.next();

    System.out.print("Enter Call duration:");

    dur=input.nextFloat();

  }


  void calculate()

  {

   if(dur<=1.5)

   {

    amount=rate()*dur+1.5f;

  }


  else if(dur<=3)

  {

    amount=rate()*dur+2.5f;

  }


  else if(dur<=5)

  {

    amount=rate()*dur+4.5f;

  }


  else

  {

    amount=rate()*dur+5f;

  }


}


void print()

{

  System.out.println(" Call Type : "+type); 

  System.out.println(" Duration : "+dur);

  System.out.println(" Charge: "+amount);

}


}



class program11

{

  public static void main(String arg[])

  {


    Bill b=new Bill();

    b.read();

    b.calculate();

    b.print();

  }

}

/*

OUTPUT:

Enter Call Type(urgent,lightning,ordinary): urgent

Enter Call duration:22

 Call Type : urgent

 Duration : 22.0

 Charge: 104.0

*/

___________________________________

//PROGRAM - 13

/*

Write a program to create interface named test. In this interface the

member function is square. Implement this interface in arithmetic class.

Create one new class called ToTestInt in this class use the object of

arithmetic class.

*/


//interface use 


interface test

{

int square();

}

 

class arithmetic implements test

{

int b;

 

arithmetic(int x)

{

b = x;

}

 

public int square()

{

return (b*b);

}

 

}

class ToTestInt

{

public int return_ans(int x)

{

arithmetic a = new arithmetic(x);  

//this method value go to class arithmetic constuctor 

return a.square();

}

}

class program13

{

public static void main(String arg[])

{

ToTestInt x= new ToTestInt();

System.out.println("\nThe square of 64 is "+x.return_ans(64)); 

//this value go to method of totestint ->return_ans

}

}

/*

OUTPUT:

The square of 64 is 4096

*/

___________________________________

//PROGRAM - 14

/*

Write a program in java which implement interface Student which has

two methods Display_Grade and Atrendance for PG_Students and

UG_Students( PG_Students and UG_Students are two different classes

for Post Graduate and Under Graduate students respectively).

*/

//using interface


interface Student

{

  void Display_Grade();

  void Display_Atten();

}



class PG_Student implements Student

{

 String name, grade;

 int m1, m2, m3, attendence, total;


 PG_Student(String name, int m1, int m2, int m3, int attendence)

 {

  this.name = name;

  this.m1 = m1;

  this.m2 = m2; 

  this.m3 = m3;

  this.attendence = attendence;

}

void Display()

{

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

  System.out.println("Marks are " + m1 + " " + m2 + " " + m3);

}


public void Display_Atten()

{

 System.out.println("The attendence is " + attendence);

}


public void Display_Grade()

{

  total = m1 + m2 + m3;

  if (total > 250)

  {

    grade = "A";

  }

  else if (total < 250)

  {

   grade = "B";

 } 

 else if (total < 200)

 {

  grade = "C";

else

{

 grade = "D";

}


System.out.println("The Grade is " + grade);


}

}


class UG_Student implements Student

{

  String name, grade;

  int m1, m2, m3, attendence, total;


  UG_Student(String name, int m1, int m2, int m3, int attendence)

  {

    this.name = name;

    this.m1 = m1;

    this.m2 = m2;

    this.m3 = m3;

    this.attendence = attendence;

  }


  void Display()

  {

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

    System.out.println("Marks are " + m1 + " " + m2 + " " + m3);

  }


  public void Display_Atten()

  {

    System.out.println("The attendence is " + attendence);

  }

  public void Display_Grade()

  {

   total = m1 + m2 + m3;

   if (total > 300)

   {

    grade = "S";

  }

  else if (total > 250)

  {

    grade = "A";

  }

  else if (total < 250)

  {

    grade = "B";

  }


  else if (total < 200)

  {

    grade = "C";

  }

  else

  {

    grade = "D";

  }


  System.out.println("The Grade is " + grade);

}

}

class program14


{

  public static void main(String arg[]) 

  {

   PG_Student pg = new PG_Student("Milan", 95, 90, 87, 88);

   pg.Display();

   pg.Display_Atten();

   pg.Display_Grade();


   UG_Student ug = new UG_Student("Bakotra", 95, 88, 93, 87);

   ug.Display();

   ug.Display_Atten();

   ug.Display_Grade();

 }

}

/*

Name is Milan

Marks are 95 90 87

The attendence is 88

The Grade is A

Name is Bakotra

Marks are 95 88 93

The attendence is 87

The Grade is A

*/

__________________________________

//PROGRAM - 16


/*

Write a JAVA program using StringBufferto 

delete, remove character.

*/


//StringBuffer


class program16

{

    public static void main(String[] args)

   {

     StringBuffer sb1 = new StringBuffer("Hello World");

     sb1.delete(0, 6);


    System.out.println(sb1);

    sb1.delete(0, sb1.length());

    System.out.println(sb1);


    sb1 = new StringBuffer("Hello World");

    sb1.deleteCharAt(0);

    System.out.println(sb1);

  }

}

/*

OUTPUT:

World


ello World

*/

___________________________________

//PROGRAM - 17


/*

Write a Java program that reads a line of integers and then displays each

integer and the sum of all integers. (use StringTokenizer class)

*/

import java.util.*;

 

class program17 


{

    public static void main(String args[])

   {

        int n;

        int sum = 0;

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter integers with one space gap:");

        String s = sc.nextLine();

        StringTokenizer st = new StringTokenizer(s, " ");

        while (st.hasMoreTokens()) 

        {

            String temp = st.nextToken();

            n = Integer.parseInt(temp);

            System.out.println(n);

            sum = sum + n;

        }

        System.out.println("sum of the integers is: " + sum);

        sc.close();

    }

}

/*

OUTPUT:

1 2 3 4 5

1

2

3

4

5

sum of the integers is: 15  

*/

__________________________________

//PROGRAM - 18

/*

Write a program in java to create a String object. Initialize this object

with your name. Find the length of your name using appropriate String

method. Find whether character “a” is in your name or not, if yes find

the number of time “a” appear in your name. Print locations of

occurrences of “a”. Try same for different String objects.

*/


class data

 {

   String name;

    

   data(String n)  //Milan

   { 

     name=n;

   }



 void disp()

 {

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

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


  int c=0;

  int len=name.length(); //5 or 4


 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 program18

{

  public static void main(String arg[])

{

  data d1=new data("Milan");

  d1.disp();

  data d2=new data("Bakotra");

  d2.disp();

 }


}

/*

OUTPUT:

Name :Milan

number of occurance :1

Possition :4

----------------

Name :Bakotra

number of occurance :1

Possition :2

number of occurance :2

Possition :7

*/

________________________________ 

Comments

Popular posts from this blog

Questions 2 : Assume there are three small caches, each consisting of four one-word blocks. On cache is direct-mapped, a second is two-way set-associative, and the third is fully associative. Find the number of hits for each cache organization given the following sequence of block addresses: 0, 8, 6, 5, 10, 15 and 8 are accessed twice in the same sequence. Make a tabular column as given below to show the cache content on each of columns as required. Show all the pass independently pass. Draw as many numbers Assume the writing policy is LRU. Memory location Hit/Mis Add as many columns as required

Quetion 6 : Consider the "in-order-issue/in-order-completion" execution sequence shown in f In Figure Decode OWE Execute 12 12 12 14 16 13 16 13 15 15 16 Write 024/06/02 11 3 4 11 12 13 13 N 15 16 a. Identify the most likely reason why I could not enter the execute fourth cycle. stage until the [2] b. Will "in-order issue/out-of-order completion" or "out-of-order issue/out-of-order completion" fix this? If so, which? Explain

7.Write a program to read a list containing item name, item code and cost interactively and produce a three-column output as shown below. NAME CODE COST Turbo C++ 1001 250.95 C Primer 905 95.70 ------------- ------- ---------- ------------- ------- ---------- Note that the name and code are left-justified and the cost is right-justified with a precision of two digits. Trailing zeros are shown.