JAVA Programs
JAVA Programs
class p1
import java.util.Scanner;
class p2
{
public static void main(String arg[])
{
Scanner x=new Scanner(System.in);
int a,b;
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="";
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);
}
}
import java.util.Scanner;
class chk
{
void area(double r)
{
System.out.println("AREA OF CIRCLE IS :"+(3.14*r*r));
}
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));
}
}
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
//jn_executive
//peon
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();
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);
}
}
}
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();
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);
void getdata(int n)
{
for(i=0;i<n;i++)
{
System.out.print("Product Name : ");
pn[i]=x.next();
for(i=0;i<n;i++)
{
System.out.println(pn[i]+" "+pp[i]+" "+ed[i]+" "+dn[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;
}
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();
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();
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;
String nc="d";
str=str.replace(oc,nc);
System.out.println("\nNEW STRING IS : "+str);
}
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);
}
}
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;
n1=nm.indexOf(" ");
n2=nm.indexOf(" ",n1+1);
}
}
class student
{
int std_no,std_sub1,std_sub2,std_sub3;
float tot,per;
String std_name;
}
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();
}
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");
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());