The resistance is a strand of wire can be computed using R
Solution
For part 1
question2.java
import java.lang.Math;
import java.util.Scanner;
public class question2{
//method to calculate resistance by taking in resistivity, length and diameter
public static double resistance(double resistivity, double diameter, double length){
return (4*resistivity*length)/(Math.PI*diameter*diameter);
}
//method to calculate diameter by taking in gauge number
public static double diameter(int n){
return 0.127*Math.pow(92, (36.0-n)/39.0);
}
public static void main(String[] args) {
//Scanner to take in inputs
Scanner sc = new Scanner(System.in);
//Getting input for wire gauge
System.out.print(\"Enter wire gauge: \");
int n = sc.nextInt();
//getting input for wire length
System.out.print(\"\ Enter wire length (in m.): \");
double l = sc.nextDouble();
//getting input for resistivity
System.out.print(\"\ Enter wire resistivity: \");
double resistivity = sc.nextDouble();
//closing the scanner
sc.close();
//calling diameter method to get diameter
double d = diameter(n);
//calling resistance method to get resistance
double resistance = resistance(resistivity, d, l);
//Print statements to print diameter and resistance
System.out.println(\"Diameter of the wire is: \" + d);
System.out.println(\"Resistance of the wire is: \" + resistance + \" ohms\");
}
}
For part 2
question2.java
import java.lang.Math;
import java.util.Scanner;
public class question2{
//method to calculate resistance by taking in resistivity, length and diameter
public static double resistance(double resistivity, double length, int n){
//calculating diameter
double diameter = 0.127*Math.pow(92, (36.0-n)/39.0);
//printing diameter
System.out.println(\"Diameter of the wire is: \" + diameter);
//returning the value of resistance
return (4*resistivity*length)/(Math.PI*diameter*diameter);
}
//method to check if inputs are greater than 0
public static boolean isValid(double n){
return n>0;
}
public static void main(String[] args) {
//Scanner to take in inputs
Scanner sc = new Scanner(System.in);
//Getting input for wire gauge
System.out.print(\"Enter wire gauge: \");
int n = sc.nextInt();
//checking if n>0
while(!isValid((double)n)){
//Getting input for wire gauge
System.out.print(\"\ That was an invalid input.\ Enter wire gauge: \");
n = sc.nextInt();
}
//getting input for wire length
System.out.print(\"\ Enter wire length (in m.): \");
double l = sc.nextDouble();
//checking if l>0
while(!isValid(l)){
//Getting input for wire length
System.out.print(\"\ That was an invalid input.\ Enter wire length (in m.): \");
l = sc.nextDouble();
}
//getting input for resistivity
System.out.print(\"\ Enter wire resistivity: \");
double resistivity = sc.nextDouble();
while(!isValid(resistivity)){
//getting input for resistivity
System.out.print(\"\ That was an invalid input.\ Enter wire resistivity: \");
resistivity = sc.nextDouble();
}
//closing the scanner
sc.close();
//calling resistance method to get resistance
double resistance = resistance(resistivity, l, n);
//Print statements to print resistance
System.out.println(\"Resistance of the wire is: \" + resistance + \" ohms\");
}
}


