Ask Question

Define a public class named Sample with two instance variables: an int called count and a String called name. Provide a complete constructor that allows both fields to be initialized when instances of Sample are created with count as its first parameter and name as its second. Neither count nor name should be public. Rather, you should provide: Both a getter and a setter for count Only a getter for name: this is how you can create a read-only variable in Java. Your getters and setters should follow the naming convention we have been using in class: get or set followed by the name of the field, capitalized. Getters should have the same return type as the variable that they are returning. Setters should not return a value.

+5
Answers (1)
  1. 12 December, 22:51
    0
    The code to this question can be given as:

    class Sample / /define class Sample.

    {

    //define private member.

    private int count;

    private String name;

    Sample (int count, String name) / /define parameterized constructor.

    {

    this. count = count; / /holding values.

    this. name = name;

    }

    public int getCount () / /define function with returntype.

    {

    return count;

    }

    public String getName () / /define function with returntype.

    {

    return name;

    }

    public void setCount (int count) / /define function with no returntype.

    {

    this. count = count;

    }

    public void setName (String name) / /define function with no returntype.

    {

    this. name = name;

    }

    }

    Explanation:

    In the above code firstly we define the class that is Sample. In this class, we define two variable that is not public. Then we define the constructor. This code we parameterized constructor because it takes the values as a parameter. To assign the value in the constructor we use this keyword. Then we used the get and set method. when we use get method because it returns a value (getcount and getname). When we use the set method it does not return any value (setcount and setname).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Define a public class named Sample with two instance variables: an int called count and a String called name. Provide a complete ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers