Download tutorial#8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
TUTORIAL#8
1.
What is the output of the following Java program?
public class MyInteger {
private int x;
public MyInteger(int myX) {
x = myX;
}
public double add(int y, int z) {
System.out.println("add(int, int): " + x + " + " + y + " + "
+ z);
return x + y + z;
}
public double add(int y, double z) {
System.out.println("add(int, double): " + x + " + " + y + "
+ " + z);
return x + y + z;
}
public double add(double y, double z) {
System.out.println("add(double, double): " + x + " + " + y
+" + " + z);
return x + y + z;
}
public static void main(String[] args)
MyInteger n1 = new MyInteger(1);
MyInteger n2 = new MyInteger(2);
double r1, r2, r3, r4;
r1 = n1.add(3, 4);
r2 = n2.add(3, 4);
r3 = n1.add(5, 6.0);
r4 = n2.add(7.0, 8.0);
System.out.println("n1.add(3, 4) = "
System.out.println("n2.add(3, 4) = "
System.out.println("n3.add(5, 6.0) =
System.out.println("n4.add(7.0, 8.0)
}
}
{
+
+
"
=
r1);
r2);
+ r3);
" + r4);
2. for the following program , state the scope of each of following elements :
a) The variable x.
b) The variable cube_y.
c) Formal parameter c d) The method cube.
e) The method find_cube.
public class Sheet10
{static int x ;
public static void find_cube(
{
int y= 3,cube_y ;
)
for (int x = 1 ; x<=3 ; x++ )
{ cube_y = cube( y) ;
System.out.println("y= "+y+"cube y=
y+= 3 ;
}
"+cube_y) ;
}
public static int cube(int c )
{ x=
c*c*c ;
return x ;
}
public static void main( String [] args )
{
find_cube() ;
}
}
3. Write three overloaded methods that take 2, 3 or 4 parameters and return the
average of the parameters.
4. Write method headings for the following methods:
a. Method h that takes two floating point numbers and returns their
average.
b. Method nothing that takes no parameters and returns nothing.
c. Method small that takes three integers and returns the smallest one.
d. Method f that takes an integer and returns a floating point number.
5. Find the errors in the following segments:
a. public static g()
b. { public static h(int x)
{…
} }
c. public static int ( float x)
{ double result;
result = x +2.4; }
d. public static void f1( int g)
{ int a = 5;
a = a + g;}
Related documents