Computer Science, asked by krishyada5, 1 year ago

Write a program in Java with two private instance variables and create two different functions to set and get the value of those variables. Create the object of that class and show the use of those two functions to get and set the value in main function.

Answers

Answered by rushilansari
5

Answer:

1 // Fig. 3.1: Account.java

2   // Account class that contains a name instance variable

3   // and methods to set and get its value.

4

5   public class Account

6   {

7      private String name; // instance variable

8

9      // method to set the name in the object        

10      public void setName(String name)              

11      {                                              

12         this.name = name; // store the name        

13      }                                              

14

15      // method to retrieve the name from the object    

16      public String getName()                          

17      {                                                

18         return name; // return value of name to caller

19      }                                                

20   } // end class Account

Answered by PurveshItankar
10

Answer:

public class Account

{

 private String name;  

 public void setName(String name)              

  {                                              

     this.name = name;          

   }                                              

   public String getName()                          

    {                                                

      return name;  

     }                                                

  }

Explanation:

Please mark the answer as Brainlist

Also rate it 5 star

Also give a thanks

please

Similar questions