Some Java Programs :)
Some Java Programs :)
_________________________
_________________________
Acknowledgement
22 Conclusion 54
23 Bibliography 55
Ans program 1-
import java.util.*;
public class employee //class
{
int pan;
String name;
double tax_income, tax;
Scanner sc= new Scanner(System.in);
void input() //input method
{
System.out.println("\fEnter Name");
name=sc.nextLine();
System.out.println("Enter Pan Number");
pan=sc.nextInt();
System.out.println("Enter Taxable Income");
tax_income= sc.nextDouble();
}
void calc() //calculate method
{
if(tax_income>=0 && tax_income<=100000)
{
tax=0;
}
else if (tax_income>=100001 && tax_income<=150000)
{
tax=100000.0*0+ (tax_income-100000)*10.0/100;
}
else if(tax_income>=150001 && tax_income<=250000)
{
tax = 100000.0*0+ (tax_income-150000)*20/100.0 + 5000;
}
else if (tax_income> 250000)
{
tax= 100000.0*0+ (tax_income-250000)*30/100.0 + 25000;
}
else if(tax_income<0)
{
System.out.println("Sorry there cannot be negative payment.");
}
}
void display() //display method
{
System.out.println(" Pan Number\t Name\t Tax Income\t Tax ");
System.out.print( pan+"\t "+ name+"\t "+ tax_income+"\t "+tax);
}
public static void main(String args[])
{
employee obj=new employee();
obj.input();
obj.calc();
obj.display();
}
}
Variable Description Table
Output 1:
Enter Name
Robin Rathore
Enter Pan Number
23221
Enter Taxable Income
150000
Pan Number Name Tax Income Tax
23424 Raju Rathore 150000.0 5000.0
Output 2:
Enter Name
Robin Rathore
Enter Pan Number
23221
Enter Taxable Income
230000
Pan Number Name Tax Income Tax
23221 Robin Rathore 230000.0 21000.0
Output 3:
Enter Name
Robin Rathore
Enter Pan Number
54632
Enter Taxable Income
-2544
Sorry there cannot be negative payment.
Pan Number Name Tax Income Tax
54632 Robin Rathore -2544.0 0.0
2. 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
volume of a sphere (vs) = 4/3* π* r* r*r (where π = 3.14 or 22/7)
Volume of a cuboid (vcd) = l* b* h
Ans-
/** A program showing function overloading */
// To find volume of a cone,a sphere and a cuboid using overloading.
import java.util.*;
public class Volume
{
double vc=0.0D,vs=0.0D, vcd=0.0D;
void volume (int s) //volume of cube
// Here we are using void as we aren't returning any value to the main
and just directly printing the output.
{
vc=s*s*s;
System.out.println("The volume of a cube = "+vc);
}
void volume(float r) //volume of sphere
{
vs=4/3*22/7*r*r*r;
System.out.println("The volume of the sphere is= "+vs);
}
void volume(int l,int b,int h) //volume of cuboid
{
vcd=l*b*h;
System.out.println("The volume of the cuboid = "+vcd);
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int s,l,b,h;
float r;
System.out.println("Enter the value of side of a cube, radius of
sphere,sides of cuboid");
s=in.nextInt();
r=in.nextFloat();
l=in.nextInt();
b=in.nextInt();
h=in.nextInt();
Volume ob=new Volume();
ob.volume(s);
ob.volume(r);
ob.volume(l,b,h);
}
}
Output :
Enter the value of side of a cube, radius of sphere,sides of cuboid
5 3.2 3 4 7
The volume of a cube = 125.0
The volume of the sphere is= 98.30400848388672
THe volume of the cuboid = 84.0
Ans-
/**
* Design a class to overload a function series() as follows:
(a) void series (int x, int n) – To display the sum of the series given
below:
x^1 + x^2 + x^3 + ................ x^n terms
(b) void series (int p) – To display the following series:
0, 7, 26, 63 p terms.
(c) void series () – To display the sum of the series given below:
1/2+1/3+1/4.....+1/10
*/
import java.util.*;
public class Series
{
void series(int x,int n) //series 1
{
double sum=0,i;
for (i=1;i<=n;i++)
sum= sum+ Math.pow(x,i);
System.out.println("The sum of series one: "+sum);
}
void series( int p) //series 2
{
int i,num=0;
for(i= 1; i<= p; i++)
{
num= (i*i*i)-1;
System.out.print(num);
if( i== p )
System.out.print(" ");
else
System.out.print(", ");
}
System.out.println();
}
void series () //series 3
{
double sum=0,i;
for( i= 2;i<=10;i++)
{
sum= sum+ 1/i;
}
System.out.println("The sum of series three: "+sum);
}
public static void main(String args[]) //main function
{
System.out.println("\f");
Scanner sc= new Scanner(System.in);
int no,pow,sr;
System.out.println("Enter(one by one) :\n The number and the till
which power of it do you want to add up for 1st series.\n The number of
terms till which you want the series to continue for series 2");
no= sc.nextInt();
pow= sc.nextInt();
sr=sc.nextInt();
Series obj= new Series();
obj.series(no,pow);
obj.series(sr);
obj.series();
}
}
Output:
Enter(one by one) :
The number and the till which power of it do you want to add up for 1st
series.
The number of terms till which you want the series to continue for series
2
17
5
The sum of series one: 7.0
0, 7, 26, 63, 124
The sum of series three: 1.928968253968254
Ans-
import java.util.*;
public class Railway_ticket
{
String name; //declaring all variables
String coach;
long mobno;
int amt;
int totalamt;
public void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name:");
name=sc.nextLine(); //accepting name
System.out.println("Which coach would you prefer:(please do not
write the option number, just write the option name)");
System.out.println("1) First_AC\n2) Second_AC\n3) Third_AC\n4)
Sleeper");
coach= sc.nextLine();
System.out.println("Enter your mobile number:");
mobno=sc.nextLong();
System.out.println("Enter the basic amount of the ticket:");
amt=sc.nextInt();
}
public void update()
{
if(coach.equalsIgnoreCase("First_AC")) //to check equality between
two strings by ignoring the case
totalamt=amt+700;
else if (coach.equalsIgnoreCase("Second_AC"))
totalamt= amt+ 500;
else if (coach.equalsIgnoreCase("Third_AC"))
totalamt= amt+ 250;
else if (coach.equalsIgnoreCase("Sleeper"))
totalamt= amt+ 0;
}
public void display() //method to display the data collected and
evaluated.
{
System.out.println("Name: "+ name);
System.out.println("Coach: "+ coach);
System.out.println("Total amount: "+ totalamt);
System.out.println("Mobile number: "+ mobno);
}
public static void main (String args[])
{
System.out.println("\f");
Railway_ticket obj=new Railway_ticket();
obj.accept();
obj.update();
obj.display();
}
}
Output 1:
Output 2:
Output 3:
Output 4:
5. Write a menu driven program to accept a number from the user and
check
(a). Whether it is a Pronic number or not. Pronic number is the number
which is the product of two
consecutive integers
Examples : 12 = 3 × 4 .
20 = 4 × 5
42 = 6 × 7
(b) Whether it is a spy number or not. A number is spy if the sum of its
digits equals to the product of
its digits.
Ans-
import java.util.Scanner;
public class Pronic_Spy
{
public static void main(String args[])
{
System.out.println("\f");
Scanner sc=new Scanner(System.in);
int n,i,a,sum=0,product=1,digit,flag=0; //declaring and initializing the
variables.
System.out.println("\t\tMENU");
System.out.println("1)Pronic Number\n2) Spy number");
System.out.println("Enter your option number:");
n=sc.nextInt();
switch(n)
{
case 1:
System.out.println("Enter your number:");
n = sc.nextInt();
for(i=1;i<n;i++)
{
if (i*(i+1)==n)
{
flag=1;
break;
}
}
if(flag==1)
System.out.println(n+" is a pronic number!");
else
System.out.println("It is not a pronic number!");
break;
case 2:
System.out.println("Enter a number:");
n=sc.nextInt(); // accepting the number.
a=n;
while(n>0)
{
digit=n%10; // Extracting digits.
sum= sum+digit;
product=product*digit;
n=n/10;
}
if(sum==product) // checking if sum and product are the same.
System.out.println(a+" is a spy number.");
else
System.out.println(a+" is not a spy number.");
break;
}
}
}
Output 1:
MENU
1)Pronic Number
2) Spy number
Enter your option number:
1
Enter your number:
12
12 is a pronic number!
Output 2:
MENU
1)Pronic Number
2) Spy number
Enter your option number:
1
Enter your number:
15
It is not a pronic number!
Output 3:
MENU
1)Pronic Number
2) Spy number
Enter your option number:
2
Enter a number:
1124
1124 is a spy number.
Output 4:
MENU
1)Pronic Number
2) Spy number
Enter your option number:
2
Enter a number:
1753
1753 is not a spy number.
Write a main method to create an object of the class and call the above.
Ans-
Output 1:
Output 2:
Output 3:
Ans-
A program to find the sum of two different series.
Ans-
t
Variable Description Table
Output 1:
Enter name:
Rabindra Singh
Enter price:
23000
Name: Rabindra Singh
Discount: 1150.0
Amount to be paid: 21850.0
Output 2:
Enter name:
Mohan P
Enter price:
42000
Name: Mohan P
Discount: 3150.0
Amount to be paid: 38850.0
Output 3:
Enter name:
Arjun Kumar
Enter price:
79000
Name: Arjun Kumar
Discount: 7900.0
Amount to be paid: 71100.0
Output 4:
Enter name:
Jitendra Ravi
Enter price:
150999
Name: Jitendra Ravi
Discount: 22649.85
Amount to be paid: 128349.15
import java.util.*;
public class Marks
{
String name;
int age,m1,m2,m3,max;
double avg;
Marks(String n,int d,int a, int b, int c) //parameterized constructors
{
name= n;
age=d;
m1=a;
m2=b;
m3=c;
}
void compute() //all calculations are done here
{
avg=(m1+m2+m3)/3; //average marks.
if(m1>m2&&m1>m3)
max=m1;
if (m2>m1&& m2>m3)
max=m2;
if (m3>m1&& m3> m2)
max=m3;
}
void display() // method to display the data.
{
System.out.println("Name: "+name);
System.out.println("Age: "+age+" years");
System.out.println("Marks in three subjects: "+m1+" "+m2+" "+m3);
System.out.println("The highest marks out of the three subject
marks: "+max);
System.out.println("The average marks: "+avg);
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String st;
int ag, ma1,ma2,ma3;
System.out.println("Enter the Student's Name, Age, Marks in three
subjects one by one:");
st=sc.nextLine();
ag=sc.nextInt();
ma1=sc.nextInt();
ma2=sc.nextInt();
ma3=sc.nextInt();
Marks obj=new Marks(st,ag,ma1,ma2,ma3);
obj.compute();
obj.display();
}
}
Output:
Enter the Student's Name, Age, Marks in three subjects one by one:
Ashwas Aayu
13
96 89 98
Name: Ashwas Aayu
Age: 13 years
Marks in three subjects: 96 89 98
The highest marks out of the three subject marks: 98
The average marks: 94.0
10. Using a switch statement, write a menu driven program:
Instructions:
1. While taking printouts follow the pattern for each program.
i) program description
ii) source code – with comments
iii) Format for variable description:
Ans-
A menu driven program to check whether a number is a composite
number or not and to find the smallest number of an accepted number.
import java.util.Scanner;
public class Number
{
public static void main(String args[]) //main function
{
Scanner in = new Scanner(System.in);
System.out.println("\t\tMENU");
System.out.println("1) Composite Number\n2) Smallest Digit.");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) //switch statement
{
case 1:
System.out.print("Enter Number: ");
int n = in.nextInt();
int c = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
c++;
}
if (c > 2)
System.out.println("Yes, it is a Composite Number");
else
System.out.println("No, it is not a Composite Number");
break; //To avoid fall-through.
case 2:
System.out.print("Enter Number: ");
int num = in.nextInt();
int s = 10;
while (num != 0)
{
int d = num % 10;
if (d < s)
s = d;
num /= 10;
}
System.out.println("The smallest digit is " + s);
break;
default: //default case.
System.out.println("Wrong choice");
}
}
}
Output 1:
MENU
1) Composite Number
2) Smallest Digit.
Enter your choice: 1
Enter Number: 13
No, it is not a Composite Number
Output 2:
MENU
1) Composite Number
2) Smallest Digit.
Enter your choice: 1
Enter Number: 12
Yes, it is a Composite Number
Output 3:
MENU
1) Composite Number
2) Smallest Digit.
Enter your choice: 2
Enter Number: 89734
The smallest digit is 3
Output 4:
MENU
1) Composite Number
2) Smallest Digit.
Enter your choice: 3
Wrong choice
Conclusion
Java is a high level, object oriented programming language developed by James
Gosling at Sun Microsystem in 1991.
There are four fundamental concepts of Object-oriented programming – Inheritance,
Encapsulation, Polymorphism, and Data abstraction.
This project has helped me to enhance my programming skills and at the same time
has made my concepts stronger. We have used methods/ functions and various other
String functions to run our programs successfully. A data type in java is a term that
specifies memory size and type of values that can be stored into the memory location.
In other words, data types define different values that a variable can take.
There is a difference between declaring a variable and initializing a variable.
Declaration tells the compiler about the existence of an entity in the program and its
location while initialization is the process of assigning a value to the variable(entity).
There are different ways to programme for the same desired result and each procedure
is appreciable till we get the required output.
Overall this was a really fun-filled project which made me ponder a lot about the
program logic and hence it helped me to utilise my time in a productive manner.