Search for more details......

Resistor Color Code Value from Java



In electronic resistor is a device that resists the current flow. In the following picture you can see a generally used resistor.















The SI unit for measure resistance is Ω(Ohm). Many students and electronics beginners harder to remember the resistor color code.



If you have a basic java knowledge for making a class and compile it, you can create a your own java program to calculate resistor value form it's color code!



 here is the ResistorColorCode.java

import java.util.Scanner;


public class ResistorColorCode {

    private String colorCodes;
    private String[]numberCodes={"BLACK","BROWN","RED","ORANGE","YELLOW","GREEN","BLUE","PINK","GRAY","WHITE"};
   
   
    private void inputColorCode(){
        System.out.print("Enter color code(red-red-green):");
        Scanner s=new Scanner(System.in);
       
        colorCodes=s.nextLine().toUpperCase();
       
    }
   
    private String convertToValues(){
        String result="";
        int tmp=0;
       
        String Codes[]=colorCodes.split("-");
        System.out.println(Codes[0]);
        result=""+getIndexOfCodeArray(Codes[0])+getIndexOfCodeArray(Codes[1]);
        System.out.println(result);
        tmp=getIndexOfCodeArray(Codes[2]);       
       
        double value=Long.parseLong(result)*Math.pow(10, tmp);
       
       
        result=Double.toString(value);
       
        return result;
       
       
    }
   
    private int getIndexOfCodeArray(String color){
       
        int res=-1;
        for(int i=0;i<numberCodes.length;i++){
           
            if(numberCodes[i].equals(color))return i;
           
        }
       
        return res;
    }
   
   
    public static void main(String[] args) {
        ResistorColorCode rcc=new ResistorColorCode();
        while(true){
            try {
                System.out.println("©Sun Software Solutions");
                rcc.inputColorCode();
                System.out.println(rcc.convertToValues()+" Ohm(s)");
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("Error input! Try again.");
            }
           
        }
    }
   
   
   
}


No comments:

Post a Comment