0% found this document useful (0 votes)
6 views

9919004039 java programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

9919004039 java programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

Write a Java program that reads a file name from the user, and
then displays information about whether the file exists, whether
the file is readable, whether the file is writable, the type of file
and the length of the file in bytes
Coding:
import java.io.*;
class Main
{
public static void main(String args[])
{
String filename =("mani.txt");
File f = new File("mani.txt");
System.out.println("File exists: "+f.exists());
System.out.println("File is readable: "+f.canRead());
System.out.println("File is writable: "+f.canWrite());
System.out.println("length of the file: "+f.length()+"
bytes");

try
{
char ch;
StringBuffer buff = new StringBuffer("");
FileInputStream fis = new
FileInputStream(filename);
while(fis.available()!=0)
{
ch = (char)fis.read();
buff.append(ch);
}
System.out.println("\nContents of the file are: ");
System.out.println(buff);
fis.close();
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find the specified
file...");
}
catch(IOException i)
{
System.out.println("Cannot read file...");
}
}
}
2.Program to calculate area of different figures using interfaces
Coding:
interface area
{
double pi = 3.14;
double calc(double x,double y);
}

class rect implements area


{
public double calc(double x,double y)
{
return(x*y);
}
}

class cir implements area


{
public double calc(double x,double y)
{
return(pi*x*x);
}
}

class Main
{
public static void main(String arg[])
{
rect r = new rect();
cir c = new cir();
area a;

a = r;
System.out.println("\nArea of Rectangle is : "
+a.calc(10,20));

a = c;
System.out.println("\nArea of Circle is : " +a.calc(15,15));
}
}

3.Write a program to read and show the contents of three


different files as a sequence.
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
String s="";
FileInputStream f1,f2,f3;
f1=new FileInputStream("mani.txt");
f2=new FileInputStream("mani2.txt");
f3=new FileInputStream("mani3.txt");
byte[] b1,b2,b3;
b1=new byte[f1.available()];
b2=new byte[f3.available()];
b3=new byte[f3.available()];
f1.read(b1);
f2.read(b2);
f3.read(b3);
String s1,s2,s3;
s1=new String(b1);
s2=new String(b2);
s3=new String(b3);
f1.close();
f2.close();
f3.close();
s="File 1\n"+s1+"\nFile 2\n"+s2+"\nFile 3\n"+s3;
System.out.println("Final Sequence is : \n"+s);
}
}

4.Write a Java Program to create an abstract class named Shape


that contains two integers and an empty method named print
Area (). Provide three classes named Rectangle, Triangle and
Circle such that each one of the classes extends the class Shape.
Each one of the classes contains only the method print Area ()
that prints the area of the given shape
Coding:
abstract class shape
{
int a=15,b=85;
abstract public void print_area();

}
class rectangle extends shape
{
public int area_rect;

public void print_area()


{
area_rect=a*b;
System.out.println("The area ofrectangle is:"+area_rect);
}

}
class triangle extends shape
{
int area_tri;

public void print_area()


{
area_tri=(int) (0.5*a*b);
System.out.println("The area oftriangle is:"+area_tri);
}
}

class circle extends shape


{
int area_circle;

public void print_area()


{
area_circle=(int) (3.14*a*a);
System.out.println("The area ofcircle is:"+area_circle);
}
}
public class Main
{

public static void main(String[] args)


{

rectangle r=new rectangle();


r.print_area();
triangle t=new triangle();
t.print_area();
circle r1=new circle();
r1.print_area();
}
}
5.Write a program to counts occurrences of a certain character
in a given string
Coding:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String str1 = "MANI";
System.out.println("Original string: "+str1);
String resultV1 = repeat_str(str1, 7);
System.out.println("\nAfter repeating 7 times: "+resultV1);
}
public static String repeat_str(String str1, int n) {
if (str1 == null || str1.isEmpty()) {
return "";
}
if (n <= 0) {
return str1;
}
if (Integer.MAX_VALUE / n < str1.length()) {
throw new OutOfMemoryError("Maximum size of a
String will be exceeded");
}
StringBuilder x = new StringBuilder(str1.length() * n);
for (int i = 1; i <= n; i++) {
x.append(str1);
}
return x.toString();
}
}
Output:
6.Write a java program to perform string operations using Array
List.
Coding:
import java.util.*;
public class Main
{
public static void main(String args[])
{
ArrayList<String> obj = new ArrayList<String>();
obj.add("Sai");
obj.add("Abhi");
obj.add("Kalyan");
obj.add("Manoj");
obj.add("Ganesh");
System.out.println("\n"+"Elements in ArrayList:");
System.out.print("\t"+obj+""+"\n" + "\n");
obj.add(0, "Mani");
obj.add(1, "teja");
System.out.println("After Inserting Elements:"+"");
System.out.print("\t"+obj+""+"\n" + "\n");
System.out.println("Search For Element:");
Scanner in = new Scanner(System.in);
String searchStr=in.nextLine();
boolean ans = obj.contains(searchStr);
if (ans)
System.out.println("\t"+"ArrayList contains" +searchStr+ "\n");
else
System.out.println("ArrayList does not contains "+searchStr);
System.out.println("Arraylist get the strings starting with given
letter:");
ArrayList<String> obj1 = new ArrayList<String>();
String start= in.next();
for(int i=0;i<obj.size();i++)
{
if(obj.get(i).startsWith(start.toUpperCase()))
{
obj1.add(obj.get(i));
}
}
System.out.print("\t"+obj1+""+"\n" + "\n");
}
}
Output Screen:

You might also like