File Information Java Program

This program take a file path and file name in the command line and displays the details of the file. This is a part of Mumbai University MCA Colleges Java Programs sem 4.


Program to accepts a filename from command line argument and displays the number of characters, words, and lines in the file.



import java.io.*;
class CountF
{
public static void main(String args[])throws IOException
{
 int ch;
 boolean prev=true;
 int char_count=0;
 int word_count=0;
 int line_count=0;
 FileInputStream fin=new FileInputStream(args[0]);
 while((ch=fin.read())!=-1)
 {
   if(ch!=' ') ++char_count;
   if(!prev && ch==' ') ++word_count;
   if(ch==' ') prev=true;else prev=false;
   if(ch=='\n') ++line_count;
 }
 char_count -= line_count*2;
 word_count += line_count;
 System.out.println("Number of Characters="+char_count);
 System.out.println("Number of Words="+word_count);
 System.out.println("Number of Lines="+line_count);
 fin.close();
 }
}


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Command line Arguments Java

This program is for displaying the command line arguments in Java. This is a part of Mumbai University MCA Colleges Java programs

class Argu
{
public static void main(String args[])
{
int n=args.length;
System.out.println("Number of Arguments are="+ n);
System.out.println("The Arguments are=");
for(int i=0;i<n;i++)
 {
System.out.println(args[i]);
}
}
}

We use the argument passed in the command line to show them in the loop. The arguments are passed to the main() in the form of string


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Count Objects using Static Variable Java

This program to count the number of objects created for a class using static member function. This is a part of Mumbai University MCA Colleges Java Program


class count
{
 static int count=0;
 public static void main(String arg[])
 {
  count c1=new count(); 
 c1.count();
  count c2=new count();
 c2.count();
  count c3=new count(); 
 c3.count();
  System.out.println("Number of Objects="+count);
 }
 static void count()
 {
 count++;
 }
}

In the above program we have taken a static variable name count. Static variable are called as class variables. There is only once instance of a static variable for whole class and all the objects of the class. In this program we increment the static variable count when we call the count() function with the object. All objects c1, c2 and c3 access the same variable and hence we can find the count of the objects created.


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Number Format Conversion Java

Write a program to accept an integer No. from keyboard and convert it into other no system. This is a part of Mumbai University MCA Colleges Java Program

import java.io.*;
class Convert
{
 public static void main(String args[])throws IOException
  {
    BufferedReader br=new BufferedReader
    (new InputStreamReader(System.in));
    System.out.println("Enter an Integer");
    String str= br.readLine();
  
    int i=Integer.parseInt(str);
    System.out.println("In Decimal:"+i);
  
    str=Integer.toBinaryString(i);
    System.out.println("In Binary:"+str);

   str=Integer.toHexString(i);
   System.out.println("In HexaDecimal:"+str);

   str=Integer.toOctalString(i);
   System.out.println("In Octal:"+str);               
   }
}

The above program will take an input using InputStreamReader as an int and then convert the numbers to different number formats like Binary, Hexadecimal and Octal using the built in String conversion

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Throwing User Defined Exception In Java


This Simple Java Program is for User Defined Exception in Java. This is a part of Mumbai University MCA Colleges Java Practicals. 

Exceptions are used to handle any abnormalities with in the program execution. The Main purpose or use of exception handling is to give the user the feel that the program terminated bug free. Exception handling avoids the abnormal termination of program.

In the Below program we create a class named MyException which Extends the Class Exception. Hence we need to import the Class Exception. The user defined MyException class calls the constructor of Exception class. In class C_area, a function calculates the area of the circle. The function throws a exception if the radius of the circle in entered negative. Which in turns calls the Exception class.




import java.lang.Exception;
class MyException extends Exception
{
MyException(String msg)
{
super(msg);
}
}

class C_area
{
double r,a;
C_area()
{
r=0;
a=0;
}
void calc_area(double r1)
{
try
{
r=r1;
if(r<0)
throw new MyException("Radius cannot be negative"); // User Defined Exception thrown
else
{
a= 3.14*r*r;
System.out.println("The area of circle is : "+ a);
}
}
catch(MyException e)
{
System.out.println("MyException triggered");
System.out.println(e.getMessage());
}
}
}

class Area
{
public static void main(String ar[])
{
C_area cir=new C_area();
cir.calc_area(-5); //Will Throw Exception
}
}


Output:-
MyException triggered
Radius cannot be negative


Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

File Properties in Java

This Simple Java Program is for Reading the Properties of File in Java. This is a part of Mumbai University MCA Colleges Java Practicals.

File Object in Java is used to to store files. The File object has many functions. The File object stores all the information about the file and has many functions to display the file properties.


The Below program opens a file named demofile.txt from a specified location (You can change the file you want to read). It then displays the various common properties of the file object.

import java.io.File;
import  java.lang.*;

class Character_file
{
public static void main(String ar[])
{
try
{
File f1= new File("D:/demofile.txt");
System.out.println("File Name:  "+ f1.getName());
System.out.println("File Path:  "+ f1.getPath());
System.out.println("File Absolute Path:   "+f1.getAbsolutePath());
System.out.println("Parent File:  "+f1.getParent());
System.out.println("File Exists?  "+f1.exists());
System.out.println("Can Write?  "+f1.canWrite());
System.out.println("Can Read?   "+f1.canRead());
System.out.println("Is Directory?  "+f1.isDirectory());
System.out.println("Is File?   "+f1.isFile());
System.out.println("Is Absolute?   "+f1.isAbsolute());
System.out.println("File last modified:  "+f1.lastModified());
System.out.println("File size:    "+f1.length());
}
catch(Exception ex)
{
System.out.println("Error occured");
}
}
}

Output:-

File Name:  demofile.txt
File Path:  D:\
File Absolute Path:   D:\demofile.txt
Parent File:  D:\
File Exists?  true
Can Write?  true
Can Read?   true
Is Directory?  false
Is File?   true
Is Absolute?   true
File last modified:  1072899408186
File size:    4 

Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

Copying File in Java


This Simple Java Program is for Copying of a Text File into another Text File in Java. This is a part of Mumbai University MCA Colleges Java Practicals.

Reading a File in Java requires you to include or import java.io file. There are many different classes used for reading a file or byte stream in java. FileInputStream is most commonly used. To write to a file we will use FileOutputStream. We can read different types of files with this


The Below program opens a file named demofile.txt from a specified location (You can change the file you want to read). It then reads the file line by line using the read() function of FileInputStream Class. The read() function will return a -1 if it encounters end of file (EOF). The contents of the demofile are then copied to demooutput file using the write() function of FileOutputStream Class. The end result is that the copying of file in java is completed and the new content of Demooutput file are displayed

 

import java.io.*;
class copyfile
{
public static void main(String ar[])
{
File f_input=new File("D:/demofile.txt"); // You can change the file names
File f_output=new File("D:/demooutput.txt");
try
{
FileInputStream fis=new FileInputStream(f_input);
FileOutputStream fos=new FileOutputStream(f_output);
int t=fis.read();
while(t!=-1)
{
fos.write(t);
t=fis.read();
}

fis.close();
fos.close(); 
fis=new FileInputStream(f_output);
t=fis.read();
while(t!=-1)
{
System.out.print((char)t);
t=fis.read();
}
}
catch(Exception e)
{
System.out.print(e);
}
}
}

Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

Read Text File in Java

This Simple Java Program is for Reading a Text File in Java. This is a part of Mumbai University MCA Colleges Java Practicals. 

Reading a File in Java requires you to include or import java.io file. There are many different classes used for reading a file or byte stream in java. FileInputStream is most commonly used. To write to a file we will use FileOutputStream. We can read different types of files with this


The Below program opens a file named demofile.txt from a specified location (You can change the file you want to read). It then reads the file line by line using the read() function of FileInputStream Class. The read() function will return a -1 if it encounters end of file (EOF). 

import java.io.*;
class filedisplay
{
public static void main(String ar[])
{
File f_input=new File("D:/demo/java/demofile.txt");   //You can change the file 
try
{
FileInputStream fis=new FileInputStream(f_input);
int t=fis.read();
while(t!=-1)
{
System.out.print((char)t);
t=fis.read();
}
fis.close();
}
catch(Exception e)
{
System.out.print(e);
}
}
}

Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

Super Class Constructor Java


This Simple Java Program is for calling the Base or Super Class Constructor in the Derived Class in Java. This is a part of Mumbai University MCA Colleges Java Practicals. 

Super Keyword is used to denote the base class for the inherited class or the child class. Super is used to call the base class constructor or the members variables of the base class. Super must be called in first line in the child or derived or inherited class constructor.


In the below program, The Class Interest is inherited by 2 derived class named Simple Interest and Compound Interest. They both call the base class constructor (Interest class constructor) using the java super keyword.


class Interest
{
double p,amt,n;
Interest(double a,double y)
{
p=a;
n=y;
}
void display()
{
System.out.println("\nAMOUNT = "+p+"\nNOS OF YRS ="+n);
}
}

class SimpleInterest extends Interest
{
SimpleInterest(double a,double y)
{
super(a,y);
}
void calc()
{
amt= (p*n*0.0925);
System.out.println("SIMPLE INTEREST="+amt);
}
}

class CompoundInterest extends Interest
{
CompoundInterest(double a,double y)
{
super(a,y);
}
void calc()
{
amt= p*Math.pow((1+(8.5/100)),n);
System.out.println("COMPOUND INTEREST="+amt);
}
}

class Bank
{
public static void main(String str[])
{
SimpleInterest s1=new SimpleInterest(25000,5);
CompoundInterest c1=new CompoundInterest(25000,5);
s1.display();
s1.calc();
c1.display();
c1.calc();
}
}

Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

Abstract Classes Java

This Simple Java Program is for performing Abstract classes in Java. This is a part of Mumbai University MCA Colleges Java Practicals.

Abstract Classes Contains Abstract Methods as well as hardcore methods (Methods which are predefined).  Abstract classes are extended or inherited.

In the Below program Abstract Class named Interest is used by 2 classes named SimpleInterest and CompoundInterest. These 2 classes uses the variables, Constructor and the display method of the abstract class. Both the classes use different definitions for the abstract method calc() to calculate the interest.

abstract class Interest
{
double p,amt,n;
Interest(double a,double y)
{
p=a;
n=y;
}
void display()
{
System.out.println("\nAMOUNT = "+p+"\nNOS OF YRS ="+n);
}
abstract void calc();
}

class SimpleInterest extends Interest
{
SimpleInterest(double a,double y)
{
super(a,y);
}
void calc()
{
amt= (p*n*0.0925);
System.out.println("SIMPLE INTEREST="+amt);
}
}

class CompoundInterest extends Interest
{
CompoundInterest(double a,double y)
{
super(a,y);
}
void calc()
{
amt= p*Math.pow((1+(8.5/100)),n);
System.out.println("COMPOUND INTEREST="+amt);
}
}

class Bank
{
public static void main(String str[])
{
Interest i;
SimpleInterest s1=new SimpleInterest(25000,5);
CompoundInterest c1=new CompoundInterest(25000,5);
i=s1;
i.display();
i.calc();
i=c1;
i.display();
i.calc();
}
}


Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

Interface in Java

This Simple Java Program is for performing Interfaces in Java. This is a part of Mumbai University MCA Colleges Java Practicals.


Interface just carry the signature of the function to be used. The class which implements a interface has to define the function which its gonna implement.

.

interface Exam
{
boolean pass(int marks);
}

interface Classify
{
String division(int avg);
}

class Result implements Exam,Classify
{
public boolean pass(int marks)
{
System.out.println("Marks obtained are "+ marks);
if(marks>=50)
return true;
else
return false;
}
public String division(int avg)
{
if(avg>=60)
return("First Division");
else if(avg>50)
return("Second Division");
else
return("Third Division");
}
public static void main(String ar[])
{
Result r =new Result();
boolean flg;
flg=r.pass(60);
if(flg==true)
System.out.println("Marks>=50");
else
System.out.println("Marks<50");
System.out.println(r.division(60)); 
}
}

Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

Java Constructors Overloading

This Simple Java Program is for performing Constructor Overloading in Java. This is a part of Mumbai University MCA Colleges Java Practicals.

Constructor is required when we declare a class object. Java Class Constructors initializes the variables of the object. Constructor Overloading is done so that the objects of different forms can be initialized at the declaration time.

The Below program uses a class named Box which finds the volume of the Box object. This class has 2 constructors one for initializing a Cube Box and other for Initializing a Rectangular Box.
As those are constructors and return nothing, they dont have a return type.


class Box
{
int l,b,h;
Box(int x)             // Constructor for Cube
{
 l=x;
 b=x;
 h=x;
}
Box(int x,int y,int z)            //Constructor for Rectangle
{
 l=x;
 b=y;
 h=z;
}
void volume()
{
          System.out.println("Volume of the box is "+(l*b*h));
}
public static void main(String args[])
{
Box cube= new Box(10);          //Calling cube constructor
Box b=new Box(10,4,7);          //Calling box constructor
cube.volume();
b.volume();
}
}

Hope this Java Program is useful to you in some sense or other. Keep following this blog for more Mumbai University MCA College Programs and simple Programs. Happy Programming and Studying

Function Overloading Java Polymorphism

This Simple Java is for performing Function Overloading / Compile Time Polymorphism. This is a part of Mumbai University MCA Colleges Java Practicals.

Function Overloading or Polymorphism is the concept of using Same Function Name (Different Signatures) to perform differently.

The Below java program calculates the areas of Circle, Triangle and Rectangle with same name function but different signatures.


class Area
{
void area(int l,int b)
{
          System.out.println("AREA OF RECTANGLE ="+(l*b));
}
void area(double b,double h)
{
System.out.println("AREA OF TRIANGLE ="+(0.5*b*h));
}
void area(double r)
{
System.out.println("AREA OF CIRCLE ="+(3.14*r*r));
}
public static void main(String args[])
{
Area a=new Area();
a.area(10.5,20.4);
a.area(2,6);
a.area(15.3);
}
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs and Simple Programs. Happy Programming and Studying.