/ part 1 (6 points) answer following questions [4 ... Question Part 1 (6 points)
ID: 3693256 • Letter: #
Question
/ part 1 (6 points) answer following questions [4 ... 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... public class SimpleCar { private double enginePower; public String color; private int speed; public SimpleCar() { enginePower = 3.2; color = "White"; speed = 0; } public SimpleCar(int iniSpeed, String iniColor, double iniEPower) { enginePower = iniEPower; color = iniColor; speed = iniSpeed; } public SimpleCar(String iniColor) { enginePower = 3.2; speed = 0; color = iniColor; } public void speedUp(int newSpeed) { speed = newSpeed; } private void paint(String newColor) { color = newColor; } public String toString() { return ("Your car has the engine power " + enginePower + "Currently it is running " + speed + " miles per hour" + "Its color is " + color); } public int getCurrentSpeed() { return speed; } } and public class SimpleCarDriver { public static void main(String args[]) { SimpleCar car1 = new SimpleCar(); //--------------1 SimpleCar car2 = new SimpleCar(10, "Red", 3.2); //-----------2 car1.enginePower = 3.6; //------------3 car2.color = "Blue"; //---------4 car1.speedUp(100); //---------5 car1.paint("Red"); //--------6 } }
Explanation / Answer
1. Public members of a class are accessible from outside of the class while aas private members are accessible from within class only. In SimpleCar class color is public so can be acessed from outside class while speed and enginePower are private and cannot be accessed from outside.
2. SimpleCar has three constructors as
a. public SimpleCar()
b. public SimpleCar(int iniSpeed, String iniColor, double iniEPower)
c. public SimpleCar(String iniColor)
3. Default constructor either has no parameters, or if it has parameters, all the parameters have default values. If there is no used defined constructor then compiler itself creates one. All other construtors other than default constructer are user defined.
4. In SImpleCarDriver.java statement 2 and 5 have errors because enginePower and method paint are private member of SimpleCar class and cannot be accessed from outside of class.
5. Object-oriented programming is a programming language model organized around objects and follows 4 principles Abstraction, Encapsulation, Inheritance and Polymorphism.
6. In object oriented programming the members are specified using access specifier like private etc thus making members hidden which cannot be accessed from outside like speen is private member of SimpleCar class and thus it cannot be accessed from outside, so it cannot be changed from outside. And the whole working and members of a car are wrapped into a single unit called class and thus encapsulation.
7. Date Class
class Date
{
int month,day,year;
String nameOfMonth;
public Date()
{
month=1; year=2001; day=1;
nameOfMonth="January";
}
public Date(int d,int m, int y)
{
month=m; year=y; day=d;
switch(m)
{
case 1:
nameOfMonth="January";
break;
case 2:
nameOfMonth="February";
break;
case 3:
nameOfMonth="March";
break;
case 4:
nameOfMonth="April";
break;
case 5:
nameOfMonth="May";
break;
case 6:
nameOfMonth="June";
break;
case 7:
nameOfMonth="July";
break;
case 8:
nameOfMonth="August";
break;
case 9:
nameOfMonth="September";
break;
case 10:
nameOfMonth="October";
break;
case 11:
nameOfMonth="November";
break;
case 12:
nameOfMonth="December";
break;
}
}
public void showDate1()
{
System.out.println(day+"/"+month+"/"+year);
}
public void showDat2()
{
System.out.println(nameOfMonth+" "+day+", "+year);
}
public void showDat3()
{
System.out.println(day+" "+nameOfMonth+" "+year);
}
public void setDate(int m,int d, int y)
{
month=m; year=y; day=d;
switch(m)
{
case 1:
nameOfMonth="January";
break;
case 2:
nameOfMonth="February";
break;
case 3:
nameOfMonth="March";
break;
case 4:
nameOfMonth="April";
break;
case 5:
nameOfMonth="May";
break;
case 6:
nameOfMonth="June";
break;
case 7:
nameOfMonth="July";
break;
case 8:
nameOfMonth="August";
break;
case 9:
nameOfMonth="September";
break;
case 10:
nameOfMonth="October";
break;
case 11:
nameOfMonth="November";
break;
case 12:
nameOfMonth="December";
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.