In the lab we will work onclass Employee so that it can guarantee that eachemplo
ID: 3612784 • Letter: I
Question
In the lab we will work onclass Employeeso that it can guarantee that eachemployee
object has reasonable data (no matter whatis passed to its constructor). The constructor
will within reason fix any bad data or, ifit is not clear how to fix the data, will throw a
checked exception.
1) Write a static method that is passed astring and that checks to see if the string ismade
up of just letters and spaces. Be sure towrite its postcondition ( it should not need to
have any preconditions.) Hint: use String'smethods length() and charAt(i). To see if a
char is a letter, you can compare it to 'a'and 'z' and to 'A' and 'Z', or you can look upsome
method in the appropriate wrapperclass.
2) Suppose class Xyz has a String field nameand an int field age Write a constructor for
class Xyz that is passed a name and an ageand stores them in the fields of the object that
it constructs if they are reasonable. But itthrows a checked exception if the name has
any characters other than spaces or letters,or if the age is negative. The exception that is
thrown should indicate in its message whatthe offending things was and its offending
value. (Call the method that you wrote inquestion 1).
3) I looked in the java documentation andfound java.util.Calendar.
Calendar cal = Calendar.getInstance();
int myYear = cal.get(Calendar.YEAR);
will get the current year (right now it willmake myYear have the value 2008)
Assume that class Employee has a field dobwhich is a string in the format mm/dd/yyyy.
Implement (i.e. write the body of) thefollowing method of class Employee:
public int getAge( ){
//returns this Employee's currentage
4) Implement the followingmethod:
static boolean isOkDate( int month, intday){
//returns true if month,day is a possiblecombination (where 0,3 represents January 3rd)
Explanation / Answer
2.
public class Xyz
{
public Xyz(String name, int age)
{
charc=check(name);
// if string does notconform
if(C!='')
{
throw(newCheckedException("'"+c+"' in name is not a letter or space.");
}
// checkif age is negative
else if(age<0)
{
throw(newCheckedException("The age of "+age+" is negative.");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.