0% found this document useful (0 votes)
47 views

10. methods.overloading.number based

The document contains various programming tasks and examples in Java, including class definitions for checking SUPERSPY numbers, function overloading, employee salary calculations, and merit certificate awards. It provides code snippets for each task, demonstrating the use of loops, conditionals, and methods. Additionally, it outlines variable descriptions and expected outputs for each program.

Uploaded by

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

10. methods.overloading.number based

The document contains various programming tasks and examples in Java, including class definitions for checking SUPERSPY numbers, function overloading, employee salary calculations, and merit certificate awards. It provides code snippets for each task, demonstrating the use of loops, conditionals, and methods. Additionally, it outlines variable descriptions and expected outputs for each program.

Uploaded by

tejassharma623
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 20
J oS OF the elements Of row "¥(i41)4" = “ny } } Question 7 Define a class to accept a n, ‘ ‘umber number is called SUPERSpy ; and check whet i Examplel: 7% HF sum of the digit equal the oe ERSPY number reoat 1021 number of the digits, Output: SUPERSPY number sum of the digits = 1+0+241- 4 Number of digits = 4 Z Example 2: Anput: 125 Output: Not an SUPERSPY numbeé [1+2+5 is not equal to 3] Ans. // a program to check Su; import java.util.*; | a class Superspy { ioe static void main(String args[ ]) Scanner it int n, d, s=0, c=0; System.out.printin("Enter a number:"); n=in.nextInt( ); while(n>0) { d=n%10; s=s+d; /10; c=c+1; } if(s==c) . System.out printin("SUPERSPY number"); 7 A ‘ System.out printin("Not a SUPERSPY number"); eos ection PaperComputer Applications (Fully Se Question 8 Define a class to overloa void display( ) d the method display( ) as follows: ' + To print the following format using nested loop. 12121 12121 eg ae . 12121 void display(int n, int m) : To print the quotient of the division of m and n if m is greater | n, otherwise print the sum of twice n and thrice m, ' double display(double a, double b, double c) : to print the value of z, where z = pq = G+) andqeatbee Ans. // a program to illustrate function overloading ° class Overload void display( ) { int 1, ¢ for(r=1; r<=3;r++) { for(c=1; c<=5;c++) if(c%2==0) System.out print(2); else System.out print(1); } System.out printIn( ); } } void display(int m, int n) { if(m>n) System.out println(m/n); else System.out printin(2*n+3*m); } void display(double a, double b, double ©) { double z, p, q; p=(atb)/c; qeatbtc; z=p"g | System.out-printin(z); } } Questius h the fotlon:.... bela. OS "TARTOG, e a class with the followi ifications: Clas. name : Employee Specifications: Member variables: eno : Employee number ename : Name of the employee age : Age of the employee basic : Basic salary [Declare the variables using appropriate data types] Member methods: void accept( ) : to accept the details using Scanner class a void calculate( ) : to calculate the net salary as per the given specifications: net = basic + hra + da - pf hra = 18.5% of basic da = 17.45% of basic f= 8.10% of basic 5000. I the age of the empires is above 50, he/she gets an additional allowance of € void print( ) : to print the details as per following format: eno ename age basic fet OX WOODOOWOOOXK xx 000K void main( ) : to create an object of the class and invoke the methods Ans. // a program to calculate employee salary import java.util.*; class Employee { String ename; int eno, age; double hra, da, pf, basic, net; void accept( ) { Scanner in = new Scanner(System.in); System.out print("Enter employee number:"); eno = in.nextInt( ); in.nextLine( ); System.out print("Enter employee name:"); ename = in.nextLine( ); System.out print("Enter employee age:"); age = in.nextInt( ); System.out-print("Enter employee salary:"); basic = in.nextint( ); } void calculate( ) { hra = basic*18.5/100.0; da = basic*17.45/100.0; pf= basic*8.1/100.0; net= basic + hra + da - pf; if (age>50) net = net + 5000; } void print( ) { System.out.printin("eno\tename\t\tage\tbasic\tnet"); System.out printIn(eno+"\t"+ename+"\t"+age +'\t"+basic+"\t"+net); } public static void main (String args| ]) ( ; Employee ob = new Employee( ); ob.acceptt ); x ob.calculate( ); . ob print( ); } } variable Description Variable Data type Description ane String to accept employee's name So int to accept employee's number age int to accept employee's age Bale double to accept employee's basic salary Tea double to store house rent allowance a double to store dearness allowance pf double to store provident fund at double to store net salary 4 (15) eine a cass to overioad the method print) a follows: void print( ): to print the format 1 23 456 78910 boolean print (int n): to check whether the number is a Dudeney number/'a number is Dudeney if the cube of the sum of the digits is equal to the number itself. Sample Input: 512 = (54142)? = (8) = 512 void print (int a, char ch): If ch = 's' or 'S,, print the square of the number else if ch = 'c or 'C, print the cube of the number. ‘Ans. // a program to design overloaded function import java.util.*; class Overload System.out print(p. + ""); PH } System.out.printin( ); } } pore print(int n) int k, sum = 0, cube; for(k = n; sum+ (k%10); cube = (int)Math.pow(sum,3); iffns: Hn print(int a, char ch) if( ch=='s' | | ch =='$/) if( ch: System.out printin( "Square =" + (a*a)); =='c' || ch =='C’) System.out.printin("Cube =" + (a*a*a)); { public static void main(String args| ]) Overload ob=new Overload( ); ob.print( ); Scanner in=1 w Scanner(System.in); System.out.printin("Enter a number:"); int no=in.nextInt( ); boolean check = ob.print(no); if (check) System.out printin("It is a Dudeney number"); else System.out-printin("It is not a Dudeney number"); System.out.printin("Enter a number"); int no1=in.nextInt( ); System.out printin("Enter the character"); char c=i ob-print(nol, 0); } } Variable Description Variable Data type i int j int P int n int k int sum int cube int a int ch char .next( ).charAt(0); Description to run outer loop for pattern printing to run inner loop for pattern printing to print the value of the pattern to eee n vber and check whether itis Dudeney number oF 7! to run the loop for checking Dudeney number to store the sum of each digit of the number to store the cube of the sum of the digits to accept a number to print square or.the cube to accept a character to print the square oF cube of the choice of the input character r Oo” “N8orithms are hh i not requi Question 3 quired, A school is giving away meri . class 10. The followin 'Y merit Certificates for the & pro, : Students who h; ee ae : Ee hans is based on the Specification iifen bare “0 Member variables: : String name + to enter name of a student double per : to enter % marks obtained by the stud String cer : to store the message rs Member methods: void input( ) : to accept the details void merit( ) : to check the Percentage and it (" et ge award the merit ("Awarded!!" or void display( ) : to display the details void main( ) : to create an object of the class and invoke the methods Ans. // a program to award merit certificate import java.util.*; class Student { String name; double per; String cer; void input( ) { Scanner in = new Scanner (System.in); : System.out.print("Enter student's name : "); name= in.nextLine( ); / te, System.out.print("Enter percentage marks : "); per = in.nextDouble( ); } void merit( ) { if (per >=90) cer = "Awarded!!"; else ‘i cer = "Not Awarded"; } void display( ) { 7 me ); out. printin("Name :" + 74 pee Seton out printn( Percentage Marks pel ‘Sample Paper - 2 (Solve r/ System.out println( "Certificate : " + cer); } void main( ) { Student ob = new Student ); ob.input ); ob.merit( ); ob.display( ); ) } Variable Description Variable Data type Description name String to enter name of the student per double to enter % marks obtained by the student cer String to store the message Question 4 Write a program to check whether number is an Abundant number or not. A number is said to be an Abundant number when the sum of its factors (excluding the itself) is greater than the number. For example: Factors of number 12 are 12,34,6. Sum of factors is 1+2+3+4+6 = 16 Thus, 12 is an Abundant number. Ans. // a program to check an abundant number import java.util.*; class Number { public static void main(String args ]) { Scanner in = new Scanner (System.in); int n, i, sum = 0; System.out.print("Enter a number : "); n = innextint( ); for(i=1; in) System.out printin("Abundant Number"); System.out printin("Not an Abundant Number"); } } t int a temporary Variavie sum int to store the marks of best four subjects avg float to store the marks of English plus best four subjects Question 8 Design a class overloading a function Compute( ) as follows: (i) void Compute(String str, int p) with one String argument and one integer displays the word in uppercase if p is 1 otherwise, displays the word in lowercase, (ii) void Compute(char chr, int p) with one character argument and one integer checks and displays whether a character is in uppercase or not if p is 1 otherwise, and displays the character is in lowercase or not. (iii) void Compute(String str, char ch) with one String argument and one character displays the first three characters of a String if chr is 'f' otherwise, displays the last characters of the String. Sample Input: SUNIL Sample Output: If chr is 'f ' then displays SUN otherwise, NIL Ans. // To perform the different tasks by using overloading import java.util.*; class Choice void Compute(String str, int p) { if(p==1) System.out printin(str.toUpperCase( ); else System.out-printin(str.toLowerCase( )); } void Compute(char chr, int p) { if(p==1) { if(Character.isUpperCase(chr)) System.out.printIn(chr+ " is an uppercase character"); else System.out printin(chr+ " is not an uppercase character"); } if(p!=1) { if(Character.isLowerCase(chr)) System.out.printin(chr+ " is a lowercase character"); else System.out.printin(chr+ " is not a lowercase character"); } } void Compute(String str, char ch) { int k; kestrlength( ); if(ch==') System.out.println(strsubstring(0,3)); else System.out.printin(strsubstring((k-3).k)); } } Variable Description Variable Data type Description str String to accept a String through argument / P int to accept the integer value through argument for choice chr char to accept a character through argument k int to find the length of the String ; an char to accept the character value through argument for choice Each program sioune ©" the progr el 8! Question 3 Design a class named Telephone : Telephone Class name Member variables Member methods: void input( ) void print( ) void calculate( ) void main( ) : to accep’ so that the logic fi rithms are not required. Flowcharts an based on the specification given below: int noc [number of calls] double bill [telephone pill to be pat ] String nm [name of the customer] t the data using the Scanner class : to print the details : : to calculate the telephone bill as per the following criteri of calls Number of calls Rate per call First 100 calls Free >100 calls and <= 200 calls % 2.50 >200 calls and <=400 calls % 3.50 >400 calls = 450 : to create an object of the class and invoke the functio Ans. // a program to calculate the call charges import java-util.*; class Telephone { int noc; double bill; String nm; Scanner in = new Scanner(Si A void input( ) (System in); { System.out.print("Enter nv noc = in.nextInt( ); ae calls: "); in.nextLine( ); System.out.print("Enter name: "); nm=in.nextLine( ); ; Nt op numbe void calculate( ) { if (noc<=100) bill =0; if (n0c>100 && noc<=200) bill = (noc-100)*2.50, if (noc>200 && noc<=400) bill = 1000+ 1002504 "3.50; oo (noc-200)'3.50; ; bill = 100°0+ 100"2,504. 200*3,50-+(noc-400)*4.50; void print( ) { System.out printin('Name = "snm); System.outprintin("Amount to be paid="sbill); void main ( ) { Telephone ob = new Telephone( ); ob.input( ); ob.calculate( ); ob-print( ); } } Variable Description Variable Data type Description ve noc int to accept the number of calls bill double to calculate the telephone bill to be paid nm. String To accept name of the customer Question 4 5] Write a program to input a number and calculate the norm of the number. The norm of a number is square root of the sum of squares of all digits of the number. For example, Sample Input: 68 The norm of 68 is 10. = 6x6 + 8x8 = 36464 = 100 luare root of 100 is 10. // a program to calculate the norm of a number import java.util.*; am Norm_Number Public static void main(String args! ]) { Scanner in = new Scanner(System.in); : int n, m, d, sum=0; cw, System.out print("Enter a number: "); n= innextInt( ); m=n; while (n>0) ee F { d = n%10; sum = sum+d*d; n=n/10; } System.out.printin("Norm of " + m + ":"+(int)Math.sqrt(sum)); } } Variable Description Variable Data type Description n int to accept the number m int to store the input number d int to extract a digit with the loop sum int to store the sum of squares of all digits of the number Question 5 ©) Whett. « oe int ad String a String b String k int Question 7 to run the outer loop to accept the Adm. No. whose name to be searched to store the Adm. No., if found to store the name, if found to store 0 or 1 for checking whether the name is present or not Write a program to perform the following operations by using function overloading con (a) Generate(int n) : To use the function with an integer type argument to generate Fi, series up to given value. (b) Generate(int n, int m) : To use the function with two integer type arguments to p; prime numbers between both the values. Ans. // A program based on function overloading to generate the series class Overloading { void Generate(int n) { int a=0, b=1, c=0, k; System.out printin(a); System.out printin(b); for(k=1; k<=(n-2);k++) { c=atb; System.out printin(c); a=b; bec; } } void Generate(int n, int m) { int i, j, p; boolean f; for(i=nji<=m;i++) { if(f==true) System.out.printIn(i); } } ) cena otring to store the string in uppercase : int to run the loop P int to calculate the length of string temp char To extract a character with the loop Question 6 Design a class, to overload a method Number( ) as follows: : (i) void Number(int num, int d): To count and display the frequency of a digit in g For example, num = 2565685 d=5 Frequency of digit 5 = 3 (ii) void Number(int n1): To find and display the sum of even digits of a number. For example, nl = 29865 Sum of even digits = 16 Write a main method to create an object and invoke the above methods. Ans. // a program to implement function overloading import java. util.*; public class Overload void Number(int num, int d) { \ int f = 0; int num1=num; while (num != 0) System.out, i fn Y Printin("Frequency of digit " ee Number(int n1) 44+" in" snumis" = "49; int s = 0; int n2=n1; while (n1!= 0) { int x = nl % 10; if (x % 2 == 0) S=S+x; nl /= 10; } System.out printIn("Sum of even digits in "+n2+" =" + 5); ie static void main(String args[ }) Scanner in =new Scann in); int nde; er(System.in); System.out.print("Enter a number: "); n = innextint( ); System.out-print("Enter a digit whi i i ea igit whose frequency is to be counted: "); System.out print("Enter another number to fin igi System out print er to find the sum of even digits Overload ob = new Overload( ); ob.Number(n, dg); ob.Number(m); } } Variable Description Variable Data type Description a int to input a number dg int to input a digit of the number whose frequency is to be'searched Re int to input another number whose sum of even digits is to be calculated num, int to accept the argument in Number( ) a int to accept the other argument in Number( ) ae int to store the value of num int to count the frequency of a digit % S int to accept the argument in Number( ) : int to extract a digit with the loop : int to store the sum of even digits nm a to store the value of nl Wane rae ing operations as per the user's it i form the following op’ per | a Wc amen ven progam per hw 230 wh nee b=3.0 is a constant. : ro (ii) To display the following pattern using for loop: A AB ABC ABCD ABCDE Display proper message for an invalid choice. Ans. // a menu driven program import java.util"; class Display { public static void main(String args[ ]) { Scanner in = new Scanner (System.in); double a, b =3.0, c=0; int k; System.out-printin(": to print the values"); System.out-printin("2: to print the pattern"); System.out-print("Enter your choice: "); kein.nextInt( ); switch(k) { case 1: System.out printin("The values are:"); for(a=1.0; a<=20.0;a=a+2) { c= atat2*atb; System.out printin(c); } break; System.out.printin("The pattern:"); forli=ch; i<=(ched); it) i { for(j=ch; j<=i; j++) System.out print((char)j); System.out println( ); break; default: System.out println("Wrong Choicet!!"), } } } ae _ Der of dis double 1 SEPE the cost of item “usomer to store the disc, amount double ; ‘ount to display the amount to be paid Question 4 Paid after discount Using a switch case Statement, write i (a) To generate and Print letters from ‘A to 2 ae yOgram to do the follo : it Unicodes, Letters Unicode A 65 B 66 Z 90 () Delay the given pattern using iteration (looping) statement: 12 12 3 1234 123 4 5 Ans. //a program to display the output as per the user's choice import java.util.*; class Display { Public static void main(String args[ ]) Scanner in = new Scanner(System.in); int apk,ij; or in nphadst 1to display ‘Letters and their Unicodes' & 2 to di System.out.printIn("Enter your choice: ) k=in.nextint( ); switch(k) System out printin("The Letters with their Unicodes:"); for(a=1;a<=26;a++) ree { System.out.println((chr)+ "\t\t\t"+(int)(chr)); p=(int)(chr)+1; chr=(char)p; break; case 2: System.out.println("The Pattern:"); for(i=1;i<=5;i++) { for(§j=1;j<=ij++) { System.out.print(j); } System.out.printin( ); } break; default: System.out.printin("Entered wrong choice!!"); } } } Variable Description number _— A tech x has even ny ~ erting with letter 'A’ mber of di Zquare of sum of these halves is eq _ print all the 4-digit tech ana is equal to ni the number is spit; Example: ers. Je number aa in E80 equal halves, then the by sample Input: 3025 . Write a program to sae ten the le tot square of the halves of 3025 = odor and 2 = 552 ns. // to display all 4-di = 3025 class TechNum digit tech numbers { public static void mai: . ( main(String args{ }) int a,d1,d2; System.out.println("4-digi ; di Pia eae tn ate { di=a/100; d2=a%100; s=((d1+d2)*(d1+d2)); if(s==a) System.out println(a); } | Variable Description Data type Description int to run the loop from 1000 to 9999 int to store the quotient (first half of the number) to store the remainder (second half of the number) these halves to store the square ‘of sum of

You might also like