Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Difference between Pre Increment and Post Increment

Pre Increment and Post Increment are the features of unary operators. Unary operators are the important concepts of programming and is very easy to learn. Unary operators are mostly used to looping statements, function calls, etc. The unary operators have high precedence in C compared to other operators.(click here to check precedence table). Unary operators are operators with only one operand. Two types of unary operators in C program:
1. Increment operator (++): it increases the value by one.
2. Decrement operator (--): It decreases the value by one.

Difference Between Pre Increment and Post Increment.

Pre Increment Post Increment
It is used to increase the value of the variable by one before using in the expression It is used to increase the value of the variable by one only after using in the expression
It value gets incremented as soon as the expression is used The original value is used in an expression and then only it is incremented.
Its syntax is : ++ variable; Its syntax is : variable ++;
code snippet:
#include <stdio.h>
#include <stdlib.h>

void main()
{
   
   int res, n;

   n = 7;

   res = ++n;
   
   printf("%d", res);
}
Output: 8
code snippet:
#include <stdio.h>
#include <stdlib.h>

void main()
{
   
   int res, n;

   n = 7;

   res = n++;
   
   printf("%d", res);
}
Output: 7

Difference between Method Overloading and Method Overriding

Method Overloading and Overriding help us to achieve polymorphism, which is one of the features of Object-Oriented-programming language

1. Method Overloading

  • Method overloading is a oops concepts in which a class can have two or more methods with same name but with different parameter
  • It is also called static polymorphism or time binding or early binding
  • It is implemented during compile time
  • There are two ways to overload method in java :
    1.Different Number of Arguments
    2.Different Data Types

Example

public class MethodOverloading 
  {
		public static void main(String[] args) 
	{
			MethodOverloading a = new MethodOverloading ();
			a.age(5);
			a.age(18, "CSE");
			a.display('H');
			a.display(12);	
	}
		public void age(int a)
		{
			System.out.println("The age is:" + a);
		}
		public void age(int a, String c)
		{
			System.out.println("The age is:" + a +" Name:" + c);
		}
		public void display(int a)
		{
			System.out.println("The age is:" + a);
		}
		public void display(char a)
		{
			System.out.println("Name is:" + a);
		}
		

}
    

2. Method Overriding

  • if sub class has the same method as declared in parent class it is called method overriding
  • it is also called dynamic binding/ dynamic polymorphism
  • It is implemented during run time

Example

class baby 
{
	
	void display()
	{
		System.out.println("This is the Child class");
	}
	
}
public class MethodOverriding extends baby
{
	public static void main(String[] args) 
	{
		 MethodOverriding obj = new  MethodOverriding ();
		 obj.display();

	}
	void display()
	{
		super.display();
		System.out.println("This is the parent class");
	}
}

Programs using Recursive Function

Recursive Function is a functtion which calls itself.Every recursive function must have a base condition to stop the function else the program will never end

Advantage of Recurive Function

1.it makes the coding clean

2.it helps solve a complex problem by breaking it down into many parts



Question: Write a program to find the sum of a number from 1 to n using recursive function

Solution:

import java.util.Scanner;

public class Recursive1
    {
	static int sum=0;

	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		System.out.print("Enter A number:");
		int n = s.nextInt();
		System.out.print("The sum of the numbers from 1 to " + n +" is " + sum(n));	
	}

	public static int sum(int n)
	{ 
		int i=n;
		if(i!= 0)
		{
			sum = sum + i;
			i--;
			return sum(i);
		}
		return(sum);				
	}
   }

Output:



Question: Write a program to find the factorial of a given number using recursive function

Solution:

import java.util.Scanner;
public class Recursive2
  {
	static int fac=1;

	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		System.out.print("Enter A number:");
		int n = s.nextInt();
		System.out.print("The factorial of the number is " + n +" is " + fac(n));	
	}

	public static int fac(int n)
	{ 
		int i=n;
		if(i!= 0)
		{
			fac = fac * i;
			i--;
			return fac(i);
		}
		return(fac);				
	}
  }

output:





Question:Write a program to print multiplication table of any number from 1 to 12 using recursive function

Solution:

import java.util.Scanner;
public class Recursive3
  {
	static int i=1;
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		System.out.print("Enter A number:");
		int n = s.nextInt();
		System.out.print("The Multiplication table of " + n +" from 1 to 12 is \n");
		mut(n);	
	}

	public static int mut(int n)
	{ 
	
		if(i<=12)
		{
			System.out.println(i +"*"+ n+ " : " + (i*n));
			i++;
			fac(n);
		}
		return(n);				
	}
  }
  

output:



Question: Write a program to find the prime number using recursive program

Solution

import java.util.Scanner;
public class Recursive4
  {
	static int count=0;
	static int i=1;
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		System.out.print("Enter a number:");
		int n = s.nextInt();
		System.out.print("The given number "+ n +" ");
		check(n);
		if(count==2)
			System.out.print("is prime");
		else
			System.out.print("is not prime");
	
	}

	public static int check(int n)
	{ 
		if(i<=n)
		{
			if(n%i==0)
			{
				count++;
			}
			i++;
			return check(n);
		}	
		return count;
			
	}
}

Output:


Question: Write a program to find the cube of a given number

Solution:

import java.util.Scanner;

public class Recursive5
  {
	static int i =1;
	static int p =1;
	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		System.out.print("Enter a number:");
		int n = s.nextInt();
		System.out.print("The cube of " + n+ " is " + cal(n));
	}

	public static int cal(int n)
	{ 
		if(i&jlt;=3)
		{
					p=p*n;
					i++;
					return cal(n);
		}						
			return p;
	}
}
output:


WAP in Java Using all the concepts of OOP, Exception handling and Packages -II

Aim:To make a simple java project for shoe shop using all the concept of OPPS,Exception handling and packages

Background:HI-TECH SHOE SHOP is a project made to help people buy shoes. This project was done to enhance my java programming skills and develop further knownledge on the concepts of OOPS,Exception Handling and pacakges

This project has been divided in 2 packages, the default package which contains one class(Welcome),and the other user defined package which contains 3 classes and 1 interface

Default Package: Welcome Class

import java.util.Scanner;
import assignment.Display;

public class Welcome {
	static Scanner input = new Scanner(System.in);
	static int y;

	public static void main(String[] args)
	{
		pattern();
		Display s = new Display();
		s.Check();
		pattern();
		s.GetChoice(s.C);
		s.GetChoice();
		pattern();
		s.printRecipe(s.sum);		
		confirmation(s.sum);
		if( y==1)
		{
			s.printRecipe(s.name,  s.itembrought,  s.sum);	
		}
		pattern();
		thankyou();
	
	}
	private static void thankyou()
	{
		if(y==1)
		{
			System.out.println("\nTHANKYOU FOR CHOOSING HI-TECH SK SHOE STORE");
		}
		
		System.out.println("\nVisit us Again");
	}
	private static void confirmation(int x) 
	{
		try
		{
			y=input.nextInt();
			if( y==1)
			{
				pattern();
				System.out.println("\nGenerating Recipt");
				System.out.println("Payed Nu: " + x + " To Tazi Shoe Store on " + java.time.LocalDate.now() );	
			}	
		}
		catch(Exception e)
		{
			pattern();
			
		}
	}
	static void pattern()
	{
		for(int i =0; i	<100;i++)
		{
			System.out.print("+");
		}
	}

}

Assignment Package: Display class

package assignment;
import java.util.InputMismatchException;

public class Display extends ItemDetails implements printingSystem 
{
		public int sum;
		public int itembrought = 0;
		int cardno;
		public void GetChoice()
		{
			System.out.println("Enter serial Number of the item you want to buy:");
			System.out.println("Press 0 to see the Total");
			boolean t =true;
			int x ;
			while(t==true)
			{
						try
						{
							x = in.nextInt();
							
							if(x==1 || x==5 ||x==6)	
							{
								sum += 500;
								itembrought++;
								System.out.println("!!!!!!!!Added to the Cart!!!!!");
								
							}
								
							else if(x==2 || x==7 || x==10)
							{
								sum +=750;
								itembrought++;
								System.out.println("!!!!!!!!Added to the Cart!!!!!");
								
							}
								
							else if(x==3 || x==4 || x==8)
							{
								sum +=900;
								itembrought++;
								System.out.println("!!!!!!!!Added to the Cart!!!!!");
								
							}
								
							else if(x==9)
							{
								sum += 5000;
								itembrought++;
								System.out.println("!!!!!!!!Added to the Cart!!!!!");
								
							}
								
							else if(x==0)
							{
								
								t=false;
							}
							else
							{
								throw new Numberlargerexception();
							}																
						}
						catch (Numberlargerexception e) 
						 {
							   System.out.print("Invalid Choice!! Please enter valid choice\n");
							   GetChoice();
						 }
						catch(InputMismatchException e)
						{
							 System.out.print("Invalid Choice!! Please enter valid choice\n");
							   GetChoice();
						}
										
			}				
		}
		
		@Override
		public void printRecipe(String nam, int iB, int Sn)
		{
			System.out.println("\nCustomer'sName: " + nam + "\nTotal Items Brought: " + iB + "\nTotal Nu: " + Sn + "\nDate Of Payment: " + java.time.LocalDate.now() );
			
		}

		@Override
		public void printRecipe(int Sn) 
		{
			System.out.println("\nTotal is Nu:" + Sn);
			System.out.print("press 1.check out");
			System.out.print("\npress any key on the board to to cancel\n");
		}
		
}

Items details class

package assignment;

import java.util.Scanner;


public class ItemDetails
{
	Scanner in = new Scanner(System.in);
	public int C;
	public String name;
	ItemDetails()
	{
		System.out.print("\nPlease Enter Your name ");
		checkname();
		System.out.print("WELCOME " + name + " TO HI-TECH SK SHOE SHOP"); 
		System.out.println("\nPress 1 for Men shoes\nPress 2 for Women Shoes");	
		
	}
	
	
	public void GetChoice(int x)
	{
		if(x == 1)
			menshoe();
		if(x==2)
			Womenshoe();
	}

	private void Womenshoe() 
	{
		System.out.println("\nDisplaying Women's shoes:");
		System.out.println("1.SANDAL --------------------MRP:500");
		System.out.println("2.HIGH HEEL------- ----------MRP:750");
		System.out.println("3.LOW HEEL-------------------MRP:900");
		System.out.println("4.CASUAL OFFICE SHOES------- MRP:900");
		System.out.println("5.JUMPER PINK SHOES ---------MRP:500");
		System.out.println("6.MOMOS SHOES ---------------MRP:500");
		System.out.println("7.PLASTIC OFFICE SHOES ------MRP:750");
		System.out.println("8.PARTY SHOES ---------------MRP:900");
		System.out.println("9.BOOT --------------------- MRP:5000");
		System.out.println("10.FLAT SLIPPERS ------------ MRP:750");	
		
	}

	private void menshoe() 
	{
		System.out.println("\nDisplaying Men's shoes:");
		System.out.println("1.NIKE BASKETBALL SHOES --------MRP:500");
		System.out.println("2.PUMA BASKETBALL SHOES ------- MRP:750");
		System.out.println("3.ADDIDAS BASKETBALL SHOES------MRP:900");
		System.out.println("4.NIKE FOOTBALL SHOES --------- MRP:900");
		System.out.println("5.PUMA FOOTBALL SHOES ----------MRP:500");
		System.out.println("6.ADDIDAS FOOTBALL SHOES ------MRP:500");
		System.out.println("7.GOLD TOE OFFICE SHOES -------MRP:750");
		System.out.println("8.HOME SLIPPERS --------------- MRP:900");
		System.out.println("9.BOOT ----------------------- MRP:5000");
		System.out.println("10.VANS SHOES ---------------- MRP:750");			
	}
	private void checkname() 
	{
		name = in.nextLine();
		try
		{
			char name0[]=name.toCharArray();
			for(char a : name0)
			{
				if(Character.isDigit(a))
					throw new Numberlargerexception();
					
			}
		}
		catch (Numberlargerexception e) 
		{
			System.out.print("Your name contains digit\nRe-Enter Name:");
			checkname();
		}	
		
	}
	public void Check()
	{
		boolean ny = true;
		while(ny == true)
		{
			
			String c = in.nextLine();
			   try
			   {
				   C = Integer.parseInt(c); 
				    if(C>3 || C<1)
				    	throw new Numberlargerexception();
				    	ny=false;
			   }
			   catch(NumberFormatException e)
			   {
				   System.out.print("Invalid Choice!! Please enter valid choice\n");
			   }
			   catch (Numberlargerexception e) 
			   {
				   System.out.print("Invalid Choice!! Please enter valid choice\n");
			   }
		}
		
	}
	
}

Numberlargerexception Class

package assignment;

public class Numberlargerexception extends Exception {
	Numberlargerexception()
	{
		super("Error");
	}

}

interface printingSystem

package assignment;

public interface printingSystem 
{
	void printRecipe(String nam, int iB, int Sn);
	void printRecipe(int Sn);
}

output 1:
















Output 2:



WAP in Java Using all the concepts of OOP,Exception handling and Packages -I

Aim: To create a program to allow people to register for credit management system and print their information and prompt user to register for the program.

Background: Credit management system program is made to help people register for a system and hence it can be implement as a backend code for many programs be it bank system, blood donation systems, school admission system and many more other systems. The codes contains complete information and also the password is set to private to ensure security. Almost all the parameters have exception handling and hence it ensures all the information given by the user is correct up to some level.
Objective: To allows users to register for a system and print their details and ask for confirmation for registration


Code Explaination: There are 2 packages default package and register package

mainBody Class

it contains 3 methods and the main method is present here

import java.util.Scanner;

import register.ShopRegis;
import register.StudentRegister;

public class mainBody  {
	static Scanner s = new Scanner(System.in);
	public static void main(String[] args)
	{	
		Intro obj = new Intro();
		String x = s.nextLine();
		obj.choose(x);
		if(obj.y==1)
		{
			StudentRegister s1 = new StudentRegister();
			s1.getname();
			s1.getSname();
			s1.Studentno();
			s1.Collegename();
			s1.emailadress();
			s1.setPassword(null);
			for(int i =0;i<50;i++)
			{
				if(i==25)
				System.out.print("Showing Details");
				else
					System.out.print("*");
			}
			obj.showDetails(s1.name,s1.Sname,s1.snum, s1.strnum, s1.email,s1.getPassword());
			Sure();
		}
		else if(obj.y==2)
		{
			ShopRegis s1 = new ShopRegis();
			s1.getname();
			s1.getSname();
			s1.phonenum();
			s1.Lis();
			s1.Location();
			s1.setPassword(null);
			for(int i =0;i<50;i++)
			{
				if(i==25)
				System.out.print("Showing Details");
				else
					System.out.print("*");
			}
			
			obj.showDetails(s1.name,s1.Sname,s1.p, s1.Lnum,s1.phone,s1.getPassword());
			Sure();
			
		}
		else
		{
			finalmessage();
		}
		
		
	}
	private static void Sure()
	{
		for(int i =0;i	<50;i++)
		{
			if(i==25)
			System.out.print("Are you Sure you want to register");
			else
				System.out.print("*");
		}
		           System.out.println("\n                       press 1 to register else press anybutton");
					String co = s.nextLine();
					try
					{
						int x =Integer.parseInt(co);
						if(x==1)
						{
							for(int i =0;i	<50;i++)
							{
								if(i==25)
									System.out.print("Registered");
								else
									System.out.print("*");
							}
						}
						
						else
							{
							for(int i =0;i	<50;i++)
							{
								if(i==25)
									System.out.print("Not-Registered");
								else
									System.out.print("*");
							}		
							}
						finalmessage();
					}
					catch(NumberFormatException e)	
					{
						for(int i =0;i	<50;i++)
						{
							if(i==25)
								System.out.print("Not-Registered");
							else
								System.out.print("*");
						}
						finalmessage();
					}
	}
	private static void finalmessage()
	{
		System.out.print("\n");
		for(int i =0; i	<55; i++)
		{
			if(i == 20)
				System.out.print("Thankyou For Using Our Service");
			else
				System.out.print("*");
		}
		
		
	}		
}
 

invalidinputException

it contain a constructor and whenever an custom made Exception is catched in the default package the program comes to this method

public class invalidinputException extends Exception {
	invalidinputException()
	{
		super("Invalid Input");
	}
}

Intro Class

This class contains 3 methods and 1 constructor, it implements an interface

import java.util.Scanner;

public class Intro implements Details {
	String name,em,Pa;
	int  Stdno;
	int y = 0;
	Intro()
	{
		for(int i = 0; i	<50;i++)
		{
			if(i==20)
				System.out.print("Welcome to Credit management System");
			else
			System.out.print("*");
		}
		System.out.println("\nPress 1. for student  Registration \nPress 2. for customer Registration \npress 0. to  cancel   Registration");
		
		
	}
	public int choose( String x)
	{
		
		Scanner s = new Scanner(System.in);
		{
			 try 
			 {
				 y = Integer.parseInt(x);
				 if(y!=1 && y!=2 && y!=0)
					 throw new invalidinputException();
				 return y;	 
			 }
			 catch(invalidinputException e)
			 {
				 System.out.print("Invalid input");
				 String X = s.nextLine();
					choose(X);
			 }
			 catch(NumberFormatException e)
			{
				System.out.println("Invalid Input");
				String X = s.nextLine();
				choose(X);
				
			}
		}
		return y;		
	}		
	@Override
	public void showDetails(String fn, String sn, String snum, int Sn, String em, String pas) {
		
		 name = fn + sn;
		  System.out.println("\n1.Name:" + name +  "\n2.Student Number:" + Sn   + "\n3.College name:" + snum + "\n4.EmailAdress:" + em  + "\n5.password:" + pas   );
			
	}
	public void showDetails(String fn, String sn, String Loc, int Ln, int pn, String pas)
	{
		 name = fn + sn;
		  System.out.println("\n1.Name:" + name +  "\n2.Liscence No:" + Ln   + "\n3.Adress:" + Loc  + "\n4Phone Number:"+ pn + "\n5.password:" + pas   );
	}
 }

Interface Details

This interface contains two methods


public interface Details {
	void showDetails(String fn, String sn, String Loc, int Ln, int pn, String pas);
	void showDetails(String fn, String sn, String snum, int Sn, String em, String pas);
	
}

Under the register package there are 4 classes

1 class contains common registration parameter other 2 contains parameters different for student and canteen owners and the clas class implements the exception

Common_Registration

package register;

import java.util.Scanner;


public class Common_Register 
{
	Scanner s = new Scanner(System.in);
	public String name;
	public String Sname;
	private String Password;
	
	public void getname()
	{
		System.out.print("Enter Your First Name:");
		name = s.nextLine();
		try 
		{
			for(int i =0; i	<name.length(); i++)
			{
				for(char j =0; j	<65 ; j++ )
				{
					
					if(name.charAt(i) == j)
					{
					   throw new ErrorException();
					}
					
				}
			}
		}
		catch (ErrorException e)
		{
			System.out.println("Please enter name Correctly");
			getname();
		}
}
	public void getSname()
	{
		System.out.print("Enter Your Second Name:");
		Sname = s.nextLine();
		try 
		{
			for(int i =0; i	<Sname.length(); i++)
			{
				for(char j =0; j	<65 ; j++ )
				{
					
					if(Sname.charAt(i) == j)
					{
					   throw new ErrorException();
					}
					
				}
			}
		}
		catch (ErrorException e)
		{
			System.out.println("Please enter name Correctly");
			getSname();
		}
		catch(Exception e)
		{
			System.out.println("Please enter name Correctly");
			getSname();
		}
	}
	public String getPassword() {
		return Password;
	}
	public void setPassword(String password) 
	{
		 String np = null;
		{
			while(password == null)
			{
				System.out.print("Enter Password:");
				password = s.nextLine();
				 if(password.length()	<8 )
				   {
					System.out.println("Password is too weak");
						setPassword(null);
				  }
				 else 
				 {
					 
					 System.out.print("Re-Enter Password:");
					 np = s.nextLine();
					 checkpassword(password,np);
				 }
				
					
			}
			
		}
	}
	private void checkpassword(String password2, String np)
	{
		if(password2.matches(np))
		{
			this.Password = password2;
		}
		else
		{
			System.out.print("PASSWORD MISMATCH\n");
			setPassword(null);
			
		}
	}		
}

ShopRegis class

package register;

public class ShopRegis extends Common_Register
{
	public String p;
	public int phone;
	public int Lnum;
		public void phonenum()
		{
			boolean choice =true;
			while(choice == true)
			{
				System.out.print("Enter your Phone number:");
				p = s.nextLine();
				try
				{
					phone = Integer.parseInt(p);
					if(phone/100000000 > 1 || phone/10000000!=1 &&  phone/10000000!=7 || phone/1000000!=17 && phone/1000000 !=77 )
					{
						throw new ErrorException();
					}
					else
					choice = false;
				}
				catch(NumberFormatException e)
				{
					System.out.println("Invalid phone number");
				}
				catch(ErrorException e)
				{
					System.out.println("Invalid phone number");
				}
			}
			
		}
		public void Lis()
		{
			boolean choice =true;
			while(choice == true)
			{
				System.out.print("Enter your Liscence number:");
				p = s.nextLine();
				try
				{
					Lnum= Integer.parseInt(p);
					choice = false;
				}
				catch(NumberFormatException e)
				{
					System.out.println("Invalid Liscence number");
				}
				
			}			
		}
		public void Location()
		{
			System.out.print("Enter your Canteen Location:");
			p=s.nextLine();
		}
}

StudentRegister Class

package register;


public class StudentRegister extends Common_Register  
{
	public String snum;
	public int strnum;
	public String email;
	boolean check = false;
	public void Studentno()
	{
		
		while(check == false)
		{
			System.out.print("Enter Student Number:");
				snum = s.nextLine();
				
			try
			{
				strnum = Integer.parseInt(snum);
				check = true;
			}
			catch(Exception e)
			{
				System.out.println("Student number should be an integer value!!");
				
			}
			
		}
		
	}
	public void Collegename()
	{
		System.out.print("Enter Your college's Abbreviation:");
		snum = s.nextLine();
		try
		{
			for(int i =0;i	<snum.length();i++)
			{
				for(char j =0; j	<65 ; j++)
					if(snum.charAt(i) == j)
					{
						throw new ErrorException();
					}
			}
		}
		catch(ErrorException e)
		{
			System.out.println("Invalid College Name");
			Collegename();
		}
			
	}
	public void emailadress()
	{
		while(check == true)
		{
			System.out.print("Enter Your  Email Adress:");
			email = s.nextLine();
			for(int i =0;i	<email.length();i++) {
				if(email.charAt(i) == '@')
				{
				   check = false;
				}	
			}
			if(check == true)
			{
				System.out.println("Email adress should contain @");
				emailadress();
			}		
		}
					
	}
}

ErrorException

package register;

public class ErrorException extends Exception
{
	public ErrorException()
	{
		super("Error");
	}
}

OUTPUTS:







Teachers Day card in java

"As each child grows older, he/she needs to learn in order to survive; teachers are the ones to instill knowledge (DeRoy)." Therefore i have made a simple java program using Swings to wish all the teachers a very happy teachers Day.

Code Explaination

In this code there are 3 classes:
1.teacher class
2.Registration
3.WiishCard classes
The teacher class contains the main method, Registration class contain codes that allows user to register and the wiish class contains the wish for the teacher

Teacher Class

public class teacher 
  {

	  public static void main(String[] args) 
	{
		Registration R1 = new Registration();
		R1.Details();
	}

  }
  

Registration class

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Registration extends JFrame
{
	private static final long serialVersionUID = -7614034343348966109L;
	Registration()
	{
		setVisible(true);
		setTitle("Register");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 570, 430);
		
	}
	JPanel cP;
	JLabel l1,L2,L3,L4;
	public JRadioButton r1,r2;
	JTextField t1,t2;
	JButton jb1;
	public void Details()
	{
		cP = new JPanel();
		cP.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(cP);
		cP.setLayout(null);
		
		l1 = new JLabel("REGISTER");
		l1.setForeground(Color.BLUE);
		l1.setFont(new Font("Times New Roman", Font.PLAIN, 30));
		l1.setBounds(170, 11, 170, 44);
		cP.add(l1);
				
		L2 = new JLabel("Tutor Name:");
		L2.setForeground(Color.BLUE);
		L2.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		L2.setBounds(10, 152, 163, 44);
		cP.add(L2);
		
		L3 = new JLabel("Tutor Gender:");
		L3.setForeground(Color.BLUE);
		L3.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		L3.setBounds(10, 207, 163, 44);
		cP.add(L3);
		
		L4 = new JLabel("Student Name:");
		L4.setForeground(Color.BLUE);
		L4.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		L4.setBounds(10, 262, 163, 44);
		cP.add(L4);
		
		 r1 = new JRadioButton("Male");
		 r1.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		 r1.setBounds(153, 220, 109, 23);
		 cP.add(r1);
		 
		 r2 = new JRadioButton("Female");
		 r2.setFont(new Font("Times New Roman", Font.PLAIN, 20));
		 r2.setBounds(296, 218, 109, 23);
		 cP.add(r2);
		
		 t1 = new JTextField();
		 t1.setBounds(153, 166, 219, 20);
		 cP.add(t1);
		 t1.setColumns(10);
		 
		 ButtonGroup bg = new ButtonGroup();
		 bg.add(r1);
		 bg.add(r2);
		 
		 jb1 = new JButton("WISH");
		 jb1.setFont(new Font("Times New Roman", Font.PLAIN, 15));
		 jb1.setBounds(193, 336, 109, 31);
		 cP.add(jb1);
		 jb1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					wish();
					WiishCards n1 = new WiishCards();
					String a,b;
					if(r1.isSelected())
					{	
						 a= t1.getText();
						 a =  "Sir " + a;
					}
					else
					{
						a=t1.getText();
						a = "Madam " + a;
					}
					b = t2.getText();
					n1.wish(a,b);
				}

				private void wish() 
				{
					setVisible(false);
					
				}
			});
		 
		 t2 = new JTextField();
		 t2.setColumns(10);
		 t2.setBounds(153, 276, 219, 20);
		 cP.add(t2);
		
	}
}

WiishCard class

 import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class WiishCards extends JFrame 
{
	public WiishCards()
	{
		setVisible(true);
		setTitle("Wishing Cards");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 570, 430);
		
		
	}
	public void wish(String a, String b )
	{
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 570, 430);
		getContentPane().setLayout(null);
		
		
		JLabel l2= new JLabel("HAPPY TEACHERS DAY  ");
		l2.setFont(new Font("MV Boli", Font.PLAIN, 35));
		l2.setForeground(Color.BLUE);
		l2.setBounds(39, 24, 478, 47);
		getContentPane().add(l2);

		JLabel l1 = new JLabel();
		l1.setText(a);
		l1.setFont(new Font("Tempus Sans ITC", Font.BOLD | Font.ITALIC, 30));
		l1.setForeground(Color.BLUE);
		l1.setBounds(39, 82, 478, 38);
		getContentPane().add(l1);
		
		JLabel L3 = new JLabel("THANKYOU ");
		L3.setForeground(Color.GREEN);
		L3.setFont(new Font("Viner Hand ITC", Font.PLAIN, 26));
		L3.setBounds(177, 131, 166, 55);
		getContentPane().add(L3);
		
		JLabel l4= new JLabel("For teaching us efficiently even during hard times)");
		l4.setForeground(Color.MAGENTA);
		l4.setFont(new Font("Viner Hand ITC", Font.PLAIN, 17));
		l4.setBounds(20, 189, 434, 70);
		getContentPane().add(l4);
		
		JLabel l5 = new JLabel("it has been an honor to get to learn so many things from you");
		l5.setForeground(Color.MAGENTA);
		l5.setFont(new Font("Viner Hand ITC", Font.PLAIN, 17));
		l5.setBounds(26, 231, 505, 75);
		getContentPane().add(l5);
		
		JLabel l6 = new JLabel();
		l6.setText("Wish From: " + b);
		l6.setForeground(Color.GREEN);
		l6.setFont(new Font("Times New Roman", Font.PLAIN, 15));
		l6.setBounds(53, 317, 434, 63);
		getContentPane().add(l6);
		
		
	}
	
}

output






















Make Registration form using only AWT

Java AWT (Abstract Window Toolkit) is an API to develop GUI in java.

it has many components like button, label, checkbox, etc. used as objects inside a Java Program.Java AWT components are platform-dependent which implies that they are displayed according to the view of the operating system. It is also heavyweight . java. awt package provides classes for AWT api.

In this program i have made a simple registration form with Awt as its major components and also used Swings combo box to get year of study.

In this program there are two classes : 1. Registration class which contains the main method and the details class which does the rest of the work

Output







Registration class

public class Registration 
{
	public static void main(String[] args) 
	{
		Details d1 = new Details();
		d1.Register();

	}
}

detailsClass

import java.awt.*;
import java.awt.event.*;
import javax.swing.JComboBox;


public class Details extends Frame implements ActionListener
{
	TextField t1;
	static JComboBox jc1;
	String[] s = new String[9]; 
	static Checkbox cb1,cb2;
	CheckboxGroup cbg;
	Button b;
	
	Details()
	{
		setVisible(true);
		setBounds(100,50,700,500);
		setTitle("Registration Form");
		setLayout(null);
		
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				dispose();
			}
		});
	}
	void Register()
	{
		setLayout(null);
		Font f = new Font("Arial",Font.BOLD,20);
		Label l1 = new Label("Registration Form");
		l1.setBounds(250, 50, 400, 40);
		l1.setForeground(Color.BLUE);
		l1.setFont(f);
		Label l2 = new Label("Student_Id:");
		l2.setBounds(50, 100, 60, 20);
		t1 = new TextField();
		t1.setBounds(150, 100, 120, 20);
		Label l3 = new Label("Semester:");
		l3.setBounds(50, 150, 60, 20);
		for(int i=1; i<=8;i++)
		{
			s[i] = i+" semester";
		}
		jc1 = new JComboBox(s);
		jc1.setBounds(150, 150, 120, 20);
		Label l4 = new Label("Type:");
		l4.setBounds(50, 200, 60, 20);
		
		
		Label l5 = new Label("Type of study:");
		l5.setBounds(50, 210, 80, 20);
		cbg = new CheckboxGroup();
		cb1 = new Checkbox("GOVERNMENT",cbg,false);
		cb2 = new Checkbox("SELF",cbg,false);
		cb1.setBounds(140, 210, 100, 20);
		cb2.setBounds(240, 210, 100, 20);
		b = new Button("Register");
		b.setBounds(150, 250, 50, 20);
		b.addActionListener(this);
		

		add(l1);
		add(l2);
		add(t1);
		add(l3);
		add(jc1);
		add(l5);
		add(cb1);
		add(cb2);
		add(b);
				
	}
	@Override
	public void actionPerformed(ActionEvent e)
	{
		Label l = new Label("PRINTING DETAILS");
		l.setBounds(250, 300, 100, 30);
		l.setForeground(Color.BLUE);
		Label l2 = new Label();
		l2.setBounds(50, 320, 150, 30);
		Label l3 = new Label();
		l3.setBounds(50, 340, 180, 30);
		Label l4 = new Label();
		l4.setBounds(50, 360, 180, 30);
		Label l5 = new Label();
		l5.setBounds(50, 380, 180, 30);
		int student =Integer.parseInt(t1.getText());
		int fees = 0;
		String Sem = (String) jc1.getSelectedItem();
		
		if(cb1.getState()==true)
			{
			if(Sem.contains("1") || Sem.contains("2"))
					fees=100*2;
			if(Sem.contains("3") || Sem.contains("4"))
					fees=100*4;
			if(Sem.contains("5") || Sem.contains("6"))
					fees=100*6;
			if(Sem.contains("7") || Sem.contains("8"))
					fees=100*8;						
			}
		else if(cb2.getState()==true)
			{
			if(Sem.contains("1") || Sem.contains("2"))
					fees=50000*2;
			if(Sem.contains("3") || Sem.contains("4"))
					fees=40000*4;
			if(Sem.contains("5") || Sem.contains("6"))
					fees=30000*6;
			if(Sem.contains("7") || Sem.contains("8"))
					fees=20000*8;	
			}
			l2.setText("Student Number:" + student+" ");
			l3.setText("Type : Government Scholarship");
			l4.setText("Semester: " + Sem);
			l5.setText("Total Amount to be paid:" + fees);
			
			
			add(l);
			add(l2);
			add(l3);
			add(l4);
			add(l5);
		
	}
 }