Coders Packet

Base Power n Prime Program in Java

By Akshay Hire

The Program is written in Java and is made to take the input of the base and power and return the output, it also asks for an integer and returned whether it is prime or not.

The first part of the program will give the output when you enter the base and power compulsory, if you enter only base or only power then it will give you a message, "Enter a valid number". 

 

The second part of the program will give the output whether the given number is prime or not.

If you entered only one number then it will tell you whether it is prime or not.

But if u entered two numbers then the prime numbers coming between those two numbers will be given as output. Thus the range of prime numbers between those two numbers is printed as output.

 

The AWT concept of Java is used in this program.

 

import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;


class Frame4 extends Frame
{
  Panel mainpanel,subpanel1,subpanel2,subpanel3,subpanel4;
  Label lbl1,lbl2,lbl3,lblbase,lblpower,lblans1,lblans2,lblnum1,lblnum2;
  TextField txtbase,txtpower,txtnum1,txtnum2;
  Button btn1,btn2;
  TextArea ans;
  Frame4()
  {
    setSize(600,600);
    setTitle("Design4");
    setLocationRelativeTo(null);
    setLayout(null);
    setUndecorated(true);
    setResizable(false);
    addComponent();
    setVisible(true);
  }
  public void addComponent()
  {
    MyKeyListener4 mkl=new MyKeyListener4(this);
    mainpanel=new Panel();
    mainpanel.setBounds(0,0,600,600);
    mainpanel.setLayout(null);
    mainpanel.setBackground(new Color(254, 200, 181));
    
    lbl1=new Label("Module-1");
    lbl1.setFont(new Font("verdena",Font.BOLD,20));
    lbl1.setForeground(new Color(217, 67, 29));
    lbl1.setBounds(10,20,150,30);
    mainpanel.add(lbl1);
    
    lbl3=new Label("CLOSE");
    lbl3.setFont(new Font("verdena",Font.BOLD,30));
    lbl3.setForeground(new Color(217, 67, 29));
    lbl3.addMouseListener(new MyMouseListener2(this));
    lbl3.setBounds(480,10,120,30);
    
    lbl3.setCursor(new Cursor(Cursor.HAND_CURSOR));
    mainpanel.add(lbl3);
    
    subpanel1=new Panel();
    subpanel1.setBackground(new Color(255, 148, 107));
    subpanel1.setBounds(10,50,580,200);
    subpanel1.setLayout(null);
    
    
    subpanel3=new Panel();
    subpanel3.setBackground(new Color(254, 200, 181));
    subpanel3.setBounds(10,10,560,180);
    subpanel3.setLayout(null);
    subpanel1.add(subpanel3);
    
    lblbase=new Label("Enter Base:");
    lblbase.setFont(new Font("verdena",Font.BOLD,20));
    lblbase.setBounds(10,10,120,20);
    subpanel3.add(lblbase);
    
    txtbase=new TextField();
    txtbase.setBounds(10,40,120,30);
    txtbase.addKeyListener(mkl);
    txtbase.setBackground(new Color(255, 148, 107));
    txtbase.setFont(new Font("verdena",Font.BOLD,20));
    subpanel3.add(txtbase);
    
    lblpower=new Label("Enter Power:");
    lblpower.setFont(new Font("verdena",Font.BOLD,20));
    lblpower.setBounds(150,10,130,20);
    subpanel3.add(lblpower);
    
    txtpower=new TextField();
    txtpower.setBounds(150,40,120,30);
    txtpower.addKeyListener(mkl);
    txtpower.setBackground(new Color(255, 148, 107));
    txtpower.setFont(new Font("verdena",Font.BOLD,20));
    subpanel3.add(txtpower);
    
    btn1=new Button("CLICK FOR ANSWER");
    btn1.setFont(new Font("verdena",Font.BOLD,20));
    btn1.setBounds(300,10,250,60);
    btn1.setActionCommand("btn1");
    btn1.setBackground(new Color(255, 25, 0));
    btn1.addActionListener(new MyActionListener4(this));
    btn1.setForeground(new Color(255, 177, 34));
    
    subpanel3.add(btn1);
    
    lblans1=new Label("ANSWER IS HERE");
    lblans1.setFont(new Font("verdena",Font.BOLD,20));
    lblans1.setAlignment(Label.CENTER);
    lblans1.setForeground(Color.BLACK);
    lblans1.setBackground(new Color(255, 148, 107));
    lblans1.setBounds(30,160,540,60);
    mainpanel.add(lblans1);
    
    mainpanel.add(subpanel1);
    
    lbl2=new Label("Module-2");
    lbl2.setFont(new Font("verdena",Font.BOLD,20));
    lbl2.setForeground(new Color(217, 67, 29));
    lbl2.setBounds(10,260,150,30);
    mainpanel.add(lbl2);
    
    subpanel2=new Panel();
    subpanel2.setBackground(new Color(255, 148, 107));
    subpanel2.setBounds(10,300,580,280);
    subpanel2.setLayout(null);
    
    subpanel4=new Panel();
    subpanel4.setBackground(new Color(254, 200, 181));
    subpanel4.setBounds(10,10,560,260);
    subpanel4.setLayout(null);
    
    lblnum1=new Label("Start Number:");
    lblnum1.setFont(new Font("verdena",Font.BOLD,20));
    lblnum1.setBounds(10,10,140,20);
    subpanel4.add(lblnum1);
    
    lblnum2=new Label("End Number:");
    lblnum2.setFont(new Font("verdena",Font.BOLD,20));
    lblnum2.setBounds(150,10,130,20);
    subpanel4.add(lblnum2);
    
    txtnum1=new TextField();
    txtnum1.setBounds(10,40,120,30);
    txtnum1.setBackground(new Color(255, 148, 107));
    txtnum1.setFont(new Font("verdena",Font.BOLD,20));
    txtnum1.addKeyListener(mkl);
    subpanel4.add(txtnum1);
    
    txtnum2=new TextField();
    txtnum2.setBounds(150,40,120,30);
    txtnum2.setBackground(new Color(255, 148, 107));
    txtnum2.setFont(new Font("verdena",Font.BOLD,20));
    txtnum2.addKeyListener(mkl);
    subpanel4.add(txtnum2);
    
    btn2=new Button("CLICK FOR ANSWER");
    btn2.setFont(new Font("verdena",Font.BOLD,20));
    btn2.setBounds(300,10,250,60);
    btn2.setActionCommand("btn2");
    btn2.addActionListener(new MyActionListener4(this));
    btn2.setBackground(new Color(255, 25, 0));
    btn2.setForeground(new Color(255, 177, 34));
    subpanel4.add(btn2);
    
    ans=new TextArea("See here answer");
    ans.setBounds(10,90,540,160);
    ans.setFont(new Font("verdena",Font.BOLD,20));
    ans.setBackground(new Color(255, 148, 107));
    subpanel4.add(ans);
    
    subpanel2.add(subpanel4);
    mainpanel.add(subpanel2);
      
    add(mainpanel);
  }
}
class MyMouseListener2 extends MouseAdapter
{
  Frame4 ref;
  public MyMouseListener2(Frame4 temp) 
  {
    ref=temp;
  }
  @Override
  public void mouseClicked(MouseEvent e) 
  {
    MyDialog4 md =new MyDialog4(ref);
    
  }
}
class MyActionListener4 implements ActionListener
{
  Frame4 ref;
  MyDialog4 mdref;
  MyActionListener4(Frame4 temp)
  {
    ref=temp;
  }
  MyActionListener4(MyDialog4 md)
  {
    mdref=md;
  }
  @Override
  public void actionPerformed(ActionEvent e) 
  {
    if(e.getActionCommand()=="btn1")
    {
      try
      {
        int base=Integer.parseInt(ref.txtbase.getText());
        int power=Integer.parseInt(ref.txtpower.getText());
        int ans=1;
        for(int i=0;i<power;i++ )
        {
          ans=ans*base;
        }
        ref.lblans1.setText(String.valueOf("Answer = "+ans));;
      }
      catch(NumberFormatException nme)
      {
        ref.lblans1.setText(String.valueOf("Enter valid numbers "));;
        ref.txtbase.setText("");
        ref.txtbase.setText("");
      }
    }
    else if(e.getActionCommand()=="YES")
    {
      mdref.ref.dispose();
      mdref.dispose();
    }
    else if(e.getActionCommand()=="NO")
    {
      mdref.dispose();
    }
    if(e.getActionCommand()=="btn2")
    {
      String str1=ref.txtnum1.getText();
      String str2=ref.txtnum2.getText();
      int num1,num2;
      if(str1.isBlank()&&str2.isBlank())
      {
        ref.ans.setText("Enter Valid Number");
      }
      else if(str1.isBlank()==false&&str2.isBlank())
      {
        num1=Integer.parseInt(str1);
        if(isprime(num1))
        {
          ref.ans.setText(num1+ " is prime");
        }
        else 
        {
          ref.ans.setText(num1+ " is not prime");
        }
      }
      else if(str1.isBlank()&&str2.isBlank()==false)
      {
        num2=Integer.parseInt(str2);
        if(isprime(num2))
        {
          ref.ans.setText(num2+ " is prime");
        }
        else 
        {
          ref.ans.setText(num2+ " is not prime");
        }
      }
      else 
      {
        num1=Integer.parseInt(str1);
        num2=Integer.parseInt(str2);
        int max=num1>num2?num1:num2;
        int min=num1<num2?num1:num2;
        String str="Prime numbers =";
        for(int i=min;i<=max;i++)
        {
          if(isprime(i))
          {
            str= str+" "+i;
          }
        }
        ref.ans.setText(str+ ".");
        str="Prime numbers =";
      }
      
    }
  }
  public boolean isprime(int num) 
  {
    if (num==2) 
    {
      return true;
    }
    for (int i = 2;i<num;i++) {
      if(num%i==0)
      {
        return false;
      }
      
    }
    return true;
  }
}
public class Design4 
{
  public static void main(String ar[])
  {
    Frame4 f4=new Frame4();
    f4.addWindowListener(new WindowCloser(f4));
  }
}
class MyDialog4 extends Dialog
{
  Frame4 ref;
  Button btnyes,btnno;
  Label lblmsg;
  MyDialog4(Frame4 temp)
  {
    super(temp,false);
    ref=temp;
    setSize(300,300);
    setLocationRelativeTo(ref);
    setTitle("confirm");
    setUndecorated(true);
    setBackground(new Color(255, 205, 68));
    setLayout(null);
    initialize();
    setVisible(true);
  }
  public void initialize()
  {
    lblmsg=new Label("Do You Want To Exit?");
    lblmsg.setBounds(10,40,220,30);
    lblmsg.setFont(new Font("verdena",Font.BOLD,20));
    add(lblmsg);
    
    btnyes=new Button("YES");
    btnyes.setBackground(Color.red);
    btnyes.addActionListener(new MyActionListener4(this));
    btnyes.setBounds(50, 100, 50, 50);
    add(btnyes);
    
    btnno=new Button("NO");
    btnno.setBackground(Color.red);
    btnno.setBounds(150, 100, 50, 50);
    btnno.addActionListener(new MyActionListener4(this));
    add(btnno);
  }
}
class MyKeyListener4 implements KeyListener
{
  Frame4 ref;
  MyKeyListener4(Frame4 temp)
  {
    ref=temp;
  }
  @Override
  public void keyTyped(KeyEvent e) 
  {
    char ch=e.getKeyChar();	
    if(Character.isDigit(ch)==false)
    {
      e.consume();
    }
  }
  @Override
  public void keyPressed(KeyEvent e) 
  {
    
  }
  @Override
  public void keyReleased(KeyEvent e) 
  {
    
    
  }	
}
import java.awt.*;
import java.awt.event.*;
public class WindowCloser extends WindowAdapter
{
  Frame f;
  public WindowCloser(Frame f1)
  {
    f=f1;
  }
  public void windowClosing(WindowEvent e)
  {
    f.dispose();
  }
}

 

output window

 

Download project

Reviews Report

Submitted by Akshay Hire (akshayjhire)

Download packets of source code on Coders Packet