Notes 1
Notes 1
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.
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
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
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
QUESTION-5
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
QUESTION-8
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.