Part 1 (6 points) Answer following questions [4 Points] Public vs. Private data
ID: 3693177 • Letter: P
Question
Part 1 (6 points)
Answer following questions
[4 Points] Public vs. Private data members
Consider the SimpleCar.Java and SimpleCarDriver.java programs given. Then answer following questions
A)How many constructors does SimpleCar class have? List all of them
B)What is the difference between a default constructor and other constructors that take parameters?
C)Which of the statements (numbered) in the SimpleCarDriver.java program are illegal (make compilation errors). Explain your answer.
[2 Points] Short answer questions
a)What is object oriented programming?
b)How does the object oriented programming achieve information hiding and encapsulation
Part 1I (14 points)
Date Class: Design and Implement a class called Date that has data members to store month (as a number), day, year, and name of the month. The class should have a three-parameter constructor that allows the data to be set at the time of new Data object instances are created. Default constructor that does not take any parameters should set the default values of 1 (month), 1 (day), 2001 (year). The class should have following three member functions to display date following formats
showDate1() should display the date in 1/1/2001 format
showDate2() should display the date in January 1, 2001 format
showDate3() should display the date in 1 January 2001 format
Also the class should have method to set the date (setDate()). This method should take month, day, and year as parameters and set the object instance data member values. Then your Date class should work with the Assignment5.java given below and produces the following output.
1/1/2001
February 12, 2010
29 August 1986
Press any key to continue . . .
Assignment5.java
public class Assignment5{
public static void main(String[] args){
Date d1 = new Date();
Date d2= new Date(2, 12, 2010);
d1.showDate1();
d2.showDate2();
d1.setDate(8, 29, 1986);
d1.showDate3();
}
}
Assignment6.java should have the following, in order:
In comments, the answers to written exercises for part one
The working Java code requested in Part #II for Assignment6.java.
these codes are provided...
and
Explanation / Answer
Part 1:
A)How many constructors does SimpleCar class have? List all of them
Answer: 3
1. SimpleCar(), 2. SimpleCar(int iniSpeed, String iniColor, double iniEPower), 3.SimpleCar(String iniColor)
B)What is the difference between a default constructor and other constructors that take parameters?
Answer: Default constructor:
1. Defined by compiler if there is no any other constructor in the program
2. NO method body
3. we cannot have an access to this constructor and we cannot change explicitly
4. Initializes the variables
Parameterised constructor:
1. Which has one or more parameters
2. Defained by the programmer
3. Initializes the value with entered parameters by the user while execution.
C)Which of the statements (numbered) in the SimpleCarDriver.java program are illegal (make compilation errors). Explain your answer.
Answer: Liine no. 3 and 6 will have compile time errors.
[2 Points] Short answer questions
a)What is object oriented programming?
Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.
b)How does the object oriented programming achieve information hiding and encapsulation
Information hiding is one of the most important principles of OOP inspired from real life which says that all information should not be accessible to all persons. Private information should only be accessible to its owner.
By Information Hiding we mean “Showing only those details to the outside world which are necessary for the outside world and hiding all other details from the outside world.”
Real Life Examples of Information Hiding
Your name and other personal information is stored in your brain we can’t access this information directly. For getting this information we need to ask you about it and it will be up to you how much details you would like to share with us.
An email server may have account information of millions of people but it will share only our account information with us if we request it to send anyone else accounts information our request will be refused.
A phone SIM card may store several phone numbers but we can’t read the numbers directly from the SIM card rather phone-set reads this information for us and if the owner of this phone has not allowed others to see the numbers saved in this phone we will not be able to see those phone numbers using phone.
In object oriented programming approach we have objects with their attributes and behaviors that are hidden from other classes, so we can say that object oriented programming follows the principle of information hiding.
In the perspective of Object Oriented Programming Information Hiding is,
“Hiding the object details (state and behavior) from the users”
Here by users we mean “an object” of another class that is calling functions of this class using the reference of this class object or it may be some other program in which we are using this class.
Information Hiding is achieved in Object Oriented Programming using the following principles,
· All information related to an object is stored within the object
· It is hidden from the outside world
· It can only be manipulated by the object itself
Advantages of Information Hiding
Following are two major advantages of information hiding. It simplifies our Object Oriented Model:
As we saw earlier that our object oriented model only had objects and their interactions hiding implementation details so it makes it easier for everyone to understand our object oriented model. It is a barrier against change propagation. As implementation of functions is limited to our class and we have only given the name of functions to user along with description of parameters so if we change implementation of function it doesn’t affect the object oriented model.
We can achieve information hiding using Encapsulation and Abstraction, so we see these two concepts in detail now.
Encapsulation
Encapsulation means “we have enclosed all the characteristics of an object in the object itself”.
Encapsulation and information hiding are much related concepts (information hiding is achieved using Encapsulation). We have seen in previous lecture that object characteristics include data members and behavior of the object in the form of functions. So we can say that Data and Behavior are tightly coupled inside an object and both the information structure and implementation details of its operations are hidden from the outer world.