Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Exercise Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Programs for Module 1

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;

public class Calculator {


public static void main(String[] args) {
// TODO Auto-generated method stub
int a,b,c=0;
Scanner sc = new Scanner(System.in);
System.out.println("enter the values of a,b");
a=sc.nextInt();
b=sc.nextInt();

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;

public class Prime {

public static void main(String[] args) {


// TODO Auto-generated method stub
int n, i;
boolean flag=false;
Scanner sc = new Scanner(System.in);
System.out.println("enter order number");
n = sc.nextInt();
if(n==0 || n==1)
System.out.println("the customer will get 50% discount");
else
{
for(i=2;i<n;i++)
{
if(n%i==0)
{
System.out.println("the customer will get 80%
discount");
flag=true;
break;
}
}
if(flag==false)
System.out.println("the customer will get 50%
discount");
}

4) Shyam has drawing competition in school, he has to draw the following


pattern. Suggest a solution to Shyam for winning the competition.
*
**
***
****
Solution:
public class Printpyramidtest {

public static void main(String[] args) {


// TODO Auto-generated method stub
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
*
**
***
****
*****
5) Define a class called CommandLine, develop a program in Java that accepts
two floats and integers as its command line arguments and perform addition,
multiplication, subtraction and division and display the result.

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

Float Arithmetic Operations


Addition: 11
Subtraction: 2.5
Multiplication: 29.25
Division: 1.444

6) A teacher gave a project to student in the classroom to find the area of


different shapes. A student has to find the area of different shapes based on
the choice of different parameters and display the results,

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 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Student s1=new Student();
Student s2=new Student("virat","2018CCE0001","CCE");
Student s3=new Student(s2);
s1.disp();
s2.disp();
s3.disp();
}
}

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;

static void totallevel(int water_filled){


total_water=3*water_filled;
System.out.println("Total water filled "+" "+total_water);
}
void usageA(int usage)
{
int total_water1=total_water-usage;
System.out.println("Water level after usage of A is"+"
"+total_water1);
}

void usageB(int usage)


{
Common1.total_water=130;
amount_used=total_water-usage;
System.out.println("Water level after usage of B is"+"
"+amount_used);
System.out.println("Current water level of the tank
is"+""+amount_used);
}
}

public class Water {

public static void main(String[] args) {


// TODO Auto-generated method stub
Common1 c= new Common1();
Common1.totallevel(50);
c.usageA(20);
c.usageB(40);
}

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

10) Develop a program in Java which has a static method min2() that


takes two int arguments and returns the value of the smallest one. Add an
overloaded function that does the same thing with two double values and
display the output.

Solution:
import java.util.Scanner;

public class Staticmin {

static int min2(int a, int b)


{
if(a<b)
return a;
else
return b;
}
static double min2(double a,double b)
{
if(a<b)
return a;
else
return b;
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Staticmin sm=new Staticmin();
Scanner sc = new Scanner(System.in);
System.out.println("enter two integers");
int a1 = sc.nextInt();
int b1 = sc.nextInt();
int im=Staticmin.min2(a1,b1);
System.out.println("enter double values");
double a2= sc.nextDouble();
double b2 = sc.nextDouble();
double dm = Staticmin.min2(a2,b2);
System.out.println("smallest of 2 integers :"+im);
System.out.println("smallest of 2 double values:"+dm);
}

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

You might also like