1. Use examples to explain the benefits of Encapsulation, Inheritance and Polymo
ID: 3827650 • Letter: 1
Question
1. Use examples to explain the benefits of Encapsulation, Inheritance and Polymorphism
2.Why we need a constructor in a class? If we don't create it, what will happen?
3.How are indefinite loops used for validating data? Why is a loop structure typically the better choice for validating data than an if statement?
4. while(10 > 1){ System.out.println("This prints forever."); } Identify the problem that exists in the above while loop.
5. Why would a programmer use curly braces in the body of a do…while loop?
Explanation / Answer
Answer:
1) Benefits of Encapsulation
Advantages of Polymorphism
· Method overloading allows methods that perform similar or closely related functions to be accessed through a common name. For example, a program performs operations on an array of numbers which can be int, float, or double type. Method overloading allows you to define three methods with the same name and different types of parameters to handle the array operations.
· Method overloading can be implemented on constructors allowing different ways to initialize objects of a class. This enables you to define multiple constructors for handling different types of initializations.
· Method overriding allows a sub class to use all the general definitions that a super class provides and add specialized definitions through overridden methods.
· Method overriding works together with inheritance to enable code reuse of existing classes without the need for re-compilation.
Inheritance Advantages:-
2)
A constructor is a bit of code that allows you to create objects from a class. You call the constructor by using the keyword new, followed by the name of the class, followed by any necessary parameters.
compiler creates a default constructor if we don’t define our own constructor (See this).Compiler created default constructor has empty body, i.e., it doesn’t assign default values to data members (In java, default constructors assign default values).
Compiler also creates a copy constructor if we don’t write our own copy constructor. Unlike default constructor, body of compiler created copy constructor is not empty, it copies all data members of passed object to the object which is being created.
3)
Programmers commonly use indefinite loops when validating input data. Validating data isthe process of ensuring that a value falls within a specified range. For example, suppose you require a user to enter a value no greater than 3. If the user enters 3 or less at the first prompt, the loop never executes. However, if the user enters a number greater than 3, the shaded loop executes, providing the user with another chance to enter a correct value. While the user continues to enter incorrect data, the loop repeats.Novice programmers often make the mistake of checking for invalid data using a decision instead of a loop. That is, they ask whether the data is invalid using an if statement; if the data is invalid, they reprompt the user. However, they forget that a user might enter incorrect data multiple times. Usually, a loop is the best structure to use when validating input data.
4)
The above code is an infinite loop. The expression 10 > 1 will always evaluate to true. Since this expression is part of a while loop, the body of the loop is entered and “Enter a new value” is displayed.Next, the expression is evaluated again. The expression 10 > 1 is still true, so the body is entered again. “Enter a new value” is displayed repeatedly. The loop never finishes because 10 > 1 is never false
5) the braces is very usefull because they simplify the reading .as per coding convention, it is advisable to use the curly even though you only have a single line of code in your loop blocks. This is to maintain the readability of your code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.