Question

In: Computer Science

Question 1                                          &nb

Question 1                                                                                                               5 Marks

This question relates to the following class

public class QuestionX {

    private static QuestionX quest = new QuestionX();

     private QuestionX(){

     }

     public static QuestionX getQuestion() {

            return quest;

     }

     public void doSomething(){

           System.out.println("In doSomething");

     }

    

     //other methods for this class to do things

}

  1. How is this class used by a client? Provide code showing how you would use this class in the main method of a program.                                         
  2. What constraints are placed on class usage by the above implementation? Explain how this has been achieved.                                                

Solutions

Expert Solution

Here are the answers for your questions in Java Programming Language.

Kindly upvote if you find the answer helpful.

####################################################################

(a) How is this class used by a client? Provide code showing how you would use this class in the main method of a program.    

Answer : In the given class QuestionX, a private static variable which holds class object is declared. Its constructor is in private mode. Hence,an object of the class using constructor cannot be created but only single object can be created using the static method getQuestionX. To further use the methods in the class we have to first get the static object of QuestionX using the static method getQuestionX.

Only one object can be created and used for this class.

CODE :

Below code demonstrates how to use QuestionX using main method by client.

QuestionX.java

public class QuestionX {
private static QuestionX quest = new QuestionX();
private QuestionX(){
}
public static QuestionX getQuestion() {
return quest;
}
public void doSomething(){
System.out.println("In doSomething");
}
}

####################################################

QuestionTest.java (with main method)

public class QuestionXTest {
public static void main(String[] args){

//Get the static object 'quest' from static method getQuestionX()
QuestionX obj = QuestionX.getQuestion();
//Call member functions of the class using the static object.
obj.doSomething();
}
}

#################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

QuestionX.java

QuestionXTest.java

################################################################

OUTPUT :

###################################################################

(b) What constraints are placed on class usage by the above implementation? Explain how this has been achieved.

Answer :

Constraints on above class usage :

  • We cannot use constructor to create object of the class.
  • We cannot create multiple insances for the class.
  • To utitlise methods of the class we have to use the static object 'quest' from the method getQuestionX().

Explanation : We cannot use this class like we normally use other classes in Java. We cannot create multiple intances of the class since the class has private constructor, but can create only one static instance and use it to access class' public member variables/methods.

  • As class' constructor has private access, it cannot be used by other classes while initialising the class object. If we observe the code, inside the class itself there is a static variable named 'quest' of type 'QuestionX' which is calling the class constructor to initialise 'quest'.So we cannot create object of the class outside the class using the constructor but can use the object already created in the class and store it in a variable.
  • We have to notice that the static variable 'quest' is also has private access i.e., it cannot be accessed outside the class but there is a method in the class that returns this static object 'getQuestionX()', fortunately the access of the method getQuestionX() is 'public', hence it can be accessed outside the class.
  • As we are not creating objects using the class constructor we cannot access class' public member variables using instance(s) as we generally do. What all we can do is we can use the snigle static object 'quest' in the class and access the class' public member variables/functions. To do so we have to first get the object 'quest' from 'getQuestionX()' method and store it in a variable of type QuestionX and use the variable to access methods of the class.

This is how we can work on the class inspite of the constraints of the implementation.

###########################################################################

Any doubts regarding this can be explained with pleasure :)


Related Solutions

Question No. 4:                                          &nb
Question No. 4:                                                                                 (10marks) Surf the Internet and collect information about Central Bank of Oman (CBO). Then organize your findings into paragraphs (write 2 full pages at least).                                            Formatting: Paragraphs, titles with numbers in bold, 1.5 line spaces, Time New Roman etc. (2.5marks). Information collected: the sequences of your ideas (definition, historical and important facts…)            (5marks). Creativity: The presentation of the ideas, (summarizing ideas into figures and tables etc.) (2.5marks).
Question 2 (a)                                           &nb
Question 2 (a)                                                                                                Choose the most desirable investment proposal from the following alternative proposals using profitability index method: Proposal X Proposal Y Proposal Z Present Value of net cashflow (Rs.) 212,000 171,800 185,200 Amount required to invest (Rs.) 200,000 160,000 180,000 (b) AMCO Ltd is considering investing in a high class production plant which will smooth out its production process and will increase production up to 40 %. This production plant needs...
Question 3                                          &nb
Question 3                                                                                  (Total: 25 marks) Sikawan Systems manufactures two products in its Sabah division. Traditionally the company has used an overhead absorption system based on machine hours. However, following a management consultancy exercise in which outside consultants reviewed the management information systems, the directors have decided to pilot an Activity Based Costing system at the Sabah division. For the coming year, 2021, Sabah’s production overheads are estimated as follows: RM Factory rent and rates 42,200 Heat and...
QUESTION ONE                                        &nb
QUESTION ONE                                                                                                                               [20] Read the excerpt below and answer the question that follows: Business ethics are moral principles that guide the decisions that companies make. In a recent survey, MediaCom, a media and market research firm, reported that more than 50% of United Kingdom consumers age 16-19 say they have purchased or stopped using a brand because of its ethics. For instance, teens are more inclined to shop at Lush, a makeup brand that sells cosmetics made from...
Question – 3                                        &nb
Question – 3                                                                                                     30 Marks Plastic Manufacturing Company: Plastic Manufacturing Company manufactures Plastic Cartons. The Competition in the plastic industry is increasing. The production manager of the company is suggesting using standard costing system. He explains “Standard costing is a control technique where actual and standard costs and revenues are compared to obtain the variances and are used as an important tool to improve the performance” The company has the following standard cost card to manufacture each plastic carton. Direct...
PRACTICAL QUESTION                                       &nb
PRACTICAL QUESTION                                                                                          Tiger Construction Ltd signs a contract on 1 May 2018 to build a theme park. The construction is scheduled to commence on 1 July 2018 and the estimated date of completion is 30 June 2021. The total contract price is $5m and the cost of the park is initially estimated at $4.5m. The following data relates to the construction period: For the year ended 30 June 2019 2020 2021 $ $ $ Costs to date 1,700,000 3,000,000 4,800,000...
PRACTICAL QUESTION                                       &nb
PRACTICAL QUESTION                                                                                          Tiger Construction Ltd signs a contract on 1 May 2018 to build a theme park. The construction is scheduled to commence on 1 July 2018 and the estimated date of completion is 30 June 2021. The total contract price is $5m and the cost of the park is initially estimated at $4.5m. The following data relates to the construction period: For the year ended 30 June 2019 2020 2021 $ $ $ Costs to date 1,700,000 3,000,000 4,800,000...
3.         Question 3                               &nb
3.         Question 3                                                                                                      [Total: 20 marks] Assume a futures contract with 8 months to maturity is employed to hedge a well-diversified portfolio over the next 6 months.  The contract size of one futures contract is $250 times the index (i.e. the futures on the index). The value of the S&P500 index is currently 1200. The current futures price is $1220. The value of the portfolio that you are hedging is $ 40 million. The Beta of the portfolio is 1.25. The risk-free rate...
5.         Question 5                               &nb
5.         Question 5                                                                                                [Total: 20 marks] The spot price of silver is $50 per ounce. The storage costs are $ 0.52 per ounce to be paid at the end of the year. The futures contract on silver has 1 year and 6 months to maturity and the risk-free rate of interest is 0.9% per year with continuous compounding. What is the futures price (per ounce) if silver is considered to be an investment asset?  (Please display all computations.)                                                                    [10 marks]                                                                                                         Please define...
2.         Question 2                               &nb
2.         Question 2                                                                                 [Total: 20 marks] It is January and a company has to purchase 30,000 barrels of oil in May. The company enters into a long futures agreement for delivery of oil in June. The futures price for June delivery is $25 per barrel of oil. When the company closes out the futures position in May, the spot price of oil is $30 per barrel and the futures price is $29 per barrel. a) Please calculate the effective price per...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT