Checking up with some real basic java mutator questions: Given.... public class
ID: 3587069 • Letter: C
Question
Checking up with some real basic java mutator questions:
Given....
public class SimpleDate {
private String month;
private int dat;
private int year;
public static final int FIRST_YEAR = 1970;
....
Part A) Write a mutator method for the year that prevents the year from being set before the first year that computers started counting time
Part B) Write a mutator method for the month. You can assume that the value is provided by the method is valid
Part C) write a mutator method for day, if the value given to this methis is less than 1, it print "Days are greater than zero" Otherwise it should set the numbers of days to provided value OR the maximum days in the month. (30 days for april/june/september/novem... 28 for febuary... and rest with 31)
Explanation / Answer
class SimpleDate
{
private String month;
private int dat;
private int year;
public static final int FIRST_YEAR=1970;
//part a. mutator function for the year
public void setYear(int year)
{
if(year>=FIRST_YEAR)
{
this.year=year;
}
else
this.year=FIRST_YEAR;
}
//part b. mutator function for the month
public void setMonth(String month)
{
this.month=month;
}
//part c. mutator function for the day
public void setDay(int date)
{
if(date<1)
{
System.out.println("Days are greater than zero");
}
else
{
int max;
switch(month)
{
case "april":
case "june":
case "september":
case "november":
max=30;
break;
case "february":
max=28;
break;
default:
max=31;
}
if(date>max)
date=max;
this.dat=date;
}
}
//utility function to check the result
public void getFullDate()
{
System.out.println(this.dat+" "+this.month+" "+this.year);
}
};
//main class to test the code
public class Mine
{
public static void main(String[] args)
{
SimpleDate day=new SimpleDate();
day.setYear(1960);
day.setMonth("february");
day.setDay(29);
day.getFullDate();
day.setYear(1980);
day.setMonth("march");
day.setDay(-5);
day.getFullDate();
day.setYear(1980);
day.setMonth("march");
day.setDay(100);
day.getFullDate();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.