0% found this document useful (0 votes)
14 views6 pages

Assignment 4

The document describes the design of multiple classes in Java that overload methods for specific functionalities. The classes include 'print' for checking spy numbers and generating patterns, 'Pattern' for generating character patterns, 'Form' for summing even digits and comparing numbers, and 'Volume' for calculating volumes of different shapes. Each class contains method definitions, examples, and variable descriptions for clarity.

Uploaded by

War Robot 69
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)
14 views6 pages

Assignment 4

The document describes the design of multiple classes in Java that overload methods for specific functionalities. The classes include 'print' for checking spy numbers and generating patterns, 'Pattern' for generating character patterns, 'Form' for summing even digits and comparing numbers, and 'Volume' for calculating volumes of different shapes. Each class contains method definitions, examples, and variable descriptions for clarity.

Uploaded by

War Robot 69
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/ 6

ASSIGNMENT 4

1) Design a class to overload method print() as follows:


void print(int n) – check whether a number is spy number or not, i.e., if the sum
and product of the digits of the number are same.
Example: 22  sum = 2 + 2 = 4, product = 2 * 2 = 4.
void print(int x, int n) – x1 + x3 + x5 + x7 + ... + x19
void print() – 99,80,63,…,0

public class print


{
void print(int n)
{
int d, sum=0, pro=1;
while(n!=0)
{
d=n%10;
sum+=d;
pro*=d;
n/=10;
}
if(pro==sum)
System.out.println("The number is a spy number.");
else
System.out.println("The number is not a spy number.");
}
void print(int x, int n)
{
double sum=0;
for(int i=1; i<=n; i+=2)
sum+=Math.pow(x,i);
System.out.println("Result: "+sum);
}
void print()
{
System.out.print("99");
for(int i=99; i>=0; )
{
int d=19;
i-=d;
if(i>=0)
System.out.print(", "+i);
d-=2;
}
}
public static void main()
{
print p=new print();
p.print(10);
p.print(7,78);
p.print();
}
}

Variable Description Table


Variable Name Data Type Function
n int In method 1: number to check for spy number.

In method 2: number of terms in the series.


d int In method 1: stores each digit of n.
In method 3: value used for subtraction from i.
sum int/double In method 1: sum of digits of n.
In method 2: sum of odd powers of x (as
double).
pro int Product of digits of n in method 1
x int Base number used for power calculation
i int Loop counter in all methods

2) Design a class to overload method execute() as follows:


void execute(int n) –
&&&&&
&&&
&
void execute(int x, int y) –
@#@#@
@#@#@
@#@#@
void execute() –
1
333
55555
public class Pattern
{
void execute(int n)
{
for(int i=n; i>=1; i-=2)
{
for(int j=i; j>=1; j--)
System.out.print("& ");
System.out.println();
}
}
void execute(int x, int y)
{
for(int i=1; i<=x; i++)
{
for(int j=1; j<=y; j++)
{
if(j%2==0)
System.out.print("# ");
else
System.out.print("@ ");
}
System.out.println();
}
}
void execute()
{
for(int i=1; i<=5; i+=2)
{
for(int j=1; j<=i; j++)
System.out.print(i+" ");
System.out.println();
}
}
public static void main()
{
Pattern e=new Pattern();
e.execute(5);
e.execute(4,5);
e.execute();
}
}
Variable Description Table
Variable Name Data Type Function
n int Number of rows for the & pattern.
x int Number of rows for the @/# pattern
y int Number of columns for the @/# pattern.
i int Loop contol variable for outer loop
j int Loop control variable for inner loop.

3) Define a class to overload method form() as follows:


int form(int n) – display sum of even digits of a number.
Example: 2368  2 + 6 + 8 = 16
double form(int a, int b) – compare the first number with the second; if first is
larger than the second, return division of two numbers, else return the product
of both.
int form(char ch1) – return the ASCII code of character.

public class Form


{
int form(int n)
{
int d, sum=0;
while(n!=0)
{
d=n%10;
if(d%2==0)
sum+=d;
n=n/10;
}
return sum;
}
double form(int a, int b)
{
if(a>b)
return (double)a/b;
else
return (double)a*b;
}
int form(char ch1)
{
return (int)ch1;
}
public static void main()
{
Form f=new Form();
System.out.println(f.form(2386));
System.out.println(f.form(1872, 1029));
System.out.println(f.form(356, 1928));
System.out.println(f.form('y'));
}
}

Variable Description Table


Variable Name Data Type Function
n int Used for extracting digits and summing even ones.
d int Stores last digit of n
sum int Stores sum of even digits.
a int Stores first number.
b int Stores secons number
ch1 char Stores character.

4) Design a class to overload method volume() as follows:


double volume(int, int, int)  l x b x h
double volume(int, int)  3.14 x r2 x h
double volume(int)  4/3 x 3.14 x r3\
public class Volume
{
double volume(int l, int b, int h)
{
return l*b*h;
}
double volume(int r, int h)
{
return 3.14*r*r*h;
}
double volume(int r)
{
return (4.0/3)*3.14*r*r*r;
}
public static void main()
{
Volume v=new Volume();
System.out.println(v.volume(4, 3, 2));
System.out.println(v.volume(4, 3));
System.out.println(v.volume(4));
}

Variable Description Table


Variable Name Data Type Function
l int Stores length of a cuboid.
b int Stores breadth of a cuboid.
h int Stores height of a cuboid in method 1 and of cylinder in
method 2.
r int Stores radius of cylinder in method 2 and of circle in
method 3

You might also like