Java Tutorial Day 3

Java Tutorial Day 3

Learning Objective: Let’s start with Java Practical List and it’s example with detail explanations. (Continue…)

prog11.java

Write a program in Java in which a subclass constructor invokes the constructor of the super class and instantiate the values.

public class prog11 {
    public static void main(String arg[])
    {
        System.out.println("---------------------------------");
        System.out.println("      Write a program in Java in which a subclass constructor invokes the constructor of the super class and instantiate the values.");
        System.out.println("---------------------------------");

        SubClass s=new SubClass(5,4);
        
    }
}
class SuperClass
{
    int x;
    SuperClass(int a)
    {
        x=a;
        System.out.println("Super Class : " +x);
    }
}
class SubClass extends SuperClass
{
    int y;
    SubClass(int l,int m)
    {
        super(l);
        y=m;
        System.out.println("Sub class : "+y);
    }
}

prog12.java

Write an application that illustrates method overriding in the same package and different packages. Also demonstrate accessibility rules in inside and outside packages

package p1;

public class prog12_1 {
    private int n=1;
    public int n_pub=2;
}
package p1;

import p1.prog12_1;

public class prog12 {
    public static void main(String arg[])
    {
        System.out.println("---------------------------------");
        System.out.println("      Write a program in Java in which a subclass constructor invokes the constructor of the super class and instantiate the values.");
        System.out.println("---------------------------------");
        
        prog18_1 p1=new prog18_1();
        System.out.println("n= "+p1.n); // this line generate error without it, output 
                                        //like following
        System.out.println("n_pub= "+p1.n_pub);
    }
}

prog13.java

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object i.e. area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle

abstract class Shape
{
    float dim1,dim2,radius;
    abstract float area();
}
class Triangle extends Shape
{
        Triangle(float d1, float d2)
        {
            dim1=d1;
            dim2=d2;
        }
        float area()
        {
            System.out.println("Area of Triangle is ");
            return (dim1*dim2)/2;
        }
}
class Rectangle extends Shape
{
    Rectangle(float d1, float d2)
    {
        dim1=d1;
        dim2=d2;
    }
    float area()
    {
        System.out.println("Area of Rectangle is ");
        return dim1*dim2;
    }
}
class Circle extends Shape
{
    Circle(float d1)
    {
        radius=d1;
    }
    float area()
    {
        System.out.println("Area of Circle is ");
        return 3.14f*radius*radius;
    }
}

public class prog13 {
    public static void main(String arg[])
    {
        System.out.println("---------------------------------");
        System.out.println("      Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, Circle. Define one method area()in the abstract class and override this area() in these three subclasses to calculate for specific object i.e. area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle");
        System.out.println("---------------------------------");

        Triangle t=new Triangle(4.3f,5.3f);
        Rectangle r=new Rectangle(2.4f,4.2f);
        Circle c=new Circle(10.5f);
        
        System.out.println(t.area());
        System.out.println(r.area());
        System.out.println(c.area());
        
    }
}

prog14.java

Write a program in Java to develop user defined exception for ‘Divide by Zero’ error.



public class prog14 {

    public static void main(String arg[])
    {
        System.out.println("---------------------------------");
        System.out.println("      Write a program in Java to develop user defined exception for 'Divide by Zero' error");
        System.out.println("---------------------------------");
        int a;
        int b;
        a = 10;
        b = 0;

        try{
            System.out.println("Division : "+a/b);
        }
        catch(Exception e)
        {
            System.out.println("Exception: " + e.toString());
        }
    }
}

prog15.java

Write an small application in Java to develop Banking Application in which user deposits the amount Rs 1000.00 and then start withdrawing of Rs 400.00, Rs 300.00 and it throws exception “Not Sufficient Fund” when user withdraws Rs. 500 thereafter

import java.util.*;


public class prog15 {
    float fund;
    void deposit(float amount)
    {
        fund=amount;
    }
    void withdraw(float money) throws Exception
    {
        float newFund=fund-money;
        if(newFund<500)
        {
            throw new Exception("Not Sufficient Fund");
        }
        else
        {
            fund=newFund;
            System.out.println("Balance After Withdraw : "+fund);
        }
    }
        public static void main(String arg[])
        {
            System.out.println("---------------------------------");
            System.out.println("      Write an small application in Java to develop Banking Application in which user deposits the amount Rs 1000.00 and then start withdrawing of Rs 400.00, Rs 300.00 and it throws exception \"Not Sufficient Fund\" when user withdraws Rs. 500 thereafter.");
            System.out.println("---------------------------------");
            prog21 b=new prog21();
            b.deposit(1000.00f);
            try
            {
                float money;
                Scanner sc=new Scanner(System.in);
                System.out.println("Enter Your Amount for withdraw : ");
                money=sc.nextInt();
                System.out.println("Withdrawing amount : "+money);
                b.withdraw(money);
                /* here test with static data so don't worry  
                money=300;
                System.out.println("Withdrawing amount : "+money);
                b.withdraw(money); */
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
            
        }
}

prog16.java

Write a program that executes two threads. One thread will print the even numbers and the another thread will print odd numbers from 1 to 50.

class NewThreadOE extends Thread
{
    NewThreadOE(String threadname)
    {
        super(threadname);
    }
    public void run()
    {
        if( getName().equals("Odd")==true)  
        {
            for(int i=1;i<=50;i=i+2)
            {
                System.out.print("\nOdd: " + i);
                try
                {
                    Thread.sleep(1000);
                }
                catch(InterruptedException e)
                {
                    System.out.println("Exception Occurred.");
                }
            }
        }
        else
        {
            for(int i=2;i<=50;i=i+2)
            {
                if(i%10==0)
                System.out.println();
                System.out.print("\nEven: " + i);
                try
                {
                    Thread.sleep(1000);
                }
                catch(InterruptedException e)
                {
                    System.out.println("Exception Occurred.");
                }
            }
        }
    }
}

public class prog16 {
    public static void main(String[] args)
    {
        System.out.println("---------------------------------");
        System.out.println("      Write a program that executes two threads. One thread will print the even numbers and the another thread will print odd numbers from 1 to 50.");
        System.out.println("---------------------------------");
        NewThreadOE t1 = new NewThreadOE("Odd");
        NewThreadOE t2 = new NewThreadOE("Even");
        t1.start();
        t2.start();
    }
}

prog17.java

Draw ten red circles in a vertical column in the center of the applet.

import java.applet.*;
import java.awt.*;

public class prog17  extends Applet{
    public void paint(Graphics g)
	{
		g.setColor(Color.red);
		g.fillOval(250,0,50,50);
		g.fillOval(250,50,50,50);
		g.fillOval(250,100,50,50);
		g.fillOval(250,150,50,50);
		g.fillOval(250,200,50,50);
		g.fillOval(250,250,50,50);
		g.fillOval(250,300,50,50);
		g.fillOval(250,350,50,50);
		g.fillOval(250,400,50,50);
		g.fillOval(250,450,50,50);
		g.drawString("Hello",170,550);
	}
}

/* <applet code="vertical_circle" height=600 width=550> </applet> */
<html>

<head> <title> Draw Circles in Vertical Columns </title></head>

<body>

<applet code="prog17.class" width=400 height=400>
</applet>
</body>
</html>

prog18.java

Built an applet that displays a horizontal rectangle in its enter. et the rectangle fill with color from left to right.

import java.applet.*;
import java.awt.*;
/*
<applet code="prog18.class" width=600 height=400>
</applet>
*/
public class prog18 extends Applet{
    public void paint(Graphics g)
    {
            Dimension d=getSize(); 
            int x = d.width/2;
            int y = d.height/2;
            g.setColor(Color.green);
            g.drawRect(x-25,y-25,100,50);
            for(int i=100;i<200;i++)
            {
                try
                {
                    Thread.sleep(30);
                    g.fillRect(i+(x-125),y-25,5,50);
                }
                catch(Exception e){}
            }
    }
}