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

In Java: a. Will the following code work? * b. Why or why not? * c. If it does,

ID: 3915322 • Letter: I

Question

In Java: a. Will the following code work? * b. Why or why not? * c. If it does, what will the following code display? * --------------------------------------------------------------------------------------------------------- * public class MyClass{ int x, y, z; public MyClass(int x, int y, int z){ this.x = x; this.y = y; this.z = z; } public int getX(){ return x; } public int getY(){ return y; } public int getZ(){ return z; } public int myFunction(){ int number = getX()*getY()*getZ(); return number; } } public class Main{ public static void main(String[] args){ MyClass myClass = new MyClass(4, 5, 6); int number = myClass.myFunction(); System.out.println(number); } }

Explanation / Answer

If the following code is written in single source file, then it will not work, because according to java standards, every public class should be in its own source file.

Therefore class MyClass should be in a different source file with the name MyClass.java.

And the class Main should be in a source file named Main.java and main function should be in Main Class in file Main.java.

If both the public classes are in different files, then the code will display the value 120, as new object myClass of type MyClass is created  with values 4,5 and 6 for x,y and z member variables of this class respectively. Then the member function myFunction of class MyClass is called for the newly created object myClass. This function get the values of member variables x,y and z and multiplies them and returns their product. This product is the output of the code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote