Tasks: Follow the directions below to complete your lab assignment For today\'s
ID: 3753699 • Letter: T
Question
Tasks: Follow the directions below to complete your lab assignment
For today's lab we will be completing Exercise P9.5 from the book. Book =Big Java Early Objects
IMPORTANT!! All of your method/class names need to match what is shown in this document. This is the public interface of your class. The classes and methods you need to implement are as follows:
Circuit – single method getResistance. Note that this is simply an empty circuit with no resistors, and therefore the resistance is always 0 for objects of this class. This is the parent class.
Resistor – subclass of Circuit. Represents a single resistor. Needs to override the getResistance method from the superclass Circuit. To compute the value of the Resistor object you simply return the resistance value.
Serial – subclass of Circuit. Contains an ArrayList<Circuit> instance variable. Resistance is computed by the formula given above. Must override inherited getResistance method.
Parallel – subclass of Circuit. Contains an ArrayList<Circuit> instance variable. Resistance is computed by the formula given above. Must override inherited getResistance method.
Read the problem description in the book for more details! (additionally, you will want to reference the material from chapter 9).
I have uploaded a file called CircuitDemo.java to Google Drive. This demonstrates how your code should function. (Note that you will need to create appropriate constructors for each class shown above). This sample code builds the circuit shown in the book with the top resistor = 100 Ohms, the bottom left resistor = 100 Ohms, and the bottom right resistor = 200 Ohms. You should make sure you can calculate this circuit by hand. If you need help with this just let me know. (Often programmers have to write programs using formulas outside of their area of expertise. Make sure you can do calculations by hand or your program will never work!).
You can choose which way to test your circuit from the two choices below. You can either use standard test procedures as we have used previously (in the CircuitDemo.java file), or you can create a new class CircuitTester to run tests using the JUnit unit test framework. Using JUnit is bonus material. What I am saying is you must test your program in either case, but if you go to the extra work to use JUnit you will receive bonus points. I would suggest at the least you create 2-3 tests in the CircuitDemo.java file before attempting the JUnit tests.
Choice 1: You should add multiple circuits to the CircuitDemo.java file to verify that your program works as expected. Take screen-shots or capture the output text and include this in your project folder. Sample output from a working project using the given CircuitDemo.java file is shown below. (Look at the java file on UTC Learn to see the test code that produced this output).
Combined resistance: 75.0
Expected: 75.0
Choice 2: BONUS: Use the JUnit unit testing framework to run multiple tests on your circuit. Use class name CircuitTester. The completeness of your testing will determine the number of bonus points you receive. (Also, your tests should pass). Feel free to use google to figure out how to use junit and get it running. (You may also discuss this among yourselves – JUST HOW TO GET IT WORKING). Once junit is working, you must think of and implement YOUR OWN tests. Of course, I am available to help you with this as well. (The basic idea is that you will need to import two .jar files into your current project, and then write tests as demonstrated in section 8.6 of the book).
Some helpful tips:
Compile often – do it.
You are responsible for a larger amount of the design process in this lab. You need to draw on paper how you want your subclasses to work and plan before you actually write code.
It may be helpful to use the Debugger or print statements to check your work. (always).
We will have a demo/tester class for this lab called CircuitDemo. You will need to fill in this class with enough tests to verify that your project works as expected. (Alternately, you may use the JUnit unit test framework from Section 8.6 in the book – more details below).
Resistor values should be stored as doubles.
There are two types of circuits we are creating, series and parallel (as well as a single resistor). The formula for computing the entire resistance of each type of circuit is given below. I googled these images from http://hyperphysics.phy-astr.gsu.edu/hbase/electric/resis.html
SAMPLE CODES
1.
public class CircuitTester01 {
2.
Explanation / Answer
Solution:
##Circuit.java
//Circuit class
public class Circuit {
//resistance variable is assigned to 0
double resistance = 0;
//Method is used to get the resistance
public double getResistance(){
return 0;
}
}
###Resistor.java
//Resistor class extending Circuit class
public class Resistor extends Circuit {
//It has its own resistance
double resistance;
//Parameterized constructor assign the value to its field while object creation
public Resistor(double R1){
resistance = R1;
}
//Overriding parent's getResistance method
@Override
public double getResistance(){
return resistance;
}
}
####Serial.java
import java.util.ArrayList;
//Serial class extending Circuit
public class Serial extends Circuit{
// Creating circuit arraylist to store circuit
ArrayList<Circuit> circuit;
//Default Constructor instantiate a new circuit
public Serial(){
circuit = new ArrayList<Circuit>();
}
// adding circuit to the arraylist
public void add(Circuit r){
circuit.add(r);
}
// Overriding Parent's class getResistance with Serial getResistance because
// it has different behavior of getting resistance
@Override
public double getResistance(){
// Initially resistance is 0
double resistance = 0;
// Total resistance = r1 + r2 +r3 + ..
for(Circuit c: circuit) {
resistance += c.getResistance(); //Calculating resistance
}
// returning total resistance
return resistance;
}
}
####Parallel.java
import java.util.ArrayList;
//Parallel class extending circuit class
public class Parallel extends Circuit {
// Creating circuit ArrayList to store circuit
ArrayList<Circuit> circuit;
// Default constructor instantiate new circuit
public Parallel(){
circuit = new ArrayList<Circuit>();
}
// adding circuit to the arraylist
public void add(Circuit r){
circuit.add(r);
}
// Overriding Parent's class getResistance with Serial getResistance because
// it has different behavior of getting resistance
@Override
public double getResistance(){
// Initially resistance is 0
double resistance = 0;
// 1/Total resistance = 1/r1 + 1/r2 + 1/r3 + ..
for(Circuit c: circuit){
resistance +=1/c.getResistance();//Calculating resistance in parallel
}
//returning total resistance
return 1/resistance;
}
}
##CircuitDemo.java
//Driver class 1
public class CircuitDemo
{ /**
method that implements tests for Circuit class and subclasses
@param args - Not Used.
*/
//Main method to check whether our classes and subclasses are working as expected or not
public static void main(String[] args)
{
Parallel circuit1 = new Parallel();
circuit1.add(new Resistor(100));
Serial circuit2 = new Serial();
circuit2.add(new Resistor(100));
circuit2.add(new Resistor(200));
circuit1.add(circuit2);
System.out.println("Combined resistance: " + circuit1.getResistance());
System.out.println("Expected: 75.0");
}
}
######CircuitTester01.java
public class CircuitTester01
{
//Main method to check whether our classes and subclasses are working as expected or not
public static void main(String[] args)
{
Serial c1 = new Serial();
c1.add(new Resistor(100));
c1.add(new Resistor(200));
c1.add(new Resistor(150));
System.out.println("c1 total R = " + c1.getResistance());
Parallel c2 = new Parallel();
c2.add(new Resistor(100));
c2.add(new Resistor(100));
c2.add(new Resistor(100));
System.out.println("c2 total R = " + c2.getResistance());
Serial c3 = new Serial();
c3.add(c1);
c3.add(c2);
System.out.println("c3 total R = " + c3.getResistance());
Parallel c4 = new Parallel();
c4.add(c1);
c4.add(c2);
System.out.println("c4 total R = " + c4.getResistance());
}
}
Sample Run of CircuitDemo:
Combined resistance: 75.0
Expected: 75.0
Sample Run of CircuitTester01:
c1 total R = 450.0
c2 total R = 33.333333333333336
c3 total R = 483.3333333333333
c4 total R = 31.03448275862069
Note: If you have any doubt please do comment below, before giving any negative feedback, I will be glad to help you out. thanks!
i have answered for choice 1 and working on choice 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.