Showing posts with label TYBCA (Sem IV) Java Programs. Show all posts
Showing posts with label TYBCA (Sem IV) Java Programs. Show all posts

WAP read charator form file and Display if charator if is alphabet then reverse it Case wheter it digit or Space

/*WAP read charator form file and Display
if charator if is alphabet then reverse it Case
wheter it digit or Space
*/
import java.io.*;
class Slip1
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("a.txt");
while((c=fr.read())!=-1)
{
if(c>=65&&c<=90)
{
c=c+32;
System.out.print((char)c);
}
else if(c>=97&&c<=122)
{
c=c-32;
System.out.print((char)c);
}
else if((char)c==' ')
{
System.out.print("Space"+' ');
}
else if(c>=48&&c<=57)
{
System.out.print("Digit"+(char)c+' ');
}
else
{
System.out.print((char)c);
}

}
fr.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
a.txt
Shinde Sachinde 9767@gmail.com
RS1234
D:\javapro\file>javac Slip1.java
D:\javapro\file>java Slip1
sHINDESpace sACHINSpace Digit9 Digit7 Digit6 Digit7 @GMAIL.COM
rsDigit1 Digit2 Digit3 Digit4
*/

To copy the content one file into the another file while copying change the case of alphabet and replase all Digit by '*'.

/*To copy the content one file into the another file
while copying change the case of alphabet
and replase all Digit by '*'.
*/
import java.io.*;
class Slip2
{
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);
}
}
}
/*Output:-
a.txtfile
Shinde Sachin 9767@gmail.com
1234R

D:\javapro\file>javac Slip2.java

D:\javapro\file>java Slip2
Copy Successfully

D:\javapro\file>
b.txt
sHINDE sACHIN ****@GMAIL.COM
****r
*/

Count the Spaces in file

//Count the Spaces in file
import java.io.*;
class Space
{
public static void main(String args[])throws IOException
{
int c,lcnt=1;
try
{
FileReader fr=new FileReader("Space.java");
while((c=fr.read())!=-1)
{
System.out.print((char)c);
if((char)c==' ')
{
lcnt ++;
}
}fr.close();
System.out.println("no of  Space in file"+lcnt);
}catch(Exception e)
{
System.out.println(e);
}
}
}


Count the lines in file

//Count the lines in file
import java.io.*;
class Line
{
public static void main(String args[])throws IOException
{
int c,lcnt=1;
try
{
FileReader fr=new FileReader("Line.java");
while((c=fr.read())!=-1)
{
System.out.print((char)c);
if((char)c=='\n')
{
lcnt ++;
}
}fr.close();
System.out.println("no of lines io file"+lcnt);
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*
D:\javapro\file>javac Line.java

D:\javapro\file>java Line
//Count the lines in file
import java.io.*;
class Line
{
        public static void main(String args[])throws IOException
        {
                int c,lcnt=1;
                try
                {
                        FileReader fr=new FileReader("Line.java");
                        while((c=fr.read())!=-1)
                        {
                                System.out.print((char)c);
                                if((char)c=='\n')
                                {
                                        lcnt ++;
                                }
                        }fr.close();
                System.out.println("no of lines io file"+lcnt);
                }catch(Exception e)
                {
                        System.out.println(e);
                }
        }
}
no of lines io file26
*/

Count the Spaces,word,lines,space in file

//Count the Spaces,word,lines,space in file
import java.io.*;
class Count
{
public static void main(String args[])//throws IOException
{
int c,scnt=0,ccnt=0,lcnt=0,wcnt=0;
try
{
FileReader fr=new FileReader("Count.java");
while((c=fr.read())!=-1)
{
ccnt++;
if((char)c==' ')
{
scnt ++;
}
if((char)c=='\n')
{
lcnt++;
}
if((char)c==' '||(char)c=='\n')
{
++wcnt;
}
}
fr.close();
System.out.println("no of  Space in file"+scnt);
System.out.println("no of  Line in file"+lcnt);
System.out.println("no of  Word in file"+wcnt);
System.out.println("no of  Character in file"+ccnt);
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
*/

Simple copy one file to another file and Display on the cmd prompt

//Simple copy one file to another file and Display on the cmd prompt
import java.io.*;
class CopyDis
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("CopyDis.java");
FileWriter fw=new FileWriter("SimpleCopy.txt");
while((c=fr.read())!=-1)
{
fw.write(c);
System.out.print((char)c);
}
System.out.println("Copy Successfully");
fr.close();
fw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
*/

Simple copy one file to another file

//Simple copy one file to another file
import java.io.*;
class Copy
{
public static void main(String args[])throws IOException
{
int c;
try
{
FileReader fr=new FileReader("Copy.java");
FileWriter fw=new FileWriter("SimpleCopy.txt");
while((c=fr.read())!=-1)
{
fw.write(c);
}
System.out.println("Copy Successfully");
fr.close();
fw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
/*Output:-
*/

Write a java program to accept n name of cities & Sort them descending order.

class SortCity
{
            public static void main(String arg[])
            {
                        String name[]=new String[10];
                        int l=arg.length;
                        String temp;
                                    for(int i=0;i<l;i++)
                                    {
                                                name[i]=arg[i];
                                    }
                                    for(int j=0;j<l;j++)
                                    {
                                                for(int k=j+1;k<l;k++)
                                                {
                                                            if((name[j].compareTo(name[k]))<0)
                                                            {
                                                                        temp=name[j];
                                                                        name[j]=name[k];
                                                                        name[k]=temp;
                                                            }
                                                }
                                    }
                                    System.out.println("Sorted City Are-");
                                    for(int i=0;i<l;i++)
                                    {         
                                                System.out.println(name[i]);
                                    }
            }
}

/*OUTPUT
D:\javapro>javac SortCity.java

D:\javapro>java SortCity  pune nagar solapur thane satara baramati
Sorted City Are-
thane
solapur
satara
pune
nagar
baramati

*/

Define abstract class Shape with abstract method area() & Volume().Calculate area & Volume of Cone & Cylinder.

import java.io.*;
import java.lang.*;
abstract class Shape
{
float pi=3.14f;
abstract void area();
abstract void volume();
}
class Cone extends Shape
{
int red,side,height;
Cone(int r,int s,int h)
{
red=r;
side=s;
height=h;
}
void area()
{
float a=pi*red*side;
System.out.println("Area of Cone-"+a);
}
void volume()
{
float v=pi*red*red*(height/3);
System.out.println("Volume of Cone-"+v);
}
}
class Cylinder extends Shape
{
int red ,height;
Cylinder(int r,int h)
{
red=r;
height=h;
}
void area()
{
float a1=2*pi*red*height+2*pi*red*red;
System.out.println("Area of Cylinder-"+a1);
}
void volume()
{
float v1=pi*red*red*height;
System.out.println("Volume of Cylinder-"+v1);
}
}
class ShapeDemo1
{
public static void main(String arg[])
{
int r,s,h;
DataInputStream din=new DataInputStream(System.in);
try
{
System.out.println("Enter the values of Redius, Side , height-");
r=Integer.parseInt(din.readLine());
s=Integer.parseInt(din.readLine());
h=Integer.parseInt(din.readLine());
Cone c1=new Cone(r,s,h);
c1.area();
c1.volume();
Cylinder c2=new Cylinder(r,h);
c2.area();
c2.volume();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
/*Output
D:\javapro\Ass2>javac ShapeDemo1.java
Note: ShapeDemo1.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

D:\javapro\Ass2>java ShapeDemo1
Enter the values of Redius, Side , height-
4
6
8
Area of Cone-75.36
Volume of Cone-100.48
Area of Cylinder-301.44
Volume of Cylinder-401.92
*/

Create an interface Shape with method area() Calculate area of Circle and Sphere. (Use final keyword)

import java.io.*;
interface Shape
{
final float pi=3.14f;
float area(int r);
}
class Circle implements Shape
{
public float area(int r)
{
return(pi*r*r);
}
}
class Sphere implements Shape
{
public float area(int r)
{
return(4*pi*r*r);
}
}
class ShapeDemo
{
public static void main(String arg[])
{
int r;
BufferedReader din=new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("Enter radius-");
r=Integer.parseInt(din.readLine());
Circle c1=new Circle();
Sphere s1=new Sphere();
System.out.println("Area of Circle:"+c1.area(r));
System.out.println("Area of Sphere:"+s1.area(r));
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
/*Output
D:\javapro\Ass2>javac ShapeDemo.java

D:\javapro\Ass2>java ShapeDemo
Enter radius-5
Area of Circle:78.5
Area of Sphere:314.0
*/

WAP to accept n cricket player, display average run of single player & all players. (Use array of object, method overloading & static keyword).

import java.io.*;
class Player
{
int p_code,run;
String p_name;
int i_played;
int nooftime_notout;
float avg;
void accept()
{
DataInputStream din=new DataInputStream(System.in);
try
{
System.out.print("Enter the player code-");
p_code=Integer.parseInt(din.readLine());
System.out.print("Enter the player name-");
p_name=din.readLine();
System.out.print("Enter the player runs-");
run=Integer.parseInt(din.readLine());
System.out.print("Enter the player inning played-");
i_played=Integer.parseInt(din.readLine());
System.out.print("Enter the player no of time not out-");
nooftime_notout=Integer.parseInt(din.readLine());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void display()
{

avg=run/(i_played-nooftime_notout);
System.out.println(p_name+"         "+avg);
}
void display(String nm)
{
if((nm.compareTo(p_name))==0)
{
avg=run/(i_played-nooftime_notout);
System.out.println(p_name+"         "+avg);
}
}
}
class PlayerDemo
{
public static void main(String arg[])
{
Player p1[]=new Player[10];
//Player p=new Player();
int i,n,ch;
boolean flag=true;
String nm;
DataInputStream din=new DataInputStream(System.in);
try
{
System.out.print("How many record accept u want-");
n=Integer.parseInt(din.readLine());
for(i=0;i<n;i++)
{
p1[i]=new Player();
p1[i].accept();
}
while(true)
{
System.out.println("\n1.Display average of all player\n2.Display average run of single player \n3.Exit ");
System.out.print("Enter ur Choice-");
ch=Integer.parseInt(din.readLine());

switch(ch)
{
case 1:
System.out.println("Player name is "+"   "+"Average is");
for(i=0;i<n;i++)
{
p1[i].display();
}
break;
case 2:
System.out.print("Enter player name whose display Average-");
nm=din.readLine();
System.out.println("\nPlayer name is "+"   "+"Average is");
for(i=0;i<n;i++)
{
p1[i].display(nm);
}
break;
case 3:
System.exit(0);
break;
default:

System.out.println("invalid Choice");

}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
/*Output
D:\javapro\Ass2>javac PlayerDemo.java
Note: PlayerDemo.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

D:\javapro\Ass2>java PlayerDemo
How many record accept u want-2
Enter the player code-101
Enter the player name-sachin
Enter the player runs-5700
Enter the player inning played-45
Enter the player no of time not out-85
Enter the player code-102
Enter the player name-dhoni
Enter the player runs-4500
Enter the player inning played-86
Enter the player no of time not out-45

1.Display average of all player
2.Display average run of single player
3.Exit
Enter ur Choice-1
Player name is    Average is
sachin               -142.0
dhoni                109.0

1.Display average of all player
2.Display average run of single player
3.Exit
Enter ur Choice-2
Enter player name whose display Average-sachin

Player name is    Average is
sachin               -142.0

1.Display average of all player
2.Display average run of single player
3.Exit
Enter ur Choice-3
*/