Java Programs 1-10
Java Programs 1-10
1. Write Java programs a. To print fibonacci series without using recursion and using
recursion.(concept of loops, data types) b. To check prime numbers. c. To sort an array
elements using bubble sort algorithm.
2. Create a class called account with the data members(Accno – integer, name
String, Phone_No: integer, balance_amt:float), and following methods : a. getinput() to get
input from the user b. Deposit() method which takes the amount to be deposited in to
his/her account and do the calculation. c. Withdraw() method which gets the amount to
be withdrawn from his/her account. d. Print the appropriate results.
3. Define a Stack class to implement the stack data structure. Include constructors to
perform initialization, method push to push an element into the stack, method pop to
remove an element from the stack and display method to display the elements of the
stack.
4. Define a class Complex with data members as two real numbers, constructors for
initialization these numbers, methods to add, subtract and multiply 2 complex numbers.
5. Write a java program to read 2 matrices and place the product in a third matrix .
Use constructors and suitable methods.
6. Write a java program to work with strings. a. Extract a portion of the string and
print it. Variable m indicates the amount of characters to be extracted from the string
starting from the nth position. b. Read a text and count all the occurrences of a particular
word. c. Replace a substring in the given string. d. Rearrange the string and rewrite in
alphabetical order. e. Compare two strings ignoring case. f. Concatenate two strings.
7. Create a Personal class to hold the personal details of an person such as name,
age, education, salary- (0basic, da, hra), years of experience, number of loans and loan
amount. Write constructors to assign values to the data members. Include an a.
isEligible() method to indicate whether the person is eligible for loan, b. taxPay() method
to indicate the amount of tax to be paid, c. isEligiblePromotion() to indicate whether the
person is eligible for a promotion. d. Display () method to display the details. Enter the
details of n employees and indicate their eligibility and the tax to be paid.
8. Create a Circle class with following members. A data member that stores the
radius of a circle. A constructor function with an argument that initializes the radius A
function that computes and returns area of a circle Create two derived classes Sector
and Segment that inherit the Circle class. Both classes inherit radius and the function that
returns the circle‟s area from Circle.
In addition to the members inherited from Circle, Sector and Segment have some
specific members as follows: Sector 1. A data member that stores the control angle of a
sector (in radians) 2. A constructor function with arguments that initialize radius and
angle 3. A function that computes and returns the area of a sector Segment 1. A data
member that stores the length of a segment in a circle 2. A constructor function with
arguments that initialize radius and length 3. A function that computes and returns the
area of a segment Create the main () function to instantiate an object of each class and
then call appropriate member functions to compute and return the area of a circle, sector
and segment. Note :Area_of_circle = pi * r 2 Area_of_Sector=(1/2) r 2 θ Area_of_segment=
r2((r-h)/r) – (r-h) (2rh-h2)1/2 Where r is the radius of a circle, θ is the central angle of a
sector in radians, h is the length of a segment and ((r-h)/r) is in radians.
9. Write a Java Program that does the following related to Inheritance: a. Create an
abstract class called Vehicle which contains the „year_of_manufacture‟ data member
and two abstract methods „getData()‟ and „putData()‟ with a constructor. b. Create two
derived classes “TwoWheeler” and “FourWheeler” and implement the abstract methods.
Make “FourWheeler” as final class. c. Create class „MyTwoWheeler‟ which is a sub-class
of “TwoWheeler” and demonstrate the use of super keyword to initialize data members
of “MyTwoWheeler”.
11. Write a Java Program that does the following related to Packages and Interfaces ,
Exception Handling: a. Create an interface Student which gets the name and branch of a
student. b. Create a package called „StudentPackage‟ which has a user-defined class
RegisterStudent. c. If a student registers above 30 credits for the semester, the method
should throw a user-defined exception called „CreditLimit‟ and display an appropriate
message. d. Create another package called „ResultPackage‟ which displays the grade for
the subject registered for particular semester and the SGPA . if SGPA is above 10 then
throws an InvalidSGPA userdefined exception. e. In the StudentPackage , collect the
marks of all the subjects in 4 semesters and calculate SGPA and CGPA.
12. a. Write a java program to implement queues of Strings using an ArrayList class of
the Collection framework. b. Create a linked list of names (String type). Use an Iterator to
traverse through the list and print those names whose length is < 5.
Fibonnaci series without recursion program number-1a
import java.util.Scanner;
class fib{
public static void main(String[] args){
int a=0,b=1,c;
int i = 0;
System.out.print("The fibonacci series is: ");
while(i<n){
System.out.print(a + " ");
c = a+b;
a = b;
b = c;
i++;
}
System.out.println("\n-----------------------------------");
}
}
}
}
balanceAmt =0;
}
//Method to deposit the amount in the bank
public void deposit(){
System.out.println("Enter the amount you wanna deposit:\t");
double deposit = sc.nextDouble();
balanceAmt += deposit;
System.out.println("Amount Deposited Successfully");
System.out.println("The account balance is:\t"+balanceAmt);
}
//Method to withdraw the amount from the bank
public void withdraw(){
System.out.println("Enter the amount you wanna withdraw:\t");
double withdraw = sc.nextDouble();
if(balanceAmt-withdraw>=0){
balanceAmt -= withdraw;
System.out.println("Amount withdrawn successful");
}
else{
System.out.println("Your account has insufficient funds");
}
System.out.println("The account balance is:\t"+balanceAmt);
}
//Method to print the details
public void printDetails(){
System.out.println("Account Number:\t"+accNo);
System.out.println("Account holders name:\t"+name);
System.out.println("Phone number:\t"+phoneNo);
System.out.println("Current balance in the account:\t"+balanceAmt);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
account a1 = new account(); //Object reference for the account
while(true){
System.out.println("1.Enter the user details");
System.out.println("2.Deposit money in the account");
System.out.println("3.Withdraw money from the account");
System.out.println("4.Get the account details");
System.out.println("5. Exit!");
System.out.print("Enter your choice:\t");
int ch = sc.nextInt();
switch(ch){
case 1:
a1.getInput();
break;
case 2:
a1.deposit();
break;
case 3:
a1.withdraw();
break;
case 4:
a1.printDetails();
break;
case 5:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
System.out.println("");
}
}
}
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.println("4. Exit!");
while(true){
System.out.print("\nEnter your choice:\t");
int ch = sc.nextInt();
switch(ch){
case 1:
System.out.print("Enter the element you want to insert:\t");
int ele1 = sc.nextInt();
s1.push(ele1);
break;
case 2:
int ele2 = s1.pop();
if(ele2 != -1){
System.out.println("The elemnet popped out from the stck
is:"+ele2);
}
else{
System.out.println("Stack underflow!");
}
break;
case 3:
s1.display();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
while(true){
System.out.println("Enter your choice");
int ch = sc.nextInt();
switch(ch){
case 1:
System.out.println("Enter the first complex number");
c1.getInput();
System.out.println("Enter the second complex number");
c2.getInput();
compute = compute.sum_c(c1,c2);
System.out.println("The sum of "+c1.a + " +i " + c1.b + " and
"+c2.a + " +i " + c2.b +" is "+compute.a + " +i (" + compute.b+")");
break;
case 2:
System.out.println("Enter the first complex number");
c1.getInput();
System.out.println("Enter the second complex number");
c2.getInput();
compute = compute.diff_c(c1,c2);
System.out.println("The sum of "+c1.a + " +i " + c1.b + " and
"+c2.a + " +i " + c2.b +" is "+compute.a + " +i (" + compute.b +")" );
break;
case 3:
System.out.println("Enter the first complex number");
c1.getInput();
System.out.println("Enter the second complex number");
c2.getInput();
compute = compute.mul_c(c1,c2);
System.out.println("The sum of "+c1.a + " +i " + c1.b + " and
"+c2.a + " +i " + c2.b +" is "+compute.a + " +i (" + compute.b+")" );
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
}
//For matrix 1.
System.out.println("For matrix 1");
System.out.print("Enter the no of rows:\t");
int rows1 = sc.nextInt();
System.out.print("Enter the no of columns:\t");
int columns1 = sc.nextInt();
//For matrix 2.
System.out.println("For matrix 2");
System.out.print("Enter the no of rows:\t");
int rows2 = sc.nextInt();
System.out.print("Enter the no of columns:\t");
int columns2 = sc.nextInt();
if(flag == 0){
System.out.println("The multiplication of two matrices is");
m3.printMatrix();
}
else{
System.out.println("The multiplication of the two matrices is not
possible");
}
}
}
String program number-6
import java.util.Scanner;
import java.util.Arrays;
class strings{
public static void main(String[] args){
while(true){
System.out.print("Enter your choice:\t");
int ch = sc.nextInt();
sc.nextLine();
switch(ch){
case 1:
System.out.print("Enter the String:\t");
String s = sc.nextLine();
System.out.print("\nEnter the position of the string from which
you wanna start extracting:\t");
int n = sc.nextInt();
System.out.print("\nEnter the no of characters to be
extracted:\t");
int m = sc.nextInt();
String k = s.substring(n-1,m+n-1);
System.out.println("\nThe extracted string is: "+k);
break;
case 2:
System.out.print("Enter the String:\t");
s = sc.nextLine();
System.out.print("\nEnter the word whose occurences you wanna
find:\n");
k = sc.next();
int counter = 0;
String arr[] = s.split(" ");
for(int i=0;i<arr.length;i++){
if(arr[i].equals(k)){
counter++;
}
}
System.out.println("\nThe no of occurences of the word "+k+" are
"+counter);
break;
case 3:
System.out.print("Enter the string:\t");
s = sc.nextLine();
System.out.print("\nEnter the substring you wanna replace:\t");
k = sc.next();
System.out.print("\nEnter the substring you wanna replace
with:\t");
String p = sc.next();
String t = s.replace(k,p);
System.out.println("\nThe new string is: "+t);
break;
case 4:
System.out.print("Enter the string:\t");
s = sc.nextLine();
k = s.toLowerCase();
char[] arr2 = k.toCharArray();
Arrays.sort(arr2);
p = new String(arr2);
System.out.println("\nThe string arranged aphabetically is:
"+p);
break;
case 5:
System.out.print("Enter the string 1:\t");
s = sc.nextLine();
System.out.print("\nEnter the string 2:\t");
k = sc.nextLine();
boolean cmp = s.equalsIgnoreCase(k);
if(cmp){
System.out.println("\nThe two strings are equal");
}
else{
System.out.println("\nThe two strings are not equal");
}
break;
case 6:
System.out.print("Enter the string 1:\t");
s = sc.nextLine();
System.out.print("Enter the string 2:\t");
k = sc.nextLine();
p = s+" "+k;
System.out.println("\nThe concatenated strings is: "+p);
break;
case 7:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
System.out.println();
p.isEligible(p);
p.taxPay(p);
p.isEligiblePromotion(p);
}
for(int i=0;i<n;i++){
switch(ch){
case 1:
System.out.println("Enter the radius of the circle");
double r = sc.nextDouble();
Circle c = new Circle(r);
System.out.println("The area of the circle with radius "+r+ " is
"+c.circle_area());
break;
case 2:
System.out.println("Enter the radius of the circle");
r = sc.nextDouble();
System.out.println("Enter the sector angle in degrees");
double d = sc.nextDouble();
double p = (Math.PI/180)*d;
Sector s = new Sector(r,p);
System.out.println("The area of the sector with radius "+r+ "
and sector angle "+ d+"(degrees) is "+s.sector_area());
break;
case 3:
System.out.println("Enter the radius of the circle");
r = sc.nextDouble();
System.out.println("Enter the length of the segment");
double l = sc.nextDouble();
Segment se = new Segment(r,l);
System.out.println("The area of the segment with radius "+r+ "
and segement length "+ l+" is "+se.segment_area());
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice");
}
}
}
}
@Override
public int getData() {
return year_of_manufacture;
}
@Override
public void putData(int year) {
year_of_manufacture = year;
}
TwoWheeler(int year) {
year_of_manufacture = year;
}
}
@Override
public int getData() {
return year_of_manufacture;
}
@Override
public void putData(int year) {
year_of_manufacture = year;
}
FourWheeler(int year){
year_of_manufacture=year;
}
package com.msrit.problem9;
import java.util.Scanner;
import Q10.ISE.ISE_department;
import Q10.Faculty;
while(true){
System.out.println("1. Add a faculty");
System.out.println("2. Get the details of a particular faculty");
System.out.println("3. Get the name of experienced faculty");
System.out.println("4. Know the no of designations a particular faculty
had");
System.out.println("5. Know the no fo research consultancy proects done by a
faculty");
System.out.println("6. Exit!");
switch(ch){
case 1:
f[counter] = new Faculty();
ise.readData(f[counter]);
counter++;
break;
case 2:
System.out.println("Enter the name of the faculty whose details
you want");
sc.nextLine();
String x = sc.nextLine();
for(int i=0;i<counter;i++){
if(x.equals(f[i].name)){
flag1 = 1;
ise.printData(f[i]);
break;
}
}
if(flag1 == 0){
System.out.println("Faculty not found!");
}
break;
case 3:
System.out.println("The faculty with experience greater than 20
years are:");
for(int i=0;i<counter;i++){
if(f[i].years_of_experience >=20){
System.out.println(f[i].name);
flag2 = 1;
}
}
if(flag2 == 0){
System.out.println("No Such record found");
}
break;
case 4:
System.out.println("Enter the name of the faculty whose detail
you want");
sc.nextLine();
x = sc.nextLine();
for(int i=0;i<counter;i++){
if(x.equals(f[i].name)){
flag3 = 1;
System.out.println("The no of designations the faculty
had are: "+f[i].no_of_designations);
break;
}
}
if(flag3 == 0){
System.out.println("Faculty not found!");
}
break;
case 5:
System.out.println("Enter the name of the faculty whose detail
you want");
sc.nextLine();
x = sc.nextLine();
for(int i=0;i<counter;i++){
if(x.equals(f[i].name)){
flag4 = 1;
System.out.println("The no of reserach consultancy
projects faculty did are: "+f[i].no_of_research_projects);
break;
}
}
if(flag4 == 0){
System.out.println("Faculty not found!");
}
break;
case 6:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
package Q10;
import Q10.Faculty;
public interface department{