Core Java Solved Slips
Core Java Solved Slips
Core Java Solved Slips
Output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac slip1.java
Slip 2.A) Write a java program to display all the vowels from a
given string
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Slip2q2 {
public static void main(String[] args) {
new MyFrame("Mouse Events");
}
}
import java.io.*;
import java.util.*;
class slip3
{
public static void main(String arg[])
{
int check,rem=0;
int sum=0,num;
System.out.println("enter the number to check armstrong or
not");
Scanner sc=new Scanner(System.in);
num=sc.nextInt();
check=num;
while(check!=0)
{
rem=check%10;
sum=sum+(rem*rem*rem);
check=check/10;
}
if(num==sum)
System.out.println("the number is armstrong");
else
System.out.println("the number is not armstrong");
}
}
Output
C:\Program Files\Java\jdk1.8.0_144\bin>javac slip3.java
import java.util.Scanner;
System.out.println("Enter radius");
int radius = scanner.nextInt();
System.out.println("Enter height");
int height = scanner.nextInt();
shape=new Cylinder(radius,height);
shape.area();
shape.volume();
}else {
System.out.println("invalid input");
}
}
abstract static class Shape{
abstract void area();
abstract void volume();
}
static class Cone extends Shape {
int radius,height;
Cone(int radius,int height){
this.radius=radius;
this.height=height;
}
void area(){
float slant_height=(height*height)+(radius*radius);
float area =
(float)(Math.PI*radius*(radius+Math.sqrt(slant_height)));
System.out.println("Area of Cone : "+area);
}
void volume(){
float volume = (float)(Math.PI*radius*radius*(height/3));
System.out.println("Volume of cone : "+volume);
}
}
static class Cylinder extends Shape {
int radius, height;
Cylinder(int r,int h){
radius =r;
height =h;
}
public void area(){
float area=(float)((2*Math.PI* radius *
height)+(2*Math.PI* radius * radius));
System.out.println("Area of Cylinder : "+area);
}
public void volume(){
float volume=(float)(Math.PI* radius * radius * height);
System.out.println("Volume of Cylinder : "+volume);
}
}
}
import java.io.*;
public class slip4
{
public static void main( String arg[])
{
String s="hello world!";
for(int i=0;i<s.length();i+=2)
{
System.out.println(s.charAt(i));
}
}
}
Output:
C:\Users\BBA11>cd C:\Program Files\Java\jdk1.8.0_144\bin
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Slip4B extends Applet implements ActionListener {
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13,
b14, b15, b16;
String s1 = "", s2;
Frame f;
Panel p2;
TextField t;
int n1, n2;
public void init(){
setLayout(new BorderLayout());
Frame title = (Frame) this.getParent().getParent();
title.setTitle("Simple Calc");
t = new TextField();
p2 = new Panel();
p2.setLayout(new GridLayout(6, 4, 2, 1));
p2.setFont( new Font("Times New Roman",Font.BOLD,15));
b1 = new Button("1");
b1.addActionListener(this);
b2 = new Button("2");
b2.addActionListener(this);
b3 = new Button("3");
b3.addActionListener(this);
b4 = new Button("+");
b4.addActionListener(this);
b5 = new Button("4");
b5.addActionListener(this);
b6 = new Button("5");
b6.addActionListener(this);
b7 = new Button("6");
b7.addActionListener(this);
b8 = new Button("-");
b8.addActionListener(this);
b9 = new Button("7");
b9.addActionListener(this);
b10 = new Button("8");
b10.addActionListener(this);
b11 = new Button("9");
b11.addActionListener(this);
b12 = new Button("*");
b12.addActionListener(this);
b13 = new Button("Clear");
b13.addActionListener(this);
b14 = new Button("0");
b14.addActionListener(this);
b15 = new Button("/");
b15.addActionListener(this);
b16 = new Button("=");
b16.addActionListener(this);
add(t, "North");
p2.add(b9);
p2.add(b10);
p2.add(b11);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b12);
p2.add(new Label(""));
p2.add(b14);
p2.add(new Label(""));
p2.add(b15);
p2.add(new Label(""));
p2.add(new Label(""));
p2.add(new Label(""));
p2.add(b16);
add(p2);
p2.add(new Label(""));
p2.add(b13);
}
public void actionPerformed(ActionEvent e1){
String str = e1.getActionCommand();
if (str.equals("+") || str.equals("-") || str.equals("*") ||
str.equals("/")){
String str1 = t.getText();
s2 = str;
n1 = Integer.parseInt(str1);
s1 = "";
}else if (str.equals("=")){
String str2 = t.getText();
n2 = Integer.parseInt(str2);
int sum = 0;
if (s2 == "+")
sum = n1 + n2;
else if (s2 == "-")
sum = n1 - n2;
else if (s2 == "*")
sum = n1 * n2;
else if (s2 == "/")
sum = n1 / n2;
String str1 = String.valueOf(sum);
t.setText("" + str1);
s1 = "";
}else if (str.equals("Clear")){
t.setText("");
}else{
s1 += str;
t.setText("" + s1);
}
}
}
/*
* <applet code="Slip4B" height=250 width=250>
*
* </applet>
*/
import java.io.*;
public class pattern {
public static void main(String[] args) {
int n=5;
for (int i=n;i>0;i--){
for (int j=i;j<=n;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
import java.io.*;
class Slip5B{
public static void main(String args[]) throws Exception{
for(int i=0;i<args.length;i++){
File file=new File(args[i]);
if(file.isFile()){
String name = file.getName();
if(name.endsWith(".txt")){
file.delete();
System.out.println("file is deleted " + file);
}else{
System.out.println("File Name : " + name + "\nFile
Location : " +file.getAbsolutePath()+"\nFile Size :
"+file.length()+" bytes");
}
}
else{
System.out.println(args[i]+ "is not a file");
}
}
}
}
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).
import java.io.*;
class NumZero extends Exception{}
public class Slip6A {
static int n;
public static void main(String args[]){
int first,last=0;
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter Number : ");
n = Integer.parseInt(dr.readLine());
if(n!=0){
last = n % 10;
first = n;
while(n>=10){
n = n / 10;
}
first=n;
System.out.print("Sum of First and Last Number is : " +
(first + last));
}else{
throw new NumZero();
}
} catch (NumZero nz) {
System.out.println("Number is Zero");
}
catch(Exception e){}
}
}
import java.util.Scanner;
}
}
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.
import java.awt.*;
import java.awt.event.*;
public class Slip7A extends Frame{
public void paint(Graphics g){
Font f = new Font("Georgia",Font.PLAIN,20);
g.setFont(f);
g.drawString("Dr D Y Patil College", 50, 70);
setBackground(Color.RED);
}
public static void main(String args[]){
Slip7A sl = new Slip7A();
sl.setVisible(true);
sl.setSize(200,300);
}
}
import java.io.*;
import java.util.*;
interface shape
{
float pi=3.14f;
void area();
}
class circle implements shape
{
int r;
circle(int r)
{
this.r=r;
}
public void area()
{
System.out.println("Area of circle is :"+(pi*r*r));
}
}
class sphere implements shape
{
int r;
sphere(int r)
{
this.r=r;
}
public void area()
{
System.out.println("Area of sphere is :"+(4*pi*r*r));
}
}
class slip8
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the redius for circle :");
int n=sc.nextInt();
shape s;
s=new circle(n);
s.area();
System.out.println("enter the redius for sphere :");
n=sc.nextInt();
s=new sphere(n);
s.area();
}
}
Output:
C:\Program Files\Java\jdk1.8.0_144\bin>java slip8
enter the redius for circle :
3
Area of circle is :28.26
enter the redius for sphere :
6
Area of sphere is :452.16
import java.io.*;
class invaliddetails extends Exception{}
class Slip9B{
static int n;
public static void main( String args[]){
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("********* Do you Want to Validate
********* \n1. Mobile Number Press : 1 \n2. PAN Card Press :
2 \nEnter Number : ");
n = Integer.parseInt(dr.readLine());
switch(n){
case 1 :
System.out.print("Enter Mobile Number : ");
Long num = Long.parseLong(dr.readLine());
if(num.toString().matches("(0/91)?[7-9][0-9]{9}")){
System.out.print("Enter Valid Mobile Number..!");
}else{
throw new invaliddetails();
}
break;
case 2 :
System.out.print("Enter PAN Number : ");
String str= dr.readLine();
if(str.matches("[A-Z]{5}[0-9]{4}[A-Z]{1}")){
System.out.print("Enter Valid PAN CARD
Number..!");
}else{
throw new invaliddetails();
}
break;
default :
throw new invaliddetails();
}
} catch (invaliddetails nz) {
System.out.println("You Enter Invalid Details...!");
}
catch (NumberFormatException e){
System.out.println("You Enter Invalid Details...!");
}
catch(Exception e){}
}
}
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class calculator {
public static void main(String[] args){
int num1=0,num2=0,option,ex=0;
Scanner sc = new Scanner(System.in);
do{
System.out.println("Enter your choice from the following
menu:");
System.out.println("1.Addition \n2.Subtraction
\n3.Multiplication \n4.Division \n5.Exit");
option = sc.nextInt();
if(option>4 || option<0){
if (option==5){
System.out.println("You want to Exit.");
}else
System.out.println("Invalid choice");
}
else{
System.out.print("Enter the first number : ");
num1=sc.nextInt();
System.out.print("Enter the second number : ");
num2=sc.nextInt();
}
switch(option){
case 1:System.out.println("Addition of "+num1+" and
"+num2+" is "+(num1+num2));
break;
case 2:System.out.println("Subtraction of "+num1+"
and "+num2+" is "+(num1-num2));
break;
case 3:System.out.println("Multiplication of "+num1+"
and "+num2+" is "+(num1*num2));
break;
case 4: if(num2==0)
System.out.println("Error!!! In Division denominator
cannot be 0!");
else{
System.out.println("In division of "+num1+" by
"+num2+" is "+(num1/num2)+" and remainder is
"+(num1%num2));
}
break;
case 5: break;
}
if(option==5){
break;
}else{
System.out.println("Do you want to continue? \n1.Yes
\n2.No");
ex=sc.nextInt();
}
}while(ex==1);
sc.close();
}
}
Slip 11.B) Write an applet application to display Table lamp. The
color of lamp should get change randomly.
import java.awt.*;
import java.applet.*;
public class Slip11B extends Applet{
public float R,G,B;
Graphics gl;
public void init(){
repaint();
}
public void paint(Graphics g){
R = (float)Math.random();
G = (float)Math.random();
B = (float)Math.random();
Color cl = new Color(R,G,B);
g.drawRect(0,250,290,290);
g.drawLine(125,250,125,160);
g.drawLine(175,250,175,160);
g.drawArc(85,157,130,50,-65,312);
g.drawArc(85,87,130,50,62,58);
g.drawLine(85,177,119,89);
g.drawLine(215,177,181,89);
g.setColor(cl);
g.fillArc(78,120,40,40,63,-174);
g.fillOval(120,96,40,40);
g.fillArc(173,100,40,40,110,180);
}
}
/*
<applet code="Slip11B.class" width="300" height="300">
</applet>
*/
import java.io.*;
class Slip12A{
public static void main(String args[]){
String arr[] = {"swarup", "Sayali", "Mahesh"};
for(int i=arr.length-1; i>=0; i--){
System.out.print(arr[i] + ' ');
}
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Slip12B extends Applet implements ActionListener{
Button b1 = new Button("Show");
List Multi = new List();
String str ="";
public void init(){
Multi.add("1");
Multi.add("2");
Multi.add("3");
Multi.add("4");
Multi.add("5");
Multi.add("6");
Multi.add("7");
Multi.add("8");
Multi.add("9");
Multi.add("10");
add(Multi);
add(b1);
b1.addActionListener(this);
}
public void paint(Graphics g){
int count = 100;
int num = Integer.parseInt(Multi.getSelectedItem());
for(int i=1;i<=10;i++){
int a = num*i;
g.drawString(i +" * " + i +" = "+ a,100,count);
count = count+20;
}
}
public void actionPerformed(ActionEvent e){
repaint();
}
}
/*
<applet code="Slip12B.class" width="300" height="300">
</applet>
*/
import java.io.*;
import java.util.*;
class slip13
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the limit of array list");
int n=sc.nextInt();
ArrayList al=new ArrayList();
System.out.println("enter the elements of arry list");
for(int i=0;i<n;i++)
{
String el=sc.next();
al.add(el);
}
System.out.println("the original array list is :"+al);
Collections.reverse(al);
System.out.println("the original reverse array list is :"+al);
}
}
Output:
C:\Program Files\Java\jdk1.8.0_144\bin>java slip13
enter the limit of array list
4
enter the elements of arry list
abcd
the original array list is :[a, b, c, d]
the original reverse array list is :[d, c, b, a]
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!".
import java.io.DataInputStream;
class Slip13B {
public static void main(String args[]){
String str;
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter Username : ");
str = dr.readLine();
System.out.print("\"Hello, " + str.toUpperCase() + ", nice
to meet you!\"");
} catch (Exception e) {}
}
}
Slip 14 A) Write a Java program to calculate power of a
number using recursion.
import java.util.*;
class Slip14A {
public static void main(String args[]){
int base,exp;
Scanner sc =new Scanner(System.in);
System.out.print("Enter the Base Number : ");
base = sc.nextInt();
System.out.print("Enter the Exponent Number : ");
exp = sc.nextInt();
int result = power(base, exp);
System.out.print("Answer : "+ result);
}
private static int power(int base, int exp) {
if(exp!=0){
return (base * power(base, exp-1));
}else{
return 1;
}
}
}
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 .
import java.awt.*;
import java.awt.event.*;
class Emp_details implements ActionListener {
Frame f;
Label empno, empname, sal;
TextField tempno, tempname, tsal;
Button next;
Emp_details() {
f = new Frame("\t Employee Details:");
empno = new Label("\t Employee Id:");
empname = new Label("\t Employee Name:");
sal = new Label("\t Employee Sal:");
tempno = new TextField(25);
tempname = new TextField(25);
tsal = new TextField(25);
next = new Button("Next");
f.add(empno);
f.add(tempno);
f.add(empname);
f.add(tempname);
f.add(sal);
f.add(tsal);
f.add(next);
next.addActionListener(this);
f.setLayout(new FlowLayout());
f.setSize(400, 400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String empno, empname, sal;
empno = tempno.getText();
empname = tempname.getText();
sal = tsal.getText();
f.setVisible(false);
new FrameDetails(empno, empname, sal);
}
}
class FrameDetails extends Frame {
Frame f;
Label empno, empname, sal;
TextField tempno, tempname, tsal;
FrameDetails(String no, String name, String s) {
f = new Frame("Employee Details:");
empno = new Label("Employee ID:");
empname = new Label("Employee Name:");
sal = new Label("Employee Salary:");
tempno = new TextField(25);
tempname = new TextField(25);
tsal = new TextField(25);
f.add(empno);
f.add(tempno);
f.add(empname);
f.add(tempname);
f.add(sal);
f.add(tsal);
tempno.setText(no);
tempname.setText(name);
tsal.setText(s);
f.setLayout(new FlowLayout());
f.setSize(400, 400);
f.setVisible(true);
}
}
class Slip14B {
public static void main(String args[]) {
new Emp_details();
}
}
import java.io.DataInputStream;
class Slip15A{
public static void main(String args[]){
String arr[] = {"saurabh", "Sapkal", "Mahesh","priya"};
int i,n=0;
boolean a=false;
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter String : ");
String s= dr.readLine();
for(i = 0; i < arr.length; i++)
{
if(arr[i].equals(s))
{
n = i;
a = true;
break;
}
}
if(a){
System.out.println("arr" + "["+ i + "]");
}else{
System.out.println("not Found");
}
} catch (Exception e) {}
}
}
import java.applet.Applet;
import java.awt.Graphics;
public class Slip15B extends Applet{
public void paint(Graphics g){
g.drawOval(80, 70, 150, 150);
g.fillOval(120, 120, 15, 15);
g.fillOval(170, 120, 15, 15);
g.drawArc(130, 180, 50, 20, 180, 180);
}
}
/*
<applet code="Slip15B.class" width="300" height="300">
</applet>
*/
import java.util.*;
public class Slip16A {
int sum =0;
public static void main(String args[]) throws Exception{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Number : ");
n =s.nextInt();
Slip16A obj = new Slip16A();
int a = obj.sum_digit(n);
System.out.println("Sum of Digit is : "+a);
}
int sum_digit(int n){
sum = n%10;
if(n==0){
return 0;
}else{
return sum +sum_digit(n/10);
}
}
}
import java.util.*;
class slip16b
{
static String[] str=new String[5];
static Scanner sc=new Scanner(System.in);
static ArrayList<String>list=new ArrayList<String>();
public static void main(String args[])
{
for(int i=0;i<str.length;i++)
{
System.out.print("please insert employee name of index
of["+i+"] :");
str[i]=sc.next();
list.add(str[i]);
}
Collections.sort(list);
System.out.println(list);
}
}
import java.io.*;
class Slip17A{
public static void main(String args[]){
int num,i,r,sum=0,temp,count=0;
num = args.length;
int a[]= new int[num];
int b[]= new int[10];
for(i=0; i<num; i++){
a[i] = Integer.parseInt(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++;
}
}
for(i=0; i<count; i++){
System.out.print(b[i] + " ");
}
}
}
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)
import java.io.*;
class Product{
String pname;
int pid, qty;
float price, total;
void accept(){
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
System.out.println("Enter the producat Name : ");
pname=br.readLine();
System.out.println("Enter pid, qty and price : ");
pid = Integer.parseInt(br.readLine());
qty = Integer.parseInt(br.readLine());
price = Float.parseFloat(br.readLine());
} catch (Exception e) { }
}
void display(){
total = qty*price;
System.out.println("pid : " + pid + "\nProduct Nmae :
"+pname+"\nQuantity : "+qty + "\nPrice : "+price+"\n Total
Amount : "+total);
}
}
class Slip17B {
public static void main(String args[]) throws IOException{
int n;
float to=0;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("How many Product you want to enter : ");
n = Integer.parseInt(br.readLine());
Product p1[]=new Product[n];
for(int i=0; i<n; i++){
p1[i]=new Product();
p1[i].accept();
}
for(int i=0; i<n; i++){
p1[i].display();
}
for(int i=0; i<n; i++){
to=to+p1[i].total;
System.out.println("Total Cost : "+to);
}
}
}
SLIP 18.A) Write a Java program to calculate area of Circle,
Triangle & Rectangle.(Use Method Overloading)
import java.io.*;
import java.util.*;
class areaCalculation
{
void area(int r)
{
System.out.println("Area of circle="+(3.14*r*r));
}
float area(int b,float h)
{
return b*h/2;
}
double area(float l,float rb)
{
return l+rb;
}
}
class slip18a
{
public static void mian(String args[])
{
int r,b,l,rb;
float h;
Scanner sc=new Scanner(System.in);
System.out.print("Enter rdius :");
r=sc.nextInt();
System.out.print("Enter base :");
b=sc.nextInt();
System.out.print("Enter height :");
h=sc.nextFloat();
System.out.print("Enter lenght :");
l=sc.nextInt();
System.out.print("Enter breadth :");
rb=sc.nextInt();
areaCalculation ac=new areaCalculation();
ac.area(r);
System.out.println("area of triangle ="+ac.area(b,h));
System.out.println("area of rectangle ="+ac.area(l,rb));
}
}
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.
import java.io.*;
class slip18b
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("a.txt");
FileWriter fw=new FileWriter("b.txt");
while((c=fr.read())!=-1)
{
if(c>=65&&c<=90)
{
c=c+32;
fw.write(c);
}
else if(c>=97&&c<=122)
{
c=c-32;
fw.write(c);
}
else if(c>=48&&c<=57)
{
fw.write('*');
}
else
{
fw.write(c);
}
}
System.out.println("Copy Successfully");
fr.close();
fw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
class slip19
{
public static void main(String ar[])
{
int n=10 , ft=0, st=1;
System.out.println("fibonacci series are" +n+ "terms:");
for(int i=1;i<=n;++i)
{
System.out.print(ft+",");
int nt=ft+st;
ft=st;
st=nt;
}
}
}
output:
C:\Users\BBA11>cd C:\Program Files\Java\jdk1.8.0_144\bin
imort java.io.*;
imort java.awt.event.*;
imort java.applet.*;
imort java.awt.event.keyListener.*;
public class slip9b extends Applet implements
MouseListener,KeyListener
{
private int x,y;
private String str=" ";
public void paint(Graphics g)
{
g.drawString(str,x,y);
}
public void mouseClicked(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void keyPressed(KeyEvent ke)
{
showStatus("key pressed");
}
public void keyReleased(KeyEvent ke)
{
showStatus("key released");
}
public void keyReleased(KeyEvent ke)
{
showStatus("key released");
}
public void keyTyped(KeyEvent ke)
{
str +=ke.getKeyChar();
repaint();
}
public void mouseEntered(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void mouseEntered(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void mouseEntered(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void mouseEntered(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void mouseEntered(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void mouseEntered(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void mouseEntered(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void mouseExited(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="Mouse is Exited in "+"x ="+x+",y="+y+"Co-ordinates";
repaint();
}
public void mousePressed(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="x ="+x+",y="+y;
repaint();
}
public void mouseReleased(MouseEvent m)
{
x=m.getX();
y=m.getY();
str="Mouse Released at "+"x ="+x+",y="+y+"Co-ordinates";
repaint();
}
}
/*
<applet code="slip19b.class" width="300" height="300">
</applet>
*/
import javax.swing.*;
import java.awt.*;
class Slip20A {
public static void main(String args[]) {
JFrame frame = new JFrame("TYBBACA");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.RED);
frame.setVisible(true);
}
}
import java.util.*;
public class Slip20B{
public static void main (String args[]){
LinkedList al = new LinkedList<>();
al.add("CPP");
al.add("JAVA");
al.add("Python");
al.add("PHP");
System.out.println("Display content using Iterator...");
Iterator il=al.iterator();
while(il.hasNext()){
System.out.println(il.next());
}
System.out.println("Display Content Revverse Using
ListIterator");
ListIterator li1=al.listIterator();
while(li1.hasNext()){
li1.next();
}
while(li1.hasPrevious()){
System.out.println("" + li1.previous());
}
}
}
import java.io.*;
class slip21
{
public static void main(String arg[])throws IOException
{
FileReader fr=new FileReader("input.txt");
FileWriter fw=new FileWriter("output.txt");
BufferedReader b=new BufferedReader(fr);
String data;
String reverse;
while((data=b.readLine())!=null)
{
String words[]=data.split(" ");
for(String a:words)
{
StringBuilder builder=new StringBuilder(a);
System.out.println(builder.reverse().toString());
}
}
}
}
Slip 21 B) Create a hash table containing city name & STD code.
Display the details of the hash table. Also search for a specific
city and display STD code of that city.
import java.util.*;
import java.io.*;
import java.io.*;
import java.util.*;
class Slip22B {
public static void main(String args[]) throws IOException{
Scanner br = new Scanner(System.in);
System.out.println("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");
System.out.print("Enter File Name : ");
String str = br.nextLine();
File file = new File(str);
System.out.print("Enter Number : ");
int num = br.nextInt();
switch(num){
case 1 :
if (file.createNewFile()) {
System.out.println("File created : " + file.getName());
} else {
System.out.println("File already exists.");
}
case 2 :
System.out.print("Enter New File Name : ");
String newone = br.nextLine();
File newfile =new File(newone);
if(file.renameTo(newfile)){
System.out.println("File renamed");
}else{
System.out.println("Sorry! the file can't be renamed");
}
break;
case 3 :
if (file.delete()) {
System.out.println("Deleted the file: " + file.getName());
} else {
System.out.println("Failed to delete the file.");
}
break;
case 4 :
System.out.println("File Location : " +file.getAbsolutePath());
break;
default :
System.out.println("Wrong Number ..!");
break;
}
}
}
import java.io.*;
import java.util.*;
public class Slip23A {
public static void main(String[] args) {
Scanner br = new Scanner(System.in);
try {
System.out.print("Enter File Name : ");
String str = br.nextLine();
File file = new File(str);
if(file.isHidden()){
System.out.println("File is Hidden");
}else{
System.out.println("File Location : " +file.getAbsolutePath());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
import java.awt.event.*;
import javax.swing.*;
public class Slip23B extends JFrame implements ActionListener{
public static void main(String s[]){
new Slip23B();
}
public Slip23B(){
this.setSize(600,500);
this.setLocation(200,200);
JMenuBar menuBar = new JMenuBar();
JMenu filMenu = new JMenu("File");
JMenu filEdit = new JMenu("Edit");
JMenu filSearch = new JMenu("Search");
JMenuItem OpenItem = new JMenuItem("Open");
JMenuItem SaveItem = new JMenuItem("Save");
JMenuItem QuitItem = new JMenuItem("Quit");
JMenuItem UndoItem = new JMenuItem("Undo");
JMenuItem RedoItem = new JMenuItem("Redo");
JMenuItem CutItem = new JMenuItem("Cut");
JMenuItem CopyItem = new JMenuItem("Copy");
JMenuItem PasteItem = new JMenuItem("Paste");
ImageIcon OpenIcon = new ImageIcon("icons/open.png");
ImageIcon SaveIcon = new ImageIcon("icons/Save.png");
ImageIcon QuitIcon = new ImageIcon("icons/Delete.png");
ImageIcon UndoIcon = new ImageIcon("icons/Undo.png");
ImageIcon RedoIcon= new ImageIcon("icons/Redo.png");
ImageIcon CutIcon = new ImageIcon("icons/Cut.png");
ImageIcon CopyIcon = new ImageIcon("icons/Copy.png");
ImageIcon PasteIcon = new ImageIcon("icons/Past.png");
filMenu.add(OpenItem);
filMenu.add(SaveItem);
filMenu.add(QuitItem);
filEdit.add(UndoItem);
filEdit.add(RedoItem);
filEdit.add(CutItem);
filEdit.add(CopyItem);
filEdit.add(PasteItem);
OpenItem.setIcon(OpenIcon);
SaveItem.setIcon(SaveIcon);
QuitItem.setIcon(QuitIcon);
UndoItem.setIcon(UndoIcon);
RedoItem.setIcon(RedoIcon);
CutItem.setIcon(CutIcon);
CopyItem.setIcon(CopyIcon);
PasteItem.setIcon(PasteIcon);
menuBar.add(filMenu);
menuBar.add(filEdit);
menuBar.add(filSearch);
this.setJMenuBar(menuBar);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
slip24 Q.1. Core Java: A) Write a java program to count number
of digits, spaces and characters from a file. [15 M]
import java.io.*;
class slip24
{
public static void main(String arg[])throws Exception
{
int cnt=0,lcnt=1,wnt=0,c;
FileInputStream fin=new FileInputStream("abc.txt");
while((c=fin.read())!=-1)
{
cnt++;
if(c==32||c==13)
wnt++;
if(c==1)
lcnt++;
}
System.out.println("number of character are"+cnt);
System.out.println("number of words are"+lcnt);
System.out.println("number of lines are"+wnt);
}
}
abc.txt
Facts about the History of India
Output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac slip24.java
import java.io.*;
public class Slip24B {
public static void main(String args[])throws Exception{
int r,n1,n2,t;
String snm, tnm, sub;
float per;
DataInputStream dr = new DataInputStream(System.in);
System.out.println("How Many Student's record You Want :");
n1 = Integer.parseInt(dr.readLine());
System.out.println("How Many Teacher's record You Want :");
n2 = Integer.parseInt(dr.readLine());
Student s1[] = new Student[n1];
Teacher t1[] = new Teacher[n2];
System.out.println("Enter Student Details");
for (int i=0; i<n1; i++){
System.out.println("Enter roll no, Student name and
Percentage");
r = Integer.parseInt(dr.readLine());
snm=dr.readLine();
per=Float.parseFloat(dr.readLine());
s1[i] = new Student(r,snm,per);
}
System.out.println("Enter Teacher Details");
for (int j=0; j<n2; j++){
System.out.println("Enter Teacher id , Teacher name and
Subject");
t = Integer.parseInt(dr.readLine());
tnm=dr.readLine();
sub=dr.readLine();
t1[j] = new Teacher(t,tnm,sub);
}
System.out.println("Student Details");
for (int i=0; i<n1; i++){
((Student) s1[i]).disp();
}
System.out.println("Teacher Details");
String str ="java";
for (int j=0; j<n2; j++){
if(str.equals(t1[j].sub)){
t1[j].disp();
}
}
}
}
import java.io.*;
class slip25
{
public static void main(String ar[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the string ");
String str=br.readLine();
String temp=str;
StringBuffer sb=new StringBuffer(str);
sb.reverse();
str=sb.toString();
if(temp.equalsIgnoreCase(str))
System.out.println("the string is palindrome");
else
System.out.println("the string is not palindrome");
}
}
output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac slip25.java
Program 1:
package series;
Program 2:
import series.*;
import java.io.*;
public class SeriesMain {
public static void main(String [] args)throws IOException{
Prime p=new Prime();
int i;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
do
{
System.out.println("Enter a number / 0 to exit");
i=Integer.parseInt(br.readLine());
p.prime(i);
p.fibonacci(i);
p.square(i);
}
while(i>0);
}
}
Output:
import java.io.*;
class slip26
{
public static void main(String arg[])
{
char ch1='a';
char ch2='b';
char ch3='B';
int asciivalue1=ch1;
int asciivalue2=ch2;
int asciivalue3=ch3;
System.out.println("the ascii value of " +ch1+ "value:
"+asciivalue1);
System.out.println("the ascii value of " +ch2+ "value:
"+asciivalue2);
System.out.println("the ascii value of " +ch3+ "value:
"+asciivalue3);
}
}
output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac slip26.java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Slip26B extends Applet{
public void init() {
setBackground(Color.BLACK);
}
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.drawRect(100, 150, 90, 120);
g.drawRect(130, 230, 20, 40);
g.drawLine(150, 100, 100, 150);
g.drawLine(150, 100, 190, 150);
g.drawLine(150, 50, 150, 100);
g.setColor(Color.ORANGE);
g.drawRect(150, 50, 20, 20);
}
}
/*
<applet code="Slip26B.class" width="300" height="300">
</applet>
*/
import java.io.*;
class NumOutRange extends Exception{}
class Slip27A{
static int n;
public static void main( String args[]){
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter Number : ");
n = Integer.parseInt(dr.readLine());
if(n>1000){
throw new NumOutRange();
}else{
for(int i=1; i<n; i++){
if(n%i==0){
System.out.println(i + " ");
}
}
}
} catch (NumOutRange nz) {
System.out.println("Num is out of range..!");
}
catch (Exception e){
System.out.println(""+e.getMessage());
}
}
}
import java.util.*;
public class Slip28A {
public static void main(String[] args) {
int count = 0;
List<String> al = new ArrayList<>();
for (int i = 0; i < args.length; i++) {
al.add(args[i]);
}
for (int i = 0; i < al.size(); i++) {
String element = al.get(i);
try {
int j = Integer.parseInt(element);
count++;
} catch (NumberFormatException e) {}
}
System.out.println(count + " integers present in list");
}
}
import java.io.*;
class NumOutRange extends Exception{}
class Slip29A{
static int n;
public static void main( String args[]){
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter Age : ");
n = Integer.parseInt(dr.readLine());
if(n<18){
throw new NumOutRange();
}else{
System.out.print("You Are eligible For Voting :) ");
}
} catch (NumOutRange nz) {
System.out.println("You Are Not eligible For Voting ....!");
}
catch (Exception e){}
}
}
Slip 29 B) Write a java program using Applet for bouncing ball. Ball
should change its color for each bounce.
import java.applet.*;
import java.awt.*;
public class Slip29B extends Applet implements Runnable {
Thread t = null;
int x1 = 10;
int y1 = 300;
int flagx1, flagy1;
int R, G, B;
public void start() {
t = new Thread(this);
t.start();
}
public void color(){
R = (int) (Math.random() * 256);
G = (int) (Math.random() * 256);
B = (int) (Math.random() * 256);
}
public void run() {
for (;;) {
try {
repaint();
if (y1 <= 50) {
flagx1 = 0;
color();
} else if (y1 >= 300) {
flagx1 = 1;
color();
}
if (x1 <= 10) {
flagy1 = 0;
color();
} else if (x1 >= 400) {
flagy1 = 1;
color();
}
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
public void paint(Graphics g) {
Color color = new Color(R, G, B);
g.setColor(color);
g.fillOval(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;
}
}
/*
* <applet code="Slip29B.class" width="420" height="320">
* </applet>
*/
import java.io.*;
class Numberiszero extends Exception{}
class Slip30A{
public static void main( String args[]){
int r,sum=0,temp;
int n;
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter Number : ");
n = Integer.parseInt(dr.readLine());
if(n==0){
throw new Numberiszero();
}else{
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum){
System.out.println("Palindrome Number ");
}else{
System.out.println("Not Palindrome");
}
}
} catch (Numberiszero nz) {
System.out.println("Number is Zero");
}
catch (NumberFormatException e){
System.out.println("Number is Invalid");
}
catch (Exception e){}
}
}
import javax.swing.*;
import java.awt.*;
class Slip30B extends JFrame;
JLabel l1,l2,l3,l4,l5,l6;
JTextField t1,t2,t3,t4;
JButton b1,b2;
JRadioButton rb1, rb2;
JCheckBox ch1,ch2,ch3;
Panel p1,p2,p3,p4,p5,p6;
GridLayout g1,g2,g3,g4,g5,g6,g7;
JFrame jf;
public ;
Slip30B(){
jf = new JFrame();
l1 = new JLabel("",JLabel.CENTER);
l1.setText("<HTML><U>Personal Information</U></HTML>");
p1 = new Panel();
g1 = new GridLayout(1,1);
p1.setLayout(g1);
p1.add(l1);
l1.setFont( new Font("Times New Roman",Font.BOLD,20));
l2 = new JLabel(" First Name : ");
t1 = new JTextField(30);
l3 = new JLabel(" Last Name : ");
t2 = new JTextField(30);
p2 = new Panel();
g2 = new GridLayout(2,1);
p2.setLayout(g2);
p2.add(l2);
p2.add(t1);
p2.add(l3);
p2.add(t2);
l4 = new JLabel(" Address : ");
t3 = new JTextField(30);
l5 = new JLabel(" Mobile Number : ");
t4 = new JTextField(30);
p3 = new Panel();
g3 = new GridLayout(2,1);
p3.setLayout(g3);
p3.add(l4);
p3.add(t3);
p3.add(l5);
p3.add(t4);
l5 = new JLabel(" Gender ");
rb1 = new JRadioButton("Male");
rb2 = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
p4 = new Panel();
g4 = new GridLayout(1,2);
p4.setLayout(g4);
p4.add(l5);
p4.add(rb1);
p4.add(rb2);
l6 = new JLabel(" Your Interests");
ch1 = new JCheckBox("Comuter");
ch2 = new JCheckBox("Sport");
ch3 = new JCheckBox("Music");
p5 = new Panel();
g5 = new GridLayout(1,2);
p5.setLayout(g5);
p5.add(l6);
p5.add(ch1);
p5.add(ch2);
p5.add(ch3);
b1 = new JButton("Submit");
b2 = new JButton("Reset");
p6 = new Panel();
g6 = new GridLayout(1,1,400,10);
p6.add(b1);
p6.add(b2);
this.setSize(500,250);
this.setVisible(true);
g7 = new GridLayout(6,1);
this.setLayout(g7);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.add(p5);
this.add(p6);
}
public static void main(String args[]){
new Slip30B();
}
}