Programs are belowNeed help answering a few question in Jav

( Programs are below)Need help answering a few question in Java and editing the two programs below, in accordance to what the questions are asking, thank you very much.

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

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

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

public vs. private in Class

4.List below the identifiers in the class Counter. Beside each indicate whether the identifier’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 for valueOfCounter 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 test your conclusion by modifying the methods in the Counter class and recompiling the program Lab05a.java.

9.Revise Counter.java again to add a method named setCounter. Give the method a parameter of the type int and then modify PLP11.java (name it xxPLP11a.java where xx are your initials) and replace the statement that attempts to modify c2.valueOfCounter directly with a call to setCounter with the value entered by the user in the previous statement as an argument. Record the output below:

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

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

Solution

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...2

The value of the c2 counter is...2

2)

The initial value of counter c1 is 0.

When we are creating the Counter class object ,using the code

Counter c1=new Counter();

(Inside the Counter class.The instance variable is valueOfCounter)

By default the instance variables are initialized with default values.

As the valueOfCounter variable is of type integer the default value is 0(Zero)

3)

Here the scope of the variable valueOfCounter is public.As we can see the variable valueOfCounter is specified with public access modifier.

4)

Identifiers in java program are nothing but the class name,method names,variable names,Interfaces.

Here in this program the list of identifiers are:

Identifier                                 :      Access Modifier

Counter(Name of the class) :     public access modifier

valueOfCounter(Variable name) :      Public access modifier

getCounter(),clickCounter(),,resetCounter() (Method names) : public access modifier

5)

Here in this program we are getting the valueOfCounter from the user as input and we are manually assigning the value to the variable valueOfCounter by calling with the object reference.

C2.valueOfCounter=count;

Here c2 is the reference which is pointing to the Counter class.By using that reference we can able to access the Counter class variables methods etc.

6)

When we changed the access modifier of valueOfCounter from public to private we cant able to access it from outside the class in which it has been declared.

As the scope of the Private access modifier is class level.

So when we change the access modifier of valueOfCounter from public to private we cant able to acees that in another class and we cant able to assign the value by using the class reference.

So we will get Compilation error when we are trying to compile.

7)By looking this we a=can able to understand that the scope of the private access modifiers is class level and the scope of the access modifier public is global.Global means we can access them from any where(from outside the class,or from outside the package)
8)

If we didn’t provide and access modifier before the identifier java provides the “default “ access modifier .

The scope of the “default ” access modifier is package level.We cant able to access the variables or methods from outside that packages if we the access modifier of them is default.

If we modify the scope of the variable valueOfCounter from private to default the we can able to access it from outside the class.As the scope of the ‘defalut’ access modifier is package level.

9)

Counter.java

public class Counter{
   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;
       }
   public void setCounter(int count)
   {
       this.valueOfCounter=count;
   }
   // end of method click} // end of class Counter
}

___________________

XXPLP11.java

import java.util.Scanner;

public class XXPLP11 {


   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.setCounter(count);
       System.out.println(\"The value of the c2 counter is...\" + c2.getCounter());

   }

}

______________________

Output:

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
The value of the c2 counter is...2

______Thank You

( Programs are below)Need help answering a few question in Java and editing the two programs below, in accordance to what the questions are asking, thank you ve
( Programs are below)Need help answering a few question in Java and editing the two programs below, in accordance to what the questions are asking, thank you ve
( Programs are below)Need help answering a few question in Java and editing the two programs below, in accordance to what the questions are asking, thank you ve
( Programs are below)Need help answering a few question in Java and editing the two programs below, in accordance to what the questions are asking, thank you ve

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site