POO JAVA - Doubt Exercise

Asked

Viewed 362 times

1

Good first follows down the exercise:

Create a class called Date including three parts of information as instance variables: one mês (tipo int), one dia (tipo int) and a ano (tipo int).

Provide a method set and a get for each instance variable. Provide a method displayDate showing the month, day and year separated by normal bars ( / ).

Write an app called DateTest that demonstrates the capabilities of the Date class.

I’m doing something wrong but as I’m learning I can’t know what it is.Can you please help me

public class Date {

    private int dia;  

    private int mes;  

    private int ano;  

    public Date(int d, int m, int a)  
    {  
        int dia=d;
        int mes=m;
        int ano=a;
    }  
    public void setDia(int d)  
    {  
        dia = d;  
    }  
    public int getDia()  
    {  
        return dia;  
    }  
    public void setMes(int m)  
    {  
        mes = m;  
    }  
    public int getMes()  
    {  
        return mes;  
    }  
    public void setAno(int a)  
    {  
        ano = a;  
    }  
    public int getAno()  
    {  
        return ano;  
    }  
    public void displayDate()  
    {  
        System.out.printf("%d/%d/%d", getDia(), getMes(), getAno());  
    }  


    public static void main( String[] args ) {
        int dia = 0;
        int mes = 0;
        int ano = 0;
        Scanner s = new Scanner( System.in );
        System.out.println( "Digite o dia: " );        
        dia = s.nextInt();
        System.out.println( "Digite o mês: " );
        mes = s.nextInt();
        System.out.println( "Digite o ano: " );
        ano = s.nextInt();

        Date d = new date(dia,mes,ano);
        System.out.println( "A data é: " + d.displayDate() );
    }

}
  • 1

    In the constructor do not use int dia = d only dia = d

2 answers

0

public Date(int d, int m, int a)  
{  
    this.dia=d;
    this.mes=m;
    this.ano=a;
}  

This operator is used when we want to reference members of the class itself.

Then according to the Exercise you must create a Datetest class and put the main method that is today in Date.

0


You are already creating the class parameters in the first lines, inside the constructor, reference them using the identifier this. Since you are learning OO, try to keep things separate. Each class in your file, to avoid confusion.


public class Date {

    private int dia;  

    private int mes;  

    private int ano;  

    public Date(int d, int m, int a)  
    {  
        this.dia=d;
        this.mes=m;
        this.ano=a;
    }  
    public void setDia(int d)  
    {  
        this.dia = d;  
    }  
    public int getDia()  
    {  
        return this.dia;  
    }  
    public void setMes(int m)  
    {  
        this.mes = m;  
    }  
    public int getMes()  
    {  
        return this.mes;  
    }  
    public void setAno(int a)  
    {  
        this.ano = a;  
    }  
    public int getAno()  
    {  
        return this.ano;  
    }  
    public String displayDate()  
    {  
       // Alterado para retornar uma String formatada em vez de printar.
       return String.format("%d/%d/%d", this.dia, this.mes, this.ano);  
    }  
}

public class DateTest {
    public static void main( String[] args ) {
        int dia = 0;
        int mes = 0;
        int ano = 0;
        Scanner s = new Scanner( System.in );
        System.out.println( "Digite o dia: " );        
        dia = s.nextInt();
        System.out.println( "Digite o mês: " );
        mes = s.nextInt();
        System.out.println( "Digite o ano: " );
        ano = s.nextInt();

        Date d = new Date(dia,mes,ano);
        System.out.println( "A data é: " + d.displayDate() );
    }

}
  • I made the adjustments, but this giving error in the last line "System.out.println( "The date is: " + d.displayDate() );"

  • 1

    Probably because the displayDate method has a void type return. To use this method the way you use in main, it must return a value (a string in this case of preference). So instead of you printing the method. vc returns the concatenated string.

  • @Caiosousa take a look now. I believe it’s corrected.

  • It worked, yes, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.