Hi mates..

I have a small question..

I have now 3 different classes, sounds ok?

this is the 1st class, called "Wire"

public class Wire
{
	private boolean Value;

	public boolean isValue() {
		return Value;
	}

	public void setValue(boolean value) {
		Value = value;
	}
	
}

This is the 2nd class, called "SingleInputGate"

public abstract class SingleInputGate implements LogicComponent
{
	private Wire Input;
	private Wire Output;
	
	public Wire getInput() {
		return Input;
	}
	public void setInput(Wire input) {
		Input = input;
	}
	public Wire getOutput() {
		return Output;
	}
	public void setOutput(Wire output) {
		Output = output;
	}
	
	public SingleInputGate( Wire input, Wire output)
	{
		setInput( input );
		setOutput( output );
	}
	
	public abstract void evaluate();
}

this is the last one, called "NotGate"

public class NotGate extends SingleInputGate
{
	public NotGate( Wire input, Wire output )
	{
		super( input, output );
	}
	
	public void evaluate()
	{
		if( super.getInput() ) // HOW TO COMPARE!!!
		{
			
		}
	}
}

As you can see, in the last class I've wrote a comment said "HOW TO COMPARE"!!
I want to compare the "super.getInput()" if it is "true" then the output will be "false", and if it is "false" the output will be "true".


Anyone can give tips on doing that?


Thanks..

Here's your getInput function from your superclass:

private Wire Input;
/*
code
*/
	public Wire getInput() {
		return Input;
	}

Here's your function call from the NotGate class.

if( super.getInput() ) // HOW TO COMPARE!!!
		{
			
		}

getInput returns an object of type Wire. You are comparing an object of type Wire to true/false. Wire isn't boolean. Do you want to check whether Input is null?

if( super.getInput() == null) 
		{
			
		}

I just reread your post and looked at the Wire class. Do you want this?

if( super.getInput().isValue ()) 
		{
			
		}

You may want to check to make sure getInput () isn't null as well before calling isValue ().

I just reread your post and looked at the Wire class. Do you want this?

if( super.getInput().isValue ()) 
		{
			
		}

You may want to check to make sure getInput () isn't null as well before calling isValue ().

first of all thanks for the reply :)

second, simply I want to see if "getInput" is "true" or "false"
after that I'll set the "setOutput" to the opposite value.

For example, if "getInput" == "true"
then, "setOutput" == false

how can I compare? or How can I know what's the value inside the "getInput"? so that I'll proceed with the same scenario


thanks..

getInput() returns an instance of the Wire class, so that cannot be directly compared to a boolean value. You probably want to use the boolean value that the isValue() method returns for that instance of Wire. ie getInput().getValue() (as previously explained by Vernon).
You don't need to compare a boolean value because it is already true or false. You could say if( super.getInput().isValue () == true) { // do stuff } but that's redundant, because all you need is if( getInput().isValue ()) { // do stuff } Similarly, to set the output wire's value to false, you need something like getOutput().setValue(false); note, finally, that you don't need the super. except for calling the superclass constructor. The ordinary methods will be inherited from the superclass.
And finally finally, don't forget Vernon's warning about null objects!

getInput() returns an instance of the Wire class, so that cannot be directly compared to a boolean value. You probably want to use the boolean value that the isValue() method returns for that instance of Wire. ie getInput().getValue() (as previously explained by Vernon).
You don't need to compare a boolean value because it is already true or false. You could say if( super.getInput().isValue () == true) { // do stuff } but that's redundant, because all you need is if( getInput().isValue ()) { // do stuff } Similarly, to set the output wire's value to false, you need something like getOutput().setValue(false); note, finally, that you don't need the super. except for calling the superclass constructor. The ordinary methods will be inherited from the superclass.
And finally finally, don't forget Vernon's warning about null objects!

I'll try this thanks a lot,,

but please I guess you missed something, I said I want to output the opposite value of the "getInput"

if it is "true" I should output "false"
if it is "false" I should output "true"


with your code I only output "false" in any case which is not the way it should be..


Thanks for the effort and I hope you'll give some suggestions :)

Then use the "!" not operator.

getOutput().setValue( ! getInput().isValue ());

getOutput().setValue( ! getInput().isValue ());

Thanks, I would try it..


This is now my final code in NotGate

public class NotGate extends SingleInputGate
{
	public NotGate( Wire input, Wire output )
	{
		super( input, output );
	}
	
	public void evaluate()
	{
		getOutput().setValue( !getInput().isValue() );
		boolean output = getOutput().isValue();
		System.out.println( output );
	}
}

Do you think this will work efficiently?

Can't say whether it will work as you want it to without seeing all the code, but it looks OK, and there will certainly be no problem with efficiency. Haven't you tested it?

Can't say whether it will work as you want it to without seeing all the code, but it looks OK, and there will certainly be no problem with efficiency. Haven't you tested it?

Yeah, it seems ok to me. Thanks mate..
I can't test it mate, according to the code of ethics. Because my professor gave a hierarchy tree and we should implement it without testing it. Later, he'll test it and grade us according to our codes.


Can I ask you another question please?

If I got this superclass

public abstract class MultiInputGate implements LogicComponent
{
	private Wire Inputs[];
	private Wire Output;
	
	public Wire[] getInputs() {
		return Inputs;
	}
	public void setInputs(Wire[] inputs) {
		Inputs = inputs;
	}
	public Wire getOutput() {
		return Output;
	}
	public void setOutput(Wire output) {
		Output = output;
	}
	
	public MultiInputGate( Wire input[], Wire output )
	{
		setInputs( input );
		setOutput( output );
	}
	
	public abstract void evaluate();
	
}

and this is my subclass

public class AndGate extends MultiInputGate
{
	public AndGate( Wire inputs[], Wire output )
	{
		super( inputs, output );
	}
	
	public void evaluate()
	{
		for( int i = 0; i < getInputs().length; i++ )
		{
			if( getInputs().isValue() == true ) //THIS IS NOT WORKING
			{
				
			}
		}
	}
}

How can I compare the array "Inputs[]" that's in the super class
if all its elements are "true" then I should set the output to "true"
otherwise, the output is "false".

I tried this "getInputs().isValue() == true" I'm sure it is wrong because eclipse said that, and also this is not how to search in an array.

Any tips?

I guess I've found a solution

I've changed the attributes in the super class to "protected" and I can now use them in my subclass.

Thanks a lot, if you have other suggestions please feel free to share them :)

I've finished my project.

Can anyone gives instructions on how to make a "package" ?

put it in a directory called the package name and put package (packagename); at the top of all the source files

swap (packagename) for the package name

Sorry to be a bother, but really I didn't got anything LOL

I've just created a folder "logic" on my desktop and I saved on it all the ".java" files that I need to be in the package "logic"

and now I don't know what to do!! LOL

In addition to the link above, here's a link.

http://java.sun.com/docs/books/tutorial/java/package/packages.html

Are you trying to create a package with the source code and an executable jar file to go with that package?

I think you're going to have to be more specific about what you are trying to do and what is going wrong. Also note that IDEs like NetBeans will create packages and jars for you when they build the project.

You can't test a piece of code you wrote? And your professor told you not to test it? Hmm. And if you use Eclipse or netbeans, they will create a package for you quite easily. If you need instructions on how to do it in either of those IDEs, I can help you out, just mention what you are using.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.