Core Java Solved Slips

Download as pdf or txt
Download as pdf or txt
You are on page 1of 111

Slip1 Q.1.

Core Java: A) Write a ‘java’ program to display


characters from ‘A’ to ‘Z’
import java.io.*;
class slip1
{
public static void main(String[] args) {
char ch;
for(ch = 'A'; ch <= 'Z';ch++)
System.out.println("the alphabets are :"+ch);
}
}

Output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac slip1.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip1


the alphabets are :A
the alphabets are :B
the alphabets are :C
the alphabets are :D
the alphabets are :E
the alphabets are :F
the alphabets are :G
the alphabets are :H
the alphabets are :I
the alphabets are :J
the alphabets are :K
the alphabets are :L
the alphabets are :M
the alphabets are :N
the alphabets are :O
the alphabets are :P
the alphabets are :Q
the alphabets are :R
the alphabets are :S
the alphabets are :T
the alphabets are :U
the alphabets are :V
the alphabets are :W
the alphabets are :X
the alphabets are :Y
the alphabets are :Z

Slip 1 .B)Write a ‘java’ program to copy only non-numeric data


from one file to another file
import java.io.*;

public class Slip1q2 {


public static void main(String[] args) throws IOException {
System.out.println("reading file...");
FileReader fileReader = new
FileReader("Slip1/src/sourcefile1q2.txt");
// enter your source file location
FileWriter fileWriter = new
FileWriter("Slip1/src/destinationfile1Q2.txt");
// enter your destination file location
int data = fileReader.read();
System.out.println("writing file...");
while (data != -1){
String content = String.valueOf((char)data);
if(Character.isAlphabetic(data)) {
fileWriter.append(content);
}else if(content.equals(" ")){
fileWriter.append(" ");
}
data = fileReader.read();
}
System.out.println("\nCOPY FINISHED SUCCESSFULLY...");
fileWriter.close();
fileReader.close();
}
}

Slip 2.A) Write a java program to display all the vowels from a
given string
import java.util.Scanner;

public class Slip2q1 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string to find vowels on it.");
String user_string = scanner.nextLine();
scanner.close();
int vowel_count = 0;
for(int i=0;i<user_string.length();i++){
if(user_string.charAt(i)=='a' ||user_string.charAt(i)=='e'
||user_string.charAt(i)=='i'
||user_string.charAt(i)=='o'
||user_string.charAt(i)=='u'
||user_string.charAt(i)=='A'
||user_string.charAt(i)=='E'
||user_string.charAt(i)=='I'
||user_string.charAt(i)=='O'
||user_string.charAt(i)=='U'){
vowel_count++;
System.out.println("Vowel found
("+user_string.charAt(i)+") at index "+i);
}
}
if (vowel_count == 0){
System.out.println("Vowel not found in given string.");
}
}
}

Slips 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.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Slip2q2 {
public static void main(String[] args) {
new MyFrame("Mouse Events");
}
}

class MyFrame extends JFrame {


TextField click_text_field, mouse_move_field;
Label click_text_label, mouse_move_label;
int x,y;
Panel panel;
MyFrame(String title) {
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

panel =new Panel();


panel.setLayout(new GridLayout(2,2,5,5));
click_text_label = new Label("Co-ordinates of clicking");
mouse_move_label = new Label("Co-ordinates of
movement");
click_text_field=new TextField(20);
mouse_move_field =new TextField(20);
panel.add(click_text_label);
panel.add(click_text_field);
panel.add(mouse_move_label);
panel.add(mouse_move_field);
add(panel);
addMouseListener(new MyClick());
addMouseMotionListener(new MyMove());
setSize(500,500);
setVisible(true);
}
class MyClick extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
x=me.getX();
y=me.getY();
click_text_field.setText("X="+x+" Y="+y);
}
}
class MyMove extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
x=me.getX();
y=me.getY();
mouse_move_field.setText("X="+ x +" Y="+y);
}
}

Slip3 Q.1. Core Java: A) Write a ‘java’ program to check whether


given number is Armstrong or not. (Use static keyword)

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

C:\Program Files\Java\jdk1.8.0_144\bin>java slip3


enter the number to check armstrong or not
153
the number is Armstrong

C:\Program Files\Java\jdk1.8.0_144\bin>java slip3


enter the number to check armstrong or not
432
the number is not armstrong

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.)

import java.util.Scanner;

public class Slip3q2 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Shape shape;
System.out.println("""
ENTER YOUR CHOICE
1. Cone
2. Cylinder""");
int choice = scanner.nextInt();
if (choice==1){
System.out.println("Enter radius");
int radius = scanner.nextInt();
System.out.println("Enter height");
int height = scanner.nextInt();
shape=new Cone(radius,height);
shape.area();
shape.volume();
}else if (choice==2){

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);
}
}
}

Slip4 Q.1. Core Java: A) Write a java program to display


alternate character from a given string.[15M]

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

C:\Program Files\Java\jdk1.8.0_144\bin>javac slip4.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip4


h
l
o
w
r
d

Slip 4.B) Write a java program using Applet to implement a


simple arithmetic calculator.

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>
*/

Slip 5.A) Write a java program to display following pattern:


5
45
345
2345
12345

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();
}
}
}

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.

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){}
}
}

Slip 6.B) Write a java program to display transpose of a given


matrix.

import java.util.Scanner;

public class Transposematrix {


public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter rows of matrix : ");
int row=sc.nextInt();
System.out.print("Enter column of matrix : ");
int col=sc.nextInt();
int[][] matrix = new int[row][col];
//create and insert value
for (int i=0;i<row;i++){
for(int j=0;j<col;j++){
System.out.print("element at index ["+i+"]["+j+"] :");
matrix[i][j]=sc.nextInt();
}
}
sc.close();
//print value of matrix
System.out.println("\nReal matrix :-\n");
for (int i=0;i<row;i++){
for(int j=0;j<col;j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
System.out.println("\nTranspose of matrix :-\n");
for (int i=0;i<row;i++){
for(int j=0;j<col;j++){
System.out.print(matrix[j][i]+" ");
}
System.out.println();
}

}
}

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);
}
}

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)
import java.io.*;
class Cricket{
String Name;
int Total_runs;
int Notout;
int Inning;
float avg;
void accept(){
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try{
System.out.print("Enter Name of Player : ");
Name = br.readLine();
System.out.print("Enter Total Runs of Player : ");
Total_runs = Integer.parseInt(br.readLine());
System.out.print("Enter Name of Tixes Not out : ");
Notout = Integer.parseInt(br.readLine());
System.out.print("Enter Innings played by players : ");
Inning = Integer.parseInt(br.readLine());
}catch (Exception e) {}
}
void average(){
avg = Total_runs/Inning;
System.out.println("Name : "+Name+"\nTotal runs :
"+Total_runs+"\nAvergae : "+avg+"\nInning : "+ Inning);
}
}
public class Slip7B {
public static void main(String args[]){
float max =0;
int n;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
System.out.print("How many Players : ");
n = Integer.parseInt(br.readLine());
Cricket ob1[]= new Cricket[n];
for(int i=0; i<n; i++){
ob1[i] = new Cricket();
ob1[i].accept();
}
for(int i=0; i<n; i++){
ob1[i].average();
}
for(int i=0; i<n; i++){
if(max<ob1[i].avg){
max = ob1[i].avg;
}
}
System.out.println("-----------------------------\nMax avg :
"+max);
} catch (Exception e) {
System.out.println("Error........."+e);
}
}
}

slip8 Q.1. Core Java: A) Define an Interface Shape with abstract


method area(). Write a java program to calculate an area of
Circle and Sphere.(use final keyword) [15 M]

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

Slip 8.B) Write a java program to display the files having


extension .txt from a given directory.
import java.io.File;
class Slip8B {
public static void main(String[] args) {
File file = new
File("C:\\Users\\Saurabh_Sapkal\\Desktop\\ln\\java\\Slips");
String[] fileList = file.list();
for(String str : fileList) {
if(str.endsWith(".txt")){
System.out.println(str);
}
}
}
}
Slip 9A)Write a java Program to display following pattern:
1
01
010
1010
import java.io*;
public class Pattern1 {
public static void main(String[] args) {
int num = 4;
for (int i=0;i<num;i++){
for(int j=0;j<=i;j++){
if (i<2){
if ((i+j)%2==0){
System.out.print("1 ");
}else{
System.out.print("0 ");
}
}else{
if ((i+j)%2==0){
System.out.print("0 ");
}else{
System.out.print("1 ");
}
}
}System.out.println();
}
}
}

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.

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){}
}
}

Slip 10 A) Write a java program to count the frequency of each


character in a given string.

import java.util.Scanner;

public class Frequencyofchar {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string :");
String str = sc.nextLine() ;
int[] freq = new int[str.length()];
int i, j;
char string[] = str.toCharArray();
for(i = 0; i <str.length(); i++) {
freq[i] = 1;
for(j = i+1; j <str.length(); j++) {
if(string[i] == string[j]) {
freq[i]++;

//Set string[j] to 0 to avoid printing visited character


string[j] = '0';
}
}
}

System.out.println("Characters and their corresponding


frequencies");
for(i = 0; i <freq.length; i++) {
if(string[i] != ' ' && string[i] != '0')
System.out.println(string[i] + "-" + freq[i]);
}
}
}

Slip 10 B) Write a java program for the following:


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Slip10B extends JFrame implements ActionListener{
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();
l1 = new JLabel("Simple Interest Calculator");
l2 = new JLabel("Principle Amount");
l3 = new JLabel("Interest Rate(%)");
l4 = new JLabel("Time(Yrs)");
l5 = new JLabel("Total Amount");
l6 = new JLabel("Interest Amount");
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();
g1= new GridLayout(1,1);
p1.setLayout(g1);
p1.add(l1);
p2 = new Panel();
g2 = new GridLayout(1,2);
p2.setLayout(g2);
p2.add(l2);
p2.add(t1);
p3 = new Panel();
g3 = new GridLayout(1,4);
p3.setLayout(g3);
p3.add(l3);
p3.add(t2);
p3.add(l4);
p3.add(t3);
p4 = new Panel();
g4 = new GridLayout(2,2);
p4.setLayout(g4);
p4.add(l5);
p4.add(t4);
p4.add(l6);
p4.add(t5);
p5 = new Panel();
g5 = new GridLayout(1,3);
p5.setLayout(g5);
p5.add(b1);
p5.add(b2);
p5.add(b3);
g6 = new GridLayout(5,1);
this.setLayout(g6);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
this.add(p5);
this.setSize(500,250);
this.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
int p = Integer.parseInt(t1.getText());
float rt = Float.parseFloat(t2.getText());
float tm = Float.parseFloat(t3.getText());
if(ae.getSource()==b1){
double iamt = (p*tm*rt)/100;
t5.setText(Double.toString(iamt));
double tamt = iamt+p;
t4.setText(Double.toString(tamt));
}
if(ae.getSource()==b2){
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
}
if(ae.getSource()==b3){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String args[]){
Slip10B s1 = new Slip10B();
}
}

Slip 11.A) Write a menu driven java program using command


line arguments for thefollowing:
1. Addition
2. Subtraction
3. Multiplication
4. Division.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>
*/

SLIP 12.A) Write a java program to display each String in reverse


order from a String array

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] + ' ');
}
}
}

SLIP 12.B) Write a java program to display multiplication table


of a given number into the List box by clicking on button

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>
*/

Slip13 Q.1. Core Java: 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. [15
M]

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();
}
}

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.

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) {}
}
}

Slip 15 B) Write an applet application to display smiley face.

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>
*/

Slip 16 A) Write a java program to calculate sum of digits of a


given number using recursion.

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);
}
}
}

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)

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);
}
}

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

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);
}
}
}

Slip19 Q.1. Core Java: A) Write a Java program to display


Fibonacci series using function. [15 M]

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

C:\Program Files\Java\jdk1.8.0_144\bin>javac slip19.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip19


fibonacci series are10terms:
0,1,1,2,3,5,8,13,21,34,
C:\Program Files\Java\jdk1.8.0_144\bin>

SLIP 19.B) Create an Applet that displays the x and y position of


the cursor movement using Mouse and Keyboard. (Use
appropriate listener)

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>
*/

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.

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);
}
}

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

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());
}
}
}

Slip21 Q.1. Core Java: A) Write a java program to display each


word from a file in reverse order. [15 M]

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());
}
}
}
}

input.txt save in bin directory


hello world

output.txt save in bin directory


output:
C:\Users\BBA11>cd C:\Program Files\Java\jdk1.8.0_144\bin

C:\Program Files\Java\jdk1.8.0_144\bin>javac slip21.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip21


olleh
dlrow

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.*;

public class Slip21B {


public static void main(String args[]){
Hashtable h1=new Hashtable<>();
Enumeration en;
int i,n,std,val,max=0;
String nm, cname, str, s=null;
DataInputStream dr = new DataInputStream(System.in);
try {
System.out.print("Enter the Now Many Record You Want : ");
n = Integer.parseInt(dr.readLine());
System.out.print("Enter the City Name & STD Code : ");
for(i=0; i<n; i++){
cname = dr.readLine();
std = Integer.parseInt(dr.readLine());
h1.put(cname,std);
}
System.out.print("Enter city name to search : ");
nm = dr.readLine();
en=h1.keys();
while(en.hasMoreElements()){
str=(String)en.nextElement();
val=(Integer)h1.get(str);
if(str.equals(nm)){
System.out.print("STD Code : " + val);
}
}
} catch (Exception e) {}
}
}

Slip 22 A) Write a Java program to calculate factorial of a


number using recursion.

public class Slip22A {


public static void main(String[] args) {
int num = 6;
long factorial = multiplyNumbers(num);
System.out.println("Factorial of " + num + " : " + factorial);
}
public static long multiplyNumbers(int num)
{
if (num >= 1)
return num * multiplyNumbers(num - 1);
else
return 1;
}
}

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.

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;
}
}
}

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.

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();
}
}
}

Slip 23 B) Write a java program to design following Frame using


Swing.

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

India is the largest and the oldest civilisation in the world.


India holds the record of not invading any other country in the
last 10,000 years of her history.
Interestingly, India was one of the richest countries on earth
before the British invaded it in the early 17th century, and was
also one of the first countries in the ..

Output:
C:\Program Files\Java\jdk1.8.0_144\bin>javac slip24.java

C:\Program Files\Java\jdk1.8.0_144\bin>java slip24


number of character are368
number of words are1
number of lines are66

Sli24.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)

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();
}
}
}
}

Slip25 Q.1. Core Java: A) Write a java program to check whether


given string is palindrome or not. [15 M]

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

C:\Program Files\Java\jdk1.8.0_144\bin>java slip25


enter the string
madam
the string is palindrome

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

NOTE: THIS QUESTION HAS TWO PROGRAMS TO BE WRITTEN AND


COMPILED DIFFERENTLY.

Program 1:
package series;

public class Prime {


int flag;
public void prime(int n){
for(int i=2;i<n;i++){
if(n%i==0)
{
flag=0;
break;
}
else
flag=1;
}
if(flag==1)
System.out.println(n+" is a prime number.");
else System.out.println(n+" is not a prime number.");
}
public void fibonacci(int n){
int first=0, second=1, c, next;
System.out.println("Fibonacci Series:");
for(int i=0;i<=n;i++)
{
if(i<=1)
next=i;
else
{
next=first+second;
first=second;
second=next;
}
System.out.println(next);
}
}
public void square(int n){
System.out.println("Square of the number is "+(n*n));
}
}

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:

Enter a number / 0 to exit


5
5 is a prime number.
Fibonacci Series:
0
1
1
2
3
5
Square of the number is 25
Enter a number / 0 to exit

Slip26 Q.1. Core Java: A) Write a java program to display ASCII


values of the characters from a file. [15 M]

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

C:\Program Files\Java\jdk1.8.0_144\bin>java slip26


the ascii value of avalue: 97
the ascii value of bvalue: 98
the ascii value of Bvalue: 66

Slip 26 B) Write a java program using applet to draw Temple.

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>
*/

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)

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());
}
}
}

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.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Slip27B extends Frame implements ActionListener{
Graphics g;
List l;
TextField t1;
Button b1;
Label l1;
public Slip27B(){
this.setLayout(new FlowLayout());
this.setSize(400,400);
this.setVisible(true);
l1 = new Label("Enter Directory ");
t1 = new TextField(20);
l = new List(10);
b1 = new Button("Display");
l1.setBounds(50,100,80,80);
t1.setBounds(50,150,80,80);
b1.setBounds(50,200,80,80);
l.setBounds(50,300,100,100);
add(l1);
add(t1);
add(b1);
add(l);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
try{
String nm = t1.getText();
File f1 = new File(nm + ":");
String s1[]=f1.list();
if(s1==null){
System.out.println("Dir not exist");
}else{
for(int i=0; i<s1.length; i++){
l.add(s1[i]);
}
}
}catch(Exception ee){}
}
}
public static void main(String args[]){
new Slip27B();
}
}
Slip 28 A) Write a java program to count the number of integers
from a given list. (Use Command line arguments).

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");
}
}

Slip 28 B) Write a java Program to accept the details of 5 employees


(Eno, Ename, Salary) and display it onto the JTable.
import javax.swing.*;
public class Slip28B {
JFrame f;
JTable j;
Slip28B(){
f = new JFrame();
f.setTitle("Employee Details");
String data[][] = {
{"1","Radhika Sapkal","50,000"},
{"2","Ramesh Devakar","20,000"},
{"3","Hardik Shrinivas","25,000"},
{"4","Bhihari Kumar","20,000"},
{"5","Swaraghini Pawar","15,000"},
};
String[] columnNames = {"Eno", "Ename", "Salary" };
j = new JTable(data, columnNames);
j.setBounds(30,40,200,300);
JScrollPane sp = new JScrollPane(j);
f.add(sp);
f.setSize(500,200);
f.setVisible(true);
}
public static void main(String args[]) {
new Slip28B();
}
}

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.

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>
*/

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.

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){}
}
}

Slip 30 B) Write a java program to design a following GUI (Use


Swing).

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();
}
}

You might also like