[go: up one dir, main page]

0% found this document useful (0 votes)
109 views33 pages

ch-7 8 Program

The document contains code snippets demonstrating the use of classes and objects in Java to perform various tasks. QB-301 shows how to calculate nPr (permutations) using a class with a factorial method. QB-302 creates an array of Student objects, each with roll number, name, and mobile number properties, and initializes and prints the data. QB-303 prints a message using a class. QB-304 calculates area and perimeter of a circle using class methods.

Uploaded by

Parth thakkare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views33 pages

ch-7 8 Program

The document contains code snippets demonstrating the use of classes and objects in Java to perform various tasks. QB-301 shows how to calculate nPr (permutations) using a class with a factorial method. QB-302 creates an array of Student objects, each with roll number, name, and mobile number properties, and initializes and prints the data. QB-303 prints a message using a class. QB-304 calculates area and perimeter of a circle using class methods.

Uploaded by

Parth thakkare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

QB-301 WAP to find nPr by using class and object

import java.util.*;
class Main_301
{
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
System.out.print("Enter r: ");
int r = sc.nextInt();
Recursion11 r11 = new Recursion11();
int Fact_N = r11.factorial(n);
System.out.println("Factorial of n = "+Fact_N);
int Fact_N_R = r11.factorial(n-r);
System.out.println("Factorial of n-r = "+Fact_N_R);

int nPr = (Fact_N)/(Fact_N_R);


System.out.println("Result of nCr = "+nPr);
}
}
class Recursion11
{
int factorial(int n)
{
int fact = 1;
for(int i=1 ; i<=n ; i++)
{
fact *= i;
}
return fact;
}
}

QB-302 create a class Student with Roll_No ,Name and Mobile_No as data member. Use
necessary method to initialize it and to print. Create at least 5 student. (Use array of
object).

class Main
{
public static void main(String[] arg)
{
Student a[] = new Student[5];
for(int i=0; i<5; i++)
{
a[i] = new Student();
System.out.println("----Enter Student "+(i+1)+" Data:----");
a[i].getData();
}
System.out.println("=============================");
System.out.println("====Student Details=============");
System.out.println("=============================");
for(int i=0; i<5; i++)
{
a[i].printData();
}
}
}
class Student
{
int RN;
String name;
long Mo_no;
void getData()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Roll no: ");
RN = sc.nextInt();
sc.nextLine();
System.out.print("Enter Student name: ");
name = sc.nextLine();
System.out.print("Enter Mobile no: ");
Mo_no = sc.nextLong();
System.out.println();
}
void printData()
{
System.out.println();
System.out.println("Roll no= "+RN);
System.out.println("Student name= "+name);
System.out.println("Mobile No= "+Mo_no);
}
}

QB-303 Write a Java program to print message using class

class CO1
{
void print()
{
System.out.println("Hello");
}
}
class Run
{
public static void main(String args[])
{
CO1 ob = new CO1 ();

ob.print();
}
}

QB-304 Write a Java program to find area and perimeter of a circle using class
class CO2
{
double area,r,peri;

double P = Math.PI;

void Area(double r)
{
area = P*r*r;
System.out.println("Area of circle = "+area);
}

void Perimeter(double r)
{
peri = 2*P*r;
System.out.println("Perimeter of circle = "+peri);
}
}
class Run
{
public static void main(String args[])
{
CO2 p1 = new CO2();

p1.Area(20);
p1.Perimeter(20);

}
}
QB-305 Write a Java program to count all digits of an integer number using class

import java.util.*;
class CO3
{
int count=0;
Scanner sc = new Scanner(System.in);

void count(int n)
{
while(n!=0)
{
n=n/10;
count++;
}
System.out.println("Count of digit = "+count);
}
}
class Run
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
CO3 p1 = new CO3();
System.out.println("Enter any integer digit n:");
int n = sc.nextInt();

p1.count(n);

}
}

QB-306 Write a Java program to find product of all digits of an integer number using
class
import java.util.*;
class CO4
{
int sum = 0,r,product=1;
Scanner sc = new Scanner (System.in);
void sumPro()
{
System.out.println("Eneter number n:");
int n = sc.nextInt();
while(n!=0)
{
r = n%10;
product=product*r;
n=n/10;
}

System.out.println("Product of all digit ="+product);

}
}
class Run
{
public static void main(String args[])
{
CO4 p1 = new CO4();

p1.sumPro();
}
}

QB-307 Write a Java program to check whether a given number is palindrome or not
using class

import java.util.*;
class CO5
{
int temp=0,rev=0,r;

void peli(int n)
{
temp = n;
while(n!=0)
{
r=n%10;
rev = (rev*10) + r;
n=n/10;
}
if(temp == rev)
{
System.out.println("Yes Pelindrome");
}
else
{
System.out.println("Not Pelindrome");
}
}
}
class run
{

public static void main(String args[])


{
Scanner sc = new Scanner (System.in);
CO5 p1 = new CO5();
System.out.println("Enetr any integer number n");
int n = sc.nextInt();

p1.peli(n);
}
}
QB-308 Write a Java program to check whether a given number is Armstrong or not
using class

import java.util.*;
class CO6
{
double sum=0;
int r,count=0,temp=0;
void arm(int n)
{
temp = n;
while(n!=0)
{
n=n/10;
count++;
}
n=temp;
while(n!=0)
{
r=n%10;
sum= sum +((Math.pow(r,count)));
n=n/10;
}
if(temp == sum)
{
System.out.println("Yes armstrong");
}
else
{
System.out.println("Not armstrong");
}
}
}
class run
{

public static void main(String args[])


{
Scanner sc = new Scanner (System.in);
CO6 p1 = new CO6();
System.out.println("Enetr any integer number n");
int n = sc.nextInt();

p1.arm(n);
}
}

QB-309 Create a class name Temperature in which create methods of given name
ferenhit(), celcius() to perform basic conversion. Call all this methods using class named
Main.
class Temperature{

void ferenhit(double celsius){


double result = celsius * (9/5.0) + 32;
System.out.println("Temperature in fahrenheit is: "+result);
}
void celsius(double fahrenheit){
double result = (fahrenheit - 32) * (5/9.0);
System.out.println("Temperature in Celsius is: "+result);
}
}

class Main{
public static void main(String[] args) {
Temperature T1 = new Temperature();
T1.ferenhit(100);
T1.celsius(212);
}
}
QB-310 Write a java program to access two variables and two methods with different
name using class.

class Test {

int var1 = 10;


boolean var2 = true;

void method1(){
System.out.println("Method 1 Called");
}
void method2(){
System.out.println("Method 2 Called");
}
}

class Main{
public static void main(String[] args) {
Test T1 = new Test();
System.out.println("Variable 1 Accessed: "+T1.var1);
System.out.println("Variable 2 Accessed: "+T1.var2);
T1.method1();
T1.method2();
}
}

QB-311 Write a Java program to perform basic Calculator operations using following
methods catagories: sum() - without arguments without return, minus() - with
arguments without return, multi() - without arguments with return divide() - with
arguments with return

import java.util.*;

class EX311
{
Scanner sc=new Scanner(System.in);
void sum() //without arguments without return,
{
System.out.println("Lets do Addition");
System.out.print("Enter value 1: ");
int a=sc.nextInt();
System.out.print("Enter value 2: ");
int b=sc.nextInt();
System.out.println("sum= "+(a+b));
}

void minus(int a, int b) //with arguments without return,


{
int c=a-b;
System.out.println("sub= "+c);
}

int multi() //without arguments with return


{
System.out.println("Lets do Multiplication");
System.out.print("Enter value 1: ");
int a=sc.nextInt();
System.out.print("Enter value 2: ");
int b=sc.nextInt();
int c=a*b;
return c;
}

double divide(int a,int b) //with arguments with return


{
double c= ((double)a/b);
return c;
}
}

class Run
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
EX311 e = new EX311();

e.sum();

System.out.println("Lets do Substraction");
System.out.print("Enter value 1: ");
int x=sc.nextInt();
System.out.print("Enter value 2: ");
int y=sc.nextInt();
e.minus(x,y);

int ans=e.multi();
System.out.println("multi= "+ans);
System.out.println("Lets do Division");
System.out.print("Enter value 1 : ");
int p=sc.nextInt();
System.out.print("Enter value 2 : ");
int q=sc.nextInt();
double ans2=e.divide(p,q);
System.out.println("division= "+ans2);
}
}

QB-312 WAP to find HCF of two numbers using recursion

import java.util.*;
class Main
{
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number 1: ");
int n1 = sc.nextInt();
System.out.print("Enter number 2: ");
int n2 = sc.nextInt();
Recursion r = new Recursion();
r.hcf(n1,n2);
}
}
class Recursion
{
void hcf(int n1, int n2)
{
if (n1%n2==0)
{
System.out.print("HCF of Two numbers n1 and n2 is="+n2);
return;
}
else
{
hcf(n2, n1%n2);
}
}
}

QB-313 Write a Java program to create an array of objects


import java.util.*;
class ArrayOf
{
public static void main(String args[])
{
//create an array of product object
Product[] obj = new Product[5] ;
//create & initialize actual product objects using constructor
for(int i=0;i<5;i++){
obj[i] = new Product();
obj[i].set();
obj[i].display();}
}
}

class Product
{
int pro_Id;
String pro_name;
void set(){
Scanner sc=new Scanner(System.in);
pro_Id=sc.nextInt();
sc.nextLine();
pro_name=sc.next();
}
void display()
{
System.out.print("Product Id = "+pro_Id + " " + " Product Name = "+pro_name);
System.out.println();
}
}

QB-314 Write a Java Program to Create a class Account. It has three data member
account id, name and balance. Define method to assign value and display value. Define
method that search account number given by the user. If account number exists, print
detail of that account. Write a program using array of object. Declare at least 5 account
and print details.

import java.util.*;
class CO11
{
int id;
String name;
double balance;
Scanner sc =new Scanner(System.in);
void assign()
{
System.out.println("Enter id no:");
id = sc.nextInt();
sc.nextLine();
System.out.println("Enter Name:");
name = sc.nextLine();
System.out.println("Enter balance:");
balance = sc.nextDouble();

}
void display()
{
System.out.println("ID="+id);
System.out.println("Name="+name);
System.out.println("Balance="+balance);
}
int search()
{
System.out.println("enter value of id you want to search x");
int x = sc.nextInt();
return x;

}
}
class run
{
public static void main(String args[])
{
CO11 a[]=new CO11[3];
for(int i=0;i<3;i++)
{
a[i]=new CO11();
a[i].assign();
a[i].display();
}
CO11 s = new CO11();

int x = s.search();

for(int i=0;i<3;i++)
{
if(x==a[i].id)
{
a[i].display(); break;
}
else
{
System.out.println("Not found");
}
}
}
}

QB-315 Design a class named Fan to represent a fan. The class contains: - Three
constants named SLOW, MEDIUM and FAST with values 1, 2 and 3 to denote the fan
speed. - An int data field named speed that specifies the speed of the fan (default
SLOW). - A boolean data field named f_on that specifies whether the fan is on (default
false). - A double data field named radius that specifies the radius of the fan (default 4).
- A data field named color that specifies the color of the fan (default blue). - A no-arg
method that creates a default fan. - A parameterized method initializes the fan objects
to given values. - A method named display() will display description for the fan. If the
fan is on, the display() method displays speed, color and radius. If the fan is not on, the
method returns fan color and radius along with the message “fan is off”. Write a test
program that creates two Fan objects. One with default values and the other with
medium speed, radius 6, color brown, and turned on status true. Display the
descriptions for two created Fan objects.

class fann
{
final int SLOW=1,MEDIUM=2,FAST=3;
int Speed;
boolean f_on;
double radius;
String color;

void defaultfan()
{
Speed=1;
f_on=false;
radius=4;
color="blue";
}

void fann(int a,boolean b,double c,String d)


{
Speed=a;
f_on=b;
radius=c;
color=d;
}

void display()
{
if(f_on==true)
{
System.out.println("Speed = "+Speed);
System.out.println("Radius = "+radius);
System.out.println("Color = "+color);

}
else
{
System.out.println("Fan is off");
System.out.println("Radius = "+radius);
System.out.println("Color = "+color);
}
}

}
class run1
{
public static void main(String args[])
{
fann f1 = new fann();
fann f2 = new fann();
f1.defaultfan();
f2.fann(2,true,6,"brown");
f1.display();
f2.display();
}
}

QB-316 Define the Rectangle class that contains: Two double fields x and y that specify
the center of the rectangle, the data field width and height, A noarg method that creates
the default rectangle with (0,0) for (x,y) and 1 for both width and height. A
parameterized method creates a rectangle with the specified x,y,height and width. -A
method getArea() that returns the area of the rectangle. -A method getPerimeter() that
returns the perimeter of the rectangle. -A method contains(double x, double y) that
returns true if the specified point (x,y) is inside this rectangle. Write a test program that
creates two rectangle objects. One with default values and other with user specified
values. Test all the methods of the class for both the objects.

class rectangle14
{
double x,y,width,hight;

void defaultData()
{
x=0;
y=0;
width=1;
hight=1;
}
void set(double a,double b,double c,double d)
{
x=a;
y=b;
width=c;
hight=d;
}
void getArea()
{
System.out.println("Area = "+(width*hight));
}
void getPeri()
{
System.out.println("Perimeter = "+(2*(width+hight)));
}
boolean search(double x,double y)
{
if(((x<=(width/2)) || (x<=(-(width/2)))) && ((y<=(hight/2)) || (y<=(-
(hight/2)))) )
{
return true;
}
else
{
return false;
}
}
}
class run
{
public static void main(String args[])
{
rectangle14 r1 = new rectangle14();
rectangle14 r2 = new rectangle14();
r1.defaultData();
r2.set(0,0,2,2);
r1.getArea();
r1.getPeri();
r2.getPeri();
r2.getArea();
boolean q = r1.search(1.1,1);
System.out.println(q);
}
}
QB-317 Write a program to calculate factorial using recursion in Java

class RecursiveFactorial {

public static void main(String args[]) {


RecursiveFactorial rf = new RecursiveFactorial();
int fact = rf.factorial(5);
System.out.println(fact);
}

/* Java factorial program with recursion. */


int factorial(int num) {
if (num > 1) {
return num * factorial(num - 1);
}
else {
return 1;
}
}
}

QB-318 Write a Java program to calculate the power of a number like power (int
number, int power) like power (2, 3) should return 8.
import java.util.*;
class Pow1 {

// Function to calculate N raised to the power P


int power(int N, int P)
{
if (P == 0)
return 1;
else
return N * power(N, P - 1);
}

// Driver code
public static void main(String[] args)
{
Pow1 p=new Pow1();
Scanner sc=new Scanner(System.in);
int N = sc.nextInt();
int P = sc.nextInt();

System.out.println(p.power(N, P));
}
}
QB-319 Write a Java program to convert Decimal to binary using recursion
import java.util.*;
class DtoB
{

// Decimal to binary conversion


// using recursion
int find(int decimal_number)
{
if (decimal_number == 0)
return 0;

else

return (decimal_number % 2 + 10 *
find(decimal_number / 2));
}

// Driver Code
public static void main(String args[])
{
DtoB d=new DtoB();
Scanner sc=new Scanner(System.in);
int decimal_number = sc.nextInt();
System.out.println(d.find(decimal_number));
}
}
QB-320 Write a Java program to find GCD and LCM of two numbers using recursion
import java.util.*;
class GcdLcm {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
GcdLcm g = new GcdLcm();
System.out.print("Enter n1:");
int n1 = sc.nextInt();
System.out.print("Enter n2:");
int n2 = sc.nextInt();
int gcd = g.hcf(n1, n2);
System.out.println("G.C.D of "+n1+ " and "+n2+ " is: "+gcd);
System.out.printf("L.C.M of "+n1+ " and "+n2+ " is: "+((n1*n2)/gcd));
}

public int hcf(int n1, int n2)


{
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}

QB-321 Write a Java program to print fibbonacci series upto n terms using recursion
class RecursiveFibo {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Please enter first number to find GCD");
int n = sc.nextInt();
System.out.print(0+" "+1);//printing 0 and 1
RecursiveFibo f = new RecursiveFibo();
f.printFibo(n-2);//n-2 because 2 numbers are already printed
}

int a=0, b=1, c;


void printFibo(int count){
if(count>0){
c = a + b;
a = b;
b = c;
System.out.print(" "+c);
printFibo(count-1);
}
}
}
QB-362 Write a program which asks user to choose one option to find area using
method overloading. 1. To calculate area of circle 2. To calculate area of rectangle 3. To
calculate area of triangle Methods to find area of circle,area of rectangle & area of
triangle should be named as "area".

QB - 363 WAP that calculates area of circle, triangle and square using method
overloading.
class OverloadDemo
{
void area(float x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
}
void area(float x, float 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");
}
}
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
}
QB-364 Write a Java program to sort the numbers by using the concept of passing
arrays to methods
import java.util.*;
class Arr{
void sort(int a[]){
int temp;
for(int i = 0; i<a.length; i++){
for(int j = i+1;j<a.length;j++){
if(a[i]>a[j]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void display(int a[]){
for(int i = 0; i<a.length; i++){
System.out.print(a[i]+" ");
}
}
}
class Run{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Arr aa = new Arr();
System.out.println("How many Elements:");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter Elements:");
for(int i = 0; i<a.length; i++){
a[i] = sc.nextInt();
}
aa.sort(a);
System.out.println("After sorting Elements:");
aa.display(a);
}
}

QB- 365 Write a Java program to display elements of one dimensional array using
passing arrays to methods.

class array1D{
void display(int[] array){
System.out.println("Yor array is:");
for(int q: array)
System.out.print(q);
}
void display(int[][] array){
System.out.println("Yor array is:");
for(int j[]: array)
for(int i: j)
System.out.print(i);
}
}
class RunD{
public static void main(String[] args) {
int[] a = {11,12,13};
int[][] b = {{1,2,3},{1,2,3},{1,2,3}};
array1D aone = new array1D();
aone.display(a);
aone.display(b);
}
}

QB- 366 Write a Java program to find sum of n numbers by using the concept of call
by value
import java.util.*;
class Arr{
Scanner sc = new Scanner(System.in);
int sum(int n){
int sum=0;
for(int i = 1; i<= n; i++){
System.out.print("Enter elements: ");
int s = sc.nextInt();
sum = sum + s;
}
return sum;
}
}
class Run{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Arr aa = new Arr();
System.out.println("How many Elements:");
int n = sc.nextInt();
System.out.println("Sum of "+n+ "numbers:"+aa.sum(n));
}
}

QB- 367 Write a Java program to find the power of number using passing an object to
the method(i.e num=5 and power=3 then ans is5^3 that is 125)
import java.util.*;
class Ex_8_367{

int num,pow,result=1;

Ex_8_367 findPow(Ex_8_367 obj){


int i;
Ex_8_367 obj1=new Ex_8_367();
for(i=1;i<=obj.pow;i++){
obj1.result=obj1.result*obj.num;
}

return obj1;

}
}
class run{
public static void main(String args[])
{
Ex_8_367 obj2=new Ex_8_367();
Ex_8_367 obj3=new Ex_8_367();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number ");
obj2.num=sc.nextInt();
System.out.println("Enter the value of Power");
obj2.pow=sc.nextInt();

obj3=obj2.findPow(obj2);
System.out.println("power of given number is
"+obj3.result);
}
}

QB- 368 Write a Java program to find GCD of two numbers using passing an object to
the method(i.e a=4 and b=6 then GCD is 2)
import java.util.*;
class EX_8_368
{
int a,b;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER a and b=");
a=sc.nextInt();
b=sc.nextInt();
}
void gcd(EX_8_368 ob1)
{
while(ob1.a!=ob1.b)
{
if(ob1.a>=ob1.b)
ob1.a=ob1.a-ob1.b;
else
ob1.b=ob1.b-ob1.a;
}
System.out.println("GCD="+ob1.a);
}
}
class Run
{
public static void main(String args[])
{
EX_8_368 ob1 = new EX_8_368();
EX_8_368 ob2 = new EX_8_368();
ob1.input();
ob2.gcd(ob1);
}
}

QB- 369 Write a Java method that accept three integers and check whether they are
consecutive are not. Return true or false. Expected Output: Input the first number: 15
Input the second number: 16 Input the third number: 17 Check whether the three said
numbers are consecutive or not!true
import java.util.Scanner;
class Main {
public static void main(String[] args)
{
Main m=new Main();
Scanner in = new Scanner(System.in);
System.out.print("Input the first number: ");
int x = in.nextInt();
System.out.print("Input the second number: ");
int y = in.nextInt();
System.out.print("Input the third number: ");
int z = in.nextInt();
System.out.print("Check whether the three said numbers are
consecutive or not!");
System.out.println(m.test(x,y,z)); }

boolean test(int x, int y, int z)


{
int max_num = Math.max(x, Math.max(y, z));
int min_num = Math.min(x, Math.min(y, z));
int middle_num = x+y+z - max_num - min_num;
return (max_num - middle_num) == 1 && (middle_num - min_num ==
1);
}
}
QB- 370 Write a program which takes five numbers as command line argument from
user, store them in one dimensional array and create mehod to display count of Positive
numbers

import java.util.Scanner;
public class CountPositiveNegative3 {
public static void main(String[] args)
{
int Size, i;
int positiveCount = 0, negativeCount = 0;
Scanner sc = new Scanner(System.in);

System.out.print(" Please Enter Number of elements in an array : " );


Size = sc.nextInt();

int[] a = new int[Size];

System.out.print(" Please Enter " + Size + " elements of an Array : ");


for (i = 0; i < Size; i++)
{
a[i] = Integer.parseInt(args[i]); //sc.nextInt();
}

positiveCount = CountPositive(a, Size);


negativeCount = CountNegative(a, Size);
System.out.println("\n Total Number of Positive Numbers in this Array = "
+
positiveCount);
System.out.println(" Total Number of Negative Numbers in this Array = " +
negativeCount);

}
public static int CountPositive(int [] a, int Size)
{
int i, positiveCount = 0;
System.out.print("\n List of Positive Numbers in this Array are :" );
for(i = 0; i < Size; i++)
{
if(a[i] >= 0)
{
System.out.print(a[i] +" ");
positiveCount++;
}
}
return positiveCount;
}
public static int CountNegative(int [] a, int Size)
{
int i, negativeCount = 0;
System.out.print("\n List of Negative Numbers in this Array are :" );
for(i = 0; i < Size; i++)
{
if(a[i] < 0)
{
System.out.print(a[i] + " ");
negativeCount++;
}
}
return negativeCount;
}
}
QB-371 Create a class comparison which has compare() method which compare two
integer value, character value and double value using method overloading

import java.util.Scanner;

class Compare
{
void compare(int a, int b) {

if (a == b) {
System.out.println("value of "+a+" and "+b+" are same");
}
else {
System.out.println("value of "+a+" and "+b+" are not same");
}

void compare(char a, char b) {


int x = (int)a;
int y = (int)b;

if (a == b) {
System.out.println("value of "+a+" and "+b+" are same");
}
else {
System.out.println("value of "+a+" and "+b+" are not same");
}

void compare(double a, double b) {

if (a == b) {
System.out.println("value of "+a+" and "+b+" are same");
}
else {
System.out.println("value of "+a+" and "+b+" are not same");
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
Compare obj = new Compare();

System.out.print("Enter first integer: ");


int n1 = in.nextInt();
System.out.print("Enter second integer: ");
int n2 = in.nextInt();
obj.compare(n1, n2);

System.out.print("Enter first character: ");


char c1 = in.next().charAt(0);
System.out.print("Enter second character: ");
char c2 = in.next().charAt(0);
in.nextLine();
obj.compare(c1, c2);

System.out.print("Enter first double: ");


double s1 = in.nextDouble();
System.out.print("Enter second double: ");
double s2 = in.nextDouble();
obj.compare(s1, s2);
}
}
QB-372 Write a Java program to find Even number and Odd number from given Array
using the concept of passing arrays to methods
class OddEven{
public static void main(String args[]){
int arr[]={1,2,5,6,3,2};
OddEven oe = new OddEven();
oe.oddprint(arr);
oe.evenprint(arr);
}
void oddprint(int a[]){
System.out.println("Odd Numbers:");
for(int i=0;i<a.length;i++){
if(a[i]%2!=0){
System.out.println(a[i]);
}
}
}

void evenprint(int a[]){


System.out.println("Even Numbers:");
for(int i=0;i<a.length;i++){
if(a[i]%2==0){
System.out.println(a[i]);
}
}
}
}
QB- 373 Write a Java program to display elements of one dimensional array using
passing arrays to methods
class array1D{
void display(int[] array){
System.out.println("Yor array is:");
for(int q: array)
System.out.print(q);

}
void display(int[][] array){
System.out.println("Yor array is:");
for(int j[]: array)
for(int i: j)
System.out.print(i);
}
}
class RunD{
public static void main(String[] args) {
int[] a = {11,12,13};
int[][] b = {{1,2,3},{1,2,3},{1,2,3}};

array1D aone = new array1D();

aone.display(a);
aone.display(b);
}
}
QB-374 Write a Java program to find sum of first n odd numbers by using the concept
of call by value.

class FindSumOdd {

int sumOfOddNums(int n) {
return n * n;
}

public static void main(String[] args) {


int n = 10;
FindSumOdd obj = new FindSumOdd();
int oddSum = obj.sumOfOddNums(n);

System.out.println("Sum of First " + n


+ " Odd numbers = " + oddSum);
}
}

QB-375 Write a Java program to add 5 in original value of array elements and display
modified elements of one dimensional array using pass by value mechanism.

class Array {

public static void main(String[] args) {


int a[] = {10, 20, 30, 40, 50};
Array obj = new Array();
obj.modifyArray(a);
for (int i = 0; i < a.length; i++) {
obj.print(a[i]);
}
}

void modifyArray(int a[]) {


for (int i = 0; i < a.length; i++) {
a[i] += 5;
}
}

void print(int a) {
System.out.println(a);
}
}

QB-376 Write a Java program to change original value, which is given from user by
using appropriate Parameter passing technique.

class CallByReference {

int a, b;

void set(int x, int y) {


a = x;
b = y;
}

void ChangeValue(CallByReference obj) {


obj.a += 10;
obj.b += 20;
}
}

public class Main {

public static void main(String[] args) {

CallByReference object
= new CallByReference();
object.set(10, 20);
System.out.println("Value of a: " + object.a
+ " & b: " + object.b);

object.ChangeValue(object);

System.out.println("Value of a: " + object.a


+ " & b: " + object.b);
}
}

QB-377 Write a Java program to Swap two values, which is given from user by using
call by value

import java.util.Scanner;
class SwapNumber
{
public static void main(String args[])
{
int x, y;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
x = sc.nextInt();
System.out.print("Enter the second number: ");
y = sc.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
swap(x, y);
}
public static void swap(int a, int b)
{
a = a + b;
b = a - b;
a = a - b;
System.out.println("After Swapping\nx = "+a+"\ny = "+b);
}
}

QB-378 Write a Java program to Swap two values, which is given from user by using
call by reference.

class CallByReference {

int a, b;

void set(int x, int y) {


a = x;
b = y;
}

void swapvalue(CallByReference obj) {


int temp;
temp = obj.a;
obj.a = obj.b;
obj.b = temp;
}
}

public class Main {

public static void main(String[] args) {

CallByReference object
= new CallByReference();
object.set(10, 20);
System.out.println("Value of a: " + object.a
+ " & b: " + object.b);
object.swapvalue(object);

System.out.println("Value of a: " + object.a


+ " & b: " + object.b);
}
}

QB-379 Write a program using call by reference concept to read an array of integers
and print its elements in reverse order.

class Array{
public static void main(String[] args) {
int a[] = {10,20,30,40,50};
Array obj = new Array();
obj.print(a);
}

void print(int a[]){


for (int i = a.length-1; i>=0;i--){
System.out.println(a[i]);
}
}
}

QB-380 Write a Java program to copy one array into another array using call by
referrence.

class Array{
public static void main(String[] args) {
int a[] = {10,20,30,40,50};
int b[] = a;
Array obj = new Array();
obj.print(b);
}

void print(int a[]){


for (int i = 0; i<a.length;i++){
System.out.println(a[i]);
}
}
}

QB-381 Write a Java program to add two distance using class.


import java.util.*;
class CO7
{
int km,m;
Scanner sc = new Scanner(System.in);
void get()
{
System.out.println("Eneter value of distance in KM:");
km= sc.nextInt();

System.out.println("Eneter value of distance in meter if any otherwise


enter 0:");
m = sc.nextInt();
}

void show(int a,int b)


{

System.out.println("Distacne = "+a+"."+b+"KM");
}

CO7 add(CO7 obj1,CO7 obj2)


{
CO7 d4 = new CO7();
d4.km = obj1.km + obj2.km;
d4.m = obj1.m + obj2.m;

if(d4.m>=1000)
{
d4.km+=1;
d4.m-=1000;
}
return d4;
}
}
class run
{
public static void main(String args[])
{
CO7 d1 = new CO7();
CO7 d2 = new CO7();
CO7 d3 = new CO7();
CO7 d4 = new CO7();
d1.get();

d1.show(d1.km,d1.m);

d2.get();
d2.show(d2.km,d2.m);

d4 = d3.add(d1,d2);

d4.show(d4.km,d4.m);

}
}
QB-382 Write a Java Program to Create a class Account. It has three data member
account id, name and balance. Define method to assign value and display value. Define
method that search account number given by the user by passing array to method. If
account number exists, print detail of that account. Write a program using array of
object. Declare at least 5 account and print details.
import java.util.*;
class Ex_8_382 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of accounts: ");
int n = sc.nextInt();
Account1 [] ac = new Account1[n];
for (int i=0;i<ac.length;i++)
{
ac[i]= new Account1();
System.out.print("Enter Account Number: ");
int p = sc.nextInt();
System.out.print("Account Holder name: ");
sc.nextLine();
String q = sc.nextLine();
System.out.print("Account Balance: ");
int r = sc.nextInt();
ac[i].setData(p,q,r);
}

Account1 obj = new Account1();


int a = obj.accountSearch();

boolean c=false;
for (int i = 0; i < ac.length; i++) {
if ((ac[i].id==a)) {
c = true;
ac[i].getData();
}
}
if (!c)
System.out.println("Account details are not found");
}
}

class Account1
{
int id;
String name;
double bal;
void setData(int x, String y, double z)
{
id =x;
name = y;
bal = z;
}
void getData()
{
System.out.println("----Account Details----");
System.out.println("Account id: "+id);
System.out.println("Account Holder name: "+name);
System.out.println("Account Balance: "+bal);
}
int accountSearch()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Account number which you want to
search: ");
int a = sc.nextInt();
return a;
}
}

QB-383 Write a java program to find min and max values from a given array using
passing arrays to methods.
import java.util.Scanner;
class Array {
public int max(int[] array) {
int max = 0;

for (int i = 0; i < array.length; i++) {


if (array[i] > max) {
max = array[i];
}
}
return max;
}
public int min(int[] array) {
int min = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
}
class Ex_8_383
{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the array range: ");
int n= sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array: ");

for(int i=0; i< arr.length; i++) {


arr[i] = sc.nextInt();
}
Array m = new Array();
System.out.println("Maximum value in the array is:
"+m.max(arr));
System.out.println("Minimum value in the array is:
"+m.min(arr));
}
}

QB-384 Define a class Time with hours and minutes as two instance data members, add
necessary instance methods to initialize and display data of class. Define a instance
method sum() which adds two Time objects. Invoke the statements like T3.sum(T1,T2)
in main ().
import java.util.*;
public class Ex_384 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter time-1 in hour");
int h1=sc.nextInt();
System.out.println("Enter time-1 in minutes");
int m1=sc.nextInt();
System.out.println("Enter time-2 in hour");
int h2=sc.nextInt();
System.out.println("Enter time-2 in minutes");
int m2=sc.nextInt();
Time t1= new Time();
Time t2= new Time();
Time t3= new Time();
t1.input(h1,m1);
t2.input(h2,m2);
t3.sum(t1,t2);
t3.print();
}
}

class Time
{
int hour;
int min;

void input(int h,int m)


{
hour=h;
min=m;
}

void sum(Time t1,Time t2)


{
min= t1.min+t2.min;
hour = (t1.hour+t2.hour)+(min/60);
min = min%60;
}

void print()
{
System.out.println("Total hours:"+hour);
System.out.println("Total minutes:"+min);
}
}

QB-385 Declare a class called coordinate to represent 3 dimensional Cartesian


coordinates(x, y, and z) define following method. - Initialize Method - Display to print
values of members - Add_coordinates, to add three such coordinates object to produce a
resultant coordinates object. - Main , to show use of above method
import java.util.*;
public class Ex_8_385 {
public static void main(String[] args) {
Coordinate c1= new Coordinate();
Coordinate c2= new Coordinate();
Coordinate c3= new Coordinate();
Coordinate r= new Coordinate();

Scanner sc = new Scanner(System.in);


System.out.println("Enter first point's coordinates:");
int x1= sc.nextInt();
int y1= sc.nextInt();
int z1= sc.nextInt();
c1.input(x1,y1,z1);

System.out.println("\nEnter Second point's coordinates:");


int x2= sc.nextInt();
int y2= sc.nextInt();
int z2= sc.nextInt();
c2.input(x2,y2,z2);

System.out.println("\nEnter Third point's coordinates:");


int x3= sc.nextInt();
int y3= sc.nextInt();
int z3= sc.nextInt();
c3.input(x3,y3,z3);
c1.display();
c2.display();
c3.display();
Coordinate r1 = r.Add_coordinates(c1,c2,c3);

System.out.print("\nThe resultant coordinate is: "+r1.x+"


"+r1.y+" "+r1.z);

}
}
class Coordinate
{
int x,y,z;
void input (int a, int b, int c)
{
x=a;
y=b;
z=c;
}

void display()
{
System.out.format("\nCoordinates of point is %d, %d,
%d",x,y,z);
}

Coordinate Add_coordinates(Coordinate c1,Coordinate c2,Coordinate


c3)
{
Coordinate c4 =new Coordinate();
c4.x=c1.x+c2.x+c3.x;
c4.y=c1.y+c2.y+c3.y;
c4.z=c1.z+c2.z+c3.z;
return c4;
}
}

QB- 386 Write a java program to reverse elements of array using logic of swapping
elements. Here, use concept of passing array as argument to method.

class arrayReverse
{
// function swaps the array's first element with last element, second
element with
void reverse (int a[], int n)
{
int i, k, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
System.out.println("Reversed array is: \n");
for (k = 0; k < n; k++) {
System.out.println(a[k]);
}
}

public static void main(String[] args) {


int[] arr = { 10, 20, 30, 40, 50 };
arrayReverse obj = new arrayReverse();
obj.reverse(arr,arr.length);
} }

You might also like