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

Notes 1

The document discusses Java packages and wrapper classes. It provides examples of code snippets and their output. It also contains questions related to packages, wrapper classes and method overloading.

Uploaded by

qwertyuiopsa0011
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)
26 views

Notes 1

The document discusses Java packages and wrapper classes. It provides examples of code snippets and their output. It also contains questions related to packages, wrapper classes and method overloading.

Uploaded by

qwertyuiopsa0011
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
You are on page 1/ 12

1. What is meant by a package? Give an example.

ANS:
Collection of predefined
classes Example: java.lang
java.io
java.util
2. Name the package that contains wrapper classes.
Ans:
java.lang
3. Name the following:
(i) A keyword used to call a package in the program.

(ii) Any one reference data type.


Ans:
(i) import
(ii) Array , class

4. Give the output of the following code:


String A = "26", B = "100";
String D = A + B + "200"; 26100200
int x = Integer.parseInt(A); 26 int y
= Integer.parseInt(B); 100
int d = x + y; 126
System.out.println("Result 1 = " + D);
System.out.println("Result 2 = " + d);
Output
Result 1 = 26100200
Result 2 = 126
5. Name any two library packages.
Ans: java.lang
java.util
6. Name the wrapper classes of char type and boolean
type.
Ans:
Character
Bollean
8. State the package that contains the class:
(i) BufferedReader
(ii) Scanner Ans:
(i) java.io
(ii) java.util
9. What is meant by a package? Name any two Java
Application Programming Interface package.
10. What will be the output when the following code
segments are executed? String s = "1001";
int x = Integer.valueOf(s); double y =
Double.valueOf(s);
System.out.println("x = " + x);
System.out.println("y = " + y);
Output: x=1001 y=1001.0
11.Name any two wrapper classes.
Ans:
Character
Bollean

13. State the method that:


(i) Converts a string to a primitive float data type.
(ii) Convert a number stored in a string variable x
to double data type.
Ans:
(i) parseFloat()
(ii) parseDouble()
16. What is the use of the keyword import?
To include a package in a program
17. Write one word answer for the following:
(i) A method that converts a string to a
primitive integer data type.
Ans:
parseInt()
20. Name the following:
(i) A package that is invoked by default.
(ii) A keyword to use the classes defined in a
package.
Ans: java.lang
import
21. Convert a string to a numeric value.
(iii) Add it to the existing total of 1000 to update
the total.
Ans:
int n=Integer.parseInt(s);
total=n+1000;
Method overloading:

More than one function having same name and


differentiate in either no of arguments or type of
arguments
Series problems
1,4,9,16,25….n 0,3,8,15,24,….
1,8,27,64,125,…
0,7,26,63,124,…
½ + 1/3 +1/4 +…+1/n
X1-x2+x3-x4…+xn

for(i=1;i<=n;++i)
{
if(i%2==0)
Sum=Sum-Math.pow(x,i);
else
Sum=Sum+Math.pow(x,i);
}
System.out.println(sum);

Pattern problems
*
**
***
****
SECTION-B
QUESTION-1

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/2 + 1/3 + 1/4 + … + 1/10
import java.util.*; class
Overload
{
void series(int x, int n)
{ double sum=0;
for(int
i=1;i<=n;++i) {
sum=sum+Math.pow(x,i);
}
System.out.println(sum);
}
void series(int p)
{
for(int i=1;i<=p;++i)
{
System.out.println(Math.pow(i,3)-1);
} } void series() {
double Sum=0;
for(int
i=2;i<=10;++i)
{
Sum=Sum+(1/i);
}
System.out.println(Sum);
}
public static void main(String args[])
{
int x,n,p;
Overload ob=new Overload();
Scanner kb=new Scanner(System.in);
System.out.println(“Enter x,n value”);
x=kb.nextInt(); n=kb.nextInt();
ob.series(x,n);
System.out.println(“Enter p value”);
p=kb.nextInt(); ob.series(p);
ob.series();
}
}

QUESTION-2
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 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:
v=l×b×h
import java.util.*;
class Overload
{
double volume(double r)
{
double v; v=4/3*22/7*Math.pow(r,3);
return(v); }
double volume(double h, double r)
{ double v;
v=22/7*r*r*h
; return(v); }
double volume(double l, double b, double h)
{ double
v;
v=l*b*h;
return(v);
}
public static void main(String args[])
{
Overload ob=new Overload();
double r,v,h,l,b
Scanner kb=new Scanner(System.in);
System.out.println(“Enter radius value”);
r=kb.nextDouble(); v=ob.volume(r);
System.out.println(“volume of sphere=”+v);
System.out.println(“Enter height and radius”);
h=kb.nextDouble();
r=kb.nextDouble(); v=ob.volume(h,r);
System.out.println(“volume of cylinder=”+v); class Overload
{
double volume(double r)
{ double v;
v=4/3*22/7*r*r*r
; return(v); }
QUESTION-3

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


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.
void check(String s1) – to display only vowels from s1, after converting it to lowercase.
Example: INPUT:
s1 = “computer” OUTPUT:
oue

QUESTION-3

Write a program to input forty words in an array. Arrange these words in descending order of
alphabets, using bubble sort technique. Print the sorted array.

QUESTION-4

Design a class to overload a function sumSeries() as follows:


(i) void sumSeries(int n, double x): with one integer argument and one double argument to
find and display the sum of the series given below:
s = x / 1 – x / 2 + x / 3 – x / 4 + x / 5 … N terms.
(ii) void sumSeries(): to find and display the sum of the following series:
s = 1 + (1 × 2) + (1 × 2 × 3) + … + (1 × 2 × 3 × 4 … × 20)

QUESTION-5

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


void area(double a, double b, double c) with three double
arguments, returns the area of a scalene triangle using the
formula:
area = √(s(s – a)(s – b)(s – c)) where
s = (a + b + c) / 2.
(ii) void area(int a, int b, int height) with three integer
arguments, returns the area of a trapezium using the formula:
area = 1/2 × height × (a + b)
(iii) void area(double diagonal1, double diagonal2) with two
double arguments, returns the area of a rhombus using the
formula: area = 1/2 × (diagonal1 × diagonal2)

QUESTION-6
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, sum = 1
/ 1 + 1 / 2 + 1 / 3 + … + 1 / n.
(ii) double series(double a, double n) with two
double arguments and returns the sum of the series,
sum = 1 / a2 + 4 / a5 + 7 / a8 + 10 / a11 + … to n terms.
Sum=Sum+(i/Math.pow(a,i+1); import java.util.*;
class Overload
{
double series(double n)
{
double sum=0;
for(int i=1;i<=n;++i)
{
Sum=Sum+(1/i);
}
return(sum);
}
double series(double a, double n)
{
double sum=0;
for(int i=1;i<=n;i=i+3)
{
Sum=Sum+(i/Math.pow(a,i+1));
}
return(Sum);
}
public static void main(String args[])
{
double n,Sum=0,a; Overload
ob=new Overload();
Scanner kb=new Scanner(System.in);
System.out.println(“Enter n value”);
n=kb.nextDouble();
Sum=ob.series(n);
System.out.println(Sum);
System.out.println(“Enter a,n value”);
a=kb.nextDouble();
n=kb.nextDouble();
Sum=ob.series(a,n);
System.out.println(Sum);
}
}

QUESTION-7

Design a class to overload a function polygon() as follows:


(i) void polygon(int n, char ch): with one integer argument and one character argument
that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y): with two integer arguments that draws a filled rectangle of
length x and breadth y, using the symbol ‘@’.
(iii) void polygon(): with no arguments that draws a filled triangle shown below.
Example: (i) Input value of n = 2, ch = ‘O’ Output:
OO
OO
(ii) Input value of x = 2, y = 5 Output:
@@@@@
@@@@@
(iii) Output:
*
**
***

QUESTION-8

Design a class to overload a function compare() as follows:


(a) void compare(int, int): to compare two integer values and print the greater of the two
integers.
(b) void compare(char, char): to compare the numeric values of two characters and print
the character with higher numeric value.
(c) void compare(String, String): to compare the length of the two strings and print the
longer of the two.
QUESTION-9

Design a class to overload a function num_calc() as follows:


a) void num_calc(int num, char ch) with one integer argument and one character
argument computes the square of integer argument if choice ch is ‘s’ otherwise finds its cube.
b) void num_calc(int a, int b, char ch) with two integer arguments and one character
argument, computes the product of integer arguments if ch is ‘p’ else adds the integers.
c) void num_calc(String s1, String s2) with two string arguments, which prints whether
the strings are equal or not.

QUESTION-10

Write a class with the name Volume using function overloading that computes the
volume of a cube, a sphere and a cuboid.
Formula: volume of a cube (vc)
= s * s * s.
volume of a sphere (vs) = 4 / 3 * π * r * r * r where π = 3.14 or 22 / 7.
volume of a cuboid (vcd) = l * b * h.

You might also like