Question

In: Computer Science

Consider the following class and the main method below. Trace the code, then answer the questions...

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?

Solutions

Expert Solution

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

Related Solutions

  Answer the questions related to the following code:             Class B             {      &nbs
  Answer the questions related to the following code:             Class B             {                         Public:                                     Void b1();                         Protected:                                     Void b2();             };             Class A : public B             {                         Public:                                     Void  a1();                         Protected: Void  a2();             };             Class C: public A               {                         Public:                                     Void c1();             }; Void main () { B temp1; A temp2; C temp3; }    Name all member functions of all classes visible through temp1 in the main function? Name all member functions of all classes visible through temp2 in the main function? Name all...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: "); input = in.nextLine(); int a = 0; int b = 0; a = Integer.parseInt(input); System.out.println("Enter the second number: "); input = in.nextLine(); b = Integer.parseInt(input); int result = 0; result = a/b; System.out.println("a/b : " + result); } Copy the code to your Main.java file...
Given the following code below: public class Calculation { //method that returns cube of the given...
Given the following code below: public class Calculation { //method that returns cube of the given number public int findMax(int arr[]){    int max=0;    for(int i=1;i<arr.length;i++){        if(max<arr[i]){           max=arr[i]; }     }     return max;   } //method that returns cube of the given number   public static int cube(int n){        return n*n*n;     }   } (5 points) Define the class CalculationTest a subclass of TestCase (10 points) Define a setUp() method (20 points) Define a test method testfindMax that exercises Calculation.findMax() in Calculation class (5 points)Define...
27) Consider the student class. There is a method setSemesterGrade(int grade) In that method write code...
27) Consider the student class. There is a method setSemesterGrade(int grade) In that method write code to throw a new IllegalStateException if the average is less than 0 And an IndexOutOfBoundsException if the average is > 0 28) Consider a student represented by the variable s1 and we are trying to set the average of s1. Write a try catch routine to catch either error listed above try { s1.setSemesterGrade(-30); s1.setSemesterGrade(200);
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } Question 21 Not yet answered Marked out of 1.00 Flag question Question text D3a - [1 Mark] How many field attributes are there in the TrafficLight class? Answer: Question 22 Not yet answered Marked out of 1.00 Flag...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } 1 :How many field attributes are there in the TrafficLight class? 2 :Name a field attribute for this class. 3 :What is the name of the method that is an accessor? 4 :What is the name of the...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Based on the below code: Answer the following questions and explain your answers: i)Presume that the...
Based on the below code: Answer the following questions and explain your answers: i)Presume that the code is instantiated in a process and it has JUST returned from its own call to fork() and there is now a clone of that process that is itself “just about” to pick up execution. Where will the clone child pick up its execution? ii)How does the clone child know it’s a clone? How does the parent process know it is not a clone,...
For Questions 1-3: consider the following code: public class A { private int number; protected String...
For Questions 1-3: consider the following code: public class A { private int number; protected String name; public double price; public A() { System.out.println(“A() called”); } private void foo1() { System.out.println(“A version of foo1() called”); } protected int foo2() { Sysem.out.println(“A version of foo2() called); return number; } public String foo3() { System.out.println(“A version of foo3() called”); Return “Hi”; } }//end class A public class B extends A { private char service; public B() {    super();    System.out.println(“B() called”);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT