Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JAVA Programs

The document contains multiple Java programs demonstrating various functionalities such as printing personal details, performing arithmetic operations, calculating grades based on marks, and computing net salaries for different employee roles. It also includes programs for checking number properties, managing arrays of items, and utilizing inheritance for product information in a shop context. Additionally, there are examples of hierarchical inheritance in a library system and input validation for employee details based on specific constraints.

Uploaded by

preetlakhani07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA Programs

The document contains multiple Java programs demonstrating various functionalities such as printing personal details, performing arithmetic operations, calculating grades based on marks, and computing net salaries for different employee roles. It also includes programs for checking number properties, managing arrays of items, and utilizing inheritance for product information in a shop context. Additionally, there are examples of hierarchical inheritance in a library system and input validation for employee details based on specific constraints.

Uploaded by

preetlakhani07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

JAVA Programs

1. Write an Application program to print your name and address.

class p1

public static void main(String args[])

System.out.println("NAME : MAULIN ");

System.out.println("ADD : ANAND ");

2. Perform arithmetic operations [ add,sub,mul,div].

import java.util.Scanner;

class p2
{
public static void main(String arg[])
{
Scanner x=new Scanner(System.in);
int a,b;

System.out.println("ENTER VALUE 1 : ");


a=x.nextInt();

System.out.println("ENTER VALUE 2 : ");


b=x.nextInt();

System.out.println("ADD : "+(a+b));
System.out.println("SUB : "+(a-b));
System.out.println("MUL : "+(a*b));
System.out.println("DIV : "+(a/b));
}
}
3. Enter marks of 3 three subjects, make total and percentage (using command line). Display grade,
“A” if per >=60 b. “B” if per 60< and >=50 c. “C” if per 50< and >=40 d. “F” if per 40<

import java.util.Scanner;
class p3
{
public static void main(String args[])
{
Scanner x=new Scanner(System.in);
int a,b,c,per;
String g="";

System.out.print("ENTER MARK 1 :");


a=x.nextInt();
System.out.print("ENTER MARK 2 :");
b=x.nextInt();
System.out.print("ENTER MARK 3 :");
c=x.nextInt();

per=((a+b+c)*100)/300;

if(per>=60)
g="A";
else if(per>=50 && per<60)
g="B";
else if(per>=40 && per<50)
g="C";
else if(per<40)
g="F";

System.out.println("PERCENTAGE : "+per);
System.out.println("GRADE : "+g);

}
}

4. Create a method area(),

overload it to calculate area of circle, rectangle and square

import java.util.Scanner;

class chk
{
void area(double r)
{
System.out.println("AREA OF CIRCLE IS :"+(3.14*r*r));
}

void area(int l,int b)


{
System.out.println("AREA OF TRIANGLE IS :"+(l*b));
}

int area(int x)
{
return (x*x);
}
}

class p4
{
public static void main(String args[])
{
chk aa=new chk();
aa.area(10.5);
aa.area(5,5);
System.out.println("AREA OF SQUAR IS :"+aa.area(5));
}
}

5. Calculate net salary of employee with following constraints by overloading salary().

i. If employee is manager, pass basic, da, hra, ma, pf, it, pt

ii. If employee is executive, pass basic, da, hra, pf, it

iii. If employee is jn_executive, pass basic, da, hra, it

iv. If employee is peon, pass basic, da.

import java.util.Scanner;

class netsal
{
double nt;

//manager
double salary(double basic,double da,double hra,double ma,double pf,double it,double pt)
{
nt=(basic+da+hra+ma)-(pt+pf+it);
System.out.println("NET SALARY IS "+nt);
return 0;
}

//executive

double salary(double basic,double da,double hra,double pf,double it)


{
nt=(basic+da+hra)-(pf+it);
System.out.println("NET SALARY IS "+nt);
return 0;
}

//jn_executive

double salary(double basic,double da,double hra,double it)


{
nt=(basic+da+hra)-(it);
System.out.println("NET SALARY IS "+nt);
return 0;
}

//peon

double salary(double basic,double da)


{
nt=(basic+da);
System.out.println("NET SALARY IS "+nt);
return 0;
}
}

class p5
{
public static void main(String args[])
{
Scanner x=new Scanner(System.in);

String emp;
double basic,da,hra,ma,pf,it,pt;
netsal aa=new netsal();

System.out.println("\n \tENTER EMPLOYEE NAME ");


emp=x.nextLine();

if(emp.equals("manager"))
{
basic=75000;
da=(basic*5)/100;
hra=(basic*6)/100;
ma=(basic*7)/100;
pf=(basic*5)/100;
it=(basic*4)/100;
pt=(basic*4)/100;

aa.salary(basic,da,hra,ma,pf,it,pt);
}

else if(emp.equals("executive"))
{
basic=50000;
da=(basic*5)/100;
hra=(basic*6)/100;
ma=(basic*7)/100;
pf=(basic*5)/100;
it=(basic*4)/100;
pt=(basic*4)/100;
aa.salary(basic,da,hra,pf,it);
}

else if(emp.equals("jn_executive"))
{
basic=40000;
da=(basic*5)/100;
hra=(basic*6)/100;
it=(basic*4)/100;

aa.salary(basic,da,hra,it);
}

else
{
basic=20000;
da=(basic*5)/100;

aa.salary(basic,da);
}

}
}

6. Write Check for positive or negative number

 Check for odd or even number

 Check for primary number

 Check for Palindrome number

 Check for Armstrong number

 Check for number whether a member of fibonacci series

 Exit

import java.util.Scanner;
class Q9
{
public static void main(String args[])
{
Scanner x=new Scanner(System.in);
int a,i,ch,n,cnt=0;

do{
System.out.println("\n\t1-> Possitive-Negative \n\t2-> Odd-Even \n\t3-> Primary
Number \n\t4-> Palimdrome \n\t5-> Armstrong \n\t6-> Fibonacci \n\t7-> Exit");
System.out.print("\n\tENTER CHOICE : ");
ch=x.nextInt();

switch(ch)
{
case 1:
System.out.print("\n\tENTER NUMBER : ");
a=x.nextInt();
if(a>0)
System.out.println("\n\tPOSITIVE NUMBER");
else
System.out.println("\n\tNEGATIVE NUMBER");
break;
case 2:
System.out.print("\n\tENTER NUMBER : ");
a=x.nextInt();
if(a%2==0)
System.out.println("\n\tEVEN NUMBER");
else
System.out.println("\n\tODD NUMBER");
break;
case 3:
System.out.print("\n\tENTER NUMBER : ");
a=x.nextInt();
for(i=1;i<=a;i++)
{
if(a%i==0)
cnt++;
}
if(cnt==2)
System.out.println("\n\tPRIMARY NUMBER");
else
System.out.println("\n\tNORMAL NUMBER");
break;
case 4:
System.out.print("\n\tENTER NUMBER : ");
a=x.nextInt();
int rev=0;
n=a;

while(n>0)
{
rev=(rev*10)+(n%10);
n=n/10;
}

if(a==rev)
System.out.println("\n\tPALINDROME NUMBER");
else
System.out.println("\n\tNOT PALINDROME");
break;
case 5:
System.out.print("\n\tENTER NUMBER : ");
a=x.nextInt();
int sum=0,r;
n=a;
while(n>0)
{
r=(n%10);
sum=sum+(r*r*r);
n=n/10;
}

if(a==sum)
System.out.println("\n\tARMSTRONG NUMBER");
else
System.out.println("\n\tNOT ARMSTRONG");
break;
case 6:
System.out.print("\n\tENTER NUMBER : ");
a=x.nextInt();
int p=0,y=1,z=0;
for(i=1;i<=a;i++)
{
System.out.print(p+",");
z=p+y;
p=y;
y=z;
}

break;
case 7:
System.out.println("\n\tTHANK YOU :)");
break;
}
}while(ch!=7);

}
}
Create abstract methods for every check into abstract Proto1 class and define into Defi1 class and
call from Menu1 class.

7. Create arrays of 5 items. Pass item name, qty and rate. Calculate and display net amount for each
item. Also display item having highest net amount.

import java.util.Scanner;
class p7
{
public static void main(String args[])
{
Scanner x=new Scanner(System.in);
int i,mx;
String nm[]=new String[5];
int qty[]=new int[5];
int rt[]=new int[5];
int amt[]=new int[5];

for(i=0;i<5;i++)
{
System.out.print("\n\tENTER NAME : ");
nm[i]=x.next();

System.out.print("\n\tENTER QTY : ");


qty[i]=x.nextInt();

System.out.print("\n\tENTER RATE : ");


rt[i]=x.nextInt();

amt[i]=qty[i]+rt[i];
}

mx=amt[0];

for(i=0;i<5;i++)
{
System.out.println("\n\tNET AMOUNT of "+i+" = "+amt[i]);
if(amt[i]>mx)
mx=amt[i];
}
System.out.println("\n\tHIGHEST NET AMOUNT IS "+mx);
}
}

8. A shop selling medicine and sports items. Display all information for each product. Product name,
product prize, expire date, deases name, sports name, made material. (use inheritance).

import java.util.Scanner;

class medicine
{
Scanner x=new Scanner(System.in);

String pn[]=new String[5];


int pp[]=new int[5];
String ed[]=new String[5];
String dn[]=new String[5];
int i;

void getdata(int n)
{
for(i=0;i<n;i++)
{
System.out.print("Product Name : ");
pn[i]=x.next();

System.out.print("Product Price : ");


pp[i]=x.nextInt();

System.out.print("Expire Date : ");


ed[i]=x.next();

System.out.print("Deases Name : ");


dn[i]=x.next();
}
}
void display(int n)
{
System.out.println("Product Name Product Price Expire Date Deases Name");

for(i=0;i<n;i++)
{
System.out.println(pn[i]+" "+pp[i]+" "+ed[i]+" "+dn[i]);
}

}
}

class sports extends medicine


{
Scanner x=new Scanner(System.in);
String sn[]=new String[5];
String sm[]=new String[5];
int i;

void getdata1(int n)
{
for(i=0;i<n;i++)
{
System.out.print("Sports Name : ");
sn[i]=x.next();

System.out.print("Material : ");
sm[i]=x.next();
}
}

void display1(int n)
{
System.out.println("Sports Name Material");
for(i=0;i<n;i++)
{
System.out.println(sn[i]+" "+sm[i]);
}

}
}

class p8
{
public static void main(String args[])
{
Scanner x=new Scanner(System.in);
int n;
sports s1= new sports();
System.out.print("Enter Number of records:");
n=x.nextInt();

s1.getdata(n);
s1.getdata1(n);
System.out.println("\n\n");
s1.display(n);
s1.display1(n);
}
}
9. In a library there are educational books and magazines are available. In educational books,
subjects are like physics and computer. The physics book contains number of practical. The
computer book contains CD. The magazine categories are like commercial and sports. Display the
info like, book name, book code, book price, subject code (P or C), Subject name, Number of
practical, contains CD or not, Magazine name, commercial type, sport’s name. Calculate total
number of educational books subject wise, magazines category wise. (Make the respective cl ass
final use hierarchical inheritance).
import java.util.Scanner;
import java.lang.*;
import java.io.*;

class library
{
String bname,sname,cd,mname,cotype,scode,ctype;
int bprice,npra,bcode,ncd;
}

class educationbooks extends library


{
void physics()
{
sname="Science";
bname="Microbiology";
bprice=500;
bcode=4808;
scode="p";
npra=450;

System.out.println("_____________________________");
System.out.println(" PHYSICS BOOK DETAIL");
System.out.println("_____________________________");
System.out.println("Book Name : "+bname);
System.out.println("Book Code : "+bcode);
System.out.println("Book Price : "+bprice);
System.out.println("Subject Name : "+sname);
System.out.println("Subject Code : "+scode);
System.out.println("NO of Practicle : "+npra);
System.out.println("_____________________________");
}
void computer()
{
sname="Computer";
bname="C Programming";
bprice=480;
bcode=4589;
scode="c";
ncd=2;

System.out.println("_____________________________");
System.out.println(" COMPUTER BOOK DETAIL");
System.out.println("_____________________________");
System.out.println("Book Name : "+bname);
System.out.println("Book Code : "+bcode);
System.out.println("Book Price : "+bprice);
System.out.println("Subject Name : "+sname);
System.out.println("Subject Code : "+scode);
System.out.println("NO of CD : "+ncd);
System.out.println("_____________________________");
}
}
class magazine extends library
{
void commercial()
{
bname="How to get profit?";
bprice=480;
ctype="Commercial";

System.out.println("_______________________________________");
System.out.println(" COMMERCIAL TYPE DETAIL");
System.out.println("_______________________________________");
System.out.println("Magazine Name : "+bname);
System.out.println("Commercial Type: "+ctype);
System.out.println("Magazine Price : "+bprice);
System.out.println("_______________________________________");
}
void sports()
{
bname="Life OK";
bprice=30;
ctype="Sports";

System.out.println("_____________________________");
System.out.println(" SPORTS TYPE DETAIL");
System.out.println("_____________________________");
System.out.println("Magazine Name : "+bname);
System.out.println("Commercial Type: "+ctype);
System.out.println("Magazine Price : "+bprice);
System.out.println("_____________________________");
}
}

class p9
{
public static void main(String args[])
{
educationbooks ebook=new educationbooks();
ebook.physics();
ebook.computer();

magazine mg=new magazine();


mg.commercial();
mg.sports();
}
}

10. Read employee details like, name, age, department, salary with following constraints - Age
should not below 20 years and over 60 years. - Department must be ‘s’, ‘p’ or ‘a’. - Salary should be
more than 1000, if it’s not than reset to 1000.
import java.util.Scanner;

class test
{
Scanner x=new Scanner(System.in);
String nm,dept;
int sal,age;

void getdata()
{
System.out.print("Enter Name : ");
nm=x.next();

System.out.print("Enter Age : ");


age=x.nextInt();

System.out.print("Enter Salary : ");


sal=x.nextInt();

System.out.print("Enter Dept : ");


dept=x.next();

void check()
{
if(age>=20 && age<=60)
{
if(dept.equals("s") || dept.equals("p") || dept.equals("a"))
{

if(sal<=1000)
{
System.out.println("\n\tName :"+nm);
System.out.println("\tAge :"+age);
System.out.println("\tDept :"+dept);
System.out.println("\tSalary : 1000");
}
else
{
System.out.println("\n\tName :"+nm);
System.out.println("\tAge :"+age);
System.out.println("\tDept :"+dept);
System.out.println("\tSalary : "+sal);
}

}
else
{
System.out.println("\n\t\tINVALID DEPARTMENT");
}
}
else
{
System.out.println("\n\t\tINVALID AGE");
}
}
}

class p10
{
public static void main(String args[])
{
test aa=new vinit();
aa.getdata();
aa.check();
}
}
11. Create class String1 to read a string and a character. Replace all occurrences with new character.
Create class String2 to read a string and a substring. Replace all occurrences with new substring.
import java.util.Scanner;
import java.io.*;

class string1
{
Scanner x= new Scanner(System.in);
void display1()
{

String str;

System.out.print("Enter String : ");


str=x.nextLine();

String nc="d";

System.out.print("\nEnter New Alphabet : ");


String oc=x.next();

str=str.replace(oc,nc);
System.out.println("\nNEW STRING IS : "+str);
}

class string2 extends string1


{
Scanner x= new Scanner(System.in);
String str,osb,nsb;
void display2()
{

System.out.print("\nEnter String : ");


str=x.nextLine();

System.out.print("\nEnter OLD SUB STRING : ");


osb=x.nextLine();

System.out.print("\nEnter NEW SUB STRING : ");


nsb=x.nextLine();

str=str.replace(osb,nsb);
System.out.print("\nNEW STRING IS : "+str);
}

class p11
{
public static void main(String args[])
{
string2 aa=new string2();
aa.display1();
aa.display2();
}
}
12. You are given a sting str =”CVM university”. Perform the following operation on it.
a. find the length of string
b. replace the character U’ by ‘R’
c. convert all character in uppercase extract and print “CVM” from given string.
import java.util.Scanner;
import java.io.*;
class p12
{
public static void main(String args[])
{
Scanner x= new Scanner(System.in);
String str="CVM university";
String os="U";
String ns="R";

int n=str.length();
String a=str.replace(ns,os);
String b=str.toUpperCase();
String c=str.substring(0,6);

System.out.println("LENGTH IS :: "+n);
System.out.println("REPLACE :: "+a);
System.out.println("UPPER IS :: "+b);
System.out.println("EXTRACT IS :: "+c);
} }
}
13. Read a string and restructure it as follows,
a. Input: “Java is useful language”
b. Output: “Javais use fullanguageX”

import java.io.*;
import java.lang.*;
import java.lang.String;
class p13
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer("Java is usefull language");
int n;
System.out.println("STRING IS ::::"+str);

n=str.indexOf(" ");
str.deleteCharAt(n);
str.insert(10," ");
str.deleteCharAt(14);
str.insert(22,"X");

System.out.println("STRING IS ::::"+str);
}
}

14. Read a name and gender. Restructure it as follows,


( Program should display message if gender is not m or f ).
a. Input: Name: ABC DEFG HIJK - Gender: M - Name: PQR STU VWXYZ - Gender: F
b. Output: Name: Mr. A. D. HIJK a. Name: Ms. P. S. VWXYZdfbdfbf

import java.io.*;
import java.lang.*;
import java.lang.String;
class p14
{
public static void main(String args[])throws IOException
{
int n1,n2;
String nm;

BufferedReader x=new BufferedReader(new InputStreamReader(System.in));

System.out.print("ENTER NAME :: ");


nm=x.readLine();

System.out.print("ENTER GENDER :: ");


char g=(char)x.read();

n1=nm.indexOf(" ");
n2=nm.indexOf(" ",n1+1);

if(g=='M' || g=='m' || g=='F' || g=='f')


{
if(g=='M' || g=='m')
{
System.out.print("Mr.");
System.out.print(nm.substring(0,1)+".");
System.out.print(nm.substring(n1+1,n1+2)+".");
System.out.print(nm.substring(n2+1));
}
else
{
System.out.print("Mrs.");
System.out.print(nm.substring(0,1)+".");
System.out.print(nm.substring(n1+1,n1+2)+".");
System.out.print(nm.substring(n2+1));
}
}
else
{
System.out.println("INVALID GENDER");
}

}
}

15. Write a menu driven an Application program to do following.


Member variable: std_no,std_name,std_sub1,std_sub2,std_sub3,total,per
1. New Student Entry
2. Calculate student result
3. Display specified student’s formatted Mark sheet
4. Delete Student Entry
5. Modify the Student info
6. Exit
import java.lang.*;
import java.io.*;

class student
{
int std_no,std_sub1,std_sub2,std_sub3;
float tot,per;
String std_name;

void input()throws IOException


{
BufferedReader x=new BufferedReader(new InputStreamReader(System.in));

System.out.print("\nENTER STUDENT NO :: ");


std_no=Integer.parseInt(x.readLine());
System.out.print("ENTER STUDENT NAME :: ");
std_name=x.readLine();

System.out.print("ENTER STUDENT MARK 1 :: ");


std_sub1=Integer.parseInt(x.readLine());

System.out.print("ENTER STUDENT MARK 2 :: ");


std_sub2=Integer.parseInt(x.readLine());

System.out.print("ENTER STUDENT MARK 3 :: ");


std_sub3=Integer.parseInt(x.readLine());
}
void result()
{
tot=std_sub1+std_sub2+std_sub3;
per=tot/3;
}
void marksheet()
{
System.out.println("\n______________________________________________");
System.out.println("\n STUDENT MARKSHEET ");
System.out.println("______________________________________________");
System.out.println("STUDENT NO : "+std_no);
System.out.println("STUDENT NAME : "+std_name);
System.out.println("_______________________________________________");
System.out.println("\tMARK OF SUBJECT 1 : "+std_sub1);
System.out.println("\tMARK OF SUBJECT 2 : "+std_sub2);
System.out.println("\tMARK OF SUBJECT 3 : "+std_sub3);
System.out.println("_______________________________________________");
System.out.println("\tTOTAL :"+tot+"\tPER : "+per);
System.out.println("_______________________________________________");
}

}
class p15
{
public static void main(String s[])throws IOException
{
int ch,i,k=0;
student detail[]=new student[20];
for(i=0;i<20;i++)
{
detail[i]=new student();
}

BufferedReader x=new BufferedReader(new InputStreamReader(System.in));

do
{
System.out.println("\n\n\n1-> NEW STUDENT ENTRY");
System.out.println("2-> CALCULATE STUDENT RESULT ");
System.out.println("3-> DISPLAY SPECIFIC STUDENT MARKSHEET");
System.out.println("4-> DELETE STUDENT ENTRY");
System.out.println("5-> MODIFY STUDENT INFORMATION");
System.out.println("6-> EXIT");

System.out.print("\n\tENTER CHOICE : ");


ch=Integer.parseInt(x.readLine());

if(ch==1)
{
detail[k].input();
System.out.print("\n\tK="+k);

}
else if(ch==2)
{
for(i=0;i<k;i++)
{
detail[i].result();
}
}
else if(ch==3)
{
System.out.print("\n\tENTER STUDENT NO : ");
int a=Integer.parseInt(x.readLine());

for(i=0;i<k;i++)
{
System.out.print("\n\tSTUDENT NO : "+detail[i].std_no);
if(detail[i].std_no==a)
detail[i].marksheet();
}
}
else if(ch==4)
{
System.out.print("\n\tENTER STUDENT NO : ");
int a=Integer.parseInt(x.readLine());

for(i=0;i<k;i++)
{

if(detail[i].std_no==a)
{
detail[i].std_no=0;
System.out.print("\n\tDELETED ");
}
}
}
else if(ch==5)
{
System.out.print("\n\tENTER STUDENT NO : ");
int a=Integer.parseInt(x.readLine());

for(i=0;i<k;i++)
{

if(detail[i].std_no==a)
{
System.out.print("\nENTER STUDENT NO :: ");
detail[i].std_no=Integer.parseInt(x.readLine());

System.out.print("ENTER STUDENT NAME :: ");


detail[i].std_name=x.readLine();

System.out.print("ENTER STUDENT MARK 1 :: ");


detail[i].std_sub1=Integer.parseInt(x.readLine());

System.out.print("ENTER STUDENT MARK 2 :: ");


detail[i].std_sub2=Integer.parseInt(x.readLine());

System.out.print("ENTER STUDENT MARK 3 :: ");


detail[i].std_sub3=Integer.parseInt(x.readLine());
}
}
}
else if(ch==6)
{
break;
}
else
{
System.out.print("ENTER VALID CHOICE ");
}
k=k+1;
}while(ch!=6);
}
}

You might also like