0% found this document useful (0 votes)
18 views4 pages

Comp 25.05

The document contains two programming assignments for a class named Mankirat Kaur, focusing on function overloading in Java. The first program calculates the volume of a cube, sphere, and cuboid using overloaded methods, while the second program performs various calculations based on user input, including squaring, cubing, and comparing strings. Each program includes variable descriptions and example implementations.

Uploaded by

scifi0740
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)
18 views4 pages

Comp 25.05

The document contains two programming assignments for a class named Mankirat Kaur, focusing on function overloading in Java. The first program calculates the volume of a cube, sphere, and cuboid using overloaded methods, while the second program performs various calculations based on user input, including squaring, cubing, and comparing strings. Each program includes variable descriptions and example implementations.

Uploaded by

scifi0740
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/ 4

COMPUTER ASSIGNMENT-II

Name- Mankirat Kaur Submission Date:25/05/2023

Class- X SMK

Prog. 1 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


4
Volume of a sphere(vs)= 3 *π*r*r*r

Volume of a cuboid(vcd)=l*b*h

//To find the volume of a cube, sphere and cuboid using function overloading
import java.util.*;
public class Compute
{
double vc=0.0D,vs=0.0D,vcd=0.0D;
void volume(int s)
{
vc=s*s*s//Calculates area of a cube
System.out.println(“The volume of a cube=”+vc);
}
void volume(float r)
{
vs=4/3*22/7*r*r*r;//Calculates area of a sphere
System.out.println(“The volume of a sphere=”+vs);
}
void volume(int l, int b, int h)
{
vcd=l*b*h;//Calculates area of a cuboid
System.out.println(“The volume of a 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 a
cuboid”);
s=in.nextInt();
r=in,nextFloat();
l=in.nextInt();
b=in.nextInt();
h=in.nextInt();
Compute o=new Compute();
ob.volume(s);
ob.volume(r);
ob.volume(l,b,h);
}
}
VARIABLE LIST

S.no. Variable Description


1 vc Calculates area of a cube
2 vs Calculates area of a sphere
3 vcd Calculates area of a cuboid
4 l Accepts length
5 b Accepts breadth
6 h Accepts height
7 s Accepts side
8 r Accepts radius
Prog. 2 Design a class to overload a function num_cal() as follows:
i. void num_cal(int num,char ch) with one integer argument and one character
argument. It computes the square of an integer if choice ch is ‘s’ otherwise,
computes its cube.
ii. void num_cal(int a, int b, char ch) with two integer arguments and one
character argument. It computes the product of integer arguments if ch is ‘p’
else adds the integers.
iii. void num_cal(String str1, String str2) with two String arguments prints the
two Strings are equal or not.

//To perform the different tasks by using Overloading


class Choice
{
void num_cal(int num, char ch)
{
if (ch==‘s’)
System.out.println(“The square of the number=”+(num*num));
else
System.out.println(“The cube of the number=”+(num*num*num));
}
void num_cal(int a, int b, char ch)
{
if (ch==‘p’)
System.out.println(“The product of the numbers=”+(a*b));
else
System.out.println(“The sum of the numbers=”+(a+b));
}
void num_cal(String str1, String str2)
{
if(str1.equals(str2))//Checks the equality between two strings
System.out.println(“Two strings are equal”);
else
System.out.println(“Two strings are not equal”)
}
}
VARIABLE LIST
1 num Accepts an integer
2 ch Accepts a character
3 str1 Accepts a string

You might also like