简体   繁体   中英

Java: inheritance and overriding methods questions

I am currently working on a problem in my java textbook that revolves around creating a Circuit superclass and a Resistor, Serial, and Parallel subclass. The Serial and Parallel classes have ArrayLists that are supposed to be populated by objects of the Circuit class. Each subclass contains a getResistance() method that is supposed to override the getResistance() method from the superclass. My issue is that regardless of input, only changing the instance variable "sum" from the Parallel class updates the results from getResistance().

PS I know this is school work and I don't expect anyone to "do my homework for me", but I am still learning about inheritance and would like to know what I am doing wrong for future reference.

Here is my main class:

public class Circuit
{ 
//this is the superclass and acts just as a container class
 public double getResistance() 
{ 
//I want this method to take an arbitrary value and return it.
 return 0;
}
public void add(Circuit input)
{
//this method is supposed to add a value to an object of the Circuit class
}
public void addAll(ArrayList<Circuit>circuits)
{
//this method is for combining ArrayList and takes in ArrayList
}   
}

Parallel subclass:

//subclass of Circuit superclass
public class Parallel extends Circuit
{

//this instance variable is of the Circuit class
private ArrayList<Circuit> parallel = new ArrayList<>();
private double resistance; 
private double sum; 

public void add(Circuit input)
{
//int count = 0; 
//adding values to populate ArrayList 
for(Circuit x: parallel)
{
    //setting the xth index value to the value to the input
   x.add(input); 
   //count++;  
}
}

public double getResistance()
{
  //this method overrides the one from the superclass
  //we are the sum of the input resistances of the ArrayList
  if(parallel.size()> 0)
   {
   for(Circuit x: parallel)
   {
    resistance = x.getResistance();
    sum=+ 1/resistance; 
   }
   }
     return sum; 
   }

 public void addAll(Serial series)
 {
 //this method is supposed to add the ArrayList of the Circuit type together
   series.addAll(parallel);  

  }
  }

Nothing to do with inheritance or overriding (it it was changing the sum variable would have no effect), rather;

The add method should add the input circuit to the list of circuits that compromise this parallel circuit .

Right now it adds all - "for each" - of the circuits (all 0 of them) from the parallel list to the other input circuit. Whoops, wrong way!

Since, in the current code there are no child circuits added to the parallel list, the loop in getResistence will never actually run.. which returns whatever value is in the unaltered sum variable.

A similar change should be made for addAll .

The sum should not be a member variable; keeping it a local variable will 'fix' another problem encountered after fixing the previous issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM