Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help on java homework. thanks 1. What happens when \"this\" is used in a constru

ID: 3670543 • Letter: H

Question

Help on java homework. thanks

1. What happens when "this" is used in a constructor’s body to call another constructor of the same class but the call is not the first statement in the constructor?

For example:

public Student(String name, String id) {

this.id = id;

this(name);

}

Select one:

a. a runtime error occurs

b. a compilation error occurs

c. nothing happens- the program compiles and runs

d. a logic error occurs

2. A static method can do which of the following? Select all that apply.

Select one or more:

a. reference instance data variables in the class

b. invoke non-static methods in the class

c. invoke other static methods in the class

d. reference static variables in the class

3. A non-static (instance) method can do which of the following? Select all that apply.

Select one or more:

a. reference instance data variables in the class

b. invoke other non-static methods in the class

c. reference static variables in the class

d. invoke static methods in the class

4. Based on the following code fragments, which of the following are legal statements? Select all that apply.

public class Bird {

public Bird() {

...

}

public int getFlightSpeed() { ...}

public static int getMaximumFlightSpeed() { ...}

}

Select one or more:

a. int n = Bird.getFlightSpeed();

b. Bird b = new Bird();
c. int n = b.getFlightSpeed();

d. Bird b = new Bird();
b.getFlightSpeed();

e. int n = Bird.getMaximumFlightSpeed();

5. Write three overloaded methods that calculate averages. The first method takes two integer parameters, the second takes three integer parameters, and the third takes four integer parameters. Each method returns a double. Make sure all methods are calculating the correct average and that the three methods are properly overloaded.

PART 2

Use the following enumerated data type for the next questions.

enum Creature { HOBBIT(false), ELF(false), DRAGON(true);

private boolean canFly;

private Creature(boolean canFly) {

this.canFly = canFly;

}

public boolean canFly() {

return canFly;

}

}

6. What is the name of the data type?

Answer:

7. What is the ordinal value for the enum constant ELF?

Answer:

8. Write a statement to declare a variable of this data type. The variable is called myCreature and initialized with the value DRAGON.

Answer:

9. Using the variable you declared about, what is output by the following?

System.out.println(myCreature);

Answer:

10.Using the variable declared above, what is output by the following?

System.out.println(myCreature.canFly());

Answer:

11. The following statement is syntactically correct (meaning it will compile).

Creature newCreature = new Creature("HOBBIT");

Select one:

a. True

b. False

Explanation / Answer

1. What happens when "this" is used in a constructor’s body to call another
constructor of the same class but the call is not the first statement in the
constructor?
For example:
public Student(String name, String id) {
this.id = id;
this(name);
}
Select one:b(constructor call must be the statement in a constructor)
a. a runtime error occurs
b. a compilation error occurs
c. nothing happens- the program compiles and runs
d. a logic error occurs

2. A static method can do which of the following? Select all that apply.
Select one or more:c,d
a. reference instance data variables in the class
b. invoke non-static methods in the class
c. invoke other static methods in the class
d. reference static variables in the class


3. A non-static (instance) method can do which of the following? Select all that
apply.
Select one or more: a,b
a. reference instance data variables in the class
b. invoke other non-static methods in the class
c. reference static variables in the class
d. invoke static methods in the class


4. Based on the following code fragments, which of the following are legal
statements? Select all that apply.
public class Bird {
public Bird() {
...
}
public int getFlightSpeed() { ...}
public static int getMaximumFlightSpeed() { ...}
}
Select one or more:b,c,d,e
a. int n = Bird.getFlightSpeed();
b. Bird b = new Bird();
c. int n = b.getFlightSpeed();
d. Bird b = new Bird();
b.getFlightSpeed();
e. int n = Bird.getMaximumFlightSpeed();


5. Write three overloaded methods that calculate averages. The first method takes
two integer parameters, the second takes three integer parameters, and the third
takes four integer parameters. Each method returns a double. Make sure all methods
are calculating the correct average and that the three methods are properly
overloaded.
/**
* @author Srinvas Palli
*
*/
public class CaliculateAverage {

   /**
   * @param a
   * @param b
   * @return
   */
   public double getAverage(int a, int b) {

       double average = (double) (a + b) / 2.0d;
       return average;

   }

   /**
   * @param a
   * @param b
   * @param c
   * @return
   */
   public double getAverage(int a, int b, int c) {

       double average = (double) (a + b + c) / 3.0d;
       return average;

   }

   /**
   * @param a
   * @param b
   * @param c
   * @param d
   * @return
   */
   public double getAverage(int a, int b, int c, int d) {

       double average = (double) (a + b + c + d) / 4.0d;
       return average;

   }

   /**
   * @param args
   */
   public static void main(String[] args) {
       CaliculateAverage caliculateAverage = new CaliculateAverage();
       System.out.println("Average of 2 and 3:"
               + caliculateAverage.getAverage(2, 3));
       System.out.println("Average of 2,3 and 4:"
               + caliculateAverage.getAverage(2, 3, 4));
       System.out.println("Average of 2,3,4 and 5:"
               + caliculateAverage.getAverage(2, 3, 4, 5));
   }
}
OUTPUT:
Average of 2 and 3:2.5
Average of 2,3 and 4:3.0
Average of 2,3,4 and 5:3.5


PART 2
Use the following enumerated data type for the next questions.
enum Creature { HOBBIT(false), ELF(false), DRAGON(true);
private boolean canFly;
private Creature(boolean canFly) {
this.canFly = canFly;
}
public boolean canFly() {
return canFly;
}
}
6. What is the name of the data type?
Answer:Creature
7. What is the ordinal value for the enum constant ELF?
Answer:false

8. Write a statement to declare a variable of this data type. The variable is
called myCreature and initialized with the value DRAGON.
Answer:Creature myCreature=Creature.DRAGON;

9. Using the variable you declared about, what is output by the following?
System.out.println(myCreature);
Answer:DRAGON

10.Using the variable declared above, what is output by the following?
System.out.println(myCreature.canFly());
Answer:true

11. The following statement is syntactically correct (meaning it will compile).
Creature newCreature = new Creature("HOBBIT");
Select one:False
a. True
b. False