Your submission should follow our general guidelines. Include a master index.htm
ID: 662782 • Letter: Y
Question
Your submission should follow our general guidelines. Include a master index.html (in our standard format) that contains or links to all the problems. Use source-highlighting to display your code. Include source-code files and any required images.
Create a class for the object Resistor. Class variables should include the resistor name (String) and value (double). Add a toString function. Write a test program to validate the operation of your class.
Modify your resistance program from the last assignment to accept command line arguments such as R1 1000 R2 500 R3 600 and build an ArrayList of resistors. Calculate the equivalent parallel resistance using a simple loop.
Modify test1.java to count and display the number of calls to PaintComponent (as well as the JPanel size).
Explanation / Answer
import java.util.*;
class Resistor{
String name;
double value;
public Resistor(String n,double v){
name = n;
value = v;
}
}
class main{
public static void main(String[] args){
System.out.println("Enter the Resistance Name and Value");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(s);
String[] arr = s.split(" ");
Resistor[] rs = new Resistor[(arr.length)/2];
for (int i = 0; i < arr.length-1; i += 2){
Resistor r = new Resistor(arr[i],Double.parseDouble(arr[i+1]));
rs[i/2] = r;
}
double num = 0;
double dem = 1;
int k = 0;
for (int i = 0; i < rs.length; i++){
double local = 1;
dem = dem * rs[i].value;
for (int j = 0; j < rs.length; j++){
if (i == k){
continue;
}
local = local * rs[j].value;
}
num = num + local;
}
System.out.println(dem/num);
}
}
/* Program for parallel resistance calcualtion; add into JPanle size code */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.