Java Exercise Programs
Java Exercise Programs
Java Exercise Programs
1) In an Examination hall students of second year and third year students sits in
an odd and even number positions consecutively. Then a teacher asks one of
the students to find the sum of even and odd positions separately. The total
seating capacity of an exam hall is 100. Develop a software application to
perform the above operations.
class Odd_Even
{
public static void main(String args[])
{
int sumeven=0, sumodd=0;
for(i=0;i<=100;i++)
{
if(i%2==0)
{
sumeven = sumeven+i;
}
else
{
sumodd = sumodd+i;
}
}
System.out.println(“The sum of even positions
is”+sumeven);
System.out.println(“The sum of odd positions
is”+sumodd);
}
}
Output:
The sum of even positions is2550
The sum of odd positions is2500
2) A Teacher asks one of the students in a class to do the arithmetic operations
based on a choice. Here at a time student have to perform one arithmetic
operation only. Write a Java program to perform the above operations
Solution:
import java.util.Scanner;
System.out.println("1:Addition\n2:Substraction\n3:Multiplication\n4:Divisio
n");
System.out.println("enter choice");
int ch=sc.nextInt();
switch(ch)
{
case 1: c=a+b;
break;
case 2: c=a-b;
break;
case 3: c=a*b;
break;
case 4: if(b!=0)
{
c=a/b;
break;
}
else
{
System.out.println("division is not
possible");
break;
}
default: System.out.println("invalid choice");
break;
}
System.out.println("the result is"+c);
}
}
Output:
enter the values of a,b
35
1:Addition
2:Substraction
3:Multiplication
4:Division
enter choice
1
the result is8
----------------------------------------
enter the values of a,b
84
1:Addition
2:Substraction
3:Multiplication
4:Division
enter choice
4
the result is2
3) Swiggy App is celebrating its birthday, so it wants to give 80% discount for
those customers who have order number which is prime and others will get
50% discount. Help the application to identify the discount rate for
customers.
import java.util.Scanner;
Solution:
class CommandLine{
public static void main(String args[]){
int a,b;
float c,d;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Float.parseFloat(args[2]);
d=Float.parseFloat(args[3]);
System.out.println(" Integer Arithmetic Operations\n");
System.out.println("Addition:"+(a+b)+ "\nSubtraction:" + (a-b) +
"\nMultiplication:" + (a*b) + "\nDivision:" + (a/b));
System.out.println("\n Float Arithmetic Operations\n");
System.out.println("Addition:" + (c+d) + "\nSubtraction:" + (c-d) +
"\nMultiplication:" + (c*d) + "\nDivision:" + (c/d));
}
}
Output:
Javac CommandLine.java
Java CommandLine 10 20 6.5 4.5
Integer Arithmetic Operations
Addition: 30
Subtraction: -10
Multiplication: 200
Division: 0
Solution:
public class Students
{
void area(int x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq
units");
}
void area(int x, int y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(double x)
{
double z = 3.14 * x * x;
System.out.println("the area of the circle is "+z+" sq units");
}
public static void main(String args[])
{
Students ob = new Students();
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
}
Output:
the area of the square is 25.0 sq units
the area of the rectangle is 132 sq units
the area of the circle is 19.625 sq units
7) Create a class named 'Rectangle' with two data members- length and breadth
and a method to calculate the area which is 'length*breadth'. The class has
three constructors which are
i -having no parameter - values of both length and breadth are assigned zero.
ii - having two numbers as parameters - the two numbers are assigned as length
and breadth respectively.
iii - having one number as parameter - both length and breadth are assigned that
number.
Now, create objects of the 'Rectangle' class having none, one and two
parameters and print their areas.
class Rectangle
{
int length;
int breadth;
void area()
{
int area=length*breadth;
System.out.println(area)
}
Rectangle()
{
length=0;
breadth=0;
}
Rectangle(int len)
{
length=len;
breadth=len;}
}
Rectangle(int l,int b)
{
length=l;
breadth=b;
}
Rectangle(Rectangle r)
{
length=r.length;
breadth=r.breadth;
}
public static void main(String args[])
{
int len, br;
Rectangle r1= new Rectangle();
r1.area();
Scanner input= new Scanner(System.in)
System.out.println(“Enter the length and breadth”);
len=input.nextInt();
br=input.nextInt();
Rectangle r2= new Rectangle(len, br);
r2.area();
Rectangle r3= new Rectangle(50);
r3.area();
Rectangle r4= new Rectangle(r2);
r4.area();}}
Output:
Area of this rectangle 40000
Area of second rectangle 140000
8) A teacher asked the class people leader to prepare the list of student details
of his classroom. By using various parameters display the details of the
students using java program. Hint: Parameterized and Copy constructors to
apply overloading.
Solution:
class Student
{
String name, rollno, branch;
Student()
{
name = "sachin";
rollno = "20181CSE0001";
branch = "CSE";
}
Student(String name, String rollno, String branch)
{
this.name=name;
this.rollno=rollno;
this.branch=branch;
}
Student(Student s)
{
this.name=s.name;
this.rollno=s.rollno;
this.branch=s.branch;
}
void disp()
{
System.out.println("name:"+name+"\nrollno:"+rollno+"\nbranch:"+branch);
}
}
public class ConOver {
Output:
name:sachin
rollno:20181CSE0001
branch:CSE
name:virat
rollno:2018CCE0001
branch:CCE
name:virat
rollno:2018CCE0001
branch:CCE
9) AVR apartments uses a common water tank to supply water for every house.
Each time the water level of the tank reduces based on the usage of each
house. The maintenance person filled the tank thrice with 50litres each time.
One of the houses used 20litres and another house used 40 liters. Display the
current water level in the tank.
Solution:
class Common1{
int total_water=0;
int cur_level=0;
int usage=0;
int amount_used=0;
int water_filled=0;
Output:
Total water filled 150
Water level after usage of A is 130
Water level after usage of B is 90
Current water level of the tank is90
Solution:
import java.util.Scanner;
Output:
enter two integers
20 30
enter double values
3.5 4.5
smallest of 2 integers :20
smallest of 2 double values:3.5
11) Develop a java program with two classes an Outer and Inner class.
Each of the class have one data member, the inner class has member
function which displays the data members. Write a main function to access
both inner and outer inner members.
Solution:
class Outer
{
int outer_x =100;
void test(){
Inner in=new Inner();
in.display();
}
class Inner
{
int p=25;
void display()
{
System.out.println("display: outer_x=" + outer_x);
System.out.println("from inner: p value ="+ p);
}
}
}
class InnerClassDemo
{
public static void main(String args[])
{
Outer otr = new Outer();
System.out.println(" outer var="+ otr.outer_x);
otr.test();
}
}
Output:
outer var=100
display: outer_x=100
from inner: p value =25