After reviewing the programming activity , explain the following discussion ques
ID: 3816577 • Letter: A
Question
After reviewing the programming activity, explain the following discussion questions (in the easiest possible way to understand):
*Programming Activity Guidance, Pause.java, SimpleDate.java, and SimpleDateClient.java all provided below:*
Textbook: Java Illuminated 4th Edition; Chapter 3 - Object-Oriented Programming, Part 1: Using Classes
Calling Methods Programming Activity 1
Programming Activity Guidance, Pause.java, SimpleDate.java, and SimpleDateClient.java all provided below:
Programming Activity 3-1 Guidance
=================================
Equivalent terms
----------------
For our purposes in this class, the following terms are synonyms:
- console
- Java console
- command line
- Command Prompt
Each of these represents the standard, text-based (non-graphical),
mechanism for user input and output.
Program output
--------------
When you run the program, you will see 2 windows:
A program animation window titled "A SimpleDate Object".
A console window.
As the animation proceeds, the following messages appear (one-by-one)
in the animation window:
1. dateObj reference declared
2. Instantiated dateObj - empty argument list
3. Set month to birth month
4. Set day to birth day
5. Set year to birth year
6. Set the date to the next day
7. Set day to 32
8. Set month to 13
9. Set object reference to null
After this, the following Java exception message appears in console window:
Exception in thread "main" java.lang.NullPointerException
at SimpleDateClient.workWithDates(SimpleDateClient.java:69)
at SimpleDateClient.main(SimpleDateClient.java:145)
The program has crashed (by design).
Click the close "x" in the animation window to close it.
The console window then displays the following message:
"Press any key to continue..."
Press a key (such as the spacebar) to close the console window.
dateObj object
--------------
At line 14 of the framework in the SimpleDateClient class, it already
defines the SimpleDate object reference that you are to use.
It is named dateObj. You will use this reference.
For example, to set the month in part 2 to say July, you would write the following statement:
dateObj.setMonth(7);
setDay function complex code
----------------------------
In the SimpleDate class file, you will see the following code in its setDay function:
day = ( dd >= 1 && isValidDay( dd ) ? dd : 1 );
Do not be alarmed by the complex look of this statement.
We will not learn it until chapter 5.
It is a form of what is called the "conditional operator".
When we do get to it, we will downplay it because it is too complex.
There are much more understandable ways to do what this statement does.
What it does is to ask if the day parameter (dd) is greater than or equal
to 1 and also is it a valid day for the month for this date object.
If it passes these tests, then it uses dd as the new value of "day" (the assignment statement).
If it does not pass these tests, it decides to set the day value to 1, which of course is a valid day.
A simpler and preferred way to do this is to use some simple "if" and "else" logic.
The important point is to see that this mutator (setter) function does not just
take the parameter and use it as the new value.
It does some validation on it to protect the object from an invalid value in its 'day' field.
------------------------------
SimpleDateClient.java
/* A client program to display SimpleDate object values
Anderson, Franceschi
*/
import java.awt.*;
import javax.swing.JFrame;
public class SimpleDateClient extends JFrame
{
private String action = "";
private int animationPause = 5; // 5 seconds between animations (was 2)
private SimpleDate dateObj; // declare SimpleDate object reference
public void workWithDates()
{
animate("dateObj reference declared");
/**** 1. Instantiate dateObj using an empty argument list */
// Part 1 student code starts here:
// Part 1 student code ends here.
animate("Instantiated dateObj - empty argument list");
/***** 2. Set the month to the month you were born */
// Part 2 student code starts here:
// Part 2 student code ends here.
animate("Set month to birth month");
/***** 3. Set the day to the day of the month you were born */
// Part 3 student code starts here:
// Part 3 student code ends here.
animate("Set day to birth day");
/***** 4. Set the year to the year you were born */
// Part 4 student code starts here:
// Part 4 student code ends here.
animate("Set year to birth year");
/***** 5. Call the nextDay method */
// Part 5 student code starts here:
// Part 5 student code ends here.
animate("Set the date to the next day");
/***** 6. Set the day to 32, an illegal value */
// Part 6 student code starts here:
// Part 6 student code ends here.
animate("Set day to 32");
/***** 7. Set the month to 13, an illegal value */
// Part 7 student code starts here:
// Part 7 student code ends here.
animate("Set month to 13");
/***** 8. Assign the value null to dateObj */
// Part 8 student code starts here:
// Part 8 student code ends here.
animate("Set object reference to null");
/***** 9. Attempt to set the month to 1 */
// Part 9 student code starts here:
// Part 9 student code ends here.
}
public SimpleDateClient()
{
super("A SimpleDate Object");
setSize(300, 300);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
// display action
g.drawString(action, 50, 250);
// object reference
int sX = 50, sY = 75;
int boxL = 75, boxH = 20;
g.drawRect(sX, sY, boxL, boxH);
g.drawString("dateObj", sX, sY - 10);
if (dateObj != null)
draw(g);
else
g.drawString("null", sX + 15, sY + 15);
}
public void draw(Graphics g)
{
int sX = 50, sY = 75;
int boxL = 75, boxH = 20;
// arrow
g.drawLine(sX + boxL, sY + boxH / 2,
sX + boxL + 25, sY + boxH / 2);
g.drawLine(sX + boxL + 25, sY + boxH / 2,
sX + boxL + 25, sY + boxH * 2);
g.drawLine(sX + boxL + 25 - 5, sY + boxH * 2 - 5,
sX + boxL + 25, sY + boxH * 2);
g.drawLine(sX + boxL + 25 + 5, sY + boxH * 2 - 5,
sX + boxL + 25, sY + boxH * 2);
// month
g.drawString("month", sX + boxL - 50, sY + 2 * boxH + 15);
g.drawRect(sX + boxL, sY + 2 * boxH, boxL, boxH);
g.drawString(Integer.toString( dateObj.getMonth( )),
sX + boxL + 5, sY + 2 * boxH + 15);
// day
g.drawString("day", sX + boxL - 50, sY + 3 * boxH + 15);
g.drawRect(sX + boxL, sY + 3 * boxH, boxL, boxH);
g.drawString(Integer.toString( dateObj.getDay( )),
sX + boxL + 5, sY + 3 * boxH + 15);
// year
g.drawString("year", sX + boxL - 50, sY + 4 * boxH + 15);
g.drawRect(sX + boxL, sY + 4 * boxH, boxL, boxH);
g.drawString(Integer.toString( dateObj.getYear( )),
sX + boxL + 5, sY + 4 * boxH + 15);
}
public void animate(String a)
{
action = a;
repaint( );
Pause.wait((double)animationPause);
}
public static void main(String [] args)
{
SimpleDateClient app = new SimpleDateClient( );
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.workWithDates( );
}
}
------------------------------------------------------
SimpleDate.java
/* A simple date class
Anderson, Franceschi
*/
public class SimpleDate
{
private int month;
private int day;
private int year;
/** default constructor
* sets month to 1, day to 1 and year to 2000
*/
public SimpleDate( )
{
setDate( 1, 1, 2000 );
}
/** overloaded constructor
* @param mm initial value for month
* @param dd initial value for day
* @param yyyy initial value for year
*
* passes parameters to set methods
*/
public SimpleDate( int mm, int dd, int yyyy )
{
setMonth( mm );
setYear( yyyy );
setDay( dd );
}
/* accessor methods */
int getMonth( ) { return month; }
int getDay( ) { return day; }
int getYear( ) { return year; }
/** mutator method */
/** setMonth
* @param mm new value for month
* if mm is between 1 and 12, sets month to mm
* otherwise, sets month to 1
*/
public void setMonth( int mm )
{
month = ( mm >= 1 && mm <= 12 ? mm : 1 );
}
/** setDay
* @param dd new value for day
* if dd is legal day for current month, sets day to dd
* otherwise, sets day to 1
*/
public void setDay( int dd )
{
day = ( dd >= 1 && isValidDay( dd ) ? dd : 1 );
}
/** setYear
* @param yyyy new value for year
* sets year to yyyy
*/
public void setYear( int yyyy )
{
year = yyyy;
}
/** sets date to the next day
*/
public void nextDay( )
{
if ( ! isValidDay( ++day ) )
{
day = 1;
if ( ++month > 12 )
{
month = 1;
year++;
}
}
}
private boolean isValidDay( int newDay )
{
int [] daysInMonth = { 0, 31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31 };
if ( newDay > daysInMonth[month] )
{
if ( month == 2 && isLeapYear( ) && newDay == 29 )
return true;
else
return false;
}
else
return true;
}
private boolean isLeapYear( )
{
return !( year % 4 != 0
||( year % 100 == 0 && year % 400 != 0 ) );
}
/** setDate
* @param mm new value for month
* @param dd new value for day
* @param yyyy new value for year
* passes parameters to setMonth, setDay, and setYear
*/
public void setDate( int mm, int dd, int yyyy )
{
setYear( yyyy ); // set year first (could be leap year)
setMonth( mm ); // set month next
setDay( dd ); // set day
}
/** toString
* @return String
* returns date in mm/dd/yyyy format
*/
public String toString( )
{
return month + "/" + day + "/" + year;
}
/** equals
* @param d Object to compare to this object
* @return true if d is equal to this object
* false, otherwise
*/
public boolean equals( Object d )
{
if ( !( d instanceof SimpleDate ) )
return false;
SimpleDate d1 = (SimpleDate)d;
if ( month == d1.month
&& day == d1.day
&& year == d1.year )
return true;
else
return false;
}
}
----------------------------
Pause.java
/* Pause class to pause applications
Anderson, Franceschi
*/
public class Pause
{
public static void wait( double seconds )
{
try
{
Thread.sleep( (int) ( seconds * 1000 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace( );
}
}
}
Complete Chapter 3, Programming Activity 1: Calling Methods. Make sure you study the Programming Activity 3-1 Guidance document. Which part of the guidance document was the most helpful to you? Why? Discuss the general flow of the program, starting in the "main" method of the Simple- DateClient class. (Do not be concerned at this point with the "paint'' or "draw" methods.) Why does setting the day to an illegal value (32) result in the day being set to 1 in- stead? Show the program statement in the SimpleDate class that does this. Why does setting the month to an illegal value (13) result in the month being set to 1 instead? Show the program statement in the SimpleDate class that does this. At the end of the program, a NullPointerException is generated. Show the program statement that causes this to happen. Explain why it happens. List the names of the SimpleDate class' "accessor" methods. List the names of the SimpleDate class' "mutator" methods. Discuss the mechanics of calling a method.Explanation / Answer
1.
a)we intantiated an object of simpledateclinet class.
b) Since it extends JFrame, it inherits few methods from it to funtion like a fram or window. One of which is the
defaultcloseoperation(). The parameter Jframe.exit on close specifies that the window will close when the cross mark on upperright corner i ppressed.
c) we invoked th method workwithdates of simpledateclass. Now whatever is in this method will be executed.
2)
day = ( dd >= 1 && isValidDay( dd ) ? dd : 1 );
What it does is to ask if the day parameter (dd) is greater than or equal
to 1 and also is it a valid day for the month for this date object.
If it passes these tests, then it uses dd as the new value of "day" (the assignment statement).
If it does not pass these tests, it decides to set the day value to 1, which of course is a valid day.
A simpler and preferred way to do this is to use some simple "if" and "else" logic.
The important point is to see that this mutator (setter) function does not just
take the parameter and use it as the new value.
It does some validation on it to protect the object from an invalid value in its 'day' field.
3)
month = ( mm >= 1 && mm <= 12 ? mm : 1 );
same logic as above.
4)
/***** 9. Attempt to set the month to 1 */
// Part 9 student code starts here:
dateObj=null;//here we put dateObj refernce variable to pint towrds null
dateObj.setMonth(1);//This is where null pointer exception happens.
Its because we tried to invoke setmonth() on refeernce which is pointing to null. The reference dateObj is pointing to a null object and we cannot invoke a setmonth() method on null;
5)
list of accessors:
getDay()
getMonth()
getYear()
6)
list of mutators:
setDay()
setMonth()
setYear()
7)
Generally, We call a method by writing its name, followed in parentheses by its arguments (one for each parameter in the method's header) As in the header (where parameters are separated by commas), arguments are are separated by commas as well. When we call a method, Java first evaluates each argument (each can be a simple or complicated expression) and transmits or passes it to its matching parameter; this just means that Java uses each argument's value to initialize it matching parameter in the method. It is equivalent to writing first-parameter = first-argument, then second-parameter = second-argument, etc.
for eg>
Math.max(double a)
Thus, when calling Math.max(3*x+5,y) above, the first parameter (a) is initialized by the value 14 (3*x+5: the equivalent of a = 3*x+5). Likewise, the second parameter (b) is initialized by the value 8 (y: the equivalent of b = y). Then, Java executes the body of the method, which typically performs some computation using these initialized parameters. It finally returns a result, by a mechanism that we discuss in the next section.
As a syntax constraint, Java requires that expression must be compatible with the return-type specified in the method's header (either be the same, or be implicitily convertible). In a void method (or a constructor), Java requires us to discard this option altogether. Typically, expression is a literal or a variable, but sometimes it is a more general expression using operators/method calls. For example, we will see methods that include the return statements
return true; return a; return i%divisor==0;
We use a return statement to terminate a method and specify what result (if any) it should return Whenever a method executes return, no matter what else it is is doing (i.e., inside loops or try-catch or ...), the method immediately terminates and returns to where the method was called (its call site). If the method returns a value, it is as if the method call is "replaced" by the value that it returns as a result.
8)
It was easy because it helped me visualize the flow of program using the GUI(Graphical user interface)
9)
All of the guidance documnet was Helpful. But most helpful was the explaination of the complex logic of set() methods .It helped me to understand it easily.
------------------------------------
/* A client program to display SimpleDate object values
Anderson, Franceschi
*/
import java.awt.*;
import javax.swing.JFrame;
public class SimpleDateClient extends JFrame
{
private String action = "";
private int animationPause = 5; // 5 seconds between animations (was 2)
private SimpleDate dateObj; // declare SimpleDate object reference
public void workWithDates()
{
animate("dateObj reference declared");
/**** 1. Instantiate dateObj using an empty argument list */
// Part 1 student code starts here:
dateObj=new SimpleDate();
// Part 1 student code ends here.
animate("Instantiated dateObj - empty argument list");
/***** 2. Set the month to the month you were born */
// Part 2 student code starts here:
dateObj.setMonth(9);
// Part 2 student code ends here.
animate("Set month to birth month");
/***** 3. Set the day to the day of the month you were born */
// Part 3 student code starts here:
dateObj.setDay(11);
// Part 3 student code ends here.
animate("Set day to birth day");
/***** 4. Set the year to the year you were born */
// Part 4 student code starts here:
dateObj.setYear(1977);
// Part 4 student code ends here.
animate("Set year to birth year");
/***** 5. Call the nextDay method */
// Part 5 student code starts here:
dateObj.nextDay();
// Part 5 student code ends here.
animate("Set the date to the next day");
/***** 6. Set the day to 32, an illegal value */
// Part 6 student code starts here:
dateObj.setDay(32);
// Part 6 student code ends here.
animate("Set day to 32");
/***** 7. Set the month to 13, an illegal value */
// Part 7 student code starts here:
dateObj.setMonth(13);
// Part 7 student code ends here.
animate("Set month to 13");
/***** 8. Assign the value null to dateObj */
// Part 8 student code starts here:
dateObj=null;
// Part 8 student code ends here.
animate("Set object reference to null");
/***** 9. Attempt to set the month to 1 */
// Part 9 student code starts here:
dateObj.setMonth(1);
// Part 9 student code ends here.
}
public SimpleDateClient()
{
super("A SimpleDate Object");
setSize(300, 300);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
// display action
g.drawString(action, 50, 250);
// object reference
int sX = 50, sY = 75;
int boxL = 75, boxH = 20;
g.drawRect(sX, sY, boxL, boxH);
g.drawString("dateObj", sX, sY - 10);
if (dateObj != null)
draw(g);
else
g.drawString("null", sX + 15, sY + 15);
}
public void draw(Graphics g)
{
int sX = 50, sY = 75;
int boxL = 75, boxH = 20;
// arrow
g.drawLine(sX + boxL, sY + boxH / 2,
sX + boxL + 25, sY + boxH / 2);
g.drawLine(sX + boxL + 25, sY + boxH / 2,
sX + boxL + 25, sY + boxH * 2);
g.drawLine(sX + boxL + 25 - 5, sY + boxH * 2 - 5,
sX + boxL + 25, sY + boxH * 2);
g.drawLine(sX + boxL + 25 + 5, sY + boxH * 2 - 5,
sX + boxL + 25, sY + boxH * 2);
// month
g.drawString("month", sX + boxL - 50, sY + 2 * boxH + 15);
g.drawRect(sX + boxL, sY + 2 * boxH, boxL, boxH);
g.drawString(Integer.toString( dateObj.getMonth( )),
sX + boxL + 5, sY + 2 * boxH + 15);
// day
g.drawString("day", sX + boxL - 50, sY + 3 * boxH + 15);
g.drawRect(sX + boxL, sY + 3 * boxH, boxL, boxH);
g.drawString(Integer.toString( dateObj.getDay( )),
sX + boxL + 5, sY + 3 * boxH + 15);
// year
g.drawString("year", sX + boxL - 50, sY + 4 * boxH + 15);
g.drawRect(sX + boxL, sY + 4 * boxH, boxL, boxH);
g.drawString(Integer.toString( dateObj.getYear( )),
sX + boxL + 5, sY + 4 * boxH + 15);
}
public void animate(String a)
{
action = a;
repaint( );
Pause.wait((double)animationPause);
}
public static void main(String [] args)
{
SimpleDateClient app = new SimpleDateClient( );
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.workWithDates( );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.