Rabu, 21 Desember 2011

soal_2_uts1

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package soal_2;

/**
 *
 * @author admin
 */
public class Soal2 {

     public String nama;
    public int tglLahir;
    public int blnLahir;
    public int thnLahir;

    public Soal2(){

    }
    public void setTglLahir(int tgl) throws Exception{
        if(tgl>0 && tgl <=31)
            this.tglLahir=tgl;
        else
            throw new Exception ("tanggal harus sesuai");
    }

     public void setBlnahir(int bulan) throws Exception{
        if(bulan>0 && bulan <=12)
            this.blnLahir=bulan;
        else
            throw new Exception ("bulan harus sesuai");
    }

      public void setThnahir(int tahun) throws Exception{
        if(tahun>0 && tahun >1900)
            this.thnLahir=tahun;
        else
            throw new Exception ("tahun harus sesuai");
    }

    /**
     * @return the nama
     */
    public String getNama() {
        return nama;
    }

    /**
     * @return the tglLahir
     */
    public int getTglLahir() {
        return tglLahir;
    }

    /**
     * @return the blnLahir
     */
    public int getBlnLahir() {
        return blnLahir;
    }

    /**
     * @return the thnLahir
     */
    public int getThnLahir() {
        return thnLahir;
    }


}

kelas main
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package soal_2;

import java.lang.Exception;
import javax.swing.JOptionPane;

/**
 *
 * @author admin
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Soal2 s = new Soal2();
        int tgl;
        int bln;
        int thn;
//        tanggal=Integer.parseInt(tgl);
        try {
            tgl = Integer.parseInt(JOptionPane.showInputDialog(null, " masukan tgl lahir anda:"));
            s.setTglLahir(tgl);
            
            bln=Integer.parseInt(JOptionPane.showInputDialog(null, "masukan bulan lahir anda:"));
            s.setBlnahir(bln);

            thn=Integer.parseInt(JOptionPane.showInputDialog(null, "Masukan tahun lahir anda:"));
            s.setThnahir(thn);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error  " + ex.getMessage()+"\n silahkan masukan yang benar");
        }

    }
}

Tidak ada komentar:

Posting Komentar