11 Replies - 835 Views - Last Post: 03 December 2012 - 07:52 PM Rate Topic: -----

#1 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Java inheritance program using gates.

Posted 03 December 2012 - 09:17 AM

I have to create a program using java inheritance while also incorporating the gave gates (and, or, nand, nor). Then i have to take the java gates and test them through the program. So far the program i have encounters errors. Im just looking for help on what i am doing wrong or what else needs to be done.

public abstract class Gates {

    public abstract boolean doOperation(boolean a, boolean B)/>/>;

}

public class AND extends Gates {
    
	public boolean doOperation(boolean a, boolean B)/>/> {
		return a && b;
	}

}

public class OR extends Gates {
    
	public boolean doOperation(boolean a, boolean B)/>/> {
		return a || b;
	}

}

public class NAND extends Gates {
    
	public boolean doOperation(boolean a, boolean B)/>/> {
		return !( a && B)/>/>;
	}

}

public class NOR extends Gates {
    
	public boolean doOperation(boolean a, boolean B)/>/> {
		return !(a || b );
	}

}

public class GatesTest {
	
	public static void main(String args[]) {
	
	Gates gates = new AND();
	System.out.println("gates.doOperation(true,false)");
	}
}



Here is the error i encounter:


Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

	at GatesTest.main(Gates.java:41)



Thank you very much

Is This A Good Question/Topic? 0
  • +

Replies To: Java inheritance program using gates.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java inheritance program using gates.

Posted 03 December 2012 - 09:23 AM

The class that is public and has a main() should be in the .java file that has its name
so
public class GatesTest {

should be in a file named GatesTest.java
Was This Post Helpful? 1
  • +
  • -

#3 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: Java inheritance program using gates.

Posted 03 December 2012 - 09:39 AM

View Postpbl, on 03 December 2012 - 09:23 AM, said:

The class that is public and has a main() should be in the .java file that has its name
so
public class GatesTest {

should be in a file named GatesTest.java


okay so i should take the GatesTest class and put it in a separate program and it should work?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1414
  • View blog
  • Posts: 3,136
  • Joined: 05-April 11

Re: Java inheritance program using gates.

Posted 03 December 2012 - 09:41 AM

View Postamanjot24, on 03 December 2012 - 04:39 PM, said:

View Postpbl, on 03 December 2012 - 09:23 AM, said:

The class that is public and has a main() should be in the .java file that has its name
so
public class GatesTest {

should be in a file named GatesTest.java


okay so i should take the GatesTest class and put it in a separate program and it should work?


In a separate file in your current project :)
You will want to have one class in each file. You can however have inner classes, but I do not believe you have learnt about them just yet

Also, you are not actually calling the method here, you are just printing out "gates.doOperation(true,false)"
System.out.println("gates.doOperation(true,false)");


Remove the quotes

This post has been edited by CasiOo: 03 December 2012 - 09:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: Java inheritance program using gates.

Posted 03 December 2012 - 09:55 AM

View PostCasiOo, on 03 December 2012 - 09:41 AM, said:

View Postamanjot24, on 03 December 2012 - 04:39 PM, said:

View Postpbl, on 03 December 2012 - 09:23 AM, said:

The class that is public and has a main() should be in the .java file that has its name
so
public class GatesTest {

should be in a file named GatesTest.java


okay so i should take the GatesTest class and put it in a separate program and it should work?


In a separate file in your current project :)/>/>
You will want to have one class in each file. You can however have inner classes, but I do not believe you have learnt about them just yet

Also, you are not actually calling the method here, you are just printing out "gates.doOperation(true,false)"
System.out.println("gates.doOperation(true,false)");


Remove the quotes



i Figured that i'd create another class. So this is my GatesTest class:

public class GatesTest {
	
	public static void main(String args[]) {
	
	Gates gates = new AND();
	System.out.println("gates.doOperation(true,false)");
	
	Gates gates1 = new OR();
	System.out.println("gates.doOperation(true,false)");
	
	Gates gates2 = new NAND();
	System.out.println("gates.doOperation(true,false)");
	
	Gates gates3 = new NOR();
	System.out.println("gates.doOperation(true,false)");
	
	}
}



Exactly how would i call the gates class to the test class? Also in the original gates class i receive errors.


public abstract class Gates {

    public abstract boolean doOperation(boolean a, boolean B)/>/>;

}

public class AND extends Gates {
    
	public boolean doOperation(boolean a, boolean B)/>/> {
		return a && b;
	}

}

public class OR extends Gates {
    
	public boolean doOperation(boolean a, boolean B)/>/> {
		return a || b;
	}

}

public class NAND extends Gates {
    
	public boolean doOperation(boolean a, boolean B)/>/> {
		return !( a && B)/>/>;
	}

}

public class NOR extends Gates {
    
	public boolean doOperation(boolean a, boolean B)/>/> {
		return !(a || b );
	}

}



ERROR:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	The public type AND must be defined in its own file
	The public type OR must be defined in its own file
	The public type NAND must be defined in its own file
	The public type NOR must be defined in its own file

	at AND.<init>(Gates.java:7)
	at GatesTest.main(GatesTest.java:5)


Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1414
  • View blog
  • Posts: 3,136
  • Joined: 05-April 11

Re: Java inheritance program using gates.

Posted 03 December 2012 - 10:11 AM

"must be defined in its own file"
"must be defined in its own file"
"must be defined in its own file"
"must be defined in its own file"

I think it wants you to move the classes into their own file
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java inheritance program using gates.

Posted 03 December 2012 - 10:26 AM

If a class is public it needs to be in its own file so AND.java, OR.JAVA ...
unless you make the class protected, then they can stay in the same .java
It is exactly the same problem you had with GatesTest not in ist onw .java but then you had no choice GatesTest has to be public.

Also

System.out.println("gates.doOperation(true,false)");

should be

System.out.println("gates.doOperation(true,false) result is: " + gates.doOperation(true,false));
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Java inheritance program using gates.

Posted 03 December 2012 - 01:56 PM

Your abstract class has no code. It could simply be an interface. If it were an interface, you could create anonymous instances. This would actually be easier.

e.g.
public class GatesTest {

	public interface Gates {
		boolean doOperation(boolean a, boolean b );
	}
	
	public void test(String name, Gates g) {
		System.out.println(name + ": doOperation(true,false) = " + g.doOperation(true,false));
	}
	
	public void test() {
		test("AND", new Gates() { public boolean doOperation(boolean a, boolean b ) {
			return a && b;
		} } );
		test("OR", new Gates() { public boolean doOperation(boolean a, boolean b ) {
			return a || b;
		} } );
	}

	public static void main(String[] args) {
		new GatesTest().test();
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java inheritance program using gates.

Posted 03 December 2012 - 02:19 PM

Sure that an abstract class with node code or method is useless but

View Postamanjot24, on 03 December 2012 - 12:17 PM, said:

I have to create a program using java inheritance while also incorporating the gave gates (and, or, nand, nor).

We have an homewrok requirement here :)
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Java inheritance program using gates.

Posted 03 December 2012 - 04:45 PM

Point. For extra fun, you could do something like:
abstract class Gates {
	public abstract String getGateName();
	public abstract boolean doOperation(boolean a, boolean b );
	public void test(boolean a, boolean b ) { System.out.println(getGateName() + " doOperation(" + a + ", " + b + ") = " + doOperation(a, b ) ); }
}



Now the base is actually helpful.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java inheritance program using gates.

Posted 03 December 2012 - 07:07 PM

Funny :) I am surprised/disappointed the solution is not recursive :^:
Was This Post Helpful? 0
  • +
  • -

#12 amanjot24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-October 12

Re: Java inheritance program using gates.

Posted 03 December 2012 - 07:52 PM

Thanks everyone for there help but i figured it out. Thank you very much

Here is the final code i ended with
//*******************************************************************
//Gates.Java
//
//This Program uses the 4 basic gates of java and uses them in a 
//inheritance class and sets up superclass and subclasses to 
//create program that prints out the basic functions of the gates
//*******************************************************************
import java.util.Scanner;

//Creates the Main class of the program and the last subclass
public class Gates {
	public static void main(String[] args){

		new SubClass_NOR();

	}
}
//Creates the Gates Class the will override all classes and 
//is used to print out the info that requires input and declares
//the variables for the program 
class SuperClass_Gates{
	int a;
	int b;

	protected SuperClass_Gates()
	{
		System.out.println("Please enter 0 or 1 as a and b,"+
				"(0 is false and 1 is true.");
		Scanner scan = new Scanner(System.in);
		System.out.print("a: ");
		a=scan.nextInt();
		System.out.print("b: ");
		b=scan.nextInt();
	}
}

//Subclass that used the variables in the AND gate and prints out 
//results using the gate

class SubClass_AND extends SuperClass_Gates{

	protected SubClass_AND(){
		if(a==1 && b==1)
			System.out.println("a&&b: 1");
		else
			System.out.println("a&&b: 0");
	}
}

//Subclass using the variables in the OR gate and prints out
//results for the gate
class SubClass_OR extends SubClass_AND{

	protected SubClass_OR(){
		if(a==1 || b==1)
			System.out.println("a||b: 1");
		else
			System.out.println("a||b: 0");
	}
}
//Subclass using the variables in the NAND gate and prints out
//results for the gate
class SubClass_NAND  extends SubClass_OR{

	protected SubClass_NAND(){
		if(a==1 && b==1)
			System.out.println("!(a&&B)/>: 0");
		else 
			System.out.println("!(a&&B)/>: 1");
	}
}

//Subclass using the variables in the NOR gate and prints out
//results for the gate
class SubClass_NOR extends SubClass_NAND{

	protected SubClass_NOR(){
		if(a==1 && b==1)
			System.out.println("!(a||B)/>: 0");
		else
			System.out.println("!(a||B)/>: 1");
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1