POO JAVA - Doubt Exercise

1

Well first follow below the exercise:

  

Create a class named Date that includes three pieces of information as instance variables:   a mês (tipo int) , a dia (tipo int) and a ano (tipo int) .

     

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

     

Write an application named DateTest   demonstrates the capabilities of the Date class.

I am doing something wrong but as I am learning I can not know what it is. Can you help me please?

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() );
    }

}
    
asked by anonymous 07.03.2018 / 18:12

2 answers

0

You are already creating the parameters of the class in the first lines, inside the constructor, refer to 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() );
    }

}
    
07.03.2018 / 18:50
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 should create a DateTest class and put the main method that is in Date today.

    
07.03.2018 / 18:48