Calcular Idade

Vingdel, vc é o usuário Delano Lima (Delano Lima de Carvalho) do UVa?
Vi no live submissions.

[]'s

eu testei o programa dele e funcionou bem. não vi erros. quanto a coisas a mais nem reparei. e quanto ao meu, que fiz, gostaram, que acharam. usei uma lógica inusitada ou diferente talvez… sempre há muitas formas de se fazer a mesma coisa. estava ali para download. segue o código…

[code]

import java.awt.;
import java.awt.event.
;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
*

  • @author raghy
    */

//falta um try catch para entradas erradas das esperadas…

public class CalcularIdade extends JFrame implements ActionListener {

JButton b1, b2;
JTextField jT1 = new JTextField();
JTextField jT2 = new JTextField();
JTextField jT3 = new JTextField();
JLabel jTvalor = new JLabel("Resultado : ");


int equacao = 0;
String data="";



 Date date= new Date();
String dataToda = "dd/MM/yyyy";
String formatod = "dd";    
  String formatoM = "MM";
    String formatoyy = "yy";
SimpleDateFormat formatter2 = new SimpleDateFormat(dataToda);


   int calculoAno;



public CalcularIdade() {

//painel e titulo doframe
setTitle("calcular idade");
setLocationRelativeTo(null);
setBounds(100,75,800,450);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setBackground(new Color(255,245,180));

    Font F = new Font("Sans Serif", Font.BOLD, 16);
    Font F1 = new Font("Sans Serif", Font.ITALIC, 14);
    Font F2 = new Font("Sans Serif", Font.ITALIC, 20);
    //botao 1 ou calcula...
    b1 = new JButton();
    b1.setText("Calcular");
    b1.setSize(100, 25);
    b1.setLocation(50, 200);
    b1.setBackground(Color.orange);
    b1.setForeground(Color.blue);
    b1.setFont(new Font("Sans Serif", Font.BOLD, 14));
    b1.setBorder(BorderFactory.createBevelBorder(1, Color.white, Color.black));
    b1.setEnabled(true);
    b1.addActionListener(this);



    b2 = new JButton();
    b2.setText("limpar");
    b2.setSize(100, 25);
    b2.setLocation(50, 260);
    b2.setBackground(Color.orange);
    b2.setForeground(Color.blue);
    b2.setFont(new Font("Sans Serif", Font.BOLD, 14));
    b2.setBorder(BorderFactory.createBevelBorder(1, Color.white, Color.black));
    b2.setEnabled(true);
    b2.addActionListener(this);





    //label1
    JLabel L1 = new JLabel();
    L1.setText("Coloque a idade");
    L1.setSize(200, 35);
    L1.setFont(F);
    L1.setLocation(45, 5);
    L1.setForeground(Color.blue);


    //label2
    JLabel L2 = new JLabel();
    L2.setText("DIA e MES com dois digitos ");
    L2.setSize(300, 35);
    L2.setFont(F);
    L2.setLocation(45, 25);
    L2.setForeground(Color.blue);


    //label2
    JLabel L3 = new JLabel();
    L3.setText("ANO com 4 digitos");
    L3.setSize(200, 35);
    L3.setFont(F);
    L3.setLocation(45, 45);
    L3.setForeground(Color.blue);




    //jT1

    jT1.setFont(F2);
    jT1.setText("");
    jT1.setLocation(40, 85);
    jT1.setSize(35, 35);



    //+
    JLabel mais = new JLabel();
    mais.setText("+");
    mais.setSize(200, 35);
    mais.setFont(F);
    mais.setLocation(93, 85);
    mais.setForeground(Color.blue);
    //+
    JLabel igual = new JLabel();
    igual.setText("=");
    igual.setSize(200, 35);
    igual.setFont(F);
    igual.setLocation(185, 85);
    igual.setForeground(Color.blue);





    //jT2

    jT2.setFont(F2);
    jT2.setText("");
    jT2.setLocation(120, 85);
    jT2.setSize(35, 35);



    //jT3

    jT3.setFont(F2);
    jT3.setText("");
    jT3.setLocation(210, 85);
    jT3.setSize(65, 35);



    //label resultado


    jTvalor.setSize(200, 35);
    jTvalor.setFont(F);
    jTvalor.setLocation(45, 135);
    jTvalor.setForeground(Color.blue);





    CalcularIdade.Painel p = new CalcularIdade.Painel();
    p.setBounds(490, 50, 270, 270);
    p.setBorder(BorderFactory.createLineBorder(Color.black, 2));
    getContentPane().setLayout(null);
    getContentPane().add(p);



    getContentPane().add(L1);
    getContentPane().add(L2);
    getContentPane().add(L3);


    getContentPane().add(mais);
    getContentPane().add(igual);

    getContentPane().add(jT1);
    getContentPane().add(jT2);
    getContentPane().add(jT3);

    getContentPane().add(jTvalor);


    getContentPane().add(b1);
    getContentPane().add(b2);








}



//acao dos botoes
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b1) {
      
       
           int dia=(Integer.parseInt(jT1.getText()) );
            int mes=(Integer.parseInt(jT2.getText()) );
             int ano=(Integer.parseInt(jT3.getText()) );
           
   data=""+jT1.getText()+"/"+jT2.getText()+"/"+jT3.getText();


  date= new Date();
 formatod = "dd";    
   formatoM = "MM";
     formatoyy = "yyyy";

formatter2 = new SimpleDateFormat(formatoyy);

calculoAno=(Integer.parseInt(formatter2.format(date)))    - ano;

SimpleDateFormat formatter3 = new SimpleDateFormat(formatod);
SimpleDateFormat formatter1 = new SimpleDateFormat(formatoM);
if ( (Integer.parseInt(formatter1.format(date))) >=mes && (Integer.parseInt(formatter3.format(date))) >=dia)
//ou seja se o mes atual for maior que o da data, ainda não fez aniversario.
{
calculoAno= ( (Integer.parseInt(formatter2.format(date))) - ano);
}

else {
       calculoAno=(   (Integer.parseInt(formatter2.format(date)))    - ano   )-1    ;
}

    }




    if (e.getSource() == b2) {

        jT1.setEnabled(true);
        jT2.setEnabled(true);
        jT3.setEnabled(true);

        jT1.setText("");
        jT2.setText("");
        jT3.setText("");


    }
}



 class Painel extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        Font F1 = new Font("Sans Serif", Font.BOLD, 30);
        g2.setColor(Color.white);
        g2.fillRect(10, 10, 250, 250);
        g2.setFont(F1);
        g2.setColor(Color.red);

        g2.drawString("" + data+""+"", 15, 125);
        formatter2 = new SimpleDateFormat(dataToda);
         g2.drawString("" + formatter2.format(date)+""+"", 15, 90);
         
           g2.drawString("" + calculoAno +"  anos.", 15, 175);

repaint();

    }
}
 
 
  public static void main(String[]args) {
 new CalcularIdade().setVisible(true);
}

}[/code]

só estranho que não consegui gerar o jar, através do netbeans… ele não enxerga o main. e não vejo o porquê.

segue o projeto para o netbeans, se alguém quiser ver , porque não gera o jar…

[quote=davidbuzatto]Vingdel, vc é o usuário Delano Lima (Delano Lima de Carvalho) do UVa?
Vi no live submissions.

[]'s[/quote]

Sim este sou eu…

[quote=davidbuzatto]Olá,

Viu só, não é tão trivial como parece :smiley:
Então, na verdade ele pede que tudo seja público, mas isso não influencia. Basta que sua classe se chame Main e que o seu código esteja no método main.
O Scanner deve ser criado a partir do System.in, como você faria em um programa normal.
[/quote]

[quote]Java Specifications:

The Java programs submitted must be in a single source code (not .class) file. Nevertheless, you can add as many classes as you need in this file. All the classes in this file must not be within any package.

All programs must begin in a static main method in a Main class.

[color=red]Do not use public classes[/color]: even Main must be non public to avoid compile error.

Use buffered I/O to avoid time limit exceeded due to excesive flushing.
[/quote]
Não tentei todos os 34 casos, mas tentei os 10 primeiro e ficou assim:[code]
C:\Users\Delano\Desktop>java Main
10

01/01/2007
10/02/2007

09/06/2007
28/02/1871

12/11/2007
01/01/1984

28/02/2005
29/02/2004

01/01/2004
02/01/2004

02/01/2130
01/01/2000

01/01/2031
02/01/1900

02/02/2031
02/02/1900

01/02/2031
01/02/2031

02/02/2031
03/02/2031

Case #1: Invalid birth date
Case #2: Check birth date
Case #3: 23
Case #4: 0
Case #5: Invalid birth date
Case #6: 130
Case #7: 130
Case #8: Check birth date
Case #9: 0
Case #10: Invalid birth date[/code]

Parece que está tudo certo. Porém acho que o problema está na minha validação, pois se nos teste eles digitam uma data inválida (exemplo: 32/11/2001) meu código pede a data de novo.

O pior é que eles não dizem o que não ficou nos padões propostos, se devo tratar esses erros, não deixam claro.

Abraço!

Olhe a descrição do input:

[]'s

[quote=davidbuzatto]Olhe a descrição do input:

[]'s[/quote]Enxuguei o código ao máximo e enviei, tomara que agora seja aceito.
Vou aguardar…

Abraço!