Q: Create a class "DemoArr", with the method "disp". Create 4 objects of this class in an array. Traverse the array and invoke "disp" on each objects.
public class DemoArr {
public static void main(String[] args) {
DemoArr[] arrs = new DemoArr[4];
for (int i = 0; i < arrs.length; i++) {
arrs[i] = new DemoArr();
}
for (DemoArr arr : arrs) {
arr.disp();
}
}
void disp() {
System.out.println("Display method");
}
}
Q: Create a class "shape" with 2 attibutes, "width" and "height". Make sure one can not access these attributes directly, and allow them to call from outside of your class.
class Shape {
// Privatly accessable
private int width;
private int height;
// Publicly accessable
// Shape(int width, int height){
// this.height = height;
// this.width = width;
// }
void setWidth(int width) {
this.width = width;
}
void setHeight(int height) {
this.height = height;
}
int getWidth() {
return width;
}
int getHeight() {
return height;
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Shape();
s.setWidth(3);
s.setHeight(0);
System.out.println(s.getHeight());
System.out.println(s.getWidth());
}
}
Q: Define a class "MyClass" and make sure clients cn instanlate it,
a) without any arg
b) with one int arg
c) with two int args.
{
}
Q: Define a class "Exp" with private static member "int cnt". How will you make sure that outsider can read the vlaue of cnt?
{
}
Q: Define 3 classess A, B, c in all these classsess create static and non-static vars as well as methods. Now define a class "Demo", in which define "main" method. From this main methid try to access all the mombers of A, B, C.
{
}
Q: Define interface "A" with "disp()" method. Derive from "A", interface "B" with "disp2" method. Derive class "C" from "B". Instantiate class "C" and call its members as well as deried from parent interface.
{
}
Q: Create abstract base class "Game" with "play()" as abstract method. From that derive following classess. Football, Cricket, Tennis. Create a class called as "GameDemo" (public class) indide a main method create an array of 'Game' (3 elements) store abjects of Football, cricket, Tennis respectively. Now traverse the array and call that cricket's play() method.
{
}
{
}
0 Comments