4 Replies - 1862 Views - Last Post: 02 October 2009 - 08:53 AM Rate Topic: -----

#1 spcm0012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-October 09

Cannot Find Variable... But its a package ! argh

Post icon  Posted 01 October 2009 - 06:32 PM

Alright, so I'm working on this program to solve voltage divider with 2 input voltages.. I have my divider.class a UseDivider.class and IO.java. here are my codes:

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


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot Find Variable... But its a package ! argh

#2 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Cannot Find Variable... But its a package ! argh

Posted 01 October 2009 - 06:47 PM

you are correct as long as the files are in the same directory imports are not necessary.
I have just run the project and it compiles fine under eclipse, what IDE are you using?

Some IDEs are specific about the order the files are compiled, either check for a "compile project" option, or be sure to compile your IO.java file first.
Was This Post Helpful? 1
  • +
  • -

#3 javafreak   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 97
  • Joined: 21-September 09

Re: Cannot Find Variable... But its a package ! argh

Posted 01 October 2009 - 06:55 PM

try to call like this

IO iobj = new IO();

iobj.readDouble();

instead of this resistor = IO.readDouble();

in main method

This post has been edited by javafreak: 01 October 2009 - 06:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 spcm0012   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-October 09

Re: Cannot Find Variable... But its a package ! argh

Posted 01 October 2009 - 07:00 PM

View PostWilliam_Wilson, on 1 Oct, 2009 - 05:47 PM, said:

you are correct as long as the files are in the same directory imports are not necessary.
I have just run the project and it compiles fine under eclipse, what IDE are you using?

Some IDEs are specific about the order the files are compiled, either check for a "compile project" option, or be sure to compile your IO.java file first.



i write my programs in notepad.. :P

and i did have the program working in my lab, but on my home computer it wont run. i guess its just my comp. thanks for the help.

View Postjavafreak, on 1 Oct, 2009 - 05:55 PM, said:

try to call like this

IO iobj = new IO();

iobj.readDouble();

instead of this resistor = IO.readDouble();

in main method



i have tryed this method, it returns the same errors. like my reply to the post above, im assuming it is just my computer.
Was This Post Helpful? 0
  • +
  • -

#5 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: Cannot Find Variable... But its a package ! argh

Posted 02 October 2009 - 08:53 AM

View Postjavafreak, on 1 Oct, 2009 - 08:55 PM, said:

try to call like this

IO iobj = new IO();

iobj.readDouble();

instead of this resistor = IO.readDouble();

in main method

Doing it this way wouldn't change anything. The method is static, thus the class it resides in does not need to be instantiated. I would suggest using a proper IDE like jCreator LE or Eclipse, they are both free and make writing and debugging code much easier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1