Java Programs

1. print hello world using java applet.Background colour must be black.

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


public class Hello extends Applet 
{

  public void paint(Graphics g) 
     {  
      setBackground(Color.BLACK);
      
      g.setColor(Color.YELLOW);
      g.drawString("Hello World",20,40);

      
     }



2. program for life cycle of applet.



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

public class LifeCycle extends Applet
{
     String output = "";
     String event;

     public void init()
     {
          event = "Initializing...";          
    printOutput();
     }

     public void start()
     {
          event = "Starting..."; 
    printOutput();
     }

     public void stop()
     {
          event = "Stopping...";
    printOutput();
     }

     public void destroy()
     {
          event = "Destroying...";
    printOutput();
     }

     private void printOutput()
     {
          System.out.println(event);
    output += event;
    repaint();
     }

     public void paint(Graphics g)
     {
          g.drawString(output, 40, 40);
     

     }

  
} 

3. Program for overloading


class overloading
{
  public static void main(String[] args)

{
    functionOverload obj = new functionOverload();
    

    obj.add(1,2);
    obj.add(11.5, 22.5);
    obj.add("Palak ","Sanghani");
    obj1.add(3,4);

}

}

class functionOverload
{


void add(int a, int b, int c)
{

  int sum = a + b + c;
  System.out.println("Sum of a+b+c is: " + sum);

}

void add(double a, double b) 
{

  double sum = a + b;
  System.out.println("Sum of a+b is: " + sum);
}

void add(String s1, String s2)
{
  String s = s1+s2;
  System.out.println(s);
}

}

4. Creating an object also show benefit of inheritance


class A
 {
  int x;
  int y;
  int get(int p, int q)
  {
    x=p; y=q; 
    return(0);
  }
  void Show()
  {
    System.out.println("The x coordinate in (5,6) is "+ x);
  }
}

class inheritance extends A
{
  public static void main(String args[])
  {
    A a = new A();
    a.get(5,6);
    a.Show();
  }
  
}

5. Find whether a given number is prime or not if prime
(i) square the number and divide by 5
if not (ii) cube the number and divide by 7

import java.util.Scanner;

public class prime {

   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       System.out.print("Enter a number : ");
       int n = s.nextInt();
       if (isPrime(n)) 
       {
           System.out.println("Prime");
           float ans= (n*n)/5; 
           System.out.println(ans);
       } 
       else
       {
           System.out.println("Not Prime");
           float ans =(n*n*n)/7;
           System.out.println(ans); 
       }
   }

   public static boolean isPrime(int n) {
       if (n <= 1) {
           return false;
       }
       for (int i = 2; i < Math.sqrt(n); i++) {
           if (n % i == 0) {
               return false;
           }
       }
       return true;
   }
}

6. Given a number, reverse the number. Find diffference
(i) if positive print positive
(ii) if negative print negative

import java.io.*;

class reverse
{
public static void main(String args[]) throws Exception
{

int sum, i,a,d,n;
float dif;
System.out.println("Enter Number ");
BufferedReader k = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(k.readLine());
n = a;
sum = 0;
while(a!=0)
{
d = a%10;
sum=sum*10+d;
a = a/10;
}
System.out.println("Number :"+n);
System.out.println("Reverse :"+sum);
dif = n-sum;
System.out.println("Difference :"+dif);
if (dif>0)
System.out.println("Positive");
else
System.out.println("Negative");
}
}

Comments

Popular posts from this blog

IT resources @ DAIICT

I blinked and the day passed ...

The world is what it is