Java praticale
Q.1. Write a Program to print all Prime
numbers in an array of ‘n’ elements. (use
command line arguments )
import [Link];
public class PrimeNumbers{
public static void main (String[] args){
int[] array = new int [5];
Scanner in = new Scanner ([Link]);
[Link]("Enter the elements of the array: ");
for(int i=0; i<5; i++)
{
array[i] = [Link]();
}
//loop through the numbers one by one
for(int i=0; i<[Link]; i++){
boolean isPrime = true;
//check to see if the numbers are prime
for (int j=2; j<array[i]; j++){
if(array[i]%j==0){
isPrime = false;
break;
}
}
//print the number
if(isPrime)
[Link](array[i] + " are the prime numbers in the array ");
}
}
}
Q.2 Define an abstract class Staff with
protected members id and name. Define
a parameterized constructor. Define one
subclass OfficeStaff with member
department. Create n objects of
OfficeStaff and display all details.
import [Link];
import [Link];
import [Link];
abstract class Staff{
String name,address;
}
class FullTimeStaff extends Staff{
String department;
double salary;
public void accept() throws IOException{
[Link]("Enter the name, address, department and salary: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
name=[Link]();
address=[Link]();
department=[Link]();
salary=[Link]([Link]());
}
public void display(){
[Link]("Name: "+name);
[Link]("Address: "+address);
[Link]("Department: "+department);
[Link]("Salary: "+salary);
[Link]("----------------------");
}
}
class PartTimeStaff extends Staff{
int hours, rate;
public void accept() throws IOException{
[Link]("Enter the name, address, No of working hours and rate per hour: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
name=[Link]();
address=[Link]();
hours=[Link]([Link]());
rate=[Link]([Link]());
}
public void display(){
[Link]("Name: "+name);
[Link]("Address: "+address);
[Link]("No of Working Hours: "+hours);
[Link]("Rate per hour: "+rate);
[Link]("----------------------");
}
}
public class sb1 {
public static void main(String [] args) throws IOException{
int i;
[Link]("Select Any One: ");
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("[Link] Time Staff");
[Link]("[Link] Time Satff");
int ch=[Link]([Link]());
switch(ch){
case 1:
[Link]("Enter the number of Full Time Staff: ");
int n=[Link]([Link]());
FullTimeStaff [] l=new FullTimeStaff[n];
for(i=0;i<n;i++){
l[i]=new FullTimeStaff();
l[i].accept();
}
for(i=0;i<n;i++){
l[i].display();
}
break;
case 2:
[Link]("Enter the number of Part Time Staff: ");
int m=[Link]([Link]());
PartTimeStaff [] h=new PartTimeStaff[m];
for(i=0;i<m;i++){
h[i]=new PartTimeStaff();
h[i].accept();
}
for(i=0;i<m;i++){
h[i].display();
}
break;
}
}
}