javaku

Jumat, 23 Desember 2011

PROGRAM HITUNG BERAT BADAN

import java.io.*;
import java.util.*;

class NilaiMahasiswa{
                final static int M=1000;
                static class Mhs{String nama;int tinggiBadan;int beratBadan;}
                static Mhs[] mahasiswa=new Mhs[M];
                static int jumlahData;
                static Scanner key=new Scanner(System.in);
                static PrintStream tulisFile=null;
                static Scanner bacaFile=null;
                static String namaFile="";
                static     int jumtg=0;
                static     int jumbr=0;
                static     int jumbrtg=0;
                static     int jumbrbr=0;
                static     int jumtgtg=0;
                static     int ind=0;
                static  double b=0;
                static  double a=0;
               
//Program Utama
public static void main(String[] args){
                               System.out.println("*=======================****========================*");
System.out.println("*                                                   *");
System.out.println("*SELAMAT ANDA MEMASUKI KAWASAN MENGITUNG BERAT BADAN*");
System.out.println("*                                                   *");
System.out.println("*=======================****========================*");
Menu();
                }
               
//Prosedur Menu
         static void Menu(){
         int pilih=0;
         while(pilih!=4){
                System.out.println("Menu:\n1.Tulis\n2.Baca\n3.Tambah\n4.Exit");
                System.out.print("Pilihanmu? ");pilih=key.nextInt();
                          if(pilih==1) Tulis();
                          else if(pilih==2) Baca();
                          else if(pilih==3) Tambah();
                               }
                }
               
//Prosedur Tulis
         static void Tulis(){
                  System.out.print("Nama File\t: ");namaFile=key.next();
                  System.out.print("Jumlah Data\t: ");jumlahData=key.nextInt();
                                for(int i=0;i<jumlahData;i++){
                                mahasiswa[i]=new Mhs();
                                        System.out.println("Data ke-"+(i+1));
                                        System.out.print("Nama\t\t: ");mahasiswa[i].nama=key.next();
                                        System.out.print("Tinggi Badan\t: ");mahasiswa[i].tinggiBadan=key.nextInt();
                                        System.out.print("Berat Badan\t: ");mahasiswa[i].beratBadan=key.nextInt();
                                }
                   String s="";
                   try{tulisFile=new PrintStream(new File(namaFile));
                        for(int i=0;i<jumlahData;i++){
                        s=mahasiswa[i].nama+"\t"+mahasiswa[i].tinggiBadan+"\t"+mahasiswa[i].beratBadan;
                        tulisFile.println(s);}
                                }catch(FileNotFoundException e){System.out.println("File Error");}
                }
               
//Prosedur Baca
         static void Baca(){
                  System.out.print("Nama File\t: ");namaFile=key.next();
                              try{bacaFile=new Scanner(new File(namaFile));
                                    int i=0;
                                    while(bacaFile.hasNext()){
                                             mahasiswa[i]=new Mhs();
                                             mahasiswa[i].nama=bacaFile.next();
                                             mahasiswa[i].tinggiBadan=bacaFile.nextInt();
                                             mahasiswa[i].beratBadan=bacaFile.nextInt();
                                             bacaFile.nextLine();
                                              i++;
                                                }
                                                jumlahData=i;
                                }catch(FileNotFoundException e){System.out.println("File Tidak Ada");}
                                Tampil();
                }
               
//Prosedur Tampil
        static void Tampil(){
                System.out.println("nama"+"\t\t"+"x"+"\t"+"y"+"\t"+"xy"+"\t"+"x^2"+"\t"+"y^2"+"\t"+"dugaan"+"\t"+"keterangan");
                                int[] tg=new int[100];
                                int[] br=new int[100];
                                int[] brtg=new int[100];
                                int[] tgtg=new int[100];
                                int[] brbr=new int[100];
                                double tmp;
                                for(int i=0;i<jumlahData;i++){
                                tg[i]=mahasiswa[i].tinggiBadan;
                                br[i]=mahasiswa[i].beratBadan;
                                brtg[i]=mahasiswa[i].tinggiBadan*mahasiswa[i].beratBadan;
                                tgtg[i]=mahasiswa[i].tinggiBadan*mahasiswa[i].tinggiBadan;
                                brbr[i]=mahasiswa[i].beratBadan*mahasiswa[i].beratBadan;
                                jumtg=tg[i]+jumtg;
                                jumbr=br[i]+jumbr;
                                jumbrtg=brtg[i]+jumbrtg;
                                jumbrbr=brbr[i]+jumbrbr;
                                jumtgtg=tgtg[i]+jumtgtg;
//Bagian Hitung Dugaan
          b= (((double)jumlahData*(double)jumbrtg)-((double)jumtg*(double)jumbr))/(((double)jumlahData*jumtgtg)-(((double)jumtg*jumtg)));
          a= ((jumbr/jumlahData)-(b*(jumtg/jumlahData)));
          tmp = b*((double)(mahasiswa[i].tinggiBadan));
          tmp+= a;
//Selesai
            System.out.println(mahasiswa[i].nama+"\t\t"+tg[i]+"\t"+br[i]+"\t"+brtg[i]+"\t"+tgtg[i]+"\t"+brbr[i]+"\t"+(int)tmp+"\t"+cekKeterangan(mahasiswa[i].beratBadan,tmp));
                                ind=i;
                                }

             System.out.println("total"+"\t\t"+jumtg+"\t"+jumbr+"\t"+jumbrtg+"\t"+jumtgtg+"\t"+jumbrbr);
             System.out.print("\n\n\n Rumus nya : ");
             buatRumus();
                }
               
//Prosedur Tambah
            static void Tambah(){
                                Baca();
                                int j=jumlahData;
            System.out.print("Jumlah data tambahan : ");jumlahData=key.nextInt();
                                jumlahData+=j;
                                for(int i=j;i<jumlahData;i++){
                                mahasiswa[i]=new Mhs();
                                        
       System.out.println("Data ke-"+(i+1));
       System.out.print("Nama\t\t: ");mahasiswa[i].nama=key.next();
       System.out.print("Tinggi Badan\t: ");mahasiswa[i].tinggiBadan=key.nextInt();
       System.out.print("Berat Badan\t: ");mahasiswa[i].beratBadan=key.nextInt();
                                }
                                String s="";
                                try{tulisFile=new PrintStream(new File(namaFile));
                                for(int i=0;i<jumlahData;i++){
                                s=mahasiswa[i].nama+"\t"+mahasiswa[i].tinggiBadan+"\t"+mahasiswa[i].beratBadan;
                                                                tulisFile.println(s);}
                                }catch(FileNotFoundException e){System.out.println("File Error");}
                               
                }
                static void buatRumus(){
                                BacaSaja();
                                double tmp;
b= (((double)jumlahData*(double)jumbrtg)-((double)jumtg*(double)jumbr))/(((double)jumlahData*jumtgtg)-(((double)jumtg*jumtg)));
                                a= ((jumbr/jumlahData)-(b*(jumtg/jumlahData)));
                               
               System.out.printf("%2f + %2f X\n",a,b);
                /*              for(int aa = 0;aa<mahasiswa.length-1;aa++){
                                 if(mahasiswa[aa] == null){return;
                                                }
                                 tmp = b*((double)(mahasiswa[aa].tinggiBadan));
                                 tmp+= a;
                                 System.out.print("Berat badan ideal untuk " + mahasiswa[aa].nama + "=" + tmp+"\n");
                                }*/
               System.out.println();
                               
                }
               
               static void BacaSaja(){
                        try{
                        bacaFile=new Scanner(new File(namaFile));
                        int i=0;
                        while(bacaFile.hasNext()){
                                               mahasiswa[i]=new Mhs();
                                               mahasiswa[i].nama=bacaFile.next();
                                               mahasiswa[i].tinggiBadan=bacaFile.nextInt();
                                               mahasiswa[i].beratBadan=bacaFile.nextInt();
                                               bacaFile.nextLine();
                                                                i++;
                                                }
                                                jumlahData=i;
                                }catch(FileNotFoundException e){System.out.println("File Tidak Ada");}
                }
               
                         static String cekKeterangan(double beratbadan,double bbs){
                         if((beratbadan>=(bbs-(0.1*bbs)))&&(beratbadan<=(bbs+(0.1*bbs)))){
                                                return "ideal";
                                }
                                                else if(beratbadan<(bbs-(0.1*bbs))){return "kurang";}
                                                else if(beratbadan>(bbs+(0.1*bbs))){return "berlebih";}                                                             
               
                               
//jika beratbadan antara 10% bbs (if beratbadan > (bbs-(0.1*bbs)) && bbs+(0.1*bbs) ) return "Berat badan ideal"
//else if beratbadan < bbs-(0.1*bbs) return "Berat badan kurang"
//else if beratbadan > bbs+(0.1*bbs) return "Berat badan berlebih"
                                return "";
                }
               
                               
}



OUTPUT:
>>LANGKAH 1:
- Pilih no1 pada menu dengan mengetik angka 1
- Berikan nama file sesuai keinginan,dengan format namafile.txt
- Masukan data yang ingin dihitung, misalnya data yang ingin dihitung seperti pada gambar


--LANGKAH 2:


- Pilih angka 2 dengan mengetik angka 2
- Setelah itu akan tampil hasil perhitungan dari data yang dimasukan

Tidak ada komentar:

Posting Komentar