Question 1- Write a Java program to create an applet to generate table of a given number between 1 to 20. If number is out of this range ask for new input for the number.
Solution -
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class print_table extends Applet implements ActionListener
{
String str="";
Font f1 = new Font("Arial",Font.BOLD,18);
Label l1 = new Label("Whose table you want to print");
TextField t1 = new TextField(2);
Button b1 = new Button("Print");
int table;
public void init()
{
add(l1);
add(t1);
add(b1);
b1.addActionListener(this);
}
public void paint(Graphics g)
{
g.setFont(f1);
int ypos=70;
if(table>0 && table<21)
{
for(int i=1;i<=10;i++)
{
str = table+"X"+i+"="+table*i;
g.drawString(str,150,ypos);
ypos +=20;
}
}
else
{
str ="Please enter the number between 1-20";
g.setColor(Color.red);
g.drawString(str,20,ypos);
}
}
public void actionPerformed(ActionEvent e)
{
table = Integer.parseInt(t1.getText());
repaint();
}
}
/*
<applet code="print_table.class" width="400" height="400"></applet>
*/
filename: print_table.java
For compiling the file type this command in cmd: javac print_table.javaFor execution of the file use this command: appletviewer print_table.java
Note: Do not forgot to add this code
/*<applet code="print_table.class" width="330" height="350"></applet>*/
in applet program otherwise applet program will not run;
if you have any doubt related to this post, you can ask in comment section and if you want solution of any previous year questions, please post that question in section we will try to solve that


0 comments:
Post a Comment