Beruflich Dokumente
Kultur Dokumente
1) What is the output of the main method?
class Ques1 {
public static void main(String[] args) {
final int SIZE = 5;
int index;
int row, col;
double[] ra = {2.3, 3.4, 5.6, 2.5};
for (index=ra.length1; index >= 0; index)
System.out.print(ra[index] + " ");
System.out.println();
int[][] rad = new int[SIZE/2][SIZE];
for (row=0; row < SIZE/2; row ++)
for (col=0; col < SIZE; col ++)
rad[row][col] = row + col;
for (col=0; col < SIZE; col++)
System.out.print(rad[1][col]);
System.out.println();
for (row=0; row < SIZE/2; row++)
System.out.print(rad[row][4]);
System.out.println();
}
}
2) What is the output of the main method?
class Gift {
public String forwhom;
private int height, width, depth;
private double cost;
private static double MAXCOST = 100.00;
public Gift() {
height = 3;
width = 4;
depth = 5;
forwhom = "me";
cost = 0.0;
}
public Gift(int h, int w, int d, double c, String s) {
height = h;
width = w;
depth = d;
if (c <= MAXCOST) cost = c;
else cost = MAXCOST;
forwhom = s;
}
public void printall() {
System.out.println(height + "x" + width + "x" + depth);
System.out.println("$" + cost + " for " + forwhom);
}
public static void changeMax(double d) {
if (d > 0) MAXCOST = d;
}
}
class Ques2 {
public static void main(String[] args) {
Gift p1 = new Gift();
p1.printall();
Gift p2 = new Gift(10, 20, 30, 20.50, "myself");
p2.printall();
Gift.changeMax(10.0);
p2.printall();
p1.forwhom = "you";
p1.printall();
Gift p3 = new Gift(3, 4, 5, 15, "nobody");
p3.printall();
}
}
3) Use the previous main method, but this class definition:
class Gift {
public String forwhom;
private int height, width, depth;
private double cost;
private final double MAXCOST = 100.00;
public Gift() {
height = 1;
width = 5;
depth = 15;
forwhom = "you";
cost = 2.0;
}
public Gift(int h, int w, int d, double c, String s) {
height = h;
width = w;
depth = d;
if (c > MAXCOST) cost = c;
else cost = MAXCOST;
forwhom = s;
}
public static void printall() {
System.out.println(height + "x" + width + "x" + depth);
System.out.println("$" + cost + " for " + forwhom);
}
public static void changeMax(double d) {
if (d > 0) MAXCOST = d;
}
}
4) Write the output of the main method if the input is:
c t t q s d
public interface shapeInterface
{
public void sayMyName();
}
public class Circle implements shapeInterface
{
public void sayMyName() { System.out.println("Circle"); }
}
public class Triangle implements shapeInterface
{
public void sayMyName() { System.out.println("Triangle"); }
}
public class Square implements shapeInterface
{
public void sayMyName() { System.out.println("Square"); }
}
class Ques4 {
public static void main(String[] args) {
boolean done = false;
int numShapes = 0;
shapeInterface[] shapes = new shapeInterface[5];
do {
System.out.println("c = circle");
System.out.println("t = triangle");
System.out.println("s = square");
System.out.println("d = done");
System.out.println("What kind of shape? ");
char choice = Keyboard.readChar();
switch (choice) {
case 'c':
shapes[numShapes++] = new Circle();
break;
case 't':
shapes[numShapes++] = new Triangle();
break;
case 's':
shapes[numShapes++] = new Square();
break;
case 'd':
done = true;
}
} while ((!done) && (numShapes < 5));
for(int i = numShapes 1; i >= 0; i)
shapes[i].sayMyName();
}
}
}
1.
2.5 5.6 3.4 2.3
12345
45
2.
3x4x5
$0.0 for me
10x20x30
$20.5 for myself
10x20x30
$20.5 for myself
3x4x5
$0.0 for you
3x4x5
$10.0 for nobody
3.
Doesn't compile
Can't change a constant
Static method can't access instance variables
4.
Square
Triangle
Triangle
Circle