Question

Consider the following code segment public class KaBoom public int b; public void m1 Your code goes here public void m2 KaBoom k) System. out. println (k. b) Complete the method body of m10 indicated by the comment line. The m10 method simply calls the m20 method by passing in the current KaBoom object variable to m20 For example: Test Result Ka Boom k new KaBoom 30 k.b 30 k.m1

Java question

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Main_KaBoom.java

import java.io.*;
import java.util.*;

class KaBoom{
   public int b;

   //Default Constructor
   KaBoom(){}

   //Parameterised Constructor
   KaBoom(int b){
       this.b = b;  
   }
  
   //User code
   public void m1(){

       KaBoom obj = new KaBoom(b);
       m2(obj);
   }

   public void m2(KaBoom k){
       System.out.println(k.b);
   }
}

//Test code
public class Main_KaBoom{

   public static void main(String args[]){

       KaBoom k = new KaBoom();
       k.b = 30;
       k.m1();
   }
}

//Output:

Command Prompt irjavac Main_KaBoom.java CaNair>java Main_KaBoom

Add a comment
Know the answer?
Add Answer to:
Java question Consider the following code segment. public class KaBoom { public int b; public void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Inner class class Outer {    private int x=10;    private static int y=20 ;...

    Java Inner class class Outer {    private int x=10;    private static int y=20 ;    publics void M1( ) {          int z=30;        class Inner           {             public void M2()              {                                                                              Sytem.out.println(“sum: ”+ (x+y+z));               }         } Inner i=new Inner();    i.M2() ; /// first call    i.M2(); // second call i.M2(); // third call }   // end of M1        publics static void main(String[] args)       {           Outer O = new...

  • Given the following Java code: class C { public int foo(C p) { return 1; }...

    Given the following Java code: class C { public int foo(C p) { return 1; } } class D extends C { public int foo(C p) { return 2; } public int foo(D p) { return 3; } } C p = new C(); C q = new D(); D r = new D(); int i = p.foo(r); int j = q.foo(q); int k = q.foo(r); (Remember that in Java every object is accessed through a pointer and that methods...

  • Given the following class: class Q2 { private int a; private int b; private int c;...

    Given the following class: class Q2 { private int a; private int b; private int c; public void setA(int a){this.a = a; } public void setB(int b){this.b = b;} public void setc(int c){this.c = c;} public int geta(){return a; } public int gets(){return b;} public int getc(){return c;} public int m1(int a, int b){ return a + b; public boolean m2 (int x, int y){ return m1(x, y) + x + y < 10; What is the output of the...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • Consider the following Java classes: class A{ public int foo () { return 1; } public...

    Consider the following Java classes: class A{ public int foo () { return 1; } public void message () { System.out.println( "A" + foo()); } } class B extends A { public int foo() {return 2; } } class C extends B { public void message () { System.out.println( "C" + foo()); } } (i) What are the outputs of the following code? (ii) What would be the outputs if Java used static dispatching rather than dynamic dispatching? B b...

  • This is for a java program public class Calculation {    public static void main(String[] args)...

    This is for a java program public class Calculation {    public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object    //Task 1. Write your code here //Print the prompt and ask the user to enter an...

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • JAVA MCQ QUESTION WHAT IS THE ANSWER AND WHY 22. Consider the following code: public class...

    JAVA MCQ QUESTION WHAT IS THE ANSWER AND WHY 22. Consider the following code: public class C public int myVar; public void outerMethod) int myVar--1 Something x- new SomethingO public void myMethod O How do you write values to the instance variable myVar from inside myMethod? A. this.myVar - B. myVar. C. super.myVar D. C.this.myVar E. super ) .myVar

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT