Write a JAVA program that will ask the user for a 3 band resistor value colors a
ID: 3764799 • Letter: W
Question
Write a JAVA program that will ask the user for a 3 band resistor value colors and the tolerance color (G for gold +/- 5%, S for Silver +/- 10% and N for None +/- 20%). Output the resistor value and its tolerance min/max. Must use SWITCH statements. Cannot use any loop structure (IT has to be JAVA not C++ or Python) Thanks!!!!!!!!!!!!!!!!!!!!!
I was given this code but there were nine errors when I tried to compile it
import java.io.*;
import java.util.*;
class Resistor
{
public static int res_val(String[] s){
int val = 0;
for (int i = 0; i < s.length; i++){
val += l[s[i]];
}
return val;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Resistor colors (separated by space ) : ");
String[] l = sc.nextLine().split(' ');
int val = res_val(l);
System.out.print("Enter tolerance color : ");
String tol = sc.nextLine();
switch(tol){
case 'G':
System.out.println("VALUE : "+val);
System.out.println("MIN VALUE : "+val - val*0.05);
System.out.println("MAX VALUE : "+val + val*0.05);
break;
case 'S':
System.out.println("VALUE : "+val);
System.out.println("MIN VALUE : "+val - val*0.1);
System.out.println("MAX VALUE : "+val + val*0.1);
break;
case 'N':
System.out.println("VALUE : "+val);
System.out.println("MIN VALUE : "+val - val*0.2);
System.out.println("MAX VALUE : "+val + val*0.2);
break;
default:
break;
}
}
}
Explanation / Answer
/***
* The java program that prompts user to enter
* three resistor values separated by space.
* Then enter a letter value G,S or N for gold,
* silver and none.
* Then print the minimum and maximum values for given
* resistors
* */
//Resistor.java
import java.util.Scanner;
class Resistor
{
public static int res_val(String[] s)
{
int val = 0;
for (int i = 0; i < s.length; i++)
{
//Convert the string value of array ,s
//to integer using parseInt method of Integer wrapper
//class and add to val
val += Integer.parseInt(s[i]);
}
//return val
return val;
}
public static void main(String[] args)
{
//Create an Scanner class
Scanner sc = new Scanner(System.in);
System.out.print("Enter Resistor colors (separated by space ) : ");
//Read a string from the console
String resistors= sc.nextLine();
//Call split method that splits based on space
String[] l =resistors.split(" ");
//call res_val with string array, l as input argument
// that returns the sum of the restor colors
int val = res_val(l);
System.out.print("Enter tolerance color G, S or N : ");
//Read a character. To read a characte call nextLine().charAt(0)
//to get first letter
char tol = sc.nextLine().charAt(0);
//Check the corresponding switch case
switch(tol)
{
case 'G':
System.out.println("VALUE : "+val);
//Close the parenthesis inside the println for value
//which has expression
System.out.println("MIN VALUE : "+(val - val*0.05));
System.out.println("MAX VALUE : "+(val + val*0.05));
break;
case 'S':
System.out.println("VALUE : "+val);
//Close the parenthesis inside the println for value
//which has expression
System.out.println("MIN VALUE : "+(val - val*0.1));
System.out.println("MAX VALUE : "+(val + val*0.1));
break;
case 'N':
System.out.println("VALUE : "+val);
//Close the parenthesis inside the println for value
//which has expression
System.out.println("MIN VALUE : "+(val - val*0.2));
System.out.println("MAX VALUE : "+(val + val*0.2));
break;
default:
break;
}
}
}
----------------------------------------------------------------
Sample Output:
Enter Resistor colors (separated by space ) : 5 6 7
Enter tolerance color : G
VALUE : 18
MIN VALUE : 17.1
MAX VALUE : 18.9