0% found this document useful (0 votes)
7 views11 pages

ch-8_PB-program_603-630

The document contains multiple Java programs demonstrating method overloading, including calculating areas of different shapes, implementing a calculator, comparing values, and managing arrays of objects. It covers various functionalities such as sorting, searching, and manipulating arrays, along with user input handling. Each program is structured with classes and methods to encapsulate the logic and operations performed.

Uploaded by

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

ch-8_PB-program_603-630

The document contains multiple Java programs demonstrating method overloading, including calculating areas of different shapes, implementing a calculator, comparing values, and managing arrays of objects. It covers various functionalities such as sorting, searching, and manipulating arrays, along with user input handling. Each program is structured with classes and methods to encapsulate the logic and operations performed.

Uploaded by

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

/* 603:

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".*/

class Shape
{
void area(double r)
{
double a=Math.PI*r*r;
System.out.println("Area of circle="+a);
}
void area(int l, int b)
{
double a=l*b;
System.out.println("Area of Rectangle="+a);
}
void area(double b, double h)
{
double a=0.5*b*h;
System.out.println("Area of Triangle="+a);
}
}
class Main1
{
public static void main(String[] args) {
Shape s=new Shape();
s.area(10);
s.area(10,20);
s.area(10.0,20.0);

}
}

/*604:Create a program that implements a calculator with overloaded


methods for addition, subtraction, multiplication, and division.The
calculator should have methods like calculate(int a, int b),
calculate(double a, double b), and so on to perform the respective
operations based on the data types of the input parameters. */

class Calculator
{
void calculate(int a, int b)
{
System.out.println("Sum="+(a+b));
}
void calculate(float a, float b)
{
System.out.println("Sub="+(a-b));
}
void calculate(long a, long b)
{
System.out.println("Mul="+(a*b));
}
void calculate(double a, double b)
{
System.out.println("Div="+(a/b));
}
}
class Main2
{
public static void main(String[] args) {
Calculator c =new Calculator();
c.calculate(10, 20);
c.calculate(10f,20f );
c.calculate(10l, 20l);
c.calculate(10.0, 20.0);

}
}

/* 605:Create a class comparison which has compare() method which


compare two integer value, character value and double value using
method overloading*/

class Comparison {

// Compare two integer values


void compare(int a, int b) {
System.out.println(":::Integer Comparison Result:::");
if(a>b)
System.out.println("Int a is greater than Int b");
else if(a<b)
System.out.println("Int a is smaller than Int b");
else
System.out.println("Int a is equals to Int b");
}

// Compare two character values


void compare(char a, char b) {
System.out.println(":::Character Comparison Result:::");
if(a>b)
System.out.println("char a is greater than char b");
else if(a<b)
System.out.println("char a is smaller than char b");
else
System.out.println("char a is equals to char b");
}

// Compare two double values


void compare(double a, double b) {
System.out.println(":::Double Comparison Result:::");
if(a>b)
System.out.println("Double a is greater than Double b");
else if(a<b)
System.out.println("Double a is smaller than Double b");
else
System.out.println("Double a is equals to Double b");
}

public static void main(String[] args) {


Comparison com = new Comparison();

// Integer comparison
com.compare(10, 10);

// Character comparison
com.compare('a', 'a');

// Double comparison
com.compare(3.14, 3.14);

}
}

/*623:Write a Java program to create an array of objects */

class Employee
{
String emp_name;

public void setEmp_name(String name) {


emp_name = name;
}

}
class Main3
{
public static void main(String[] args) {
Employee [] emp=new Employee[2];
emp[0]=new Employee();
emp[0].setEmp_name("Nisarg");
emp[1]=new Employee();
emp[1].setEmp_name("Apurva");
System.out.println("Name of emp1="+emp[0].emp_name);
System.out.println("Name of emp2="+emp[1].emp_name);
}
}

/*624: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). */

import java.util.Scanner;

class Student
{
int Roll_No;
String Name;
String Mobile_No;
void set(int roll_No, String name, String mobile_No) {
Roll_No = roll_No;
Name = name;
Mobile_No = mobile_No;
}
void get()
{
System.out.println("Roll.No="+Roll_No);
System.out.println("Name="+Name);
System.out.println("Mobile No="+Mobile_No);
System.out.println();
}
void search(String name,Student [] s1)
{
for(int i=0;i<s1.length;i++)
{
if(s1[i].Name.equals(name))
{
s1[i].get();
}
}
}
}
class Main4
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Student [] s=new Student[5];
for(int i=0;i<s.length;i++)
{
s[i]=new Student();
}
//Entering details of Student
for(int i=0;i<s.length;i++)
{
System.out.println(":::::Enter Details of Student
["+(i+1)+"] ::::::");
System.out.print("Entre Roll.No=");
int r=sc.nextInt();
System.out.print("Entre Name=");
String n=sc.next();
System.out.print("Entre Mobile=");
String mo=sc.next();
System.out.println();
s[i].set(r, n, mo);
}
//printing details of student
System.out.println(":::::Printing Details of Students:::::");
for(int i=0;i<s.length;i++)
{
System.out.println(":::::Details of Student ["+(i+1)+"]
::::::");
s[i].get();
}

}
}

/*625: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.Scanner;

class Account
{
String AccountId;
String Name;
double Balance ;
void set(String accountId, String name, double balance) {
AccountId = accountId;
Name = name;
Balance = balance;
}
void get()
{
System.out.println("ID="+AccountId);
System.out.println("Name="+Name);
System.out.println("Balance="+Balance);
System.out.println();
}
void search(String Acc, Account [] a)
{
for(int i=0;i<a.length;i++)
{
if(a[i].AccountId.equals(Acc))
{
a[i].get();
}
}
}
}
class Main6
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Account [] ac=new Account[5];
for(int i=0;i<ac.length;i++)
{
ac[i]=new Account();
}
//Entering details of Student
for(int i=0;i<ac.length;i++)
{
System.out.println(":::::Enter Details of Account
["+(i+1)+"] ::::::");
System.out.print("AccountId=");
String id=sc.next();
System.out.print("Entre Name=");
String n=sc.next();
System.out.print("Entre Balance=");
double bal=sc.nextDouble();
System.out.println();
ac[i].set(id, n, bal);
}
System.out.println("Enter Account Id to Search Details");
String id=sc.next();
Account ac1=new Account();
ac1.search(id,ac);
}
}

/*626:Write a Java program to sort the numbers by using the concept


of passing arrays to methods */

import java.util.Arrays;
import java.util.Scanner;

class Sorting
{
void display(int [] a)
{
Arrays.sort(a);
for(int i=0;i<a.length;i++)
{
System.out.println("Element at arr["+i+"]="+a[i]);
}
}
}
class Main11
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int []a=new int[]{26,-32,1,56,2,-1,42,39};
Arrayarg arr=new Arrayarg();
arr.display(a);
}
}

/*627:Write a Java program to display elements of one dimensional


array using passing arrays to methods */

import java.util.Scanner;

class Arrayarg
{
void display(int [] a)
{
for(int i=0;i<a.length;i++)
{
System.out.println("Element at arr["+i+"]="+a[i]);
}
}
}
class Main7
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int []a=new int[]{3,4,8,12,22};
Arrayarg arr=new Arrayarg();
arr.display(a);
}
}

/*628:Write a Java program to find Even number and Odd number from
given Array using
the concept of passing arrays to methods */
import java.util.Scanner;

class EvenOdd
{
void display(int [] a,int []even,int[] odd)
{
int evenIndex=0;
int oddIndex=0;
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
even[evenIndex++]=a[i];
}
else
{
odd[oddIndex++]=a[i];
}
}
}
}
class Main8
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int []a=new int[]{3,4,8,12,22,31,42,99,101,21};
int [] even=new int[a.length];
int [] odd=new int[a.length];
EvenOdd eo=new EvenOdd();
eo.display(a,even,odd);
System.out.println("::::Even Numbers from array::::");
for (int e : even) {
if(e!=0)
{
System.out.println("Num="+e);
}
}
System.out.println("::::Odd Numbers from array::::");
for (int o : odd) {
if(o!=0)
{
System.out.println("Num="+o);
}
}
}
}

/*629:Write a java program to find min and max values from a given
array using passing arrays to methods. */

import java.util.Scanner;

class MaxMin
{
void maxMin(int [] a)
{
int max=a[0];
int min=a[0];
for(int i=0;i<a.length;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}

}
System.out.println("Max Element from Array="+max);
System.out.println("Min Element from Array="+min);
}
}
class Main9
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter size of array=");
int size=sc.nextInt();
int []a=new int[size];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Element at arr["+i+"]=");
a[i]=sc.nextInt();
}
MaxMin mm=new MaxMin();
mm.maxMin(a);

}
}

/*630:Write a java program to reverse elements of array using logic


of swapping elements. Here, use concept of passing array as argument
to method. method. Use Scanner class to enter Array elements. */

import java.util.Scanner;

class Reverse
{
void rev(int [] a)
{
int n=a.length;
for(int i=0;i<n/2;i++)
{
for(int j=n-1-i;j>i;j--)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
break;
}

}
}
}
class Main10
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter size of array=");
int size=sc.nextInt();
int []a=new int[size];
for(int i=0;i<a.length;i++)
{
System.out.print("Enter Element at arr["+i+"]=");
a[i]=sc.nextInt();
}
Reverse r=new Reverse();
r.rev(a);
System.out.println("After Reverse");
for (int a1: a) {

System.out.println(a1);
}

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy