In: Computer Science
Consider the following class and the main method below. Trace the code, then answer the questions on the right.
public class SomeClass { private String aName; private int aNumber; private boolean amAwesome; public SomeClass(String name, int number){ aName = name; aNumber = number; amAwesome = true; } public SomeClass(String name, int number, boolean awesome){ aName = name; aNumber = number; amAwesome = awesome; } public void methodAwesome(int number){ if(amAwesome) aNumber += number - 5; amAwesome = !amAwesome; } public int methodFantastic(boolean fantastic){ fantastic = fantastic && amAwesome; if(!fantastic && aNumber > 0) aName = aName + " is fantastic"; return aNumber *= -2; } public void methodAmazing(int num){ if(aNumber < 0) aNumber = num; } public static void main(String[] args) { SomeClass awesome = new SomeClass("Awesome", 5); awesome.methodAmazing(10); int awNum = awesome.methodFantastic(true); SomeClass amazing = new SomeClass("Amazing", -10, false); amazing.methodAwesome(7); amazing.methodAmazing(18); SomeClass fantastic = new SomeClass ("Fantastic", 11, true); int fanNum = fantastic.methodFantastic(true); fantastic.methodAwesome(awNum + fanNum); } }
1.How many objects are created in the main method?
2. What is the initial state of each object? (What are the values of each object’s variables?)
3. What is the state of each object just before the main method exits?
1.How many objects are created in the main method? Number of objects are created in the main method = 3 objects names are awesome, amazing, fantastic ========================================= 2. What is the initial state of each object? (What are the values of each object’s variables?) initial state of awesome object is aName = "Awesome"; aNumber = 5; amAwesome = true; initial state of amazing object is aName = "Amazing"; aNumber = -10; amAwesome = false; initial state of fantastic object is aName = "Fantastic"; aNumber = 11; amAwesome = true; ========================================= 3. What is the state of each object just before the main method exits? state of awesome object just before the main method exits aName='Awesome', aNumber=-10, amAwesome=true state of amazing object just before the main method exits aName='Amazing', aNumber=18, amAwesome=true state of fantastic object just before the main method exits aName='Fantastic', aNumber=-59, amAwesome=false