0% found this document useful (0 votes)
243 views108 pages

Java Programming Concepts and Examples

1. The document is an assignment on concepts of programming submitted by Vishal Jagadale. 2. It contains 15 programs solving problems related to basic programming concepts like Hello World, data types, operators, conditional statements, loops etc. 3. For each problem, the full code for the Java program solving it is listed, with comments explaining what the program is doing.

Uploaded by

VISHAL DILIP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views108 pages

Java Programming Concepts and Examples

1. The document is an assignment on concepts of programming submitted by Vishal Jagadale. 2. It contains 15 programs solving problems related to basic programming concepts like Hello World, data types, operators, conditional statements, loops etc. 3. For each problem, the full code for the Java program solving it is listed, with comments explaining what the program is doing.

Uploaded by

VISHAL DILIP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ASSIGNMENT ON

CONCEPTS OF
PROGRAMMING
Name: Vishal Jagadale
Roll No.: 1232.

Vishal Jagadale
Jagdalevishal633@[Link]
DAY_1
1 : write program to test Hello World.

public class Helloworld {

public static void main(String[] args)


{

[Link]("Hello World");

}
2 : Write a program to addition of two numbers also addition of two
characters.

public class Additon {

public static void main(String[] args) {

int a, b, add;
a=12;
b=15;
add=a+b;

[Link]("Addtion of two numbers is


"+add);

char c1= 'V';


char c2= 'J';
String s1=[Link](c1);
String s2=[Link](c2);

[Link]("Addtion of two characters is


"+s1+s2);
}

}
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 {

public static void main(String[] args)


{
double ci=0, i, p, ti, a, b=0;

Scanner sc = new Scanner([Link]);


[Link]("Enter Principal
Amount:");
p=[Link]();
[Link]("Enter interest rate:");
i=[Link]();
[Link]("Enter time in years");
ti=[Link]();
[Link]();

b=p;
for(a=1;a<=ti;a++)
{
ci=(p*i*1)/100;
p=p+ci;

b=p-b;

[Link]("Compunded amount is:"+p);


[Link]("Compund interest is:"+b);

}
4:Write a program to calculate power of a number.

import [Link];

public class Power {

public static void main(String[] args){

int no, pr, a, result=1;

try (Scanner sc = new Scanner([Link])) {


[Link]("Enter a Number:");
no=[Link]();
[Link]("Enter power:");
pr=[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;
}

[Link](no+" to the power "+pr+"


is "+result);
}
}

}
5:Write a program to swap two numbers.

import [Link];

public class SwapNos {


public static void main(String[] args) {

int no1, no2, temp;

Scanner no=new Scanner([Link]);


[Link]("Enter First Number:");
no1=[Link]();
[Link]("Enter Second Number:");
no2=[Link]();
[Link]();

[Link]("Two numbers you entered are


"+no1+" and "+no2);

temp=no1;
no1=no2;
no2=temp;

[Link]("After swapping numbers are


"+no1+" and "+no2);
}

}
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];

public class Power {

public static void main(String[] args){

int no, pr, a, result=1;

try (Scanner sc = new Scanner([Link])) {


[Link]("Enter a Number:");
no=[Link]();
[Link]("Enter power:");
pr=[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;
}

[Link](no+" to the power "+pr+"


is "+result);
}
}

}
8:Check if number is a prime number or not.

import [Link];
public class PrimeNO {
public static void main(String[] args) {

int num, i, count=0;


[Link]("Enter a Number:");
Scanner no=new Scanner([Link]);
num=[Link]();
[Link]();

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) {

int num,temp, a, sum=0;


[Link]("Enter a number:");
Scanner no=new Scanner([Link]);
num=[Link]();
[Link]();

temp=num;

for(a=num;a>0;a--)
sum=sum+a;
[Link]("Sum of all numbers from 1 to
"+temp+" is "+sum);

10:Check whether the number is palindrome or not?


import [Link];
public class PalindromeNo {
public static void main(String[] args) {

int num, rem, sum=0, temp;


[Link]("Enter your number");
Scanner sc=new Scanner([Link]);
num=[Link]();
[Link]();

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];

public class SumofOddandEven {


public static void main(String[] args) {

int num, sume=0, sumo=0, a;


[Link]("Enter a number:");
Scanner sc=new Scanner([Link]);
num=[Link]();
[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);
}
}

12: Write a program to enter a number and print its reverse.

import [Link];
public class ReverseNO {
public static void main(String[] args) {

int num, temp, sum=0, rem;


[Link]("Enter a Number");
Scanner no=new Scanner([Link]);
num=[Link]();
[Link]();

temp=num;

while(num>0) {
rem=num%10;
sum=sum*10+rem;
num=num/10;
}
[Link]("The reverse number of
"+temp+" is " +sum);

13:Write a program to print all Prime numbers between 1 to n.

import [Link];
public class PrimeNoUptoN {
public static void main(String[] args) {

int num, i, j, count;


[Link]("Enter a Number:");
Scanner ip=new Scanner([Link]);
num=[Link]();

for(i=2;i<=num;i++) {
count=0;
for(j=2;j<i;j++){
if(i%j==0) {
count++;
}
}
if(count==0) {
[Link](i+" ");
}
}
}
}

14:Write a program to check entered number is Armstrong number or


not.

import [Link];
public class ArmstrongNO {
public static void main(String [] args) {

int num, count=0, result=0, mul=1, cnt, rem,


temp;

[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;

[Link]("Enter Three Numbers: ");


Scanner ip=new Scanner([Link]);
n1=[Link]();
n2=[Link]();
n3=[Link]();
[Link]();

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

1:Java program to print the following pattern on the console


*
**
***
****
*****

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) {

int rollno, sub1, sub2, sub3, sub4, sub5, total;


double perct;
String name;

[Link]("Enter Your name:");


Scanner input=new Scanner([Link]);
name=[Link]();

[Link]("Enter your roll no.:");


rollno=[Link]();
[Link]("Enter your 5 subject's
marks.");
[Link]("Subject 1:");
sub1=[Link]();
[Link]("Subject 2:");
sub2=[Link]();
[Link]("Subject 3:");
sub3=[Link]();
[Link]("Subject 4:");
sub4=[Link]();
[Link]("Subject 5:");
sub5=[Link]();

[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.

public class swapTwoNos {

public static void swap(int a, int b) {


int temp;
temp = a;
a = b;
b = temp;
[Link]("The two numbers after
swapping are:"+a+" and "+b);

public static void main(String[] args) {


int a = 10;
int b = 20;
[Link]("The values of two numbers
are:"+a+" and "+b);
swap(a, b);
}

4:Write functions for making addition of different types(use Function


Overloading);

public class functionOverloading {

public static void addition(int a, int b) {


int result=a+b;
[Link]("Addition of "+a+" and
"+b+" is "+result);
}
public static double addition(double a, double b)
{
double result=a+b;
[Link]("Addition of "+a+" and
"+b+" is "+result);
return result; }

public static void addition() {


int a = 15;
int b =25;
int result=a+b;
[Link]("Addition of "+a+" and
"+b+" is "+result);
}
public static void addition(int a) {
int b =50;
int result=a+b;
[Link]("Addition of "+a+" and
"+b+" is "+result);
}
public static void addition(String a, int b) {
[Link]("Addition of string and
number is "+a+" "+b);
}

public static void main(String[] args) {


addition();
addition(55);
addition(10, 20);
addition(15.50, 29.5);
addition(55, 32.6);
addition("Vishal", 633);
}

}
5:Write a program to accept array of 5 numbers and display it.

public class arrryOfFiveE {


public static void main(String[] arg) {
int i;
int [] numarr=new int[5];
numarr [0]=15;
numarr [1]=17;
numarr [2]=24;
numarr [3]=31;
numarr [4]=22;
[Link](" Elements of array are: ");
for (i=0;i<[Link];i++)
[Link](" "+numarr[i]);

}
6:Write a program which read aaray of 5 elements and print reverse
array.

public class arrayReverse {


public static void main(String[] arg) {
int i;
int [] numarr= {22, 16, 18, 21, 25, 62};

[Link]("Elements of array are:");


for (i=0;i<[Link];i++)
[Link](" "+numarr[i]);

[Link]("");

[Link]("Reverse array elements


are:");
for (i=[Link]-1;i>=0;i--)
[Link](" "+numarr[i]);
}
}
7:Write a Java program , accept array ,accept number from user and
find the index of number in array if present else show message not
exist.

import [Link];

public class arrayFromUser {


public static void main(String[]args) {
int size, i, num;
int [] userarr;

[Link]("Enter the size of an


array:");

Scanner input =new Scanner([Link]);


size=[Link]();

userarr=new int[size];

[Link]("Elements of array are: ");


for (i=0;i<[Link];i++) {
userarr[i]=[Link]();
}

[Link]("The elements you entered


are :");
for(i=0;i<[Link];i++)
[Link](+userarr[i]+",");
//for(int i1 : userarr)
//[Link](i1+",");

[Link]("");

[Link]("Enter a Number to search:");


num=[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];

public class arrrayMaxMin {


public static void main(String[] args) {
int size, i, max, min;
[Link]("Enter the size of an
array:");

Scanner input=new Scanner([Link]);


size=[Link]();

[Link]("Enter the elements of an


array:");
int [] userarr=new int[size];
for(i=0;i<[Link];i++) {
userarr[i]=[Link]();
}
[Link]("Elements of the array:");
[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);

9: Write a program to create an array of integers and perform


following operations on that array like
finding the sum, average, maximum and minimum number in that
array. Accept the numbers of the
array from user.

import [Link];

public class arrayOperations {


private static Scanner input;

public static void main(String[] args) {


int size, i, sum, avg, max, min;
[Link]("Enter the size of an
array:");

input = new Scanner([Link]);


size=[Link]();

[Link]("Enter the elements of an


array:");
int [] userarr=new int[size];

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);

10: Write a program to input basic salary of an employee and


calculate its Gross salary according to
following: Basic Salary <= 10000 : HRA = 20%, DA = 80% Basic Salary
<= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%

import [Link];

public class grossSalary {


public static void main(String[] args) {
int bs;
double gs, hra, da;

[Link]("Enter your basic salary Rs.");


Scanner input= new Scanner([Link]);
bs=[Link]();
[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];

public class menuChoice {


public static void main(String[] arg) {
int q, c, t1, t2, t3, t4, t5, t, choice;
boolean flag=false;
Scanner input=new Scanner([Link]);
[Link]("1: Pen");
[Link]("2: Pencil");
[Link]("3: Notebook");
[Link]("4: Bottle");
[Link]("5: Colorbox");
[Link]("6: Exit");
t=t1=t2=t3=t4=t5=0;

while(flag!=true) {
[Link]("Enter your choice:");
choice=[Link]();

switch(choice) {

case 1: [Link]("Enter quantity:");


q=[Link]();
c=10;
t1=q*c;
break;

case 2: [Link]("Enter quantity:");


q=[Link]();
c=5;
t2=q*c;
break;

case 3: [Link]("Enter quantity:");


q=[Link]();
c=20;
t3=q*c;
break;

case 4: [Link]("Enter quantity:");


q=[Link]();
c=30;
t4=q*c;
break;

case 5: [Link]("Enter quantity:");


q=[Link]();
c=50;
t5=q*c;
break;
case 6: flag=true;
break;

}
[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.

public class stringArrUpper {


public static void stringArr(){
int i;
String [] arr= {"Vishal",
"Yogesh","Saurabh","Tejas", "Aditya"};

for(i=0;i<[Link];i++) {
[Link](" "+arr[i]);
}

[Link]();
[Link]("Upper Case Array:");

for(i=0;i<[Link];i++) {
[Link](" "+arr[i].toUpperCase());
}
}

public static void main(String[]args) {


stringArr();
}

}
2:Write a Java program to accept 2D aaray [Link] all
elements.

public class TwoDarr {


public static void main(String[] args) {
int i, j;
int [][] arrtd=new int [2][2];
arrtd [0][0]=15;
arrtd [0][1]=21;
arrtd [1][0]=16;
arrtd [1][1]=48;

[Link]("2D Array of Elements is:");


for(i=0;i<2;i++) {
for(j=0;j<2;j++) {
[Link](arrtd[i][j]+" ");
}

[Link]();
}

3:Write a java program to make the addition of two 2D array And


store result in Third array.
public class AddOf2Darr {
public static void main(String[]args) {

int i, j;

int [][] arr1= {{10,15,20},{25,30,35}};


int [][] arr2= {{23,28,38},{36,34,44}};
int [][] arr3=new int[2][3];

[Link]("Third array after adding two


arrays is");
for(i=0;i<2;i++) {
for(j=0;j<3;j++) {
arr3[i][j]=arr1[i][j]+arr2[i][j];
[Link](arr3[i][j]+" ");
}
[Link]();
}

[Link] a function /method which takes variable no of int numbers as


an argument and returns the sum of these arguments as an output.
public class ReturnSum {
public static int sum(int a, int b) {
int sum=a+b;
[Link]("Sum of given integers is
"+sum);
return sum;
}

public static int sum(int a, int b, int c) {


int sum=a+b+c;
[Link]("Sum of given integers is
"+sum);
return sum;
}

public static int sum(int a, int b, int c, int d) {


int sum=a+b+c+d;
[Link]("Sum of given integers is
"+sum);
return sum;
}

public static void main(String[]args) {

sum(15,20);
sum(25,99,88);
sum(36, 58, 52, 55);
}

}
5:Write a program to merge two arrays into a single array.

public class ArrayMerge {


public static void main(String[] args) {

int i;
int[] arr1= {3,43,5};
int[] arr2= {14,55,82,98};

[Link]("Elements of first array");

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]+" ");
}

6:Write a java program to sort array.


import [Link];

public class ArraySort {


public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner([Link]);
[Link]("Enter the size of an array:");
n = [Link]();
int a[] = new int[n];
[Link]("Enter the elements:");
for (int i = 0; i < n; i++)
{
a[i] = [Link]();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
[Link]("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
[Link](a[i] + ",");
}
[Link](a[n - 1]);
}
}
7:Write a java program to convert char array into String.

public class CharArrTostring {


public static void main(String []args) {
int i;
char[] arr = { 'V', 'i', 's', 'h', 'a', 'l'
};
[Link]("Elements of character
array are:");
for(i=0;i<[Link];i++) {
[Link](arr[i]+" ");
}
[Link]();
[Link]("After coversion:");
String str = [Link](arr);
[Link](str);
}
}
8: Create a java application for the following.
Create a Customer class , with data members (all private : tight
encapsulation)
name(String),email(String),age(int).Supply a parameterized
constructor to accept all details from [Link] an argument less
constructor to init default name to "Riya" , email to
"riya@[Link]",age=[Link] a method displayCustomer to display
customer details.

public class Customer {


private String name;
private String email;
private int age;

Customer(String name, String email, int age) {


[Link]=name;
[Link]=email;
[Link]=age;
}

Customer() {
[Link]=name;
[Link]=email;
[Link]=age;

name="Riya";
email="riya@[Link]";
age=25;
}

public void displayCustomer() {


[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);

public class TestCustomer {


public static void main(String[]args) {
Customer Cust1=new Customer();
[Link]();
Customer Cust2=new Customer("Vishal",
"jagdalevishal633@[Link]", 23);
[Link]();

}
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;
}

public void getDate() {


[Link]("Day:"+day);
[Link]("Month:"+month);
[Link]("Year:"+year);
}
public void setDate() {
Scanner input= new Scanner([Link]);
[Link]("Enter Day:");
day=[Link]();
[Link]("Enter Month:");
month=[Link]();
[Link]("Enter Year:");
year=[Link]();
[Link]();
}

public void displayDate() {


[Link](+day+" "+month+","+year);
}

public class TestDate {


public static void main(String[]args) {
Date D1=new Date();
[Link]("Default Date: ");
[Link]();
[Link]("Getter:");
[Link]();
[Link]("Setter:");
[Link]();
[Link]("Updated Date: ");
[Link]();
}

}
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 void acceptDate() {


Scanner input= new Scanner([Link]);
[Link]("Enter Day:");
day=[Link]();
[Link]("Enter Month:");
month=[Link]();
[Link]("Enter Year:");
year=[Link]();
[Link]();
}

public void displayDate() {


[Link](+day+" "+month+","+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]();
}

2:>Create a java applicstion for the following.


Create a Customer class , with data members (all private : tight
encapsulation)
name(String),email(String),age(int), creditLimit(double)

2.1 Write acceptInfo() method to accept customer details:

2.2 Write a method , showDetails to display customer name & credit


limit only.
Naming convention : public void setCreditLimit(double limit) {...}
public double getCreditLimit(){...}

2.3 Create a TestCustomer class . Use scanner to accept user i/ps.


Create 2 customers object.
Display customer details of both customers.
Prompt user , for changing creditLimit of the customer2.
Display new credit limit on the console.

import [Link];

public class Customer {


private String name;
private String email;
private int age;
private double creditlimit;

Scanner ip=new Scanner([Link]);

Customer() {
[Link]=name;
[Link]=email;
[Link]=age;
[Link]=creditlimit;
}

public void acceptInfo() {


[Link]("Enter Cusomer Details:");
[Link]("Enter name: ");
name=[Link]();
[Link]("Enter email: ");
email=[Link]();
[Link]("Enter age: ");
age=[Link]();
[Link]("Enter creditlimit: ");
creditlimit=[Link]();
}

public void showDetails() {

[Link]("Custemer Details:");
[Link]();
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}

public void setCreditlimit(double creditlimit) {


[Link]=creditlimit;
}

public double getCreditlimit() {


return creditlimit;
}

public class TestCustomer {


public static void main(String[]args) {
Customer cust1=new Customer();
Customer cust2=new Customer();

[Link]();
[Link]();

[Link]();
[Link]();

[Link]();
[Link]("Customer's existing credit
limit: "+[Link]());
[Link](12000);

[Link]("Customer's updated credit


limit: "+[Link]());
[Link]();
[Link]();
}

}
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

3.2. Write methods to display the details of an employee and calculate


the gross
and net salary.
* Goss Salary = Basic Salary + HRA + Medical
* Net Salary = Gross Salary – (PT + PF)

Create a TestEmployee [Link] Object of employee class and


assign values and display Details.

import [Link];

public class Employee {


private int id;
private String name;
private double bs_sal;
private double hra;
private double medl=500;
private double pf;
private double pt=200;
private double net_sal;
private double gs_sal;

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]();
}

private double gsSal(double bs_sal) {


this.bs_sal=bs_sal;
[Link]=50.0*bs_sal/100.0;
this.gs_sal=this.bs_sal+[Link]+[Link];
return this.gs_sal;
}

private double netSal(double bs_sal) {


this.bs_sal=bs_sal;
[Link]=12.0*bs_sal/100.0;
this.net_sal=this.gs_sal-([Link]+[Link]);
return this.net_sal;
}

public void displyDetails() {


gsSal(bs_sal);
netSal(bs_sal);

[Link]("Emp_id: "+id);
[Link]("Emp_name: "+name);
[Link]("Emp_Gross salary: "+gs_sal);
[Link]("Emp_Net salary: "+net_sal);
}

public class TestEmployee {


public static void main(String[]args) {
Employee emp1=new Employee();
[Link]();
[Link]();
}

}
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;
}

public 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]();
}

private String gradecal(int totalmarks) {


[Link]=totalmarks;

if(totalmarks>=75)
[Link]="A";
else if(totalmarks>=60)
[Link]="B";
else
[Link]="C";
return grade;

public void disply() {


[Link]("Name:"+name);
[Link]("Roll no.:"+roll_no);
[Link]("Total marks out of
100:"+totalmarks);
[Link]("Grade:
"+gradecal([Link]));

package package1;

public class TestStudent {


public static void main(String[]args) {
Student s1=new Student();
[Link]("Enter Details: ");
[Link]();
[Link]("Students Details: ");
[Link]();

}
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;
}

public void accept() {


Scanner ip=new Scanner([Link]);
[Link]("Enter Batch name: ");
name=[Link]();
[Link]("Enter Batch no.: ");
batch_no=[Link]();
[Link]("Enter Batch size.: ");
size=[Link]();
[Link]("Enter Batch year: ");
year=[Link]();
[Link]();
}

public void display() {


[Link]("Batch Name:"+name);
[Link]("Batch no.:"+batch_no);
[Link]("Batch size:"+size);
[Link]("Year : "+year);

}
package package2;

public class TestBatch {

public static void main(String[]args) {

Batch b1=new Batch();


[Link]("Enter details of batch :");

[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];

public class TestStudent {


public static void main(String[]args) {
Batch b1=new Batch(); //b1 object from diff.
package
[Link]("Enter Details: ");
[Link]();
[Link]("Students Details: ");
[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]();
}

private String gradecal(int totalmarks) {


[Link]=totalmarks;

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];

public class TestBatch {

public static void main(String[]args) {

Student s1=new Student();//object from diff.


package
[Link]("Enter details of Student :");

[Link]();//not accessible as default access


[Link]();//modifier is there for these
methods
}

}
1.3:
Create new package pack2.
create class testStudent in pack3;
create object of Student class from pack1 and access [Link] to
check accessibility.

CASE : 1 When public 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;
}

public void accept() { // default access modifier


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]();
}

private String gradecal(int totalmarks) {


[Link]=totalmarks;

if(totalmarks>=75)
[Link]="A";
else if(totalmarks>=60)
[Link]="B";
else
[Link]="C";
return grade;

public void display() {


[Link]("Name:"+name);
[Link]("Roll no.:"+roll_no);
[Link]("Total marks out of
100:"+totalmarks);
[Link]("Grade:
"+gradecal([Link]));

package package3;

import [Link];

public class TestStudnent {


public static void main(String[]args) {
Student s1=new Student();
[Link]("Enter details of Student :");

[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]();
}

private String gradecal(int totalmarks) {


[Link]=totalmarks;

if(totalmarks>=75)
[Link]="A";
else if(totalmarks>=60)
[Link]="B";
else
[Link]="C";
return grade;

void display() { // default access modifier


[Link]("Name:"+name);
[Link]("Roll no.:"+roll_no);
[Link]("Total marks out of
100:"+totalmarks);
[Link]("Grade:
"+gradecal([Link]));

}
package package3;

import [Link];

public class TestStudnent {


public static void main(String[]args) {
Student s1=new Student();//object from diff.
package
[Link]("Enter details of Student :");

[Link]();//not accessible as default access


[Link]();//modifier is there for these
methods

}
2.1: Create Employee class with empid,name,address,[Link]
Getter Setters

package package1;

import [Link];

public class Employee {


private int id;
private String name;
private String addr;
private double sal;

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]();
}

public void displayInfo() {


[Link]("Employee id:"+id);
[Link]("Employee name:"+name);
[Link]("Address:"+addr);
[Link]("Salary:"+sal);
}

public int getid() {


return [Link];
}

public void setSal(double sal) {


[Link]=sal;
}

package package1;

public class TestEmployee {


public static void main(String[]args) {
Employee emp=new Employee();

[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 class Employee {


private int id;
private String name;
private String addr;
private double sal;
Employee(){
[Link]=name;
[Link]=id;
[Link]=addr;
[Link]=sal;
}

Employee(int id, String name, String addr, double


sal){
[Link]=id;
[Link]=name;
[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]();
[Link]();
}

public void displayInfo() {


[Link]("Employee id:"+id);
[Link]("Employee name:"+name);
[Link]("Address:"+addr);
[Link]("Salary:"+sal);
}

public int getid() {


return [Link];
}

public void setSal(double sal) {


[Link]=sal;
}

package package1;

public class TestEmpArr {


public static void main(String[]args) {

Employee[] allemp=new Employee[5];

Employee emp1= new Employee(1, "Vishal",


"Satara", 15000);
Employee emp2= new Employee(2, "Saurabh",
"Ratnagiri", 19000);
Employee emp3= new Employee(3, "Yogesh",
"Mumbai", 25000);
Employee emp4= new Employee(4, "Niti", "Pune",
25000);
Employee emp5= new Employee(5, "Mati", "Nagpur",
25000);

allemp[0]=emp1;
allemp[1]=emp2;
allemp[2]=emp3;
allemp[3]=emp4;
allemp[4]=emp5;

[Link]("With for loop");


[Link]("________________");
for(int i=0;i<[Link];i++) {
allemp[i].displayInfo();
[Link]("________________");
}

/*[Link]("With for each loop");


for(Employee emp: allemp) {
[Link]();
[Link]("________________");
}*/
}

}
package package1;

public class TestEmpArr {


public static void main(String[]args) {

Employee[] allemp=new Employee[5];

Employee emp1= new Employee(1, "Vishal",


"Satara", 15000);
Employee emp2= new Employee(2, "Saurabh",
"Ratnagiri", 19000);
Employee emp3= new Employee(3, "Yogesh",
"Mumbai", 25000);
Employee emp4= new Employee(4, "Niti", "Pune",
25000);
Employee emp5= new Employee(5, "Mati", "Nagpur",
25000);

allemp[0]=emp1;
allemp[1]=emp2;
allemp[2]=emp3;
allemp[3]=emp4;
allemp[4]=emp5;

/*[Link]("With for loop");


[Link]("________________");
for(int i=0;i<[Link];i++) {
allemp[i].displayInfo();
[Link]("________________");
}*/

[Link]("With for each loop");


[Link]("________________");
for(Employee emp: allemp) {
[Link]();
[Link]("________________");
}
}

}
2.3:create array of 5 employees ...show those employee who are
getting salary >20000.

package package1;

import [Link];

public class Employee {


private int id;
private String name;
private String addr;
private double sal;

Employee(){
[Link]=name;
[Link]=id;
[Link]=addr;
[Link]=sal;
}

Employee(int id, String name, String addr, double


sal){
[Link]=id;
[Link]=name;
[Link]=addr;
[Link]=sal;

public void displayInfo() {


[Link]("Employee id:"+id);
[Link]("Employee name:"+name);
[Link]("Address:"+addr);
[Link]("Salary:"+sal);
}

public void salSort() {


if([Link]>20000) {
displayInfo();

[Link]("____________________________");
}
}

package package1;

public class TestSal {


public static void main(String[]args) {

Employee[] allemp=new Employee[5];

Employee emp1= new Employee(1, "Vishal",


"Satara", 15000);
Employee emp2= new Employee(2, "Saurabh",
"Ratnagiri", 19000);
Employee emp3= new Employee(3, "Yogesh",
"Mumbai", 46000);
Employee emp4= new Employee(4, "Niti", "Pune",
25000);
Employee emp5= new Employee(5, "Mati", "Nagpur",
35000);

allemp[0]=emp1;
allemp[1]=emp2;
allemp[2]=emp3;
allemp[3]=emp4;
allemp[4]=emp5;

[Link]("Employee details whose salary


is >20000: ");
[Link]("____________________________");
for(int i=0;i<[Link];i++) {
allemp[i].salSort();
}
}

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;

public class Date {


private int date;
private String month;
private int year;

Date(){
[Link]=date;
[Link]=month;
[Link]=year;
}

public void mutator(int date, String month, int year)


{
[Link]=date;
[Link]=month;
[Link]=year;

public int dayAccessor(){


return [Link];
}

public String monthAccessor(){


return [Link];
}

int yearAccessor(){
return [Link];
}

public void printDate() {


[Link]("Date: "+dayAccessor()+"
"+monthAccessor()+","+yearAccessor());
}

package package1;

public class TestDate {


public static void main(String[]args) {
Date d1=new Date();
[Link](27, "December", 1999);//mutator
[Link]("Day:
"+[Link]());//access day
[Link]("Month:
"+[Link]());//access month
[Link]("Year:
"+[Link]());//access year

[Link]();
}

}
3.2
Create two objects and initialize them using no-argument and
parameterized
constructor respectively. Print the date.

package package1;

public class Date {


private int date;
private String month;
private int year;

Date(){
[Link]=date;
[Link]=month;
[Link]=year;
}

Date(int date, String month, int year){


[Link]=date;
[Link]=month;
[Link]=year;
}

public int dayAccessor(){


return [Link];
}

public String monthAccessor(){


return [Link];
}

int yearAccessor(){
return [Link];
}

public void printDate() {


[Link]("Date: "+dayAccessor()+"
"+monthAccessor()+","+yearAccessor());
}

package package1;

public class TestDate {


public static void main(String[]args) {
Date d1=new Date();
Date d2=new Date(10, "April", 1998);
[Link]("Default date constructor");
[Link]();

[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)

4.1 Supply a parameterized constructor to accept all details from user

package package1;

public class Customer {


private String name;
private String email;
private int age;
private double creditlimit;

Customer(String name, String email, int age, double


creditlimit) {
[Link]=name;
[Link]=email;
[Link]=age;
[Link]=creditlimit;
}

public void showDetails() {

[Link]("Custemer Details:");
[Link]();
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}

package package1;

public class TestCustomer {


public static void main(String[]args) {
Customer cust1=new Customer("Vishal",
"jagdalevishal633@[Link]", 25, 15000);

[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;

public class Customer {


private String name;
private String email;
private int age;
private double creditlimit;

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;
}

public void showDetails() {

[Link]("Custemer Details:");
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}

package package1;

public class TestCustomer {


public static void main(String[]args) {

Customer cust1=new Customer("Vishal",


"jagdalevishal633@[Link]", 25, 15000);

Customer cust2=new Customer();

[Link]("Parameterized
constructor:");

[Link]("------------------------------");
[Link]();

[Link]("------------------------------");
[Link]("Default constructor:");
[Link]("------------------------------");
[Link]();

4.3 Write a method , getDetails to return String form of customer


name & credit limit only.
4.4 Supply getter & setter for creditLimit.
package package1;

public class Customer {


private String name;
private String email;
private int age;
private double creditlimit;

Customer(String name, String email, int age, double


creditlimit) {
[Link]=name;
[Link]=email;
[Link]=age;
[Link]=creditlimit;
}

public void showDetails() {

[Link]("Custemer Details:");
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}

public void setCreditlimit(double creditlimit) {


[Link]=creditlimit;
}

public double getCreditlimit() {


return creditlimit;
}

}
package package1;

public class TestCustomer {


public static void main(String[]args) {
Customer cust1=new Customer("Vishal",
"jagdalevishal633@[Link]", 25, 15000);

[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];

public class Customer {


private String name;
private String email;
private int age;
private double creditlimit;

Scanner ip=new Scanner([Link]);

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;
}

public void acceptInfo() {


[Link]("Enter Cusomer Details:");
[Link]("Enter name: ");
name=[Link]();
[Link]("Enter email: ");
email=[Link]();
[Link]("Enter age: ");
age=[Link]();
[Link]("Enter creditlimit: ");
creditlimit=[Link]();
}

public void showDetails() {

[Link]("Custemer Details:");
[Link]("Name:"+name);
[Link]("Email:"+email);
[Link]("Age:"+age);
[Link]("Creditlimit:"+creditlimit);
}

public void setCreditlimit(double creditlimit) {


[Link]=creditlimit;
}

public double getCreditlimit() {


return creditlimit;
}

package package1;

public class TestCustomer {


public static void main(String[]args) {

Customer cust1=new Customer();


Customer cust2=new Customer("Vishal",
"jagdalevishal633@[Link]", 25, 15000);

[Link]();

[Link]("------------------------------");
[Link]();

[Link]("------------------------------");
[Link]();

[Link]("------------------------------");
[Link]();
[Link]("Your Creditlimit is
Rs."+[Link]());

[Link]("------------------------------");
[Link](18000);
[Link]("Your Updated Creditlimit is
Rs."+[Link]());

}
}

You might also like