How to analyze and run the code in java


Discuss the below:

Q: Access the monster.java program. Compile and run the program. There are ten lines of code marked (1)..(10) that you must explain after analyzing the code, running the program, and examining the output. Explanation of each of the lines of code (1..10), that is, what the code does logically and what it does in the program.

public class monster
{
static int claws = 5; //(1)
static String color = "red";
static int eyes = 4;

static void display()
{
System.out.println("color is: " +color);
System.out.println("claws are: " +claws);
System.out.println("eyes are: " +eyes);
}
}
class monsterTest
{
public static void main (String args[])
{
monster.display(); //using class
monster mymonster = new monster(); //(2)
monster yourmonster = new monster();
monster theirmonster = yourmonster; //(3)

mymonster.claws = 15; // (4)
monster.color = "green"; // (5)
theirmonster.eyes = 7; // (6)

System.out.println("claws: " + mymonster.claws); // (7)
System.out.println("color: " + monster.color); // (8)
System.out.println("color: " + yourmonster.color); // (9)
System.out.println("eyes: " + yourmonster.eyes); // (10)
}

}

/*
javac monster.java
java monsterTest

Output
color is: red
claws are: 5
claws: 15
color: green
color: green
*/

Solution Preview :

Prepared by a verified Expert
JAVA Programming: How to analyze and run the code in java
Reference No:- TGS01935727

Now Priced at $25 (50% Discount)

Recommended (95%)

Rated (4.7/5)