Question
Java - Object Oriented Programming
16. Declare a class named customer that has two private fields? write a set method to make sure that fage 125 years or less than othen it is set to o (default value 17. What does it indicate when declaring a classs instance variables with private access modifier? 18. What is the role of a constructor? 19. The data part of by an objects instance variables is known as? 20. Do all methods have to always return a value or reference? 21. What keyword is associated with creating objects? 22. use the class Timew to create two instances named startIime, and EndTime? public class Timew. Assume the use of default constructor Give an example of each: 24. How many parameters does a default constructor have? 25. When you must use the get and set accessors?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 .

Add a comment
Know the answer?
Add Answer to:
Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • The Java class called Holiday is started below. An object of class Holiday represents a holiday...

    The Java class called Holiday is started below. An object of class Holiday represents a holiday during the year. This class has three instance variables: name, which is a String representing the name of the holiday day, which is an int representing the day of the month of the holiday month, which is a String representing the month the holiday is in public class Holiday { private String name; private int day; private String month; // your code goes here...

  • JAVA Write a class called Pen that contains the following information: 1. Private instance variables for...

    JAVA Write a class called Pen that contains the following information: 1. Private instance variables for the price of the pen (float) and color of the pen (String). 2. A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. 3. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • need help in java , on jgrasp Write a class named Room that has two fields...

    need help in java , on jgrasp Write a class named Room that has two fields one for the room's length and one for the idth The class should have a constructor that sets the length and the width anda method named "getArea" that returns the room's area (area = length x width). should also have a method named "isEqual" (with a Room argument) that determines the two rooms are equal or not. (25 points). Do not write the demo...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • In object-oriented programming, the object encapsulates both the data and the functions that operate on the...

    In object-oriented programming, the object encapsulates both the data and the functions that operate on the data. True False Flag this Question Question 101 pts You must declare all data members of a class before you declare member functions. True False Flag this Question Question 111 pts You must use the private access specification for all data members of a class. True False Flag this Question Question 121 pts A private member function is useful for tasks that are internal...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • Java Write a Temperature class that has two private instance variables: • degrees: a double that...

    Java Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have - four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT