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.

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)

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.

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");
}
}