SAMPLE INDEX-merged
SAMPLE INDEX-merged
SAMPLE INDEX-merged
Model Project
Topics:
1. If else ladder - 1 Program
2. Switch - 3 Programs (General, Loops and Patterns)
3. Function overloading - 1 Program
4. Constructor - 1 Program (Parameterized or Non Parameterized)
5. String - 2 programs
6. Array - 3 Programs (General, Linear OR Binary Search & Selection OR Bubble Sort)
7. Special Numbers - 1 Program
Guidelines
“Java Programs”
Submitted By :
Submitted To :
Std./Sec :
Academic Year : 2020 - 21
Date :
_________________ __________________
Internal Examiner External Examiner
1 If – else 1
2 Switch 3
3 Function overloading 8
4 Constructor 9
5 String 12
6 Arrays 16
7 Special numbers 21
8 Bibliography 22
The selection statements allow to choose the set-of-instructions for execution depending
upon an expression’s truth value. Java provides two types of selection statements and one of them
is the if statement.
i. if
An if statement tests a particular condition; if the condition evaluates to true, a course-of-
action is followed i.e, a statement or set-of-statements is executed. Otherwise (if the condition
evaluates to false), the course-of-action is ignored.
Format:
if(expression)
statement;
ii.if-else
Another form of if that allows for this kind of either-or condition by providing an else
clause is known as if-else
statement.The if-else statement tests an expression and depending upon its truth value one of the
two sets-of-action
is executed.
Format:
if(expression)
statement 1;
else
statement 2;
iii. if-else-if:
A common programming construct in Java is the if-else-if ladder, which is often also
called the if-else-if staircase
because of its appearance.
Format:
if(expression1)
statement 1;
else if(expression2)
statement 2;
else if(expression 3)
statement 3;
:
:
else
statement n;
Program 1:
The results of an examination are decided by the total marks obtained by the students. The total
marks are calculated with the marks of 5 subjects, the divisions of the students are found out
using the following rules:
If the total marks are less than 35%, the student fails.
If the total marks are greater than equal to 35% but less than 50%,the student get a 3rd division.
If the total marks are greater equal to 50% but less than 60%, the student gets a second division.
Page 1 of 22
If the total marks greater than equal to 60%, the student get a 1st division.
Write a program in Java to print the name of the student, total marks and his division by taking
the marks of 5 subjects and the name of the student as input:
import java.util.*;
public class test
{
public void display()
{
Scanner sc=new Scanner(System.in));
String name;
double m1,m2,m3,m4,m5,totalm;
System.out.println(“Enter the name of the student”);
name=sc.read();
System.out.println(“Enter the marks of the student”);
m1=sc.nextInt();
m2= sc.nextInt();
m3= sc.nextInt();
m4= sc.nextInt();
m5= sc.nextInt();
totalm=(m1=m2+m3+m4+m5)/5;
if(totalm<35)
System.out.println(“The student is fail”);
else
if(totalm>=35&&totalm<5
System.out.println(“The student is third division”);
else
if(totalm>=50&&totalm<60)
System.out.println(“The student is second division”);
else
if(totalm>=60)
System.out.println(“The student is first class”);
}
}
Sample Input/Output:
Input:
Enter the name of the student
Tina Sharma
Enter the marks of the student
89
91
78
67
90
Output:
The student is first class
Page 2 of 22
II. SWITCH
Java provides a multiple – branch selection statement known as switch. This selection
statement successively tests the value of an expression against a list of integer or character
constants, for equality. When a match is found, the statements associated with that constant are
executed.
Format:
switch (expression )
{
case constant1 : statement –sequence1 ;
break ;
case constant : statement – sequence2 ;
break ;
case constant : statement – sequence3 ;
break ;
:
case constant -1 :statement – sequence-1 ;
break ;
[default : statement – sequence n] ;
}
Program 2:
Using a switch statement, write a menu driven program to convert a given temperature from
Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message should
be displayed.
import java.util.*;
public class statement1
{
public void display()
{
Scanner sc = new Scanner(System.in);
int ch;
System.out.println("Enter 1.Fahrenheit to Celsius");
System.out.println("Enter 2.Celsius to Fahrenheit");
System.out.println("Enter your choice");
ch = sc.nextInt();
switch(ch)
{
case 1:
{
double Celsius,Fahrenheit;
System.out.println("Enter the value for Fahrenheit");
Fahrenheit = sc.nextDouble();
Celsius=5/9*(Fahrenheit-32);
System.out.println("Fahrenheit to Celsius is"+Celsius);
break;
}
case 2:
Page 3 of 22
{
double Fahrenheit,Celsius;
System.out.println("Enter the value for Celsius");
Celsius= sc.nextDouble();
Fahrenheit=(1.8*Celsius)+32;
System.out.println("Celsius to Fahrenheit is"+Fahrenheit);
break;
}
default:
System.out.println("Wrong choice");
}
}
}
Sample Input/Output:
Input:
Enter 1.Fahrenheit to Celsius
Enter 2.Celsius to Fahrenheit
Enter your choice
1
Enter the value for Fahrenheit
102
Output:
Fahrenheit to Celsius is 38.8
Program 3:
Using a switch statement, write a menu driven program:
import java.util.*;
public class loops
{
public void display()
{
Scanner sc = new Scanner(System.in);
int ch;
int no;
System.out.println("Enter the value for number");
no= sc.nextInt();
System.out.println("Enter 1.For Perfect number");
System.out.println("Enter 2.For sum of digits");
System.out.println("Enter 3.For Palindrome number");
System.out.println("Enter your choice");
ch= sc.nextInt();
switch(ch)
Page 4 of 22
{
case 1:
{
int sum=0;
for(int i=1;i<no;i++)
{
if(no%i==0)
sum=sum+i;
}
if(sum==no)
System.out.println(no+"is a perfect number");
else
System.out.println(no+"is not a perfect number");
}
break;
case 2:
{
int sum=0,d;
while(no>0)
{
d=no%10;
sum=sum+d;
no=no/d;
}
System.out.println("Sum of digits is"+sum);
}
break;
case 3:
{
int rev=0,d,n;
n=no;
while(no>0)
{
d=no%10;
rev=rev*10+d;
no=no/10;
}
if(rev==no)
System.out.println(no+"is a palindrome number");
else
System.out.println(no+"is not a palindrome number");
}
break;
default:
System.out.println("Wrong choice");
}
}
}
Page 5 of 22
Sample Input/Output:
Input:
Enter the value for number
65
Enter 1.For Perfect number
Enter 2.For sum of digits
Enter 3.For Palindrome number
Enter your choice
1
Output
65 is not a perfect number
Program 4:
Using a switch statement, write a menu driven program to perform the following pattern:
1. 1 2. 55555 3. @
22 4444 @#
333 333 @#@
4444 22 @#@#
55555 1 @#@#@
import java.util.*;
public class Pattern
{
public void display()
{
Scanner sc = new Scanner(System.in);
int ch;
System.out.println("Enter 1.For Triangle");
System.out.println("Enter 2.For Inverted Triangle");
System.out.println("Enter 3.For Special Triangle ");
System.out.println("Enter your choice");
ch= sc.nextInt();
switch(ch)
{
case 1:
{
for(int i = 1; i <=5 ; i++)
{
for(int j = 1; j <=i ; j++)
{
System.out.print(i+ “ ”);
}
System.out.println();
}
}
break;
case 2:
{
Page 6 of 22
for(int i = 5; i >=0 ; i--)
{
for(int j = 1; j <=i ; j++)
{
System.out.print(i+ “ ”);
}
System.out.println();
}
}
break;
case 5:
{
for(int i = 1; i <=0 ; i++)
{
for(int j = 1; j <=i ; j++)
{
if(j %2 = =0)
System.out.print(“#”+ “ ”);
else
System.out.print(“@”+ “ ”);
}
System.out.println();
}
}
break;
default:
System.out.println("Wrong choice");
}
}
}
Sample Input/Output:
Input:
Enter 1.For Triangle
Enter 2.For Inverted Triangle
Enter 3.For Special Triangle
Enter your choice
2
Output:
55555
4444
333
22
1
Page 7 of 22
III. FUNCTION OVERLOADING
A function name having several definitions in the same scope that are differentiable
by the number or types of their arguments, is said to be an overloaded function. Process of
creating overloaded functions is called function overloading.
Program 5:
Write a class with the name volume using function overloading that computes the volume of a
cube, a sphere and a cuboid. Formula:
volume of a cube (vc) = s*s*s
Sample Input/Output:
Input:
s = 10, l = 5, b = 7, h = 9
Output:
Volume of cube is 1000
Volume of cuboid is 315
Volume of sphere is 128.625
Page 8 of 22
IV. CONSTRUCTOR
A member function with the same name as its class is called Constructor and it is used to
initialize the objects of that class type with a legal initial value.
Types of Constructors:
A constructor that accepts no parameter is called the non- parameterized constructor. Non
– parameterized constructors are considered default constructors.
Format:
class A
{
int I;
public void getval ( ){…}
public void prnval ( ){…}
: //member functions definations
}
class B
{
public void Test( )
{
A 01 = new A( );
01.getval( );
01.prnval( );
}
}
Parameterized constructors are the ones that receive parameters and initialize objects with
received values.
Format:
class ABC
{
int i;
float j;
char k;
public ABC (int a ,float b ,char c) //parameterized constructor
{
i = a;
j = b;
k = c;
}
: //other members
}
Page 9 of 22
Program 6:
import java.util.*;
public class salary
{
String name,address,phone,subject_specialization;
double monthly_salary,income_tax;
public salary()
{
name=" ";
address=" ";
phone=" ";
subject_specialization=" ";
monthly_salary=0.0;
income_tax=0.0;
}
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name");
name = sc.next();
System.out.println("Enter address");
address = sc.next();
System.out.println("Enter phone number");
phone = sc.next();
System.out.println("Enter subject specialization");
subject_specialization = sc.next();
System.out.println("Enter monthly salary");
monthly_salary = sc.nextDouble();
}
public void display()
{
System.out.println("Name:"+name);
System.out.println("Address:"+address);
System.out.println("Phone no:"+phone);
System.out.println("Subject specialization:"+subject_specialization);
System.out.println("Monthly salary:"+monthly_salary);
System.out.println("Income tax:"+income_tax);
}
public void compute()
{
double as = monthly_salary*12;
Page 10 of 22
if(as > 175000)
income_tax = 5.0/100*(as-175000);
else
income_tax = 0.0;
}
Public static void main()
{
salary tr = new salary();
tr.accept();
tr.display();
tr.compute();
}
}
Sample Input/Output:
Input:
Enter the name
Prakash
Enter address
Sunshine Park,Balewadi
Enter phone number
9856738027
Enter monthly salary
50000
Output:
Name: Prakash
Address: Sunshine Park,Balewadi Pune-411009
Phone no: 9856738027
Monthly salary: 50000.0
Income tax: 21250.0
Page 11 of 22
V. STRING FUNCTIONS
(i) Character class whose instances or objects can hold single character data.This class offers
many methods to manipulate or inspect single – character data.
(ii) String class whose instances or objects can hold unchanging string(immutable string) i.e.,
once initialized its contents cannot be modified.
(iii) StringBuffer class whose instances or objects can hold strings that can be changed or
modified.
Methods used to obtain information about an object are known as accessor methods.The class
string provides many accessor methods that may be used to perform operations on strings.
Some most used accessor methods of String class:
String concat(String str) Concatenates the specified string to the end of this
string (current String object ) string.
boolean endswith ( String str) Tests if the this string ( current String object ) ends
with the specified suffix ( str ).
boolean equals ( Strings str )
Compares the this string ( current String object ) to
the specified object str.
boolean equalsIgnoreCase
(String str ) Compares the this string ( current String object ) to str
, ignoring case considerations.
int indexOf ( char ch )
Returns the index2 within the this string ( current
String object ) of the first occurrence of the specified
character.
int lastIndexOf ( char ch )
Returns the index within the this string of the last
occurrence of the specified character.
int length ( )
Returns the length of the this string.
String valueOf ( all types) Removes white space from both ends of the this
String.
Program 7:
Write a program that encodes a word into piglatin .To translate word into a piglatin convert the
word into uppercase and then place the first vowel of the original word as the start of the new
word along with the remaining alphabets. The alphabets present before the vowel being shifted
towards the end followed by “AY”.
Example:
import java.util.*;
public class piglatin
{
public void display()
{
Scanner sc = new Scanner(System.in);
String str;
int len,p=0;
char ch;
System.out.println("Enter a word");
str = sc.next();
len = str.length();
str = str.toUpperCase();
for(int i = 0;i < len; i++)
Page 13 of 22
{
ch = str.charAt(i);
if(ch =='A' || ch=='E' ||ch=='I' ||ch=='O' ||ch=='U')
{
p=i;
break;
}
}
for(int i = p;i < len; i++)
System.out.print(str.charAt(i));
for(int i = 0 ;i < p; i++)
System.out.print(str.charAt(i));
System.out.print("AY");
}
}
Sample Input/Output:
Input:
Enter a word
London
Output:
ONDONLAY
Program 8:
Program to check whether the string is palindrome or not. A Palindrome is a string that reads the
same from left to right and vice- versa.
Example:
MADAM, ARORA, 151, 1221 etc…
importjava.util.*;
class Palindrome
{
public static void main(String args [])
{
String original, reverse=””;
Scanner in = new Scanner(System.in);
System.out.println(“Enter a string to check if it is a palindrome”);
original =in.next ( );
int length = original.length( );
for ( int i = length – 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt( i );
if (original.equals(reverse))
System.out.println(“Entered string is a palindrome.”);
Page 14 of 22
else
System.out.println(“Entered string is not a palindrome.”);
}
}
Sample Input/Output:
Input:
Enter a string to check if it is a palindrome
45654
Output:
Entered string is a palindrome.
Page 15 of 22
VI. ARRAYS
An Array is a collection of variables of the same type that are referenced by a common
name.
Format:
type array-name[ ]= new type[ size]; or type[ ] arrayname = new type [size]
Format:
or as
Program 9:
General Program
Write a program in Java to input 5 numbers by the user in an array and find the minimum and
maximum value along with the sum of the elements.
import java.util.*;
public class max_min
{
public void display()
{
Scanner sc = new(System.in);
int value[]=new int[5];
int min=value[0],max=value[0],len,sum=0;
len=value.length;
System.out.println("Enter the elements");
for(int i = 0;i < 5; i++)
{
value[i]=sc.nextInt();
}
for(int i = 0;i < len; i++)
{
sum = sum + value[i];
if(value[i] > max)
max = value[i];
else
if(value[i] < min)
min = value[i];
Page 16 of 22
}
System.out.println("Sum of the elements:"+sum);
System.out.println("Maximum value:"+max);
System.out.println("Minimum value:"+min);
}
}
Sample Input/Output:
Input:
Enter the elements
9
6
4
2
5
Output:
Sum of the elements: 26
Maximum value: 9
Minimum value: 2
Searching:
Sometimes you need to search for an element in an array. To accomplish this task , you
can use different searching techniques. It consists of two very common search techniques viz .,
linear search and binary search.
Linear search refers to the searching technique in which each element of an array is
compared with the search item, one by one, until the search – item is found or all elements have
been compared.
Binary Search is a search technique that works for sorted arrays. Here search item is
compared with the middle element of the array. If the search- item matches with the element ,
search finishes.If the search – item is less than the middle perform (in ascending order) perform
binary search in the first half of the array , otherwise perform binary search in the latter half of
the array.
Program 10:
Binary Search
import java.util.*;
public class binary
{
public void display()
Page 17 of 22
{
Scanner sc = new(System.in);
int A[]=new int[5];
int n,flag=0,L=0,U=4,M=0;
System.out.println("Enter the 5 values");
for(int i = 0;i < 5;i++)
{
A[i] = sc.nextInt();
}
System.out.println("Enter the number to be searched");
N = sc.nextInt();
while(L<=U)
{
M=(L+U)/2;
if(n>A[M])
L=M+1;
else
if(n<A[M])
U=M-1;
else
{
flag=1;
break;
}
}
if(flag==1)
System.out.println("Element present at position:"+(M+1));
else
System.out.println("Element not present");
}
}
Sample Input/Output:
Input:
Enter the 5 values
67
86
98
56
58
Enter the number to be searched
98
Output:
Element present at position: 3
Sorting:
Sorting of an array means arranging the array elements in a specified order i.e., either
ascending or descending order. There are several sorting techniques available e.g., shell sort,
Page 18 of 22
shuttle sort, bubble sort, selection sort, quick sort, heap sort, etc. But the most popular techniques
are selection sort and bubble sort.
(i)Selection Sort
Selection sort is a sorting technique where next smallest (or next largest) element is found
in the array and moved to its correct position e.g., the smallest element should be at 1st position
(for ascending array), second smallest should be at 2nd position and so on.
(ii) Bubble Sort
The idea of bubble sort is to move the largest element to the highest index position in the
array. To attain this, two adjacent elements are compared repeatedly and exchanged if they are
not in the correct order.
Program 11:
Bubble Sort:
Write a program to enter 10 numbers and arrange it in ascending order using bubble sort
technique.
import java.util.*;
public class bubble
{
public void display()
{
Scanner sc = new(System.in);
int A[]=new int[5];
int temp;
System.out.println("Enter the elements");
for(int i = 0;i < 5;i++)
{
A[i] = sc.nextInt();
}
for(int i = 0; i < 5; i++)
{
for(int j = 0;j < 4-i; j++)
{
if(A[j] > A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
System.out.println("Array in ascending order is:");
for(int i = 0;i < 5; i++)
System.out.println(A[i]);
}
}
Page 19 of 22
Sample Input/Output:
Input:
Enter the elements
5
4
7
8
9
Output:
Array in ascending order is:
4
5
7
8
9
Page 20 of 22
VII. SPECIAL NUMBER PROGRAM
Program 12:
Write a program to check whether the entered number is a Armstrong number or not. An
Armstrong number is such a number whose sum of the cube of the digit is equal to the number.
Example: 153
13 +23+33=153
import java.util.*;
public class armstrong
{
public void display()
{
Scanner sc = new(System.in);
int no,digit,copy,sum = 0;
System.out.println("Enter the number");
no = sc.nextInt();
copy=no;
while(copy>0)
{
digit=copy%10;
int cube=digit*digit*digit;
sum=sum+cube;
copy=copy/10;
}
if(sum == no)
System.out.println("It is an armstrong number");
else
System.out.println("It is not a armstrong number");
}
}
Sample Input/Output:
Input:
Enter the number
153
Output:
It is an armstrong number
Page 21 of 22
BIBLIOGRAPHY
www.Google.com
o http://amanjava.blogspot.in/
o http://www.guideforschool.com
o http://www.icsej.com/
Page 22 of 22