Slp27
Q1) Define an Employee class with suitable attributes having getSalary() method, which returns salary withdrawn by a particular employee. Write a class Manager which extends a class Employee, override the getSalary() method, which will return salary of manager by adding traveling allowance, house rent allowance etc. [10 marks]
class Employee {
String name;
double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public double getSalary() {
return salary;
}
}
class Manager extends Employee {
double ta,ha;
public Manager(String name, double salary, double ta,double ha){
super(name, salary);
this.ta = ta;
this.ha = ha;
}
@Override
public double getSalary() {
return super.getSalary() + ta + ha;
}
}
public class Slip27_2{
public static void main(String[] args) {
Employee e1 = new Employee("Aaaaa", 5000.0);
System.out.println("Employee Name = "+e1.name+" Salary: " + e1.getSalary());
Manager m = new Manager("Bbbb", 8000.0, 1000.0, 2000.0);
System.out.println("Manager Name = "+m.name+" Salary: " + m.getSalary());
}
}
Q2) Write a program to accept a string as command line argument and check whether it is a file or directory. Also perform operations as follows: i)If it is a directory,delete all text files in that directory. Confirm delete operation from user before deleting text files. Also, display a count showing the number of files deleted, if any, from the directory. ii)If it is a file display various details of that file.
import java.io.*;
class DirDemo
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String dirname=args[0],ext;
int ch,i,cnt=0;
File f1=new File(dirname);
ext="txt";
if(f1.isFile())
{
System.out.println(f1+"is a File\n");
System.out.println("Path : "+f1.getAbsolutePath());
System.out.println("File Size : "+f1.length()+"bytes\n");
}
else if(f1.isDirectory())
{
System.out.println(args[0]+"Is a Directory\n");
System.out.println("Contents of : "+dirname);
String s[]=f1.list();
for(i=0;i<s.length;i++)
{
File f=new File(dirname,s[i]);
if(f.isFile())
{
cnt++;
System.out.println(s[i]+"is a File\n");
}
else
System.out.println(s[i]+"is a Directory\n");
}
System.out.println("Total Number Of Files :"+cnt);
System.out.println("Do you want to delete files with extension 'txt'(1/0):?");
ch=Integer.parseInt(br.readLine());
if(ch==1)
{
for(i=0;i<s.length;i++)
{
File f=new File(dirname,s[i]);
if(f.isFile() && s[i].endsWith(ext))
{
System.out.println(s[i]+"->deleted");
f.delete();
}
}
}
}
}
}
Comments
Post a Comment