Selasa, 30 Mei 2023

[FIX] UAS Matkul Pemrograman Lanjut (PTI 4B)

Membuat Kalkulator + Converter suhu sederhana dengan Netbeans

 

[coding yg paling fix terbaru cek di github ya sobb..]

 
package shkalkulator;
 
import java.awt.Color;
import javax.swing.*;
 
/**
 * UAS Matkul Java Lanjut PTI 4B
 *
 * @author Soni Hendarkanto PTI 4B With Apache NetBeans IDE 16, Belsoft JAVA JDK
 * 19 on Linux Mint 21.1 "VERA" Cinnamon Desktop Edition
 *
 * My github for this project: https://github.com/Soni-Hendar-Kip/SHKalkulator
 *
 * link tutor: - original version from pak dosen rozi, and -
 * https://www.tutorialsfield.com/simple-calculator-program-in-java-using-swing/
 * -
 * https://www.duniaprogramming.com/2017/02/cara-membuat-program-kalkulator-keren.html
 * and -
 * http://ngoding-java.blogspot.com/2016/02/membuat-aplikasi-konverter-suhu-dengan.html
 */
public class NoxUASJavaLanjut extends javax.swing.JFrame {
 
    String TandaHitung = ""; //untuk mewakili simbol operator hitung +-/*
    String LayarAwal; //untuk mewakili layar awal
    boolean FirstClick = false;
 
    double Angka_ke1 = 0; //untuk mewakili angka pertama yg akan di hitung pada kalkulator
    double Angka_ke2 = 0; //untuk mewakili angka kedua
    double Hasil_Hitung; //untuk mewakili hasil perhitungan
 
    //UAS
    public Double NoxSuhuuuu; //variable hitung suhu, dianggap satuanya adalah celcius
    public Double NoxCelc; //celcius bro
    public Double NoxFahrell; //tau ndiri lah
    public Double NoxKelvinSanjaya; //mosok dadak di tulis
    public Double NoxRemunerasi; //reamur bro
 
    public String NoxHasilSuhu;
 
    public NoxUASJavaLanjut() {
        initComponents();
        NoxRadioBtOn_.setEnabled(false);
    }
 
    public void AturTextLayar(String k) {
        LayarAwal = NoxLayar_.getText();
        if (LayarAwal.equals("0")) {
            LayarAwal = LayarAwal.replace("0", "");
        }
        if (!TandaHitung.equals("") && FirstClick) {
            NoxLayar_.setText(k);
            FirstClick = false;
        } else {
            NoxLayar_.setText(LayarAwal + k);
        }
    }
 
    public void AturWarnaTombolAngka(boolean yes_or_no) {
        setBackground(Color.yellow);
    }
 
    public void HilangkanWarnaTombol(boolean yes_or_no) {
        NoxBt0_.setBackground(Color.LIGHT_GRAY);
        NoxBt1_.setBackground(Color.LIGHT_GRAY);
        NoxBt2_.setBackground(Color.LIGHT_GRAY);
        NoxBt3_.setBackground(Color.LIGHT_GRAY);
        NoxBt4_.setBackground(Color.LIGHT_GRAY);
        NoxBt5_.setBackground(Color.LIGHT_GRAY);
        NoxBt6_.setBackground(Color.LIGHT_GRAY);
        NoxBt7_.setBackground(Color.LIGHT_GRAY);
        NoxBt8_.setBackground(Color.LIGHT_GRAY);
        NoxBt9_.setBackground(Color.LIGHT_GRAY);
        NoxBtBagi_.setBackground(Color.magenta);
        NoxBtClear_.setBackground(Color.LIGHT_GRAY);
        NoxBtKali_.setBackground(Color.magenta);
        NoxBtKoma_.setBackground(Color.magenta);
        NoxBtKurang_.setBackground(Color.magenta);
        NoxBtPersen_.setBackground(Color.magenta);
        NoxBtSamaDengan_.setBackground(Color.blue);
        NoxBtTambah_.setBackground(Color.magenta);
        NoxBtDelete_.setBackground(Color.blue);
    }
 
    public void Modiarrr() {
        NoxBt1_.setEnabled(false);
        NoxBt2_.setEnabled(false);
        NoxBt3_.setEnabled(false);
        NoxBt4_.setEnabled(false);
        NoxBt5_.setEnabled(false);
        NoxBt6_.setEnabled(false);
        NoxBt7_.setEnabled(false);
        NoxBt8_.setEnabled(false);
        NoxBt9_.setEnabled(false);
        NoxBt0_.setEnabled(false);
        NoxBtAbout_.setEnabled(false);
        NoxBtAkarKuadrat_.setEnabled(false);
        NoxBtAkarKubik_.setEnabled(false);
        NoxBtBagi_.setEnabled(false);
        NoxBtClear_.setEnabled(false);
        NoxBtDelete_.setEnabled(false);
        NoxBtKali_.setEnabled(false);
        NoxBtKoma_.setEnabled(false);
        NoxBtKurang_.setEnabled(false);
        NoxBtPangkatKuadrat_.setEnabled(false);
        NoxBtPersen_.setEnabled(false);
        NoxBtSamaDengan_.setEnabled(false);
        NoxBtTambah_.setEnabled(false);
        NoxLayar_.setEnabled(false);
        NoxRadioBtOff_.setEnabled(false);
        NoxRadioBtOn_.setEnabled(true);
        NoxRadioCelcius_.setEnabled(false);
        NoxRadioFahrenheit_.setEnabled(false);
        NoxRadioKelvin_.setEnabled(false);
        NoxRadioReamur_.setEnabled(false);
        jPanel2.setBackground(Color.LIGHT_GRAY);
        jLabel2.setForeground(Color.GRAY);
        jLabel3.setForeground(Color.GRAY);
        jLabel1.setForeground(Color.red);
    }
 
    public void Muruppp() {
        NoxBt1_.setEnabled(true);
        NoxBt2_.setEnabled(true);
        NoxBt3_.setEnabled(true);
        NoxBt4_.setEnabled(true);
        NoxBt5_.setEnabled(true);
        NoxBt6_.setEnabled(true);
        NoxBt7_.setEnabled(true);
        NoxBt8_.setEnabled(true);
        NoxBt9_.setEnabled(true);
        NoxBt0_.setEnabled(true);
        NoxBtAbout_.setEnabled(true);
        NoxBtAkarKuadrat_.setEnabled(true);
        NoxBtAkarKubik_.setEnabled(true);
        NoxBtBagi_.setEnabled(true);
        NoxBtClear_.setEnabled(true);
        NoxBtDelete_.setEnabled(true);
        NoxBtKali_.setEnabled(true);
        NoxBtKoma_.setEnabled(true);
        NoxBtKurang_.setEnabled(true);
        NoxBtPangkatKuadrat_.setEnabled(true);
        NoxBtPersen_.setEnabled(true);
        NoxBtSamaDengan_.setEnabled(true);
        NoxBtTambah_.setEnabled(true);
        NoxLayar_.setEnabled(true);
        NoxRadioBtOff_.setEnabled(true);
        NoxRadioBtOn_.setEnabled(false);
        NoxRadioCelcius_.setEnabled(true);
        NoxRadioFahrenheit_.setEnabled(true);
        NoxRadioKelvin_.setEnabled(true);
        NoxRadioReamur_.setEnabled(true);
        jPanel2.setBackground(Color.getHSBColor(255, 255, 255));
        jLabel2.setForeground(Color.black);
        jLabel3.setForeground(Color.black);
        jLabel1.setForeground(Color.black);
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
    }// </editor-fold>                        
 
    private void NoxBt9_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
        AturTextLayar("9");
        HilangkanWarnaTombol(true);
        NoxBt9_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt6_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
        AturTextLayar("6");
        HilangkanWarnaTombol(true);
        NoxBt6_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt5_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
        AturTextLayar("5");
        HilangkanWarnaTombol(true);
        NoxBt5_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt2_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
        AturTextLayar("2");
        HilangkanWarnaTombol(true);
        NoxBt2_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt3_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:        
 
        AturTextLayar("3");
        HilangkanWarnaTombol(true);
        NoxBt3_.setBackground(Color.GREEN);
    }                                       
 
    private void NoxBtClear_ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        NoxLayar_.setText("");
 
        HilangkanWarnaTombol(true);
        NoxBtClear_.setBackground(Color.blue);
        NoxRadioCelcius_.setEnabled(true);
        NoxRadioCelcius_.setSelected(true);
        NoxRadioFahrenheit_.setEnabled(true);
        NoxRadioKelvin_.setEnabled(true);
        NoxRadioReamur_.setEnabled(true);
    }                                           
 
    private void NoxBtTambah_ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
 
        TandaHitung = "+";
        FirstClick = true;
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
 
        HilangkanWarnaTombol(true);
        NoxBtTambah_.setBackground(Color.YELLOW);
    }                                            
 
    private void NoxBtKurang_ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
 
        TandaHitung = "-";
        FirstClick = true;
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
 
        HilangkanWarnaTombol(true);
        NoxBtKurang_.setBackground(Color.YELLOW);
    }                                            
 
    private void NoxBtKali_ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
 
        TandaHitung = "*";
        FirstClick = true;
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
 
        HilangkanWarnaTombol(true);
        NoxBtKali_.setBackground(Color.YELLOW);
    }                                          
 
    private void NoxBtBagi_ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
 
        TandaHitung = "/";
        FirstClick = true;
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
 
        HilangkanWarnaTombol(true);
        NoxBtBagi_.setBackground(Color.YELLOW);
    }                                          
 
    private void NoxBtKoma_ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        if (NoxLayar_.getText().contains(".")) {
            return;
        } else {
            AturTextLayar("."); //koma dalam inggris menjadi . (titik)
        }
 
        HilangkanWarnaTombol(true);
        NoxBtKoma_.setBackground(Color.YELLOW);
 
    }                                          
 
    private void NoxBtPersen_ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
 
        Angka_ke2 = Double.parseDouble(NoxLayar_.getText());
        Hasil_Hitung = Angka_ke1 * Angka_ke2 / 100;
        NoxLayar_.setText(Double.toString(Hasil_Hitung));
    }                                            
 
    private void NoxBtSamaDengan_ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
        Hasil_Hitung = 0;
        Angka_ke2 = Double.parseDouble(NoxLayar_.getText());
        if (TandaHitung == "+") {
            Hasil_Hitung = Angka_ke1 + Angka_ke2;
        } else if (TandaHitung == "-") {
            Hasil_Hitung = Angka_ke1 - Angka_ke2;
        } else if (TandaHitung == "*") {
            Hasil_Hitung = Angka_ke1 * Angka_ke2;
        } else if (TandaHitung == "/") {
            Hasil_Hitung = Angka_ke1 / Angka_ke2;
        }
 
        String SHasil = Double.toString(Hasil_Hitung);
        NoxLayar_.setText(SHasil);
 
        HilangkanWarnaTombol(true);
        NoxBtSamaDengan_.setBackground(Color.RED);
    }                                                
 
    private void NoxBtAbout_ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        new abot().setVisible(true);
 
        /*JOptionPane.showMessageDialog(rootPane, "UAS Matkul Pemrograman lanjut\n"
                + "Dosen: Pak Fahrur Rozi\n\n"
                + "Kalkulator Sederhana + Converter Suhu Sederhana Juga   \n\n"
                + "Soni Hendarkanto (PTI 4B)\n"
                + "21183207051\n\n"
                + "NB:\n"
                + "Converter suhu hanya bisa satu arah, dari Celcius ke Lainnya,\n"
                + "misal Celcius ke Farenheit,\n"
                + "         Celcius ke Kelvin,\n"
                + "         Celcius ke Reamur,\n\n"
                + "Tidak bisa dari dari Farenheit ke Kelvin,\n"
                + "atau dari Reamur ke Farenheit", "Tentang Kalkulator ini", HEIGHT);*/
    }                                           
 
    private void NoxBt7_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
        AturTextLayar("7");
        HilangkanWarnaTombol(true);
        NoxBt7_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt8_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
        AturTextLayar("8");
        HilangkanWarnaTombol(true);
        NoxBt8_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt4_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
        AturTextLayar("4");
        HilangkanWarnaTombol(true);
        NoxBt4_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt1_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
 
        AturTextLayar("1");
        HilangkanWarnaTombol(true);
        NoxBt1_.setBackground(Color.GREEN);
 
    }                                       
 
    private void NoxBt0_ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        AturTextLayar("0");
        HilangkanWarnaTombol(true);
        NoxBt0_.setBackground(Color.GREEN);
    }                                       
 
    private void NoxBtDelete_ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        int PanjangAngka = NoxLayar_.getText().length();
        int DinasPajak = PanjangAngka - 1;
 
        if (PanjangAngka > 0) {
            StringBuilder HapusKarakter = new StringBuilder(NoxLayar_.getText());
            HapusKarakter.deleteCharAt(DinasPajak);
            NoxLayar_.setText(HapusKarakter.toString());
        }
 
        HilangkanWarnaTombol(true);
        NoxBtDelete_.setBackground(Color.YELLOW);
    }                                            
 
    private void NoxRadioBtOff_ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        Modiarrr();
        NoxLayar_.setText("");
        jLabel1.setText("OFF");
    }                                              
 
    private void NoxBtAkarKuadrat_ActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        // TODO add your handling code here:
 
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
        Double AkarKuadrat = Math.sqrt(Angka_ke1);
        NoxLayar_.setText(Double.toString(AkarKuadrat));
    }                                                 
 
    private void NoxBtAkarKubik_ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // Akar Kubik adalah akar 3, misal 27 hasilnya 3
 
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
        Double AkarKubik = Math.cbrt(Angka_ke1);
        NoxLayar_.setText(Double.toString(AkarKubik));
    }                                               
 
    private void NoxBtPangkatKuadrat_ActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        // Rumus pangkat kuadrat adalah Math.pow(angka, 2) artinya angka x angka;
        // bisa juga Math.pow(angka, 3) artinya angka x angka x angka;
        // Math.pow(7, 4) artinya 7x7x7x7;
 
        Angka_ke1 = Double.parseDouble(NoxLayar_.getText());
        Double PangkatKuadrat = Math.pow(Angka_ke1, 2);
        NoxLayar_.setText(Double.toString(PangkatKuadrat));
    }                                                    
 
    private void NoxRadioBtOn_ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        // TODO add your handling code here:
        Muruppp();
        NoxLayar_.setText("0");
        jLabel1.setText("Kalkulator Anti Mumet");
    }                                             
 
    private void NoxRadioCelcius_ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
        NoxLayar_.setText("0");
 
        NoxRadioFahrenheit_.setEnabled(true);
        NoxRadioKelvin_.setEnabled(true);
        NoxRadioReamur_.setEnabled(true);
    }                                                
 
    private void NoxRadioFahrenheit_ActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        // TODO add your handling code here:
        LayarAwal = NoxLayar_.getText();
 
        if (LayarAwal.equals("")) { //agar kalau layar kosong, tidak error
            JOptionPane.showMessageDialog(null, "Maaf...\n"
                    + "Angka di layar masih kosong");
            NoxRadioCelcius_.setSelected(true);
        } else {
            NoxSuhuuuu = Double.valueOf(NoxLayar_.getText());
            NoxFahrell = NoxSuhuuuu * 1.8 + 32;
            NoxHasilSuhu = Double.toString(NoxFahrell);
            NoxLayar_.setText(NoxHasilSuhu + " 'F");
 
            //NoxRadioFahrenheit_.setEnabled(false);
            NoxRadioKelvin_.setEnabled(false);
            NoxRadioReamur_.setEnabled(false);
            //NoxRadioCelcius_.setSelected(true);
        }
    }                                                   
 
    private void NoxRadioKelvin_ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        LayarAwal = NoxLayar_.getText();
 
        if (LayarAwal.equals("")) { //agar kalau layar kosong, tidak error
            JOptionPane.showMessageDialog(null, "Maaf...\n"
                    + "Angka di layar masih kosong");
            NoxRadioCelcius_.setSelected(true);
        } else {
            NoxSuhuuuu = Double.valueOf(NoxLayar_.getText());
            NoxKelvinSanjaya = NoxSuhuuuu + 273.15;
            NoxHasilSuhu = Double.toString(NoxKelvinSanjaya);
            NoxLayar_.setText(NoxHasilSuhu + " 'K");
 
            NoxRadioFahrenheit_.setEnabled(false);
            //NoxRadioKelvin_.setEnabled(false);
            NoxRadioReamur_.setEnabled(false);
            //NoxRadioCelcius_.setSelected(true);
        }
    }                                               
 
    private void NoxRadioReamur_ActionPerformed(java.awt.event.ActionEvent evt) {                                                
        // TODO add your handling code here:
        LayarAwal = NoxLayar_.getText();
 
        if (LayarAwal.equals("")) { //agar kalau layar kosong, tidak error
            JOptionPane.showMessageDialog(null, "Maaf...\n"
                    + "Angka di layar masih kosong");
            NoxRadioCelcius_.setSelected(true);
        } else {
            NoxSuhuuuu = Double.valueOf(NoxLayar_.getText());
            NoxRemunerasi = NoxSuhuuuu * 0.8;
            NoxHasilSuhu = Double.toString(NoxRemunerasi);
            NoxLayar_.setText(NoxHasilSuhu + " 'R");
 
            NoxRadioFahrenheit_.setEnabled(false);
            NoxRadioKelvin_.setEnabled(false);
            //NoxRadioReamur_.setEnabled(false);
            //NoxRadioCelcius_.setSelected(true);
        }
    }                                               
 
    /**
     * @param args the command line arguments
     */
 
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton NoxBt0_;
    private javax.swing.JButton NoxBt1_;
    private javax.swing.JButton NoxBt2_;
    private javax.swing.JButton NoxBt3_;
    private javax.swing.JButton NoxBt4_;
    private javax.swing.JButton NoxBt5_;
    private javax.swing.JButton NoxBt6_;
    private javax.swing.JButton NoxBt7_;
    private javax.swing.JButton NoxBt8_;
    private javax.swing.JButton NoxBt9_;
    private javax.swing.JButton NoxBtAbout_;
    private javax.swing.JButton NoxBtAkarKuadrat_;
    private javax.swing.JButton NoxBtAkarKubik_;
    private javax.swing.JButton NoxBtBagi_;
    private javax.swing.JButton NoxBtClear_;
    private javax.swing.JButton NoxBtDelete_;
    private javax.swing.JButton NoxBtKali_;
    private javax.swing.JButton NoxBtKoma_;
    private javax.swing.JButton NoxBtKurang_;
    private javax.swing.JButton NoxBtPangkatKuadrat_;
    private javax.swing.JButton NoxBtPersen_;
    private javax.swing.JButton NoxBtSamaDengan_;
    private javax.swing.JButton NoxBtTambah_;
    private javax.swing.JTextField NoxLayar_;
    private javax.swing.JRadioButton NoxRadioBtOff_;
    private javax.swing.JRadioButton NoxRadioBtOn_;
    private javax.swing.JRadioButton NoxRadioCelcius_;
    private javax.swing.JRadioButton NoxRadioFahrenheit_;
    private javax.swing.JRadioButton NoxRadioKelvin_;
    private javax.swing.JRadioButton NoxRadioReamur_;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroupSuhu_;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration                   
}
 





Tidak ada komentar:

Posting Komentar