Java Programming Concepts and Examples
Java Programming Concepts and Examples
CONCEPTS OF
PROGRAMMING
Name: Vishal Jagadale
Roll No.: 1232.
Vishal Jagadale
Jagdalevishal633@[Link]
DAY_1
1 : write program to test Hello World.
[Link]("Hello World");
}
2 : Write a program to addition of two numbers also addition of two
characters.
int a, b, add;
a=12;
b=15;
add=a+b;
}
3:Find the compound amount and compound interest on the principal
Rs.20,000 borrowed at 6% compounded annually for 3 years.
import [Link];
public class Compound_int {
b=p;
for(a=1;a<=ti;a++)
{
ci=(p*i*1)/100;
p=p+ci;
b=p-b;
}
4:Write a program to calculate power of a number.
import [Link];
if(pr==0)
{
[Link]("If the power of any
number is 0 answer is 1 always");
}
else
{
for(a=1;a<=pr;a++)
{
result=result*no;
}
}
5:Write a program to swap two numbers.
import [Link];
temp=no1;
no1=no2;
no2=temp;
}
6:Write a program to find factorial of a given number.
import [Link];
public class Factorial {
public static void main(String[] args) {
int num, temp, mul=1;
[Link]("Enter a Number:");
Scanner no=new Scanner([Link]);
num=[Link]();
[Link]();
temp=num;
while(num!=0) {
mul=mul*num;
num--;
}
[Link]("The Factorial of "+temp+" is
"+mul);
}
}
7:Write a program to find m to the power n
import [Link];
if(pr==0)
{
[Link]("If the power of any
number is 0 answer is 1 always");
}
else
{
for(a=1;a<=pr;a++)
{
result=result*no;
}
}
8:Check if number is a prime number or not.
import [Link];
public class PrimeNO {
public static void main(String[] args) {
if (num==1)
[Link]("1 is neither a prime nor
a composite number.");
else if(num==2)
[Link]("2 is only prime number
which is also an even number.");
else {
for(i=2;i<num;i++) {
if(num%i==0)
count++;
}
if(count==0)
[Link](num+" is a prime
number.");
else
[Link](num+" is not a prime
number.");
}
}
9:Sum of series :
1+2+3+….+n
import [Link];
public class SumOfn {
public static void main(String[] args) {
temp=num;
for(a=num;a>0;a--)
sum=sum+a;
[Link]("Sum of all numbers from 1 to
"+temp+" is "+sum);
temp=num;
while(num>0) {
rem=num%10;
sum=sum*10+rem;
num=num/10;
}
if(temp==sum)
[Link]("The number is a
Palindrome number");
else
[Link]("The number is not a
Palindrome number");
}
}
11:Write a program to find sum of all even and odd numbers between
1 to n.
import [Link];
for(a=1;a<=num;a++) {
if(a%2==0) {
sume=sume+a;
}
else {
sumo=sumo+a;
}
}
[Link]("Sum of even numbers is
"+sume);
[Link]("Sum of odd numbers is
"+sumo);
}
}
import [Link];
public class ReverseNO {
public static void main(String[] args) {
temp=num;
while(num>0) {
rem=num%10;
sum=sum*10+rem;
num=num/10;
}
[Link]("The reverse number of
"+temp+" is " +sum);
import [Link];
public class PrimeNoUptoN {
public static void main(String[] args) {
for(i=2;i<=num;i++) {
count=0;
for(j=2;j<i;j++){
if(i%j==0) {
count++;
}
}
if(count==0) {
[Link](i+" ");
}
}
}
}
import [Link];
public class ArmstrongNO {
public static void main(String [] args) {
[Link]("Enter a Number");
Scanner ip=new Scanner([Link]);
num=[Link]();
[Link]();
temp=num;
while(temp!=0) {
temp=temp/10;
count++;
}
cnt=count;
temp=num;
while(temp!=0) {
rem=temp%10;
while(cnt!=0) {
mul=mul*rem;
cnt--;
}
result=result+mul;
cnt=count;
temp=temp/10;
mul=1;
}
if(result==num)
[Link](num+" is an Armstrong
Number");
else
[Link](num+" is not an Armstrong
Number");
}
}
15:Write a program to find greatest of three numbers using nested if-
else.
import [Link];
public class GreatestNo {
public static void main(String[] args) {
int n1, n2, n3;
if(n1>n2) {
if(n1>n3)
[Link](n1+" is the greatest
of all numbers.");
else
[Link](n3+" is the greatest
of all numbers.");
}
else {
if(n2>n3)
[Link](n2+" is the greatest
of all numbers.");
else
[Link](n3+" is the greatest
of all numbers.");
}
}
DAY_2
import [Link];
public class PrintStarPattren {
public static void main(String [] args) {
int num, i, j;
[Link]("Enter a Number:");
Scanner input=new Scanner([Link]);
num=[Link]();
[Link]();
for(i=1;i<=num;i++) {
for(j=1;j<=i;j++) {
[Link]("*");
}
[Link]("");
}
}
}
2:Write a program which will accept student information like
rollno,name,5 subject [Link] total and [Link]
grade..
per >75 grade :A
per<74 and >60 :B
per<59 :C
import [Link];
public class StudentNameRollGrade {
public static void main(String[] args) {
[Link]();
total=(sub1+sub2+sub3+sub4+sub5);
perct=total/500.0*100.0;
[Link]("Name: "+name);
[Link]("Roll No.: "+rollno);
[Link]("Total Marks: "+total);
[Link]("Percentage: "+perct);
if(perct>75)
[Link]("Grade A");
else if(perct>60)
[Link]("Grade B");
else
[Link]("Grade C");
}
}
3:Write function to swap two numbers.
}
5:Write a program to accept array of 5 numbers and display it.
}
6:Write a program which read aaray of 5 elements and print reverse
array.
[Link]("");
import [Link];
userarr=new int[size];
[Link]("");
for(i=0;i<[Link];i++) {
if(userarr[i]==num) {
[Link]("Number is present
at index"+i);
break;
}
else if(i==([Link]-1))
[Link]("Number does not
exist in the array.");
else
;
}
[Link]();
}
8:Write a Java program to find the maximum and minimum value of
an array.
import [Link];
for(i=0;i<[Link];i++) {
[Link](userarr[i]+",");
}
[Link]();
max=userarr[0];
min=userarr[0];
for(i=0;i<[Link];i++) {
if(max<userarr[i]) {
max=userarr[i];
}
if(min>userarr[i]) {
min=userarr[i];
}
}
[Link]("Max: "+max);
[Link]("Min: "+min);
import [Link];
for(i=0;i<[Link];i++) {
userarr[i]=[Link]();
}
[Link]();
[Link]("Elements of an array:");
for(i=0;i<[Link];i++) {
[Link](userarr[i]+",");
}
[Link]();
sum=0;
for(i=0;i<[Link];i++) {
sum=sum+userarr[i];
}
avg=sum/[Link];
max=userarr[0];
min=userarr[0];
for(i=0;i<[Link];i++) {
if(max<userarr[i])
max=userarr[i];
}
for(i=0;i<[Link];i++) {
if(min>userarr[i])
min=userarr[i];
}
[Link]("Sum :"+sum);
[Link]("Average:"+avg);
[Link]("Maximum :"+max);
[Link]("Minimum :"+min);
import [Link];
if(bs<=10000) {
hra=20;
da=80;
gs=bs+(hra/100.0*bs)+(da/100.0*bs);
}
else if(bs<=20000){
hra=25;
da=90;
gs=bs+(hra/100.0*bs)+(da/100.0*bs);
}
else {
hra=30;
da=95;
gs=bs+(hra/100.0*bs)+(da/100.0*bs);
}
[Link]("Gross salary is Rs."+gs);
}
}
11:Write a menu driven program for stationary [Link] are 1:Pen
2:Pencil 3:NoteBook 4:Bottle 5:ColorBox.
1 pen cost is 10Rs,Pencil is 5 [Link] is 20 rs Bottle is 30 rs and
ColorBox is at 50 Rs.
Calculate Total of all purchesed items.
import [Link];
while(flag!=true) {
[Link]("Enter your choice:");
choice=[Link]();
switch(choice) {
}
[Link]();
t=t1+t2+t3+t4+t5;
[Link]("Total amount of all purchased
items is Rs."+t);
}
}
DAY_3
1:Write a function to accept array of [Link] all elements in
uppercase.
for(i=0;i<[Link];i++) {
[Link](" "+arr[i]);
}
[Link]();
[Link]("Upper Case Array:");
for(i=0;i<[Link];i++) {
[Link](" "+arr[i].toUpperCase());
}
}
}
2:Write a Java program to accept 2D aaray [Link] all
elements.
[Link]();
}
int i, j;
sum(15,20);
sum(25,99,88);
sum(36, 58, 52, 55);
}
}
5:Write a program to merge two arrays into a single array.
int i;
int[] arr1= {3,43,5};
int[] arr2= {14,55,82,98};
for(i=0;i<[Link];i++) {
[Link](arr1[i]+" ");
}
[Link]();
[Link]("Elements of second array");
for(i=0;i<[Link];i++) {
[Link](arr2[i]+" ");
}
[Link]();
[Link]("Elements of array after
merging");
int []arr3=new int[[Link]+[Link]];
for(i=0;i<[Link];i++) {
arr3[i]=arr1[i];
}
for(i=0;i<[Link];i++) {
arr3[[Link]+i]=arr2[i];
}
for(i=0;i<[Link];i++) {
[Link](arr3[i]+" ");
}
Customer() {
[Link]=name;
[Link]=email;
[Link]=age;
name="Riya";
email="riya@[Link]";
age=25;
}
}
9:Create Date class with data members day,month,[Link] getter
setter for data [Link] display function to display date.
import [Link];
public class Date {
private int day;
private String month;
private int year;
Date(){
[Link]=day;
[Link]=month;
[Link]=year;
day=10;
month="April";
year=1998;
}
}
DAY_4
1:Create Date class with data members day,month,year.
Write a method to accept all data [Link] display function to
display date.
import [Link];
public class Date {
private int day;
private String month;
private int year;
Date(){
[Link]=day;
[Link]=month;
[Link]=year;
}
}
public class TestDate {
public static void main(String[]args) {
Date D1=new Date();
[Link]("Default Date: ");
[Link]();
[Link]("Accepting date from user:");
[Link]();
[Link]("Date by user: ");
[Link]();
}
import [Link];
Customer() {
[Link]=name;
[Link]=email;
[Link]=age;
[Link]=creditlimit;
}
[Link]("Custemer Details:");
[Link]();
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("Customer's existing credit
limit: "+[Link]());
[Link](12000);
}
3:Consider that payroll software needs to be developed for
computerization of
operations of an ABC organization. The organization has employees.
3.1. Construct a class Employee with following members using private
access
specifies:
Employee Id integer
Employee Name string
Basic Salary double
HRA double
Medical double
PF double
PT double
Net Salary double
Gross Salary double
Please use following expressions for calculations:
* HRA = 50% of Basic Salary
* PF = 12% of Basic Salary
* PT = Rs. 200
import [Link];
Employee(){
[Link]=id;
[Link]=name;
this.bs_sal=bs_sal;
}
public void acceptinfo(){
Scanner ip=new Scanner([Link]);
[Link]("Enter name:");
name=[Link]();
[Link]("Enter id:");
id=[Link]();
[Link]("Enter Basic salary:");
bs_sal=[Link]();
[Link]();
}
[Link]("Emp_id: "+id);
[Link]("Emp_name: "+name);
[Link]("Emp_Gross salary: "+gs_sal);
[Link]("Emp_Net salary: "+net_sal);
}
}
DAY_5
1.1:Create 2 classes Student and Batch. Student class is in pack1 and
Batch
class is in pack2. Write accept() and display() methid in both the class
to accept and to display info.
Write a Test class to print Student and Batch
information.
package package1;
import [Link];
public class Student {
public int roll_no;
public String name;
private String grade;
int totalmarks;
public Student(){
[Link]=name;
this.roll_no=roll_no;
[Link]=totalmarks;
}
if(totalmarks>=75)
[Link]="A";
else if(totalmarks>=60)
[Link]="B";
else
[Link]="C";
return grade;
package package1;
}
package package2;
import [Link];
public class Batch {
public int batch_no;
public int year;
private String name;
private int size;
public Batch(){
[Link]=name;
this.batch_no=batch_no;
[Link]=year;
[Link]=size;
}
}
package package2;
[Link]();
[Link]();
}
}
1.2:Use the Student and Batch classes created earlier. It should
contain
public rollNo,Public Name, private Grade and default totalMarks
attributes and using Batch
class, check accessibility of there attributes in same package .
CASE: 1
package package1;
import [Link];
}
CASE: 2
package package1;
import [Link];
public class Student {
public int roll_no;
public String name;
private String grade;
int totalmarks;
public Student(){
[Link]=name;
this.roll_no=roll_no;
[Link]=totalmarks;
}
void accept() {
Scanner ip=new Scanner([Link]);
[Link]("Enter name: ");
name=[Link]();
[Link]("Enter roll no.: ");
roll_no=[Link]();
[Link]("Enter totalmarks out of 100:
");
totalmarks=[Link]();
[Link]();
}
if(totalmarks>=75)
[Link]="A";
else if(totalmarks>=60)
[Link]="B";
else
[Link]="C";
return grade;
void disply() {
[Link]("Name:"+name);
[Link]("Roll no.:"+roll_no);
[Link]("Total marks out of
100:"+totalmarks);
[Link]("Grade:
"+gradecal([Link]));
}
package package2;
import [Link];
}
1.3:
Create new package pack2.
create class testStudent in pack3;
create object of Student class from pack1 and access [Link] to
check accessibility.
package package1;
import [Link];
public class Student {
public int roll_no;
public String name;
private String grade;
int totalmarks;
public Student(){
[Link]=name;
this.roll_no=roll_no;
[Link]=totalmarks;
}
if(totalmarks>=75)
[Link]="A";
else if(totalmarks>=60)
[Link]="B";
else
[Link]="C";
return grade;
package package3;
import [Link];
[Link]();
[Link]();
}
CASE : 2 When default access modifier is there
package package1;
import [Link];
public class Student {
public int roll_no;
public String name;
private String grade;
int totalmarks;
public Student(){
[Link]=name;
this.roll_no=roll_no;
[Link]=totalmarks;
}
void accept() {
Scanner ip=new Scanner([Link]);
[Link]("Enter name: ");
name=[Link]();
[Link]("Enter roll no.: ");
roll_no=[Link]();
[Link]("Enter totalmarks out of 100:
");
totalmarks=[Link]();
[Link]();
}
if(totalmarks>=75)
[Link]="A";
else if(totalmarks>=60)
[Link]="B";
else
[Link]="C";
return grade;
}
package package3;
import [Link];
}
2.1: Create Employee class with empid,name,address,[Link]
Getter Setters
package package1;
import [Link];
Employee(){
[Link]=name;
[Link]=id;
[Link]=addr;
[Link]=sal;
}
public void acceptInfo() {
Scanner ip= new Scanner([Link]);
[Link]("Enter employee details: ");
[Link]("Enter Name: ");
[Link]=[Link]();
[Link]("Enter id: ");
[Link]=[Link]();
[Link]("Enter address: ");
[Link]=[Link]();
[Link]("Enter salary: ");
[Link]=[Link]();
}
package package1;
[Link]();
[Link]("Employee details are: ");
[Link]();
[Link]();
[Link]("Your Employee id
is:"+[Link]());
[Link](56000);
[Link]();
[Link]("Updated employee details are:
");
[Link]();
}
}
2.2 :create array of 5 employees...show all employees using for loop as
well as for each loop...in same assignment
}
public void acceptInfo() {
Scanner ip= new Scanner([Link]);
[Link]("Enter employee details: ");
[Link]("Enter Name: ");
[Link]=[Link]();
[Link]("Enter id: ");
[Link]=[Link]();
[Link]("Enter address: ");
[Link]=[Link]();
[Link]("Enter salary: ");
[Link]=[Link]();
[Link]();
}
package package1;
allemp[0]=emp1;
allemp[1]=emp2;
allemp[2]=emp3;
allemp[3]=emp4;
allemp[4]=emp5;
}
package package1;
allemp[0]=emp1;
allemp[1]=emp2;
allemp[2]=emp3;
allemp[3]=emp4;
allemp[4]=emp5;
}
2.3:create array of 5 employees ...show those employee who are
getting salary >20000.
package package1;
import [Link];
Employee(){
[Link]=name;
[Link]=id;
[Link]=addr;
[Link]=sal;
}
[Link]("____________________________");
}
}
package package1;
allemp[0]=emp1;
allemp[1]=emp2;
allemp[2]=emp3;
allemp[3]=emp4;
allemp[4]=emp5;
Problem STatement3
3->Create Date Class with Data Members day,month, year
3.1:
Create an object and initialize it using mutator methods and accesses
it using
accessor methods. Print the date.
package package1;
Date(){
[Link]=date;
[Link]=month;
[Link]=year;
}
int yearAccessor(){
return [Link];
}
package package1;
[Link]();
}
}
3.2
Create two objects and initialize them using no-argument and
parameterized
constructor respectively. Print the date.
package package1;
Date(){
[Link]=date;
[Link]=month;
[Link]=year;
}
int yearAccessor(){
return [Link];
}
package package1;
[Link]("__________________________________");
[Link]("Parameterized date
constructor");
[Link]();
}
}
Problem Statement 4:
->Create a java application for the following.
Create a Customer class , with data members (all private : tight
encapsulation)
name(String),email(String),age(int), creditLimit(double)
package package1;
[Link]("Custemer Details:");
[Link]();
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}
package package1;
[Link]();
}
4.2 Supply an argument less constructor to init default name to "Riya"
, email to "riya@[Link]",age=25,creditLimit=10000
(Must use constructor chaining)
package package1;
Customer() {
[Link]="Riya";
[Link]="riya@[Link]";
[Link]=25;
[Link]=10000;
}
Customer(String name, String email, int age, double
creditlimit) {
[Link]=name;
[Link]=email;
[Link]=age;
[Link]=creditlimit;
}
[Link]("Custemer Details:");
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}
package package1;
[Link]("Parameterized
constructor:");
[Link]("------------------------------");
[Link]();
[Link]("------------------------------");
[Link]("Default constructor:");
[Link]("------------------------------");
[Link]();
[Link]("Custemer Details:");
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}
}
package package1;
[Link]();
[Link]("------------------------------");
[Link]();
[Link]("Your Creditlimit is
Rs."+[Link]());
[Link]("------------------------------");
[Link](180000);
[Link]("Your Updated Creditlimit is
Rs."+[Link]());
}
4.5 Create a TestCustomer class . Use scanner to accept user i/ps.
Create 2 customers using 2 different constructors(4.1 : customer1 ,4.2
: customer2)
Display customer details of both customers.
Prompt user , for changing creditLimit of the customer2.
Display new credit limit on the console.
package package1;
import [Link];
Customer() {
[Link]="Riya";
[Link]="riya@[Link]";
[Link]=25;
[Link]=10000;
}
[Link]("Custemer Details:");
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}
package package1;
[Link]();
[Link]("------------------------------");
[Link]();
[Link]("------------------------------");
[Link]();
[Link]("------------------------------");
[Link]();
[Link]("Your Creditlimit is
Rs."+[Link]());
[Link]("------------------------------");
[Link](18000);
[Link]("Your Updated Creditlimit is
Rs."+[Link]());
}
}