Divider.java
/**
A program designed to calculate the DC voltage with
respect to ground at the middle of two resistors
@author Mike Spick
@date September 18th, 2009
*/
public class Divider
{
private double Divider;
private double OutputVoltage1;
private double OutputVoltage2;
private double finalOutputVoltage;
private double voltage1;
private double voltage2;
private double resistor1;
private double resistor2;
public Divider(double volt1, double volt2, double ohms1, double ohms2)
{
voltage1 = volt1;
voltage2 = volt2;
resistor1 = ohms1;
resistor2 = ohms2;
}
public void setVoltage1(double setVoltage1)
{
voltage1 = CheckVoltage(setVoltage1);
}
public void setVoltage2(double setVoltage2)
{
voltage2 = CheckVoltage(setVoltage2);
}
public void setResistor1(double setResistance1)
{
resistor1 = CheckResistor(setResistance1);
}
public void setResistor2(double setResistance2)
{
resistor2 = CheckResistor(setResistance2);
}
public double CheckResistor(double resistor)
{
while(resistor > 100000)
{
System.out.println("Please enter a resistance lower then 100k ohms.");
resistor = IO.readDouble();
}
return resistor;
}
public double CheckVoltage(double voltage)
{
while((-20 > voltage) || (voltage > 20))
{
System.out.println("Please enter a voltage lower then 20v and/or higher then -20v.");
voltage = IO.readDouble();
}
return voltage;
}
public void Calculate()
{
OutputVoltage1 = resistor2/(resistor1 + resistor2) * voltage1;
OutputVoltage2 = resistor1/(resistor1 + resistor2) * voltage2;
finalOutputVoltage = OutputVoltage1 + OutputVoltage2;
}
public double getVoltageOutput()
{
return finalOutputVoltage;
}
public void Display()
{
System.out.println("Voltage Out: " +finalOutputVoltage);
}
public static void main(String[] args)
{
Divider newDivider = new Divider(10, -10, 5, 15);
newDivider.setVoltage1(10);
newDivider.setVoltage2(-10);
newDivider.setResistor1(5);
newDivider.setResistor2(15);
newDivider.Calculate();
newDivider.Display();
}
}
UseDivider
/**
A class to test the Divider class.
@author Mike Spick
@date Sept. 23rd, 2009
*/
public class UseDivider
{
public static void main(String[] args)
{
String testName;
System.out.println("Confirm the constructor works");
Divider newDivider = new Divider(10,15,100,150);
newDivider.Calculate();
System.out.println("Voltage Out = "+newDivider.getVoltageOutput());
System.out.println("Expected: 12.0");
{
System.out.println("-------------------------------");
System.out.println("Enter Voltage One: ");
newDivider.setVoltage1(IO.readDouble());
System.out.println("Enter Voltage Two: ");
newDivider.setVoltage2(IO.readDouble());
System.out.println("Enter Resistor One: ");
newDivider.setResistor1(IO.readDouble());
System.out.println("Enter Resistor Two: ");
newDivider.setResistor2(IO.readDouble());
newDivider.Calculate();
System.out.println("Output Voltage is: " +newDivider.getVoltageOutput());
}
}
}
IO
/**
The import class from the keyboard. All methods are static.
Liang; Scanner reference is section 2.13, page 52-55; Getting Input from the Console
section 8.8, page 286-2289; Text I/O
@author Kenn Baker
@version September 7, 2007
*/
import java.util.Scanner;
public class IO
{
/**
Read a string from the keyboard
@return String from keyboard
*/
public static String readString()
{
Scanner in = new Scanner(System.in); // Opposite to System.out
String i = in.next();
return i;
}
/**
Read an integer from keyboard. Verify/retry for integer
@return Integer from keyboard
*/
public static int readInt()
{
int i = 0; //Initialize for return
boolean failure = false;
Scanner in = new Scanner(System.in);
do
{
failure = false; //Reinitialize for if
if (in.hasNextInt())
i = in.nextInt(); //Good one
else
{
failure = true;
String temp = in.next(); //Clear the buffer
System.out.print("Please enter an integer: ");
}
}while(failure);
return i;
}
/**
Read an double from keyboard. Verify/retry for double
@return Double from keyboard
*/
public static double readDouble()
{
double i = -1.0; //Initialize for return
boolean failure = false;
Scanner in = new Scanner(System.in);
do
{
failure = false; //Reinitialize for if
if (in.hasNextDouble())
i = in.nextDouble(); // Good one
else
{
failure = true;
String temp = in.next(); //Clear the buffer
System.out.print("Please enter a double: ");
}
}while(failure);
return i;
}
}
when i try and compile the Divider.java file, i get the error:
"Cannot find symbol
symbol: variable IO
location: class Divider"
for the lines:
resistor = IO.readDouble();
voltage = IO.readDouble();
i can not seem to solve this for the life of me. i need to hand this in tomorrow morning and have been going through this code for a few hours now trying to find the issue.
Please Help!
EDIT:
just so you know, they are all in the same directory so i shouldnt have to import the files, correct?
This post has been edited by spcm0012: 01 October 2009 - 06:39 PM

New Topic/Question
Reply



MultiQuote





|