In the lab we will work on class Employee so that it can guarantee that each emp
ID: 3619415 • Letter: I
Question
In the lab we will work on class Employee so that it can guarantee that each employeeobject has reasonable data (no matter what is passed to its constructor). The constructor
will within reason fix any bad data or, if it is not clear how to fix the data, will throw a
checked exception.
1) Write a static method that is passed a string and that checks to see if the string is made
up of just letters and spaces. Be sure to write its postcondition ( it should not need to
have any preconditions.) Hint: use String's methods 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 up some
method in the appropriate wrapper class.
2) Suppose class Xyz has a String field name and an int field age Write a constructor for
class Xyz that is passed a name and an age and stores them in the fields of the object that
it constructs if they are reasonable. But it throws 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 what the offending things was and its offending
value. (Call the method that you wrote in question 1).
3) I looked in the java documentation and found java.util.Calendar.
Calendar cal = Calendar.getInstance();
int myYear = cal.get(Calendar.YEAR);
will get the current year (right now it will make myYear have the value 2008)
Assume that class Employee has a field dob which is a string in the format mm/dd/yyyy.
Implement (i.e. write the body of) the following method of class Employee:
public int getAge( ){
//returns this Employee's current age
4) Implement the following method:
static boolean isOkDate( int month, int day){
//returns true if month,day is a possible combination (where 0,3 represents January 3rd)
//(February 29 is ok. But -1,5 or 3,92 or 10,31 (November 31?) are all not ok
Explanation / Answer
please rate - thanks as per my message hope this is good 1) program with method to check if string is valid import java.util.*;import static java.lang.Character.*;
class checkstring
{
public static void main(String args[])
{String name;
Scanner in=new Scanner(System.in);
System.out.print("Enter a name: ");
name=in.nextLine();
if(checkit(name))
System.out.println("The name is valid");
else
System.out.println("The name is invalid");
}
public static boolean checkit(String n)
{int i;
for(i=0;i<n.length();i++)
if(!isSpaceChar(n.charAt(i))&&!isLetter(n.charAt(i)))
return false;
return true;
}
} 2) main in above should help 3) program to calculate age 4) program to check valid date
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.