Java Programs for Beginners: Examples
Java Programs for Beginners: Examples
Solution :
Slip 1 - B) Write a ‘java’ program to copy only non-numeric data from one file to another file.
Solution :
import [Link].*;
try {
int ch;
if ( ch)) {
[Link](ch);
[Link]();
[Link]();
} catch (IOException e) {
Solution:
[Link]();
Slip 2 - B) Design a screen in Java to handle the Mouse Events such as MOUSE_MOVED and
MOUSE_CLICK and display the position of the Mouse_Click in a TextField.
Solution:
import [Link].*;
import [Link].*;
setLayout(new FlowLayout());
tf = new TextField(30);
add(tf);
addMouseListener(this);
addMouseMotionListener(this);
setSize(400, 400);
setVisible(true);
addWindowListener(new WindowAdapter() {
dispose();
});
int x = [Link]();
int y = [Link]();
new MouseEventDemo();
}
Slip 3 - A) Write a ‘java’ program to check whether given number is Armstrong or not. (Use static
keyword)
Solution:
import [Link];
public class Slip_3_1_A {
static int temp;
public static void main(String agrs[]){
Scanner scan=new Scanner([Link]);
Output:
Slip 3 - B)Define an abstract class Shape with abstract methods area () and volume (). Derive
abstract class Shape into two classes Cone and Cylinder. Write a java Program to calculate area and
volume of Cone and Cylinder.(Use Super Keyword.)
Solution:
import [Link];
class Slip3B{
public static void main(String args[]) throws Exception{
int r,h,s;
Scanner scan = new Scanner([Link]);
[Link]("Enter Radius, Height and Side Values : ");
r = [Link]();
h = [Link]();
s = [Link]();
Shape s1;
Cone c1 = new Cone(r,s);
s1=c1;
[Link]("Area of Cone is : " + [Link]());
[Link]("Volume of Cone is : " +[Link]());
Cylinder cy = new Cylinder(r,h);
s1 =cy;
[Link]("Area of Cylinder is : " + [Link]());
[Link]("Area of Cylinder is : " + [Link]());
}
}
Output:
Sip 4 - A) Write a java program to display alternate character from a given string.[
Solution:
import [Link];
class Slip4A {
public static void main(String args[]){
Scanner scan=new Scanner([Link]);
try {
[Link]("Enter String : ");
String str = [Link]();
for(int i=0;i<[Link]();i+=2) {
[Link](" " + [Link](i));
}
} catch (Exception e) {}
}
}
Output:
Slip 4 - B) Write a java program using Applet to implement a simple arithmetic calculator.
Solution:
import [Link].*;
import [Link].*;
import [Link].*;
/*
</applet>
*/
setLayout(new FlowLayout());
add(tf1);
add(tf2);
add(addBtn);
add(subBtn);
add(mulBtn);
add(divBtn);
add(new Label("Result:"));
add(tfResult);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
try {
double result = 0;
if ([Link]() == addBtn) {
if (num2 != 0) {
} else {
return;
[Link]([Link](result));
[Link]("Invalid Input");
} }}
Slip 5 - A) Write a java program to display following pattern: 5 4 5 3 4 5 2 3 4 5 1 2 3 4 5
Solution:
int n=6;
[Link](j+" ");
[Link]();
Output:
Slip 5 - B) Write a java program to accept list of file names through command line. Delete the files
having extension .txt. Display name, location and size of remaining files.
Solution:
import [Link].*;
class Slip5B{
public static void main(String args[]) throws Exception{
for(int i=0;i<[Link];i++){
File file=new File(args[i]);
if([Link]()){
String name = [Link]();
if([Link](".txt")){
[Link]();
[Link]("file is deleted " + file);
}else{
[Link]("File Name : " + name + "\nFile Location : " +[Link]()+"\
nFile Size : "+[Link]()+" bytes");
}
}
else{
[Link](args[i]+ "is not a file");
}
}
}
}
Output:
Slip 6 - A) Write a java program to accept a number from user, if it zero then throw user defined
Exception “Number Is Zero”, otherwise calculate the sum of first and last digit of that number. (Use
static keyword).
Solution:
import [Link];
static int n;
int first,last=0;
n = [Link]();
if(n!=0){
last = n % 10;
first = n;
while(n>=10){
n = n / 10;
first=n;
}else{
[Link]("Number is Zero");
catch(Exception e){}
Output:
slip 6 - B) Write a java program to display transpose of a given matrix.
Solution:
//creating a matrix
int original[][]={{1,3,4},{2,4,3},{3,4,5}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](original[i][j]+" ");
[Link]();//new line
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](transpose[i][j]+" ");
[Link]();//new line
}
}}
Output:
Slip 7 - A) Write a java program to display Label with text “Dr. D Y Patil College”, background color
Red and font size 20 on the frame.
Solution:
import [Link].*;
[Link](f);
setBackground([Link]);
[Link](true);
[Link](500,300);
}
Output:
Slip 7 - B) Write a java program to accept details of ‘n’ cricket player (pid, pname, totalRuns,
InningsPlayed, NotOuttimes). Calculate the average of all the players. Display the details of player
having maximum average. (Use Array of Object)
Slip 8 - A) Define an Interface Shape with abstract method area(). Write a java program to calculate
an area of Circle and Sphere.(use final keyword)
Solution:
import [Link].*;
interface Shape{
final float pi= 3.14F;
double area();
}
class Circle implements Shape{
int rad;
Circle(int r){
rad=r;
}
public double area(){
return pi*rad*rad;
}
}
class Sphere implements Shape{
int rad;
Sphere(int r){
rad =r;
}
public double area(){
return 4*pi*rad*rad;
}
}
class Slip8A {
public static void main(String args[]) throws Exception{
int r;
Scanner sc = new Scanner([Link]);
[Link]("Enter the Radius : ");
r=[Link]();
Shape sh;
Circle cl=new Circle(r);
sh=cl;
[Link]("Area of Circle : " + [Link]());
Output:
Slip 8 - B) Write a java program to display the files having extension .txt from a given directory.
Solution:
import [Link];
class Slip8B {
public static void main(String[] args) {
File file = new File(" Your File Path ");
String[] fileList = [Link]();
for(String str : fileList) {
if([Link](".txt")){
[Link](str);
}
}
}
}
Output:
Solution:
class Slip9A {
public static void main(String args[]){
int i,j,k=1;;
for(i=1; i<=4; i++){
for(j=1; j<=i; j++){
if(k%2==1){
[Link](1 + " ");
}else{
[Link](0 + " ");
}
k++;
}
[Link]();
}
}
Output:
Slip 9 - B) Write a java program to validate PAN number and Mobile Number. If it is invalid then
throw user defined Exception “Invalid Data”, otherwise display it.
Solution:
import [Link];
class Slip9B{
static int n;
try {
n = [Link]();
switch(n){
case 1 :
if([Link]().matches("(0/91)?[7-9][0-9]{9}")){
}else{
break;
case 2 :
[Link]("Enter PAN Number : ");
if([Link]("[A-Z]{5}[0-9]{4}[A-Z]{1}")){
}else{
break;
default :
catch(Exception e){}
Output:
Slip 10 - A) Write a java program to count the frequency of each character in a given string.
Solution:
{
public static void main(String[] args) {
int i, j;
freq[i] = 1;
if(string[i] == string[j]) {
freq[i]++;
string[j] = '0';
Output:
Slip 10 - B) Write a java program for the following:
Solution:
import [Link].*;
import [Link].*;
import [Link].*;
JLabel l1,l2,l3,l4,l5,l6;
JTextField t1,t2,t3,t4,t5;
JButton b1,b2,b3;
Panel p1,p2,p3,p4,p5;
GridLayout g1,g2,g3,g4,g5,g6;
JFrame jf;
public Slip10B(){
jf = new JFrame();
[Link](JLabel. CENTER);
l4 = new JLabel("Time(Yrs)");
t1 = new JTextField(20);
t2 = new JTextField(20);
t3 = new JTextField(20);
t4 = new JTextField(20);
t5 = new JTextField(20);
b1 = new JButton("Calculate");
b2 = new JButton("Clear");
b3 = new JButton("Close");
p1 = new Panel();
[Link](g1);
[Link](l1);
p2 = new Panel();
g2 = new GridLayout(1,2);
[Link](g2);
[Link](l2);
[Link](t1);
p3 = new Panel();
g3 = new GridLayout(1,4);
[Link](g3);
[Link](l3);
[Link](t2);
[Link](l4);
[Link](t3);
p4 = new Panel();
g4 = new GridLayout(2,2);
[Link](g4);
[Link](l5);
[Link](t4);
[Link](l6);
[Link](t5);
p5 = new Panel();
g5 = new GridLayout(1,3);
[Link](g5);
[Link](b1);
[Link](b2);
[Link](b3);
g6 = new GridLayout(5,1);
[Link](g6);
[Link](p1);
[Link](p2);
[Link](p3);
[Link](p4);
[Link](p5);
[Link](500,250);
[Link](true);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent ae){
int p = [Link]([Link]());
float rt = [Link]([Link]());
float tm = [Link]([Link]());
if([Link]()==b1){
float r=rt/100;
[Link]([Link](iamt));
[Link]([Link](tamt));
if([Link]()==b2){
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
if([Link]()==b3){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Output:
Slip 11 - A) Write a menu driven java program using command line arguments for the following: 1.
Addition 2. Subtraction 3. Multiplication 4. Division.
Solution :
import [Link];
int a,b,n;
try {
a = [Link](args[0]);
b = [Link](args[1]);
n = [Link]();
switch(n){
case 1:
break;
case 2:
break;
case 3:
case 4:
break;
} catch (Exception e) {}
Output:
Slip 11 - B) Write an applet application to display Table lamp. The color of lamp should get change
randomly.
Solution :
import [Link].*;
import [Link].*;
Graphics gl;
repaint();
R = (float)[Link]();
G = (float)[Link]();
B = (float)[Link]();
Color cl = new Color(R,G,B);
[Link](0,250,290,290);
[Link](125,250,125,160);
[Link](175,250,175,160);
[Link](85,157,130,50,-65,312);
[Link](85,87,130,50,62,58);
[Link](85,177,119,89);
[Link](215,177,181,89);
[Link](cl);
[Link](78,120,40,40,63,-174);
[Link](120,96,40,40);
[Link](173,100,40,40,110,180);
/*
</applet>
*/
Output:
Slip 12 - A) Write a java program to display each String in reverse order from a String array.
Solution :
class Slip12A{
}
}
Output:
Slip 12 - B) Write a java program to display multiplication table of a given number into the List box
by clicking on button.
Solution :
import [Link].*;
import [Link].*;
import [Link].*;
[Link]("1");
[Link]("2");
[Link]("3");
[Link]("4");
[Link]("5");
[Link]("6");
[Link]("7");
[Link]("8");
[Link]("9");
[Link]("10");
add(Multi);
add(b1);
[Link](this);
for(int i=1;i<=10;i++){
int a = num*i;
count = count+20;
repaint();
/*
</applet>
*/
Output:
Slip 13 - A) Write a java program to accept ‘n’ integers from the user & store them in an ArrayList
collection. Display the elements of ArrayList collection in reverse order.
Solution :
import [Link].*;
import [Link];
class Slip13A{
String temp=null;
int i,j,n;
try{
[Link]("Enter How May Element You Want = ");
n = [Link]();
[Link]();
name[i] = [Link]();
}catch(Exception e){}
Output:
Slip 13 - B) Write a java program that asks the user name, and then greets the user by name.
Before outputting the user's name, convert it to upper case letters. For example, if the user's name
is Raj, then the program should respond "Hello, RAJ, nice to meet you!".
Solution :
import [Link];
class Slip13B {
String str;
try {
str = [Link]();
} catch (Exception e) {}
}
Output:
Solution :
import [Link].*;
class Slip14A {
int base,exp;
base = [Link]();
exp = [Link]();
}else{
return 1;
Output:
Slip 14 - B) Write a java program to accept the details of employee (Eno, EName, Sal) and display it
on next frame using appropriate event .
Solution :
import [Link].*;
import [Link].*;
Frame f;
Button next;
Emp_details() {
[Link](empno);
[Link](tempno);
[Link](empname);
[Link](tempname);
[Link](sal);
[Link](tsal);
[Link](next);
[Link](this);
[Link](new FlowLayout());
[Link](400, 400);
[Link](true);
empno = [Link]();
empname = [Link]();
sal = [Link]();
[Link](false);
}
class FrameDetails extends Frame {
Frame f;
[Link](empno);
[Link](tempno);
[Link](empname);
[Link](tempname);
[Link](sal);
[Link](tsal);
[Link](no);
[Link](name);
[Link](s);
[Link](new FlowLayout());
[Link](400, 400);
[Link](true);
class Slip14B {
Output:
Slip 15 - A) Write a java program to search given name into the array, if it is found then display its
index otherwise display appropriate message.
Solution :
import [Link];
class Slip15A{
int i,n=0;
boolean a=false;
try {
String s= [Link]();
if(arr[i].equals(s))
n = i;
a = true;
break;
if(a){
[Link]("not Found");
} catch (Exception e) {}
Output:
Solution :
import [Link];
import [Link];
/*
</applet>
*/
Output:
Slip 16 - A) Write a java program to calculate sum of digits of a given number using recursion.
Solution :
import [Link].*;
int n;
int a = obj.sum_digit(n);
sum = n%10;
if(n==0){
return 0;
}else{
Output:
Slip 16 - B) Write a java program to accept n employee names from user. Sort them in ascending
order and Display them.(Use array of object and Static keyword)
Slip 17 -A) Write a java Program to accept ‘n’ no’s through command line and store only Armstrong
no’s into the array and display that array.
Solution :
class Slip17A{
int num,i,r,sum=0,temp,count=0;;
num = [Link];
a[i] = [Link](args[i]);
sum =0;
temp =a[i];
while(a[i]!=0){
r = a[i]%10;
sum = sum+r*r*r;
a[i] = a[i]/10;
if(temp==sum){
b[count] = temp;
count++;
}
Output:
Slip 17 - B) Define a class Product (pid, pname, price, qty). Write a function to accept the product
details, display it and calculate total amount. (use array of Objects)
Solution :
import [Link].*;
class Product{
String pname;
void accept(){
pname=[Link]();
pid = [Link]([Link]());
qty = [Link]([Link]());
price = [Link]([Link]());
} catch (Exception e) { }
void display(){
total = qty*price;
}
class Slip17B {
int n;
float to=0;
n = [Link]([Link]());
p1[i]=new Product();
p1[i].accept();
p1[i].display();
Output:
Slip 18 - A) Write a Java program to calculate area of Circle, Triangle & Rectangle.(Use Method
Overloading)
Solution :
import [Link].*;
class AreaCalculate{
void area(int r){
return b*h/2;
return l+db;
class Slip18A {
int r, b, l, db;
float h;
r = [Link]();
b = [Link]();
h = [Link]();
l = [Link]();
db = [Link]();
[Link](r);
Output:
Slip 18 - B) Write a java program to copy the data from one file into another file, while copying
change the case of characters in target file and replaces all digits by ‘*’ symbol.
Solution :
import [Link].*;
class Slip18B{
int c;
while ((c=[Link]())!=-1){
if([Link](c)==false){
if([Link](c)){
[Link]([Link](c));
}else if([Link](c)){
[Link]([Link](c));
}else{
[Link]('*');
[Link]();
[Link]();
Output:
[Link]
[Link]
Solution :
import [Link];
class Slip19A {
int i,a,b,c,n;
try {
n = [Link]();
a = b = 1;
c = a + b;
[Link](" "+c);
a = b;
b = c;
} catch (Exception e) {}
}
public static void main(String args[]){
fibo();
Output:
Slip 19 - B) Create an Applet that displays the x and y position of the cursor movement using
Mouse and Keyboard. (Use appropriate listener).
Slip 20 - A) Write a java program using AWT to create a Frame with title “TYBBACA”, background
color RED. If user clicks on close button then frame should close.
Solution :
import [Link].*;
import [Link].*;
class Slip20A {
[Link](400, 400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link]().setBackground([Link]);
[Link](true);
Slip 20 - B) Construct a Linked List containing name: CPP, Java, Python and PHP. Then extend your
java program to do the following: i. Display the contents of the List using an Iterator ii. Display the
contents of the List in reverse order using a ListIterator.
Solution :
import [Link].*;
[Link]("CPP");
[Link]("JAVA");
[Link]("Python");
[Link]("PHP");
Iterator il=[Link]();
while([Link]()){
[Link]([Link]());
ListIterator li1=[Link]();
while([Link]()){
[Link]();
while([Link]()){
[Link]("" + [Link]());
Output:
Slip 21 - A) Write a java program to display each word from a file in reverse order.
Solution :
import [Link].*;
import [Link];
class Slip21A{
while([Link]()){
String s=[Link]();
buffer=[Link]();
[Link](ans);
}catch(Exception e){
[Link]("Error...!");
[Link]();
[Link]();
Output:
[Link]
[Link]
Slip 21 - B) Create a hashtable containing city name & STD code. Display the details of the
hashtable. Also search for a specific city and display STD code of that city.
Solution :
import [Link].*;
import [Link];
Enumeration en;
int i,n,std,val,max=0;
try {
n = [Link]();
[Link]();
cname = [Link]();
std = [Link]();
[Link]();
[Link](cname,std);
}
[Link]("Enter city name to search : ");
nm = [Link]();
en=[Link]();
while([Link]()){
str=(String)[Link]();
val=(Integer)[Link](str);
if([Link](nm)){
} catch (Exception e) {}
Solution :
import [Link];
{
if (num >= 1)
else
return 1;
Output:
Slip 22 - B) Write a java program for the following: 1. To create a file. 2. To rename a file. 3. To
delete a file. 4. To display path of a file.
Solution :
import [Link].*;
import [Link].*;
class Slip22B {
[Link]("1. Press 1 Create File\n2. Press 2 Rename a File\n3. Press 3 Delete a File\n4.
Press 4 Display Path of a File");
switch(num){
case 1 :
if ([Link]()) {
} else {
case 2 :
if([Link](newfile)){
[Link]("File renamed");
}else{
break;
case 3 :
if ([Link]()) {
} else {
break;
case 4 :
break;
break;
}
Output:
Slip 23 - A) Write a java program to check whether given file is hidden or not. If not then display its
path, otherwise display appropriate message.
Solution :
import [Link].*;
import [Link].*;
try {
[Link]("File is Hidden");
}else{
} catch(Exception e) {
[Link]();
Output:
Solution :
import [Link].*;
import [Link].*;
public class Slip23B extends JFrame implements ActionListener{
new Slip23B();
public Slip23B(){
[Link](600,500);
[Link](200,200);
[Link](OpenItem);
[Link](SaveItem);
[Link](QuitItem);
[Link](UndoItem);
[Link](RedoItem);
[Link](CutItem);
[Link](CopyItem);
[Link](PasteItem);
[Link](OpenIcon);
[Link](SaveIcon);
[Link](QuitIcon);
[Link](UndoIcon);
[Link](RedoIcon);
[Link](CutIcon);
[Link](CopyIcon);
[Link](PasteIcon);
[Link](filMenu);
[Link](filEdit);
[Link](filSearch);
[Link](menuBar);
[Link](true);
}
@Override
Output:
Slip 24 - A) Write a java program to count number of digits, spaces and characters from a file.
Solution :
import [Link].*;
class Slip24A{
int c;
int letter=0;
int space=0;
int num=0;
int other=0;
while ((c=[Link]())!=-1){
if([Link](c)){
num ++;
}else if([Link](c)){
letter++;
}else if([Link](c)){
space++;
}else{
other ++;
[Link]();
[Link]();
Output:
[Link]
Slip 24 - B) Create a package TYBBACA with two classes as class Student (Rno, SName, Per) with a
method disp() to display details of N Students and class Teacher (TID, TName, Subject) with a
method disp() to display the details of teacher who is teaching Java subject. (Make use of finalize()
method and array of Object)
Solution :
import TYBBACA.*;
import [Link].*;
int r,n1,n2,t;
float per;
n1 = [Link]([Link]());
n2 = [Link]([Link]());
r = [Link]([Link]());
snm=[Link]();
per=[Link]([Link]());
tnm=[Link]();
sub=[Link]();
[Link]("Student Details");
((Student) s1[i]).disp();
[Link]("Teacher Details");
if([Link](t1[j].sub)){
t1[j].disp();
Slip 25 - A) Write a java program to check whether given string is palindrome or not.
Solution :
import [Link];
int i=0,h=0;
try {
[Link]("Enter String : ");
int j= [Link]()-1;
while(i<j){
if([Link](i++) != [Link](j--)){
h=h+i;
if(h>0){
}else{
[Link]("String is palindrome");
} catch (Exception e) {}
}
Output:
Slip 25 - B) Create a package named Series having three different classes to print series: i. Fibonacci
series ii. Cube of numbers iii. Square of numbers Write a java program to generate ‘n’ terms of the
above series.
Slip 26 - A) Write a java program to display ASCII values of the characters from a file.
Solution :
import [Link].*;
class Slip26A{
public static void main(String args[]) throws IOException{
char ch;
FileReader fr = new FileReader("[Link]");
int c;
while ((c=[Link]())!=-1){
ch=(char)c;
if([Link](ch)==false && ([Link](c)==false)){
[Link]("ASCII "+ch+" : "+ c);
}
}
[Link]();
}
}
Output:
Slip 26 - B) Write a java program using applet to draw Temple.
Solution :
import [Link];
import [Link];
import [Link];
setBackground([Link]);
}
[Link]([Link]);
[Link]([Link]);
/*
*/
Output:
Slip 27 - A) Write a java program to accept a number from user, If it is greater than 1000 then
throw user defined exception “Number is out of Range” otherwise display the factors of that
number. (Use static keyword).
Solution :
import [Link].*;
class Slip27A{
static int n;
try {
[Link]("Enter Number : ");
n = [Link]([Link]());
if(n>1000){
}else{
if(n%i==0){
[Link](""+[Link]());
Output:
Slip 27 - B) Write a java program to accept directory name in TextField and display list of files and
subdirectories in List Control from that directory by clicking on Button.
Solution :
import [Link].*;
import [Link].*;
import [Link].*;
Graphics g;
List l;
TextField t1;
Button b1;
Label l1;
public Slip27B(){
[Link](new FlowLayout());
[Link](400,400);
[Link](true);
l = new List(10);
b1 = new Button("Display");
[Link](50,100,80,80);
[Link](50,150,80,80);
[Link](50,200,80,80);
[Link](50,300,100,100);
add(l1);
add(t1);
add(b1);
add(l);
[Link](this);
if([Link]()==b1){
try{
String nm = [Link]();
String s1[]=[Link]();
if(s1==null){
}else{
[Link](s1[i]);
}catch(Exception ee){}
Slip 28 - A) Write a java program to count the number of integers from a given list. (Use Command
line arguments).
Solution :
import [Link].*;
int count = 0;
[Link](args[i]);
try {
int j = [Link](element);
count++;
} catch (NumberFormatException e) {}
Output:
Slip 28 - B) Write a java Program to accept the details of 5 employees (Eno, Ename, Salary) and
display it onto the JTable.
Solution:
import [Link].*;
JFrame f;
JTable j;
Question_2(){
f = new JFrame();
[Link]("Employee Details");
String data[][] = {
{"1","Tushar","50,000"},
{"2","Mihir","20,000"},
{"3","ANurag","25,000"},
{"4","Abhishekr","20,000"},
};
[Link](30,40,200,300);
[Link](sp);
[Link](500,200);
[Link](true);
}
public static void main(String args[]) {
new Question_2();
Output:
Slip 29 - A) Write a java program to check whether given candidate is eligible for voting or not.
Handle user defined as well as system defined Exception.
Solution:
import [Link].*;
class Voting
int age=[Link]();
if((age>=18)&&(age<=100))
else
Slip 29 - B) Write a java program using Applet for bouncing ball. Ball should change its color for
each bounce.
Solution:
import [Link].*;
import [Link].*;
import [Link].*;
//
Visit [Link]
*/
Thread t=null;
int flagx3,flagy3,flagx4,flagy4;
addMouseListener(this);
t=new Thread(this);
[Link]();
for(;;)
try
repaint();
if(y1<=50)
flagx1=0;
else if(y1>=300)
flagx1=1;
if(x1<=10)
flagy1=0;
else if(x1>=400)
flagy1=1;
if(y2<=50)
flagx2=0;
else if(y2>=300)
flagx2=1;
if(x2<=10)
flagy2=0;
else if(x2>=400)
flagy2=1;
if(y3<=50)
flagx3=0;
else if(y3>=300)
flagx3=1;
if(x3<=10)
flagy3=0;
else if(x3>=400)
flagy3=1;
if(y4<=50)
flagx4=0;
else if(y4>=300)
flagx4=1;
if(x4<=10)
flagy4=0;
else if(x4>=400)
flagy4=1;
[Link](10);
}catch(InterruptedException e){}
}
public void paint(Graphics g)
[Link](10,50,410,270);
[Link]([Link]);
[Link](x1,y1,20,20);
if(flagx1==1)
y1-=2;
else if(flagx1==0)
y1+=2;
if(flagy1==0)
x1+=4;
else if(flagy1==1)
x1-=4;
[Link]([Link]);
[Link](x2,y2,20,20);
if(flagx2==1)
y2-=4;
else if(flagx2==0)
y2+=4;
if(flagy2==0)
x2+=3;
else if(flagy2==1)
x2-=3;
[Link]([Link]);
[Link](x3,y3,20,20);
if(flagx3==1)
y3-=6;
else if(flagx3==0)
y3+=6;
if(flagy3==0)
x3+=2;
else if(flagy3==1)
x3-=2;
[Link]([Link]);
[Link](x4,y4,20,20);
if(flagx4==1)
y4-=5;
else if(flagx4==0)
y4+=5;
if(flagy4==0)
x4+=1;
else if(flagy4==1)
x4-=1;
Slip 30 - A) Write a java program to accept a number from a user, if it is zero then throw user
defined Exception “Number is Zero”. If it is non-numeric then generate an error “Number is
Invalid” otherwise check whether it is palindrome or not.
Solution:
import [Link].*;
import [Link];
class slip{
int n;
try {
n = [Link]();
if(n==0){
}else{
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
if(temp==sum){
}else{
[Link]("Not Palindrome");
[Link]("Number is 0");
}
Output: