Computer Applications

Your friend has tried to write a program using a class as shown below:

............... class Friend_list // Line 1
{
    void disp_name()
    {
        System.out.println("Amit Kumar");
    }
    void disp_address()
    {
        System.out.println("Lane B: 3/1, Akash Ganga");
        System.out.println("Mayur Vihar, New Delhi");
    }
    public static void main()
    {
        Friend_list myself = new ...............(); // Line 2
        ................disp_name(); // Line 3
        myself................();// Line 4
    }
}

Due to confusion, he could not complete the above program. He left blank spaces in some of the statements marked with Line 1, Line 2, Line 3 and Line 4 that are to be filled with appropriate keywords/objects.

Help your friend to complete his program by answering the following questions:

(a) Name the access specifier that should be filled in the statement marked with Line 1.

(b) Name the element to be filled in the statement marked with Line 2.

(c) What should be filled in the statement marked with Line 3?

(d) What function name should be filled in the statement marked with Line 4?

Objects & Classes

8 Likes

Answer

(a) public

(b) Friend_list

(c) myself

(d) disp_address()

Explanation

The detailed explanation of the code is as follows:

1. Class Declaration:

public class Friend_list
  • This line defines a class named Friend_list.
  • The keyword public makes the class accessible from anywhere in the program.

2. Method disp_name():

void disp_name()
{
    System.out.println("Amit Kumar");
}
  • This is a method named disp_name(), which prints "Amit Kumar".
  • The void keyword means that the method does not return any value.
  • The System.out.println() statement prints the text and moves the cursor to the next line.

3. Method disp_address():

void disp_address()
{
    System.out.println("Lane B: 3/1, Akash Ganga");
    System.out.println("Mayur Vihar, New Delhi");
}
  • This method prints an address in two separate lines.
  • The first System.out.println() prints "Lane B: 3/1, Akash Ganga".
  • The second System.out.println() prints "Mayur Vihar, New Delhi".

4. main() Method:

public static void main()
{
    Friend_list myself = new Friend_list(); 
    myself.disp_name(); 
    myself.disp_address();
}
  • The main() method is the entry point of the program.

  • Inside main(), an object of the Friend_list class is created using: Friend_list myself = new Friend_list();

    • myself is an object reference of the class.
    • The new keyword creates an instance (object) of Friend_list.
  • The object myself is then used to call the methods:

    myself.disp_name(); 
    myself.disp_address();
    
    • disp_name() prints "Amit Kumar".
    • disp_address() prints the two-line address.

The completed code is as follows:

public class Friend_list 
{
    void disp_name()
    {
        System.out.println("Amit Kumar");
    }
    void disp_address()
    {
        System.out.println("Lane B: 3/1, Akash Ganga");
        System.out.println("Mayur Vihar, New Delhi");
    }
    public static void main()
    {
        Friend_list myself = new Friend_list(); 
        myself.disp_name(); 
        myself.disp_address();
    }
}

Answered By

6 Likes


Related Questions