Objects and Classes the Counter object You will download an

Objects and Classes -- the Counter object

You will download an Answer Sheet and two Java source programs, and stepping through the Answer Sheet, run and observe output from the program(s), modify and test your changes while answering additional questions. Follow the steps enumerated below.
Procedure:

Prepare: Create a folder for the materials you will use for this Post Lab. Call it Lab11 perhaps.

Download the files and follow the steps: The Answer Sheet is a Word document (PLP11AnsweSheet.docx). The source program files are Java files (PLP11.java & Counter.java). Put them in the folder you creaed in the previous step.

Compile and run the programs in Eclipse: When directed, compile the programs and test using Eclipse. Both program should be copied to the directory in Eclipse.

Change the programs: When directed, modify the programs as directed in the Answer Sheet.

Observe & Answer: When directed, enter answes on the lines provided on the Answer Sheet. Modify the programs as directed as well. You should change the names of the client program. Be sure to update in Eclipse.

PLP11.java

/* **********************************************************     

  Program that will test the basic facilities of the

*user-defined class Counter.

*********************************************************/

import java.util.Scanner;

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

int count = 0;Scanner kBd = new Scanner(System.in);

/* **********************************************************   

    First create 2 \"instances\" of the object Counter*

and report on the initial contents (value) of those*     

instances...

******************************************************** */

    Counter c1 = new Counter( ); // create an instance of Counter  

  Counter c2 = new Counter( ); // create another instance   

System.out.println(\"Counter1 value at start is...\" + c1.getCounter( ));  

  System.out.println(\"Counter2 value at start is...\" + c2.getCounter( ));
/* **********************************************************   

    Next invoke the methods click and reset to explore*

     what those methods do. And how they do it. Report*    

   (echo) the value(s) of the 2 Counter instances after*   

    the invocations.** ******************************************************** */
    c1.clickCounter( );        // increment Counter c1  

c2.clickCounter( );        // increment Counter c2   

c2.clickCounter( );        // increment c2 again  

  System.out.println(\"After one \\\"call\\\" to click, Counter1 value is....\" +c1.getCounter( ));  

  System.out.println(\"After two \\\"calls\\\" to click, Counter2 value is...\" +c2.getCounter( ));   

c1.resetCounter( );        // reinitialize Counter c1  

  System.out.println(\"After \\\"call\\\" to reset, Counter1 value is...\" +c1.getCounter( ));  

  System.out.print(\"Enter a value for the c2 counter...\");    

count = kBd.nextInt();    

c2.valueOfCounter = count;  

  System.out.println(\"The value of the c2 counter is...\" + c2.getCounter());

  } // end of method main

} // end of class PLP11

Counter.java

/* ********************************************************
*
* A simple class to illustrate data abstraction in
* the Java language.
*
* ******************************************************** */
public class Counter
{
public int valueOfCounter; // contents 0 - 99
public void resetCounter( ) // reinitialize counter to 0
{ valueOfCounter = 0; } // end of method reset
public int getCounter( ) // retrieve contents of counter
{ return valueOfCounter; } // end of method get
public void clickCounter( ) // increment contents of counter
{ valueOfCounter = ++valueOfCounter % 100; } // end of method click
} // end of class Counter

Questions:

1. Compile and run PLP11.java and use the lines below to record the messagesgenerated by the program run:

2. What is the initial value of the counter c1? How does \"Java\" insure that the initialvalue will be predictable?

3. _________________ Is the integer variable valueOfCounter public or private? Howdo you know?

4.List below the identifiers in the class Counter. Beside each indicate whether theidentifier’s access modifier is public or private:

5. How does the program modify the contents of the valueOfCounter for the object c2?

6.Revise the Counter class file (Counter.java) to make the access modifier forvalueOfCounter private. Recompile the programs and record output below:

7.What does this suggest about the access modifiers private and public?

8.What is the \"default\" access modifier for a variable or method? You may want to testyour conclusion by modifying the methods in the Counter class and recompiling theprogram Lab05a.java.

Solution

===========================================================
1. Compile and run PLP11.java and use the lines below to record the messagesgenerated by the program run:

------------
Answer:
------------

   Counter1 value at start is...0
   Counter2 value at start is...0
   After one \"call\" to click, Counter1 value is....1
   After two \"calls\" to click, Counter2 value is...2
   After \"call\" to reset, Counter1 value is...0
   Enter a value for the c2 counter...

===========================================================
2. What is the initial value of the counter c1? How does \"Java\" insure that the initialvalue will be predictable?

------------
Answer:
------------

   Initial value of counter c1 is 0.
   Since the line Counter c1 = new Counter(); creates c1 object using default constructor of Counter class.
   Inside Counter class counter variable valueOfCounter of type premitive integer.
   The default values for all premitives in java are assigned to zero.
  
   Hence the c1 initial value is zero.

==========================================================
3. _________________ Is the integer variable valueOfCounter public or private? Howdo you know?


------------
Answer:
------------

       valueOfCounter of countr is public variable.

       Sicne the statement public int valueOfCounter; in class Counter class intialize the variable with
       public access specifier.


===========================================================
4.List below the identifiers in the class Counter. Beside each indicate whether theidentifier’s access modifier is public or private:

------------
Answer:
------------

   Only one identifier is present in the class Counter i,e valueOfCounter
   and it is of type public.

   Sicne the statement public int valueOfCounter; in class Counter class intialize the variable with public access specifier.


===========================================================
5. How does the program modify the contents of the valueOfCounter for the object c2?

------------
Answer:
------------

   For object c2 when clickCounter() method is invoked the value of valueOfCounter is incremented by 1.
   Since the statement c2.clickCounter() invokes the clickCounter function.

===========================================================
6.Revise the Counter class file (Counter.java) to make the access modifier forvalueOfCounter private. Recompile the programs and record output below:

------------
Answer:
------------

   Exception in thread \"main\" java.lang.Error: Unresolved compilation problem:
       The field Counter.valueOfCounter is not visible

       at PLP11.main(PLP11.java:37)
  
   Since the private variables can not accessed outside of the class. hence the above exception raised by compiler.


===========================================================
7.What does this suggest about the access modifiers private and public?

------------
Answer:
------------

   The error suggest inorder to access valueOfCounter identifier outside of class, The valueOfCounter identifier must of type public.
  
===========================================================
8.What is the \"default\" access modifier for a variable or method? You may want to testyour conclusion by modifying the methods in the
Counter class and recompiling theprogram Lab05a.java.

------------
Answer:
------------

  
   If all variables and methods of Counter class made to default access specifier.
Only the classes within the same package can access the methods and variables of Counter class.


Output remains same:
  
   Counter1 value at start is...0
   Counter2 value at start is...0
   After one \"call\" to click, Counter1 value is....1
   After two \"calls\" to click, Counter2 value is...2
   After \"call\" to reset, Counter1 value is...0
   Enter a value for the c2 counter...


===========================================================


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site