slip1
A) Write a ‘java’ program to display characters from ‘A’ to ‘Z’.
public class Slip1q1 {
public static void main(String[] args) {
for(char c='A';c<='Z';c++){
[Link](c+" ");
}
[Link]();
for(char c='a';c<='z';c++){
[Link](c+" ");
}
}
}
Write a ‘java’ program to copy only non-numeric data from one file to another file
import [Link].*;
public class Slip1q2 {
public static void main(String[] args) throws IOException {
[Link]("reading file...");
FileReader fileReader = new FileReader("Slip1/src/[Link]");
// enter your source file location
FileWriter fileWriter = new FileWriter("Slip1/src/[Link]");
// enter your destination file location
int data = [Link]();
[Link]("writing file...");
while (data != -1){
String content = [Link]((char)data);
if([Link](data)) {
[Link](content);
}else if([Link](" ")){
[Link](" ");
}
data = [Link]();
}
[Link]("\nCOPY FINISHED SUCCESSFULLY...");
[Link]();
[Link]();
}
}
slip2:
Write a java program to display all the vowels from a given string
import [Link];
public class Slip2q1 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string to find vowels on it.");
String user_string = [Link]();
[Link]();
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++;
[Link]("Vowel found ("+user_string.charAt(i)+") at
index "+i);
}
}
if (vowel_count == 0){
[Link]("Vowel not found in given string.");
}
}
}
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 [Link].*;
import [Link].*;
import [Link].*;
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);
[Link](JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
panel =new Panel();
[Link](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);
[Link](click_text_label);
[Link](click_text_field);
[Link](mouse_move_label);
[Link](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=[Link]();
y=[Link]();
click_text_field.setText("X="+x+" Y="+y);
}
}
class MyMove extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
x=[Link]();
y=[Link]();
mouse_move_field.setText("X="+ x +" Y="+y);
}
}
}
slip3
A) Write a ‘java’ program to check whether given number is Armstrong or not. (Use
static keyword)
import [Link];
public class Slip3Q1 {
static boolean is_armstrong(int n){
int temp,last,digits=0,sum=0;
temp = n;
while (temp>0){
temp=temp/10;
digits++;
}
temp = n;
while (temp>0){
last = temp%10;
sum+=[Link](last,digits);
temp=temp/10;
}
return n == sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number to check armstrong : ");
int number = [Link]();
[Link]();
if (is_armstrong(number)){
[Link](number+" is a armstrong number.");
}
else {
[Link](number+" is not a armstrong number.");
}
}
}
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 [Link];
public class Slip3q2 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Shape shape;
[Link]("""
ENTER YOUR CHOICE
1. Cone
2. Cylinder""");
int choice = [Link]();
if (choice==1){
[Link]("Enter radius");
int radius = [Link]();
[Link]("Enter height");
int height = [Link]();
shape=new Cone(radius,height);
[Link]();
[Link]();
}else if (choice==2){
[Link]("Enter radius");
int radius = [Link]();
[Link]("Enter height");
int height = [Link]();
shape=new Cylinder(radius,height);
[Link]();
[Link]();
}else {
[Link]("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){
[Link]=radius;
[Link]=height;
}
void area(){
float slant_height=(height*height)+(radius*radius);
float area = (float)([Link]*radius*(radius+[Link](slant_height)));
[Link]("Area of Cone : "+area);
}
void volume(){
float volume = (float)([Link]*radius*radius*(height/3));
[Link]("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*[Link]* radius * height)+(2*[Link]* radius *
radius));
[Link]("Area of Cylinder : "+area);
}
public void volume(){
float volume=(float)([Link]* radius * radius * height);
[Link]("Volume of Cylinder : "+volume);
}
}
}
slip4:
A) Write a java program to display the alternate characters from a given string.
import [Link];
public class Alternatecharfromstr {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]
("Enter a string :");
String str = [Link]();
[Link]();
for (int i=0;i<[Link]();i+=2){ //run only 2 mulriple index
[Link]([Link](i)+" ");
}
}
}
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
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++){
[Link](j+" ");
}
[Link]();
}
}
}
B)Write a java program to accept list of file names through command line and delete
the files having extension “.txt”. Display the details of remaining files such as
FileName and size.
import [Link].*;
class Slip12
{
public static void main(String args[]) throws Exception
{
for(int i=0;i {
File file=new File(args[i]);
if([Link]())
{
String name = [Link]();
if([Link](".txt"))
{
[Link]();
[Link]("file is deleted " +file);
}
else
{
[Link](name + " "+[Link]()+" bytes")
}
}
else
{
[Link](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 [Link].*;
class NumberZero extends Exception
{
NumberZero()
{}
}
class Number
{
static int no;
Number() throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
[Link]("Enter no");
no=[Link]([Link]());
try
{
if(no==0)
{
throw new NumberZero();
}
cal();
}//end of try
catch(NumberZero e)
{
[Link]("no is zero");
}
}
void cal()
{
int l=0,r=0;
l = no%10;
//[Link]("no = "+no);
if(no>9)
{
while(no>0)
{
r = no%10
}
[Link]("Addotion of first and last digit = "+(l+r));
}
else
[Link]("Addotion of first and last digit = "+l);
}
}
class Slip19
{
public static void main(String a[]) throws IOException
{
Number n= new Number();
}
}
B) Write a java program to display transpose of a given matrix.
import [Link];
public class Transposematrix {
public static void main(String[] args) {
Scanner sc= new Scanner([Link]);
[Link]("Enter rows of matrix : ");
int row=[Link]();
[Link]("Enter column of matrix : ");
int col=[Link]();
int[][] matrix = new int[row][col];
//create and insert value
for (int i=0;i<row;i++){
for(int j=0;j<col;j++){
[Link]("element at index ["+i+"]["+j+"] :");
matrix[i][j]=[Link]();
}
}
[Link]();
//print value of matrix
[Link]("\nReal matrix :-\n");
for (int i=0;i<row;i++){
for(int j=0;j<col;j++){
[Link](matrix[i][j]+" ");
}
[Link]();
}
[Link]("\nTranspose of matrix :-\n");
for (int i=0;i<row;i++){
for(int j=0;j<col;j++){
[Link](matrix[j][i]+" ");
}
[Link]();
}
}
}
slip 7:
A) Define an Interface Shape with abstract method area(). Write a java program to
calculate an area of Circle and Sphere.(use final keyword)
import [Link];
interface Shape{
void area();
}
class Circle implements Shape{
final float PI=3.14f;
float areacircle,radius;
Scanner s=new Scanner([Link]);
void accept(){
[Link]("Enter the Radius of circle : ");
radius=[Link]();
}
public void area(){
areacircle=PI*radius*radius;
}
public void show()
{
[Link]("Area of circle is : "+areacircle);
}
}
class Sphere implements Shape{
final float PI=3.14f;
float areasphere,radius;
Scanner s=new Scanner([Link]);
void accept(){
[Link]("Enter the Radius of sphere : ");
radius=[Link]();
}
public void area(){
areasphere=4*PI*radius*radius;
}
public void show(){
[Link]("Area of Sphere is : "+areasphere);
}
}
class InterfaceCircleSphere
{
public static void main(String args[]){
Circle s1=new Circle();
[Link]();
[Link]();
[Link]();
Sphere s2=new Sphere();
[Link]();
[Link]();
[Link]();
}
}
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)
import [Link];
interface Shape{
void area();
}
class Circle implements Shape{
final float PI=3.14f;
float areacircle,radius;
Scanner s=new Scanner([Link]);
void accept(){
[Link]("Enter the Radius of circle : ");
radius=[Link]();
}
public void area(){
areacircle=PI*radius*radius;
}
public void show()
{
[Link]("Area of circle is : "+areacircle);
}
}
class Sphere implements Shape{
final float PI=3.14f;
float areasphere,radius;
Scanner s=new Scanner([Link]);
void accept(){
[Link]("Enter the Radius of sphere : ");
radius=[Link]();
}
public void area(){
areasphere=4*PI*radius*radius;
}
public void show(){
[Link]("Area of Sphere is : "+areasphere);
}
}
class InterfaceCircleSphere
{
public static void main(String args[]){
Circle s1=new Circle();
[Link]();
[Link]();
[Link]();
Sphere s2=new Sphere();
[Link]();
[Link]();
[Link]();
}
}
slip9:
A) Write a java Program to display following pattern:
1
0 1
0 1 0
1 0 1 0
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){
[Link]("1 ");
}else{
[Link]("0 ");
}
}else{
if ((i+j)%2==0){
[Link]("0 ");
}else{
[Link]("1 ");
}
}
}[Link]();
}
}
}
slip 10:
A) Write a java program to count the frequency of each character in a given string.
import [Link];
public class Frequencyofchar {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string :");
String str = [Link]() ;
int[] freq = new int[[Link]()];
int i, j;
char string[] = [Link]();
for(i = 0; i <[Link](); i++) {
freq[i] = 1;
for(j = i+1; j <[Link](); j++) {
if(string[i] == string[j]) {
freq[i]++;
//Set string[j] to 0 to avoid printing visited character
string[j] = '0';
}
}
}
[Link]("Characters and their corresponding frequencies");
for(i = 0; i <[Link]; i++) {
if(string[i] != ' ' && string[i] != '0')
[Link](string[i] + "-" + freq[i]);
}
}
}
A) Write a menu driven java program using command line arguments for the following:
import [Link];
public class calculator {
public static void main(String[] args){
int num1=0,num2=0,option,ex=0;
Scanner sc = new Scanner([Link]);
do{
[Link]("Enter your choice from the following menu:");
[Link]("[Link] \[Link] \[Link] \
[Link] \[Link]");
option = [Link]();
if(option>4 || option<0){
if (option==5){
[Link]("You want to Exit.");
}else
[Link]("Invalid choice");
}
else{
[Link]("Enter the first number : ");
num1=[Link]();
[Link]("Enter the second number : ");
num2=[Link]();
}
switch(option){
case 1:[Link]("Addition of "+num1+" and "+num2+" is "+
(num1+num2));
break;
case 2:[Link]("Subtraction of "+num1+" and "+num2+" is
"+(num1-num2));
break;
case 3:[Link]("Multiplication of "+num1+" and "+num2+"
is "+(num1*num2));
break;
case 4: if(num2==0)
[Link]("Error!!! In Division denominator cannot be
0!");
else{
[Link]("In division of "+num1+" by "+num2+" is "+
(num1/num2)+" and remainder is "+(num1%num2));
}
break;
case 5: break;
}
if(option==5){
break;
}else{
[Link]("Do you want to continue? \[Link] \[Link]");
ex=[Link]();
}
}while(ex==1);
[Link]();
}
}
my class program
transpose of matrix
import [Link];
class TransposeMatrix {
int i, j,temp;
int row,column;
int array[][];
TransposeMatrix(int row,int column)
{
[Link]=row;
[Link]=column;
array=new int [row][column];
}
TransposeMatrix(int array[][])
{
[Link]=array;
[Link]=[Link];
[Link]=array[0].length;
}
void read(Scanner sc)
{
// int array[][] = new int[row][column];
[Link]("Enter matrix:");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
array[i][j]=[Link]();
// [Link](" ");
}
}
}
void trans()
{
int temp[][]=new int[column][row];
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
temp[j][i]=array[i][j];
}
}
array=temp;
}
void display()
{
//displaying the matrix
[Link]("The above matrix after Transpose is :");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
[Link](array[i][j]+" ");
}
[Link](" ");
}
}
}
public class TransposeMain {
public static void main(String[] args) {
int row,column;
//creating the instance of TransposeMatrix
//accessing the elements using scanner class
[Link]("Enter rows:");
Scanner s=new Scanner([Link]);
row = [Link]();
[Link]("enter column :");
column = [Link]();
TransposeMatrix t=new TransposeMatrix(row,column);
[Link](s); //calling a method
[Link]();
[Link]();
}
exception user defined throws
import [Link];
class AgeNotMatchException extends Throwable
{
AgeNotMatchException(String s)
{
super(s);
}
}
public class CustomException3 {
String name;
int age;
public CustomException3(String name, int age) {
[Link] = name;
[Link] = age;
try
{
if(age<17 || age>25)
{
throw new AgeNotMatchException("age is not in a rangeof 17
to 25");
}
}
catch(AgeNotMatchException e)
{
[Link](e);
}
}
void display()
{
[Link]("name of the person is "+name);
[Link]("age of the person is "+age);
}
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("Enter the person name");
String name=[Link]();
[Link]("Enter the person age");
int age=[Link]();
CustomException3 c=new CustomException3(name,age);
[Link]();