Chapter 4 Savitch Exercises (part)

Display 4.1

public class DateFirstTry {
public String month;
public int day;
public int year;

public void writeOutput() {
System.out.println(month + " " + day + ", " + year);
}}
-------------------------------

1. Write a method called makeItNewYears that could be added to the class DateFirstTry in Display 4.1 The method makeItNewYears has no parameters and sets the instance variable to "January" and the day instance variable to 1. It does not change the year instance variable.

 

 

2. Write a method called yellIfNewYear that could be added to the class DateFirstTry in Display 4.1 The method yellIfNewYear has no parameters and outputs the string "Hurrah!" provided the month instance variable has the value "January" and the day instance variable has the value 1. Otherwise, it outputs the string "Not New Year's Day".

 

 

Display 4.2

public class DateSecondTry {
private String month;
private int day;
private int year;

public void writeOutput() {
System.out.println(month + " " + day + ", " + year);
}
public int getDay() { return day;}

public int getYear() { return year;}

public int getMonth() {
if (month.equalsIgnoreCase("January")) return 1;
else if (month.equalsIgnoreCase("February")) return 2;
... }
}
-------------------------------

3. Write a method called getNewYear that could be added to the class DateSecondTry in Display 4.2 The method getNewYear returns an int value equal to the value of the year instance variable plus one.

 

 

4. Write a method called happyGreeting that could be added to the class DateSecondTry in Display 4.2. The method happyGreeting writes the string "Happy Days" to the screen a number of times equal to the value of the instance variable day. For example, if the value of day is 3, then it should write the following to the screen:

Happy Days!
Happy Days!
Happy Days!

Use a local variable.

 

5. The method writeOutput in Display 4.2 uses the instance variables month, day, and year, but gives no object name for these instance variables. Every instance variable must belong to some object. To what object or objects do these instance variables in the definition of writeOutput belong?

 

6. Rewrite the definitions of the methods getDay and getYear in Display 4.2 using the this parameter.

 

 

7. Rewrite the method getMonth in Display 4.2 using the this parameter.

 

8. When should an instance variable be marked private?

9. When should a method be marked private?

10. If a class is named coolClass, what names are allowed as constructors in the class CoolClass?

11. Suppose you have defined a class like the following for use in a program:

public class YourClass {
private int information;

public YourClass(int newInfo, char more newInfo) { <Details not shown> }
public YourClass() {<Details not shown>}
public void doStuff() {<Details not shown>} }

Which of the following are legal in a program that uses this class?

YourClass anObject = new YourClass(42, 'A');
YourClass anotherObject = new YourClass(41.99, 'A');
YourClass yetAnotherObject = new YourClass();
yetAnotherObject.doStuff();
YourClass oneMoreObject;
oneMoreObject.doStuff();
oneMoreObject.YourClass(99, 'B');

 

12. What is a no-argument constructor? Does every class have a no-argument constructor? What is a default constructor?


Revised: November 3, 2008