Objects and Classes -- the Counter object You will download an Answer Sheet and
ID: 3576200 • Letter: O
Question
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.
Explanation / Answer
Ans 1:
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...
Ans 2:
The initial value of counter1 = 0
It gets its value as
Ans 3:
variable valueOfCounter is public as in the declaration of this variable inside counter class, it is declared as
public int valueOfCounter;
Ans 4:
Basically Identifiers are the names of variables, methods, classes,etc.
Identifier access modifier
Counter(class) public
valueOfCounter(var) public
resetCounter( ) public
getCounter( ) public
clickCounter( ) public
Ans 5:
c2.clickCounter( ); is called for two times so the valueOfCounter is increased to 2 for the object c2.
Ans 6:
After changing Counter class access modifier to private it gives error as follow:
error: modifier private not allowed here private class Counter 1 error
Ans 7:
The private access modifier is only accessible within class where as public access modifier is accessible everywhere.
Ans 8:
If you don't use any modifier, it is treated as default.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.