Page 256 - Cs_withBlue_J_C11_Flipbook
P. 256
2
2
68 = (6) + (8) = 36 + 64 = 100
2
2
2
100 = (1) + (0) + (0) = 1 + 0 + 0 = 1
Hence, 28 is a happy number.
Example 2:
2
2
12 = (1) + (2) = 1 + 4 = 5
Hence, 12 is not a happy number.
Design a class Happy to check if a given number is a happy number. Some of the members of the class are given below:
Data Members/Instance variables
N : Stores the number
Member Functions
Happy( ) : Constructor to assign 0 to n
void getnum (int nn) : Assigns the parameter value to the number n = nn
int sum_sq_digits (int x) : Returns the sum of the square of the digits of the number x
void ishappy () : Checks if the given number is a happy number by calling the function sum_sq_digits(int) and displays
an appropriate message
Also define a main function to create an object and call the methods to check for the happy number.
6. Define a class Student to check whether the student is eligible for class XI.
Data Members
String name : Name of the student
doubel mathmark : Marks in Maths subject
double pcbmarks : Average marks of Physics, Chemistry and Biology
double comp : Marks of computer
Member Methods
Student() : Non-parameterised Constructor
void input() : Inputs the data members
void eligible() : Checks the eligibility of the course on the following conditions and prints the result along with the
name
Marks Criteria for eligibility
>=90% in all subjects Pure Science with Computer Sci
>=90% in Maths and PCB Bio Science without Computer Sci
70% to 89% in any subject Commerce
Below 70% in any subject Humanities
7. A person has taken a loan to buy a vehicle. He has to repay the loan according to the following criteria.
Time Rate of Interest
For 1 Year 4.5%
For 2 years 5%
For 3 years 7.5%
More than 4 years 10%
Design a class Loan with the following specifications.
Data Members
int principal : Principal amount to be taken
int time : Time of repayment
double rate : Rate of interest
Member Methods
void accept() : Inputs the principal and the time
double cal_interest() : Calculates the Simple Interest according to the above criteria and returns the interest
void display() : Prints the principal and the Interest calculated
8. Write a Java program to create a class palindrome_prime to print the palindromic_prime numbers between m and n.
(A palindrome is a number whose reverse is equal to the number and a prime number is a number that is divisible only by 1 and
the number itself.)
254254 Touchpad Computer Science-XI

