Declare a class named Customer that has two private fields? Write a set method t
ID: 3789937 • Letter: D
Question
Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return a value or reference? What keyword is associated with creating objects? Use the class TimeW to create two instances named startime. and EndTime? public class TimeW. Assume the use of default constructor. What two type of scope can identifiers in Java have? Give an example of each: How many parameters does a default constructor have? When you must use the get and set accessors?Explanation / Answer
16.ans)
public class Customer {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
if(this.age>125||this.age<0)
{
this.age=0;
}else
this.age=age;
}
}
17.ans)
It indicates the fields or attributes which are declared with private access modifier those attributes are accessable within the class only.through private access modifiers we can acheive data security.
18.ans)whenever you want to create object we want to need pre instantiation logic to initialize those objects.
for example if we need while creating the object manually we need to set the values.ex:Customer.setAge(5);
if we have multiple instance variables we need to initialize all variables after creating the object.so for these java provide constructor because initialisation process is repeating task so sun provided default constructor and while creating the objects if we want to create the object with user defined values sun provided paramererisable constructor.so while creating object jvm call this constructors to create the object with prepopulated values.
19.ans)the data part of by instance variable's known as in java is called member variables.
20)all the methods returns a value for the variables.and for objects it returns reference
21)to creating the objects java provided new keyword.
22)the use of default constructor while creating the object it calls the constructor.to create the object with default values default constructor is useful.
23)the scope of java identifiers are block scope:the identifiers which are declared in blocks,withing methods
object scope:the identifiers which are declared as instance/member have the object scope
24)default constructor don't have any parameters.
25)if we declared as member variables as private we must use getters and setters.
public class Account{
private int balance;
public int getBalance()
{
this.balance=balance;
}
}
after end this class we can't access balance member variable in outside of the class.in this case we need getters and setters.To provide security for the data we declared as private .those are not accessble by outside of the class .In this case we must use getters and setters .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.