amanjot24's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
19 (0.03 per day)
Joined:
07-October 12
Profile Views:
119
Last Active:
User is offline Dec 03 2012 07:49 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   amanjot24 has not set their status

Posts I've Made

  1. In Topic: Java inheritance program using gates.

    Posted 3 Dec 2012

    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");
    	}
    }
    
    
  2. In Topic: Java inheritance program using gates.

    Posted 3 Dec 2012

    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)
    
    
  3. In Topic: Java inheritance program using gates.

    Posted 3 Dec 2012

    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?
  4. In Topic: Java program that reads .txt files?

    Posted 4 Nov 2012

    View Postpbl, on 04 November 2012 - 06:52 PM, said:

    Use a Scanner lot easier

    Scanner in1 = new Scanner(new File("Filea.txt"));
    Scanner in2 = new Scanner(new File("Fileb.txt"));
    
    while(in1.hasNextLine() && in2.hasNextLine()) {
       String line1 = in1.nextLine();   
       String line2 = in2.nextLine();
       String[] token1 = line1.split(" ");
       String[] token2 = line2.split(" ");
       if(token1.length != token2.length) {
           ... not same number of number
       }
       int[] tot = new int[token1.length];
       for(int i = 0; i < tot.length; ++i) {
          tot[i] = Integer.parseInt(token1[i]) + Integer.parseInt(token2[i]));
          ... do whatever you want with to[i] here
       }
    }
    
    


    What do you mean by not same number of number?
  5. In Topic: Java program that reads .txt files?

    Posted 4 Nov 2012

    View Postpbl, on 04 November 2012 - 06:22 PM, said:

    Sorry that easy ?

    two Scanner that scan.nextLine() and String.split(" ")
    should do the job
    determine the size of the matrix based on first call... but actually will work with a triangular matrix :)
    use Integer.parseInt() to read each token

    add column by column


    okay so this i where i stand right now?

    import java.util.Scanner;
    import java.io.File;
    
    
    // Matrix Addition
    public class Matrices {
    	public final static String filename1 = "FileA.txt";
    	public final static String filename2 = "FileB.txt";
    
    	public static void main(String[] args) {
    		Scanner inFile1;
    		Scanner inFile2;
    		String in1, in2;
    		boolean eof = false, eol;
    		int mv1, mv2, mvs;
    
    		try {
    			inFile1 = new Scanner(new File(filename1));
    		} catch (Exception e) {
    			System.out.println("File could not be opened: " + filename1);
    			System.exit(0);
    		}
    		try {
    			inFile2 = new Scanner(new File(filename2));
    		} catch (Exception e) {
    			System.out.println("File could not be opened: " + filename2);
    			System.exit(0);
    		}
    
    		System.out.println("Result of the matrix addition\n");
    
    		//read file line by line until eof
    		while (!eof) {
    			if (((in1 = inFile1.readLine()) != null) && ((in2 = inFile2.readLine()) != null))
    			{
    
    				//read and add all values per line
    				eol = false;
    				while (!eol) {
    					if (((mv1 = in1.nextInt()) != null) && ((mv2 = in2.nextInt()) != null)) 
    					{
    						mvs = mv1 + mv2;
    						System.out.print(mvs + " ");
    					}
    					else eol = true;
    				} // while (!eol)
    				System.out.println();
    			}
    			else eof = true;
    		} // while (!eof)
    
    		inFile1.close();
    		inFile2.close();
    	}
    }
    
    


    Am i in the right direction i get and error with ".readLine and .nextInt" though.

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

amanjot24 hasn't added any friends yet.

Comments

amanjot24 has no profile comments yet. Why not say hello?