Page 467 - Cs_withBlue_J_C11_Flipbook
P. 467
The output of the preceding program is as follows:
Enter radius of cone
4
Enter height of cone
7
Volume of cone: 117.30133333333333
Enter radius of cylinder
6
Enter height of cylinder
8
Volume of cylinder: 984.896
7. Write a program to create a package Eleven containing class CountOne having the following specifications.
Package : Eleven
Class name : CountOne
Data Members
int n : Stores number as binary
Member Methods
CountOne (int x) : Constructor to assign the data members
int count() : Counts and returns number of 1’s in a binary number
Write a second class Evil in package Orange_prog to import class CountOne of package Eleven and use the method count() to
print if a number is an evil number or not. An evil number is a non-negative number that has an even number of 1s in its binary
expansion.
Example: 5 as its binary 101 has 1’s count = 2
9 as its binary 1001 has 1’s count = 2
The class description is given below:
Package : Orange_prog
Class name : Evil
Data Members
int num, bin : Stores number and its binary equivalent
Member Methods
void accept() : Accepts a positive integer decimal number
int tobin(int a) : Converts the decimal number to binary and returns the result
void check() : Checks if the number is an evil number or not using method count() in class
CountOne of package Eleven.
Ans. package Eleven;
public class CountOne
{
int n;
public CountOne(int x)
{
n=x;}
public int count()
{
int x=n,c=0;
while(x>0)
{
c+= x%10==1?1:0;
x=x/10;
}
465
Packages 465

