Program to create one or more rectangle or circle in applet and play using thread

23:33 Unknown 0 Comments


In this program to create applet and using paint to create more design and also  play design. For example one design start from left side and move to right side and turn to other side creating like loop. In same time other design also performing same task.

Let’s start to program

1.      Create java class with any name
      package applet_demo;
      import java.applet.Applet;
      import java.awt.Color;              
      import java.awt.Graphics;
      public class Kartoon extends Applet implements Runnable{
            int width =700;
             int height= 600;
            int x=50,x1=150,x2=300;
             int y=50,y1=200,y2=50;
              @Override
             public void init(){
            setSize(width,height);
            setBackground(new Color(55,20,30));
      }
            @Override
            public void start(){
             Thread t = new Thread(this); 
              t.start();
             }
            @Override
             public void paint(Graphics g){
                         g.setColor(Color.red);
                        g.fillOval(x, y, 50, 50);
                        g.fill3DRect(x1, y1, 60, 60, true);
                        g.fillOval(x2, y2, 50, 50);
                        g.setColor(Color.ORANGE);
                        g.fillOval(300,200, 50, 50);
                        g.drawString("java pivot", 150, y);
                        g.drawString("learn java from begin", 300, 300);
                        g.setColor(Color.GREEN);
                        g.fillRect(x, 250, 35, 25);
            }

            int xdirection =10;
            int ydirection = 10;
            @Override
             public void run() {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            while (true){
                        if(x<=0||x+50>=width){
                                    xdirection *= -1;
            
                         }  
                        if(y<=0||y+50>=height){
                                                 ydirection *= -1;
            }
            x+=xdirection;
             y+=ydirection;
            //for rect
             if(x1<=0||x1+60>=width){
                                     xdirection *= -1; 
            }  
            if(y1<=0||y1+50>=height){
                        ydirection *= -1;
             }
             x1+=xdirection;
            y1+=ydirection;
            // code for
            if(x2<=0||x2+60>=width){
                                     xdirection *= -1; 
            }  
             if(y2<=0||y2+50>=height){
                                     ydirection *= -1;
            }
             x2-=xdirection;
             y2-=ydirection;
           repaint();
           try {
                         Thread.sleep(100);
           } catch (InterruptedException e) {
       }}

      }}


Run this code.
Output:



0 comments:

program to print employee details using multilevel inheritance

21:57 Unknown 0 Comments

in this program used multi level inheritance,  method and switch statement. 

//program to print employee details using multilevel inheritance


package management;
import java.util.Scanner;
class teacher{
    void tech(){
        System.out.println("!!!Subject!!!\t\t!!Empid!!\n1)java\t\t\t101\n2)php\t\t\t102\n3)Android\t\t103\n\n");
    }
}
class Admin extends teacher{   
     void admin(){
        System.out.println("!!!Salary!!!\t\t!!Shift!!\n1)rs25,000\t\t9:00 AM to    5:00PM\n2)rs30,000\t\t9:00 AM to 5:30PM)\n3)rs45,000\t\t5:00 PM to   9:00AM\n\n");
    }
}
class Manage extends Admin{
    void manager(){
        System.out.println("!!!HR Salary!!!\t\t!!!FinanceSalary!!!\n
1)Rs35,000\t\t40,000\n2)Rs45,000\t\t47,000\n1)Rs55,000\t\t57,000\n");
    }
}
public class Management {
    public static void main(String[] args) {
        // TODO code application logic here
        Manage m=new Manage();
       Scanner s=new Scanner(System.in);
       System.out.println("Enter your Department\n 1.teacher\n2.admin\n3.manager\n");
       String dept=s.nextLine();
      
                if(dept.equals("teacher"))
                 {   m.tech();
                 }
                 else if(dept.equals("admin"))
                   {  m.admin();
                     }
                  else if(dept.equals("manager"))
                    {  m.manager();
                     }
                   else
                    { System.out.println("Invalid Input again try");
                     }
   
    }


}



output:



0 comments:

Method Overriding in java

23:52 Unknown 0 Comments

Same method (name, parameter) declare in both main class and sub class, that method is known as method overriding.

same method used in single class that method is known as method overloading, while same method used in more than one class or different class that method is known as method overriding

Syntax:
public class Fruits {
                void sweetfruits(){
                                ...
                 }
}
class Mango extends Fruits{
                void sweetFruits(){
                                 ...
                 }
}


Example:

output:



If want to print both method in single object then must be use super keyword, see below:
Example:

output:


I want change something that
Fruits f = new Mango();
       f.sweetfruits();
then what will be the output:
Example:

output:

These all about method overriding concept. As per requirements you can use method overriding concept.

0 comments:

Nested class demo program

11:15 Unknown 0 Comments

The Java programming language allows you to define a class within another class. Such as that class is known as nested class.

Syntax:
class OuterClass{
            . . .
class  InnerClass{
                        . . .
            }
}
 Example:




OUTPUT:

Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.
Syntax:
class OuterClass {
    ...
    static class StaticNestedClass {
        ...
    }
    class InnerClass {
        ...
    }
}

Inner class have access to other members of the enclosing class, if they are declared private. Static nested classes do not have access to other members of the enclosing class.
As a member of the OuterClass, a nested class can be declared private, public  protected

Non-Static nested class  Syntax:
class OuterClass{
            . . .
class  InnerClass{
                        . . .
            }
}
Static nested class Syntax:
class OuterClass{
            . . .
static class  InnerClass{
                        . . .
            }
}

Non static nested class example:
Already declare above.

Static nested class example:

OUTPUT:


If we use static or non-static nested class then the main different is how to declare object. Below the description:
Non-static nested class:
OuterClass o1= new OuterClass();
OuterClass.InnerClass obj= o1.new InnerClass();
Static nested class:
OuterClass.InnerClass obj= new OuterClass.InnerClass();

0 comments:

Constructor demo program

21:29 Unknown 0 Comments


Constructor:
1.      Name same as class name
2.      No return type
3.      Initialization
4.      Can has parameter
5.      Constructor can be overloaded
6.      Constructor called when object is created
Declaration:
            class Const_Demo
{
            Const_Demo(){            //constructor name must be same as class name
                        // constructor body
            }
Public static void main(String[] args){
            Const_Demo obj = new Const_Demo();
}

}

// program to using constructor

package constructordemo;
public class ConstructorDemo {
            public ConstructorDemo() {
                         System.out.println("This is constructor demo program");
             }
             public static void main(String[] args) {
                         ConstructorDemo obj = new ConstructorDemo();
             }
}



Constructor also accept overloading. Below the program constructor overloading

constructor overloading program

//constructor overloading program

package constructordemo;
public class ConstructorDemo            //class name as ConstructorDemo
 {
             public ConstructorDemo()     //constructor initialized as ConstructorDemo
 {
                         System.out.println("This is constructor demo program");
              }
            public ConstructorDemo(int a, int b)              //constructor overloading
 {
                        int sum = a+b;
                        System.out.println("add of two no. is:"+sum);
             }
            public ConstructorDemo(int a)           //constructor overloading {
                        System.out.println("This is constructor overloading body");
             }
             public static void main(String[] args)
 {
                        ConstructorDemo obj1 = new ConstructorDemo();               //constructor called by creating obj.
                         ConstructorDemo obj2 = new ConstructorDemo(10,20);     //constructor called by creating obj.
                        ConstructorDemo obj3 = new ConstructorDemo(1);             //constructor called by creating obj.
             }
}





if you want to print object then what will be happen, let’s see.
package constructordemo;
public class ConstructorDemo {
              public ConstructorDemo() {
                        //constructor body……
              }
public static void main(String[] args) {
                        ConstructorDemo obj1 = new ConstructorDemo();
                         System.out.println(obj1);
}
}
Output:

0 comments:

e gift