Por favor alguém que já tenha feito as questões do Deitel me ajude, estou pensando nessa questão desde Sexta-feira. Eu já tentei e está dando estou de pilha e eu nem estou usando ainda o método checkDay. Se alguém já fez essa questão por favor me ajude pois estou estudando a mais ou menos dois meses e gostaria muito de fazer o livro do Deitel (3 Edição) e pra isso preciso terminar o cap 8 pra poder seguir.
OBS : Desculpem minha dúvida, sou iniciante e estou tentando fazer o livro de Deitel.
Questão 8.5 = Modifique a classe Date da figura 8.8 para realizar a verificação de erros nos valores inicializadores para as variáveis de instância month, day , year. Além disso, forneça um método nextDay para incrementar o dia por um. O objeto Date sempre deve permanecer em um estado consistente. Escreva um programa que testa o método nexDay em um laço que imprime a data durante cada iteração do laço para ilustrar que o método nextDay funciona corretamente. Certifique-se de testar os seguintes casos.
1-Incrementar para o próximo mês
2-Incrementar para o próximo ano
// Fig. 8.8: Date.java
// Declaration of the Date class.
package com.deitel.jhtp3.ch08;
public class Date extends Object {
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
// Constructor: Confirm proper value for month;
// call method checkDay to confirm proper
// value for day.
public Date( int mn, int dy, int yr )
{
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
System.out.println( "Month " + mn +
" invalid. Set to month 1." );
}
year = yr; // could also check
day = checkDay( dy ); // validate the day
System.out.println(
"Date object constructor for date " + toString() );
}
// Utility method to confirm proper day value
// based on month and year.
private int checkDay( int testDay )
{
int daysPerMonth[] = { 0, 31, 28, 31, 30,
31, 30, 31, 31, 30,
31, 30, 31 };
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for leap year
testDay == 29 &&
( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
System.out.println( "Day " + testDay +
" invalid. Set to day 1." );
return 1; // leave object in consistent state
}
// Create a String of the form month/day/year
public String toString()
{ return month + "/" + day + "/" + year; }
}
package questoesLivro;
public class Date {
private int month;
private int day;
private int year;
public Date() {
setDate(1, 1, 1);
}
public Date(int d, int m, int y) {
setDate(d, m, y);
}
public void setDate(int d, int m, int y) {
setDia(d);
setMes(m);
setAno(y);
}
/* private int checkDay(int testDay) {
int daysPerMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };
if (testDay > 0 && testDay < daysPerMonth[month])
return testDay;
if (month == 2 && testDay == 29
&& (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
return testDay;
System.out.println("Day " + testDay + "invalid. set to day 1");
return 1;
}
*/
public void setMes(int m) {
if ((m >= 1) && (m <= 12)) {
month = m;
}
if (m > 12) {
nextYear();
}
}
public void setDia(int d) {
if (d >= 1 && d < 32) {
//checkDay(d);
day = d;
}
if (d > 31) {
nextMonth();
}
}
public void setAno(int y) {
year = ((y > 1 && y <= 3000) ? y : 1);
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int nextDay() {
setDia(getDay() + 1);
while (getDay() >= 1 && getDay() < 32) {
System.out.println("DIA ====> " + getDay());
nextDay();
}
return getDay();
}
public int nextMonth() {
setMes(getMonth() + 1);
while (getMonth() >= 1 && getMonth() <= 12) {
System.out.println("Mes ====> " + getMonth());
setDia(1);
nextDay();
}
if (getMonth() > 12) {
setDia(1);
nextYear();
}
return getMonth();
}
public int nextYear() {
setAno(getYear() + 1);
while ((getYear() > 1 && getYear() <= 3000)) {
System.out.print("Ano ====> " + getYear());
setDia(1);
setMes(1);
System.out.println("Mes ====> " + getMonth());
nextDay();
}
return getYear();
}
public String toString() {
{
return day + "/" + month + "/" + year;
}
}
}
// Classe de Teste
package questoesLivro;
public class TesteDate {
private static Date d;
public static void main(String args[]) {
d = new Date(30,11,4);
d.nextDay();
//d.nextMonth();
//d.nextYear();
}
}