0% found this document useful (0 votes)
16 views17 pages

Java Class Design and Function Overloading

The document contains a series of programming questions and answers related to user-defined methods, class design, function overloading, and exception handling in Java. It covers topics such as the differences between private and public members, call by value vs call by reference, and provides examples of class structures for various functionalities. Additionally, it includes coding exercises for implementing specific methods and classes with detailed explanations of their expected behavior.

Uploaded by

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

Java Class Design and Function Overloading

The document contains a series of programming questions and answers related to user-defined methods, class design, function overloading, and exception handling in Java. It covers topics such as the differences between private and public members, call by value vs call by reference, and provides examples of class structures for various functionalities. Additionally, it includes coding exercises for implementing specific methods and classes with detailed explanations of their expected behavior.

Uploaded by

Axel Vyrn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

User-defined method

1. How are private members of a class different from public members?


(2018)
Answer:
Scope of the private members is within the class whereas the scope of the public
members is global.

2. State a difference between call by value and call by reference.


(2019)
Answer:
call by value call by reference

In call by value the method creates its In call by reference, reference of the
new set of variables (formal parameters) actual parameters is passed on to the
to copy the value of actual parameters method. No new set of variables is
and works with them.
created.

Any change made in the formal Any change made in the formal
parameter is not reflected in the actual parameter is always reflected in the
parameter. actual parameters.

Primitive data types are passed by call by Reference types like (objects, array etc.)
value. are passed by call by reference.

3. Rewrite the following using ternary operator:


if(bill > 10000)
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;
(2018)
Answer:
discount = bill > 100 ? bill * 10.0 /100 : bill * 5.0 /100;

4. Give the output of the following program segment and also mention how many
times the loop is executed :

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


int i;
for (i=5 ; i>10 ; i++)
[Link](i);
[Link](i*4);
(2018)
Answer:
20
Loop will be executed for 0 times.

5. Design a class to overload a function series() as follows:


(a) void series (int x, int n) – To display the sum of the series given below:
x1 + x2 + x3 + ……………. xn terms
(b) void series (int p) – To display the following series: 0, 7, 26, 63 p terms.
(c) void series ( ) – To display the sum of the series given below:
1 1 1 1
2
+ 3
+ 4
+ ............ + 10
(2019)
Answer:
import [Link].*;
import [Link].*;
class OverloadSeries
{ void series(int x, int n)
{ int i; .
double a;
double sum = 0;
for (i = 1; i < = n; i++)
{ a = [Link](x, i);
sum = sum + a;
}
[Link](“Sum::” +sum);
}

void series(int p)
{ int i;
for (i=1 ; i<=p ; i++)
{ [Link]((i * i * i) – 1 + ” “);
}
}

void series( )

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


{ double i;
double s = 0;
for (i =-2; i < = 10; i+ +)
{s = s + 1/i;
}
[Link](“Sum:” +s);
}
}

6. Design a class to overload a function volume() as follows:


(i) double volume (double R) — with radius (R) as an argument, returns the
volume of the sphere using the formula.
V = 4/3 × 22/7 × R3
(ii) double volume (double H, double R) – with height(H) and radius(R) as the
arguments, returns the volume of a cylinder using the formula.
V = 22/7 × R2 × H
(iii) double volume (double L, double B, double H) – with length(L), breadth(B)
and Height(H) as the arguments, returns the volume of a cuboid using the
formula.
(2018)
Answer:
class ShapesVolume
{public static double volume (double R)
{double V = 4.0 / 3 * 22.0 / 7 * [Link](R, 3);
return V;
}
public static double volume(double H, double R)
{double V = 22.0 / 7 * R * R * H ;
return V;
}
public static double volume (double L, double B, double H)
{double V = L * B * H;
return V;
}
}

7. What are the two ways of invoking functions?


(2017)
Answer:

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


By Value and By Reference

8. Differentiate between constructor and function.


(2017)
Answer:
Constructors must be named with the same name as the class name. They
cannot return anything, even void (the object itself is the implicit return).
Functions must be declared to return something, although it can be void.

9. Differentiate between formal parameter and actual parameter.


(2016)
Answer:
Formal parameter Actual parameter

They are variables defined in the They are values given to the function
func­tion signature. at the time of function call.

They receive value from actual They give values to formal parameter
parameters.

10. Write a function prototype of the following:


A function PosChar which takes a string argument and a character argument and
returns an integer value.
(2016)
Answer:
int PosChar (String str, char ch)

11. Design a class to overload a function check ( ) as follows :


(i) void check (String str, char ch) – to find and print the frequency of a character
in a string.
Example : *
Input: str = “success”
ch = ‘s’ .
Output: number of s present is = 3

(ii) void check(String si) – to display only vowels from string si, after converting it
to lowercase.
Example:

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


Input: s1 = “computer”
Output: o u e
(2017)
Answer:
class CharacterVowel
{ public void checkering str, char ch)
{ int c = 0, code,i,s;
str = [Link]( );
int len = [Link]( );
for (code=97 ; code<122 ; code++)
{ c = 0;
for (i = 0; i < len; i+ +)
{ ch = [Link](i);
s = (int) ch;
if(s = = code)
c = c + 1;
}
ch = (char)code;
if(c ! = 0)
[Link](“Frequency of “+ch+ “is” +c);
}
}
public void check(String si)
{ int i;
char ch=0, chr=0;
for(i=0 ; i<[Link]() ; i++)
{ ch = [Link](i);
if([Link](ch))
chr = Character. toLo werCase(ch);
if((s1 .charAt(i)==’a’) 11 (s1 .charAt(i) = ‘u’) | j(s1 .charAt(i) = = ‘o’)| | (s1
.charAt(i)==’i’) 11 (s1 .charAt(i) = = ‘e’))
[Link](s1 .charAt(i));
}
}
}

12. Define a class named BookFair with the following description:


Instance variables/Data members :
String Bname — stores the name of the book

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


double price — stores the price of the book
Member methods :
(i) BookFair() — Default constructor to initialise data members
(ii) void Input() — To input and store the name and the price of the book.
(iii) void calculate() — To calculate the price after discount. Discount is
calculated based on the following criteria.

Price Discount

Less than or equal to Rs. 1000 2% of price

More than Rs. 1000 and less than or 10% of price


equal to Rs. 3000

More than Rs. 3000 15% of price

(iv) void display() — To display the name and price of the book after discount.
Write a main method to create an object of the class and call the above member
methods.
(2016)
Answer:
import [Link];
class BookFair
{ String Bname;
double price;
BookFair()
{ Bname =” “;
Price = 0;
}
void input()
{ Scanner S = new Scanner([Link]);
[Link] ("Enter Book name");
Bname=[Link]();
[Link] ("Enter Price");
price = [Link]();
}
void calculate ()
{ double d;
if (price <= 1000)
d=2.0/100* price;

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


else if (price <= 3000)
d=10.0/100* price;
else
d=15.0/100* price;
price=price-d;
}
void display ( )
{ [Link] ("Book Name" + Bname);
[Link] ("Price" + price);
}
public static void main ()
{ BookFair b= new BookFair ();
[Link] ();
[Link] ();
[Link]();
}
}

13. What are the values of a and b after the following function is executed, if the
values passed are 30 and 50:
void paws(int a, int b)
{ a=a + b;
b=a – b;
a=a – b;
[Link] (a+ “,” +b);
}
(2015)

Answer:
The value of a will be 50 and
The value of b will be 30.

14. What is the function of catch block in exception handling ? Where does it appear
in a program ?
(2015)

Answer:

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


Catch block is used as exception handler in exception handling. We can put the
code to deal with the execution that might arise, in this block. Catch block must
appear just below the tiy block.

15. List the variables from those given below that are composite data types:
(i) static int x;
(ii) arr[i]=10;
(iii) [Link]();
(iv) boolean b;
(v) private char chr;
(vi) String str;
(2014)
Answer:
The composite data types are:
(ii) arr[i] = 10;
(iii) [Link]();
(vi) String str;

16. Define a class named movieMagic with the following description:


Instance variables/data members:
int year — to store the year of release of a movie.
String title — to-store the title of the movie
float rating — to store the popularity rating of the movie
(minimum rating=0.0 and maximum rating=5.0)
Member methods:
(i) movieMagic() - Default constructor to initialize numeric data members to 0
and String datamember to “ ”.
(ii) void accept() - To input and store year, title and rating.
(iii) void display() - To display the title of a movie and a message based on the
rating as per the table below.

Rating Message to be displayed

0.0 to 2.0 Flop

2.1 to 3.4 Semi-hit

3.5 to 4.5 Hit

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


4.6 to 5.0 Super Hit

Write a main method to create an object of the class and call the above member
methods.

(2014)

Answer:
import [Link].*;
class movieMagic
{ int year;
String title;
float rating:
public movieMagic()
{ year=0;
title=" ";
rating=0.0;
}
void accept()throws IOException
{ InputStreamReader IR = new InputStreamReader ([Link]);
BufferedReader br=new BufferedReader(IR);
[Link]("Enter the year of release :");
year=[Link]([Link]());
[Link] ("Enter the title of movie:");
title=[Link]();
[Link] ("Enter the popularity rating :");
rating=[Link]([Link]());
}
void display()
{ [Link] ("The title of movie is :" + title);
[Link](“The film is a “);
if(rating>=0.0 && rating <=2.0)
{ [Link]("Flop");
}
else if(rating>=2.1 && rating <=3.4)
{ [Link]("Semi-hit");
}
else if(rating>=3.5 && rating <=4.5)

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


{ [Link]("Hit");
}
else if (rating >= 4.6 && rating <= 5.0)
{[Link]("Super Hit");
}
}
public static void main()throwsIOException
{ movieMagic obj=new movieMagic();
[Link]();
[Link]();
}
}

Variable Description:
[Link] Variable name Datatype Purpose

1. year int to store the year of release of the movie.

2. title String to store the title of movie

3. rating float to store popularity rating of the movie

4. IR - for input stream reader

5. br - for buffered reader

6. obj - for object of the class

17. Design a class to overload a function Joystring( ) as follows :


(i) void Joystring (String s, char ch1 char ch2) with one string argument and two
character arguments that replaces the character argument ch1 with the character
argument ch2 in the given string s and prints the new string.
Example:
Input value of s = “TECHNALAGY”
ch1=‘A’,
ch2=‘O’
Output: TECHNOLOGY

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


(ii) void Joystring (String s) with one string argument that prints the position of
the first space and the last space of the given string s.
Example:
Input value of = “Cloud computing means Internet based computing”
Output: First index : 5
Last index : 36

(iii) void Joystring (String s1, String s2) with two string arguments that combines
the two string with a space between them and prints the resultant string.
Example :
Input value of s1 =“COMMON WEALTH”
Input value of s2 =“GAMES”
Output: COMMON WEALTH GAMES
(use library functions)
(2015)
Answer:
class Overload
{ void Joystring (string s, char ch1, char ch2)
{ S = [Link] (ch1, ch2);
[Link] ("New String" +S);
}
void joystring (String S)
{ int i1=[Link](");
int i2=[Link] IndexOf(");
[Link] ("Position of 1st space" +i1);
[Link] ("Position of last space" +i2);
}
void Joystring (string s1, string s2)
{ String s3=[Link](" ");
s3=[Link](s2);
[Link]("new string" +s3);
}
}

18. Design a class to overload a function area( ) as follows:


(i) double area (double a, double b, double c) with three double arguments,
returns the area of a scalene triangle using the formula:
area = (𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐))

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


where s=(a+b+c)/2

(ii) double area (int a, int b, int height) with three integer arguments, returns the
area of a trapezium using the formula:
area = ½ height (a + b)

(iii) double area (double diagonal 1, double diagonal 2) with two double
arguments, returns the area of a rhombus using the formula :
area = ½ (diagonal 1 x diagonal 2)
(2014)

Answer:
class Overload
{ double area(double a, double b, double c)
{ double s, r;
s=(a+b+c)/2;
r=s*(s-a)* (s-b)*(s-c);
double ar = [Link] (r);
return ar;
}
double area(int a, int b, int height)
{ double area=(height*(a+b))/2;
return area;
}
double area(double diagonal 1,double diagonal2)
{ double area=(diagonal1*diagonal2)/2;
return area;
}
}

The variable description is as follows:


[Link] Variable Name Datatype Purpose

1. a double to enter side of the triangle

2. b double to enter side of the triangle

3. c double to enter side of the triangle

4. s double to store the value of s in the area formula used.

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


5. r double to store the value of r in area formula used

6. area double to store area of various shapes

7. a int to enter side of trapezium

8. b int to enter side of trapezium

9. height int to enter height of trapezium

10. diagonal 1 double to enter diagonal of rhombus

11. diagonal 2 double to enter diagonal of rhombus

19. Give one example each of a primitive data type and a composite data type.
(2012)
Answer:
Primitive data type: int
Composite data type: Class

20. Design a class to overload a function series ( ) as follows :


(i) double series (double n) with one double argument and returns the sum of the
series,
1 1 1 1
sum= 1
+ 2 + 3 +........+ 𝑛
(ii) double series (double a, double n) with two double arguments and returns the
sum of the series.
1 4 7 10
sum= 2 + 5 + 8 + +......+ to n terms
𝑎 𝑎 𝑎 𝑎11
(2013)

Answer :
public class series
{ public double series(double n)
{ int a;
double s=0;
for (a = 1; a < = n; a ++)
s= s+1/a;
return s;
}
public double series (double a, double n)
{ int a, i;

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


double sum= 0,. b = 1;
double c=1/(a*a);
sum=sum+c;
for(i=5;i<=n;i+=3)
sum=sum+((b+3)/[Link](a,i));
return s;
}
}

21. Differentiate between call by value or pass by value and call by reference or pass
by reference.
(2012)
Answer:
Call by Value: Actual parameters are copied into the formal parameters and the
called method works with the formal parameters. Any change in the formal
parameter is not reflected back in the actual parameter.

Call by reference: When a method is called, the reference/address on which the


data is stored is sent to the formal parameters. Any change in formal parameters
is reflected in the actual parameters.

22. What are the two ways of invoking functions ?


(2011)
Answer:
Two ways to call a function are :
1. Call by value
2. Call by reference.

23. What is the role of keyword void in declaring functions ?


(2011)
Answer:
At the time of function declaration void signifies that function has no return type,
that means function will not return any value

24. Give the prototype of a function search which receives a sentence sentnc and a
word wrd and returns 1 or 0 ?
(2011)

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


Answer:
int search (String sentnc, String wrd)

25. In the program given below, state the name and the value of the
(i) method argument or argument variable
(ii) class variable
(iii) local variable
(iv) instance variable
class myClass
{ static int x=7;
int y=2;
public static void main(String args[])
{ myClass obj= new myClass();
[Link](x);
[Link](5);
int a=6;
[Link](a);
}
void sampleMethod(int n)
{ [Link](n);
[Link](y);
}
}
(2012)

Answer:
(i) n
(ii) x
(iii) a
(iv) y

26. Define a class called Library with the following description :


Instance variables/data members :
int acc_num — stores the accession number of the book
String title — stores the title of the book
String author — stores the name of the author
Member methods:
(i) void input ( ) — To input and store the accession number, title and author.

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


(ii) void compute ( ) — To accept the number of days late, calculate the
display the fine charged at the rate of Rs. 2 per day.
(iii) void display( ) — To display the details in the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above member
methods.
(2012)

Answer:
import [Link].*;
class library
{ int acc_num;
String title;
String author;
void input()throws IOException
{ BufferedReader br = new Bufferedreader (new inputStreamReader
([Link]));
[Link] ("Enter accession number");
acc_num= [Link](br. readLine());
[Link]("Enter title of the book");
title= br. readLine();
[Link] ("Enter author of the book");
author = [Link]();
}
void compute(int days)
{ int Fine;
fine = 2*days;
[Link]("fine charged=" +fine);
}
void display()
{ [Link]("Accession Number"+"\t" + "Title"+"\t" + "Author");
[Link](acc_num +"\t" + title + "\t" + author);
}
public static void main()throws IOException
{ Library obj = new Library ();
[Link] ()
[Link] (4);
[Link]();
}

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer


}

27. Give the prototype of a function check which receives a character ch and an
integer n and returns true or false.
(2010)
Answer:
boolean check (char ch, int n)

Get complete access to ICSE Computer chapterwise now: [Link]/icsecomputer

You might also like