Question: Please edit my java code to match the following description...please use ArrayList as we don't know how many components the user is going to enter:

Please edit my java code to match the following description...please use ArrayList as we don't know how many components the user is going to enter:

An electric circuit will be described by the user. Each line will describe either a Resistor or a DC Voltage source or be a single word command.

The format for describing (for example) a 5.2 Ohm resistor connected between nodes 2 and 3 is:

r 2 3 5.2

The format for describing a 6.5 Volt source connected between nodes 1 and 2 (where the positive side of the source is connected to node 1) is:

v 1 2 6.5

A complete circuit could be described as follows:

v 1 0 2.0

r 1 2 0.25

v 2 0 3

r 2 3 0.5

r 3 0 1.0

For polarized components (such as a voltage source), the order does matter. Thus:

v 1 0 2.0

is equivalent to:

v 0 1 -2.0

In addition to lines describing the components of a circuit, there are 2 other single word commands that can be entered:

"spice" and "end" .

The end is the simplest to understand and implement. When the end command is entered, the program should print All Done and terminate.

The spice command should print the spice description of the circuit entered so far. In the spice description, uppercase letters are used, components are numbered sequentially and DC is used in the description of voltage sources. An example session follows (the lines in bold denote output from the program; the non-bold lines are input):

v 1 0 2.0

r 1 2 0.25

v 2 0 3

r 2 3 0.5

r 3 0 1.0

spice

V1 1 0 DC 2.0

R1 1 2 0.25

V2 2 0 DC 3.0

R2 2 3 0.5

R3 3 0 1.0

end

All Done

-----------------------------------------------------------------------------------------------------------------------------------------------------

UserMain Class

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package coe318; import java.util.*; /** * * @author Asus */ public class UserMain { public static void main(String[] args){ ArrayList cir = new ArrayList(); System.out.println("Simple Analog Circuit with only Resistors " + "and DC voltage source."); System.out.println(" Please enter either a DC voltage source (v) or Resistor(r) " + "between two nodes followed by it's value."); Scanner scan = new Scanner(System.in); System.out.println(" Type (v) for Voltage Source, (r) for Resistor," + "(spice) for spice format, (end) to finish: "); String s1 = scan.nextLine(); if(s1.equals("spice")){ System.out.println("You must enter the elements first " + " Enter (v) , (r), or (end)"); s1 = scan.nextLine(); } while(!s1.equals("end")){ System.out.println("The two nodes: "); int n1 = scan.nextInt(); int n2 = scan.nextInt(); System.out.println("The value: "); double va1 = scan.nextDouble(); Node node = new Node(n1,n2); cir.add(new Circuit(s1,va1,node)); for(int i =0 ; i < cir.size(); i++){ System.out.println(cir.get(i)); } System.out.println("(v),(r),(end),(spice)?"); s1 = scan.next(); if(s1.equals("spice")){ for(int i =0 ; i < cir.size(); i++){ System.out.println(cir.get(i)); } s1 = scan.next(); } } System.out.println("All Done"); }

}

------------------------------------------------------------------------------------------------------------------------------------------------------

Circuit Class

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package coe318;

import java.util.ArrayList;

/** * * @author Asus */ public class Circuit { private Resistor resist; private Voltage volts; private ArrayList ele; private Node node; private double value; private static int rID, vID; private ArrayList voltage = new ArrayList(); private ArrayList resis = new ArrayList(); public Circuit(String s,double value,Node n){ rID = 0; vID = 0; ele = new ArrayList(); ele.add(s); this.value = value; node = n; if( ele.equals("r")){ resist = new Resistor(value,n); rID++; } if(ele.equals("v")){ volts = new Voltage(value,n); vID++; } } public String toString() { String s = " "; for(String ELE : ele){ if(ele.equals("spice")){ if(ele.equals("r")){ rID++; s+= "R" +rID+""; } if(ele.equals("v")){ vID++; s+= "V" + vID + s + "DC" +value; } s = ELE +s+ node + s+ value; } } return s; } public String spiceToString(){ String s =" "; for(String ELE : ele){ if(ELE.equals("r")){ rID++; s = "R"+rID +s+ node + s+ value; } if(ELE.equals("r")){ } } return s; } }

--------------------------------------------------------------------------------------------------------------------------------------------------------

Resistor class

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package coe318;

import java.util.ArrayList;

/** * * @author Asus */ public class Resistor { private Node node; private static int resistorID = 0; private ArrayList rID = new ArrayList(); private ArrayList res = new ArrayList(); private double RC; public Resistor(double resistance,Node n) { if(resistance < 0){ throw new IllegalArgumentException("The resistance must be greater than or equal to 0"); } node = n; if(node == null){ throw new IllegalArgumentException("The nodes must have different values "); } rID.add(resistorID + 1); resistorID = resistorID + 1; res.add(resistance); } public String toString(){ String s = " "; for(int i = 0; i < rID.size();i++){ s = "r"+ s +node +s + res.get(i); }

// for(int resist : rID){ // s = "r"+ resist + s + node +s+ + RC; // } return s; } }

-------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package coe318;

import java.util.ArrayList;

/** * * @author Asus */ public class Voltage { private Node node; private static int voltsID = 0; private ArrayList voltage = new ArrayList(); private ArrayList DC = new ArrayList(); public Voltage(double volts,Node node) { this.node = node; if(node == null){ throw new IllegalArgumentException("The nodes must have different values "); } voltage.add(voltsID + 1); voltsID = voltsID + 1; DC.add(volts); } public String toString(){ String s = " "; for(int i = 0; i < DC.size() ;i++) { s = s + "V" + s + node + DC.get(i); } return s; } }

--------------------------------------------------------------------------------------------------------------------------------------------------------

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package coe318;

import java.util.ArrayList;

/** * * @author Asus */ public class Node { private int n1,n2; private static int nodeID; private ArrayList nodes = new ArrayList(); public Node(int node1, int node2){ if(node1 == 0 && node2 == 0){ throw new IllegalArgumentException("The nodes must have a different values "); } n1 = node1; n2 = node2; nodes.add(nodeID++); nodeID++; } public int[] getNodes() { int[] nodes = {n1,n2}; return nodes; } public String toString() { String s = n1 + " " + n2; return s; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!