Welcome to Java Examples

Take a cup of tea and Let's Start programming

Showing posts with label command line arguments. Show all posts
Showing posts with label command line arguments. Show all posts
Declare a class called book having author_name as private data member. Extend book class to have two sub classes called book_publication & paper_publication.
Each of these classes have private member called title. Write a complete program to show usage of dynamic method dispatch (dynamic polymorphism) to display book or paper publications of given author. Use command line arguments for inputting data.


import java.util.Scanner;

class book
{
private String[] author_name = {"jaimin","naitik"};

void display()
{
for(int i=0;i<author_name.length;i++)
{
System.out.println("Author names --> \t"+author_name[i]);
}
}
}

class book_publication extends book
{
private String[][] title = {{"java","c lang","oopc"},{"os","micro processor 8085","microprocessor 8086"}};

void display(int j)
{
System.out.println("Books name of given author are....");
System.out.println("------------------------------------------------");
for(int i=0;i<3;i++)
{
System.out.println(title[j][i]);
}
}
}

class paper_publication extends book
{
private String[][] title ={{"Atul","Easy paper Solution"},{"Gala paper Solution","alpha paper Solution"}};

void display(int j)
{
System.out.println("paper publication name of given author are....");
System.out.println("------------------------------------------------");

for(int i=0;i<2;i++)
{
System.out.println(title[j][i]);
}
}
}

class publication
{
public static void main(String [] args)
{
Scanner jaimin = new Scanner(System.in);
book o=new book();
book_publication b_o=new book_publication();
paper_publication p_o=new paper_publication();


int length=args.length;
int author=0,choice;
if(length==0)
{
System.out.println("please enter author name");
}

if(length>=2)
{
System.out.println("Add only one name to search names");
}

if(args[0].equals("jaimin"))
{
author=0;
}
else if(args[0].equals("naitik"))
{
author=1;
}
else
{
System.out.println("You have added wrong name of author");
System.exit(-1);
}


System.out.println("press \"1\" to display book author names\npress \"2\" to display book title names\npress \"3\" to display paper publication names ");
choice=jaimin.nextInt();

switch(choice)
{
case 1:
o.display();
break;

case 2:
b_o.display(author);
break;

case 3:
p_o.display(author);
break;

default:
System.out.println("wrong choice....");
System.exit(-2);
break;

}

}
}

output



Declare a class called employee having employee_id and employee_name as members. Extend class employee to have a subclass called salary having designation and monthly_salary as members. Define following:
- Required constructors
- A method to find and display all details of employees drawing salary more than Rs. 20000/-.
- Method main for creating an array for storing these details given as command line arguments and showing usage of above methods.



import java.util.Scanner;
class Employee
{
String[] employee_id;
String[] employee_name;
}

class Salary extends Employee
{
String[] Designation;
double[] monthly_salary;

Salary(int j)
{
/*initialization of array */

employee_name=new String[j];
employee_id=new String[j];
Designation=new String[j];
monthly_salary= new double[j];

}

void display(int j)
{


System.out.println("---------------------------------------------------------------");
System.out.println("---------------------------------------------------------------");
System.out.println("\t Details of employee who have salary above 20000");
System.out.println("---------------------------------------------------------------");
System.out.println("---------------------------------------------------------------\n \n");


System.out.format("%-15s %-15s %-25s %-10s %n","employee id","employee name","employee Designation","Monthly Salary");
System.out.println("----------------------------------------------------------------------------");
for(int i=0;i<j;i++)
{

if(monthly_salary[i]>=20000)
{
System.out.format("%-15s %-15s %-25s %-10s %n",employee_id[i],employee_name[i],Designation[i],monthly_salary[i]);

}
}
}


public static void main(String [] args)
{
Scanner jaimin=new Scanner(System.in);
int length=args.length;

Salary obj = new Salary(length);


if(length==0)
{
System.out.println("please enter employee id");
}

for(int i=0;i<length;i++)
{
obj.employee_id[i]=args[i];

System.out.println("\n\n enter the details of \""+args[i]+"\" employee id");

System.out.print("\n name of employee -->");
obj.employee_name[i]=jaimin.next();

System.out.print("\n Designation of employee -->");
obj.Designation[i]=jaimin.next();

System.out.print("\nMonthly salary of employee -->");
obj.monthly_salary[i]=jaimin.nextDouble();


}

obj.display(length);
}
}

output