Emre Alpogunc
Emre Alpogunc

Reputation: 51

The Type is Already Defined Error in Java

I'm trying to finish my assignment and I have to sum all of my answers in one ".java" file. But when I tried to add all of the classes to one file I get a "The type que2 is already defined" error for que2,3 and 4. Why am I getting this error ? But if I run them in separate files I can run them without any error. I'm used to python and I've never experienced something like this.Thank you and have a good day. I'm using eclipse btw.

/* *****************************************************************************
 *  Name:         Emre Cokcalışı
 *  Student ID:   6321211
 *  Department:   Industrial Engineering
 *
 *  Assignment ID: A1 Question 1 
 *
 *  Description:  Prints the number of stars to the console
 *                depending on the counts stored in an array.   
 *
 *  Sources:      Give references for the sources that you used in your 
 *                program if there areany
 **************************************************************************** */
package tes;
import java.util.Scanner;
import java.util.Base64;    
import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;   
import javax.crypto.SecretKey; 
public class tes {
          public static void main(String[] args) {
              Scanner sc = new Scanner(System.in);
              int a = 0;
              while (a != 6) {
                  System.out.println("                      ");
                  System.out.println("Select Operation:");
                  System.out.println("1:Addition");
                  System.out.println("2:Subtraction");
                  System.out.println("3:Multiplication");
                  System.out.println("4:Division");
                  System.out.println("5:Remainder");
                  System.out.println("6:Exit");
                  System.out.println("                      ");
                  System.out.print("Enter a choice: ");
                  int n = sc.nextInt();
                  a = n;
                  
                  if (n==6) {
                     break;
                  } 
                  else if(n==1) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     
                     System.out.println("Result of "+n1+"+"+n2+" is "+(n1+n2));
                  }
                  else if(n==2) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     
                     System.out.println("Result of "+n1+"-"+n2+" is "+(n1-n2));
                  }
                  else if(n==3) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     
                     System.out.println("Result of "+n1+"*"+n2+" is "+(n1*n2));
                  }
                  else if(n==4) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     if(n2==0) {
                         System.out.println("Please change the n2 value.");
                     }
                     else {
                         System.out.println("Result of "+n1+"/"+n2+" is "+(n1/n2));
                     }                
                     }
                  else if(n==5) {
                     System.out.print("Please,enter the first number: ");
                     int n1 = sc.nextInt();
                     System.out.print("Please,enter the second number: ");
                     int n2 = sc.nextInt();
                     
                     System.out.println("Result of "+n1+"%"+n2+" is "+(n1%n2));
                  }
              }
          }
}
/* *****************************************************************************
 *  Name:         Emre Cokcalışı
 *  Student ID:   6321211
 *  Department:   Industrial Engineering
 *
 *  Assignment ID: A1 Question 2 
 *
 *  Description:  Prints the number of stars to the console
 *                depending on the counts stored in an array.   
 *
 *  Sources:      Give references for the sources that you used in your 
 *                program if there areany
 **************************************************************************** */
class que2 {

    public static void main(String[] args) {
        String s1 = "Welcome to Java";
        String s2 = s1;
        String s3 = new String("Welcome to Java"); 
        String s4 = "Welcome to Java";
        
        System.out.println(s1 == s2);
        System.out.println(s2 == s3);
        System.out.println(s1.equals(s2));
        System.out.println(s2.equals(s3));
        System.out.println(s1.compareTo(s2));
        System.out.println(s1==s4);
        System.out.println(s1.charAt(0));
        System.out.println(s1.indexOf("j"));
        System.out.println(s1.indexOf("to"));
        System.out.println(s1.lastIndexOf("a"));
        System.out.println(s1.lastIndexOf("o", 15));
        System.out.println(s1.length());
        System.out.println(s1.substring(5));
        System.out.println(s1.substring(5, 11));
        System.out.println(s1.startsWith("Wel"));
        System.out.println(s1.substring(s1.lastIndexOf(" ")+1));
    }

}
/* *****************************************************************************
 *  Name:         Emre Cokcalışı
 *  Student ID:   6321211
 *  Department:   Industrial Engineering
 *
 *  Assignment ID: A1 Question 3 
 *
 *  Description:  Prints the number of stars to the console
 *                depending on the counts stored in an array.   
 *
 *  Sources:      Give references for the sources that you used in your 
 *                program if there areany
 **************************************************************************** */
class que3 {
    public static void main(String []args) { 
        int sum=0;
        // iterate from 0 to N 
        for (int num = 0; num < 51; num++) 
        {    
            // Short-circuit operator is used  
            if (num % 3 == 0 && num % 5 == 0){
                sum+=num;
                System.out.println(num+"");  
        }  
    }   System.out.println("Sum is: ");
        System.out.println(sum); 
}
    
}
/* *****************************************************************************
 *  Name:         Emre Cokcalışı
 *  Student ID:   6321211
 *  Department:   Industrial Engineering
 *
 *  Assignment ID: A1 Question 4 
 *
 *  Description:  Prints the number of stars to the console
 *                depending on the counts stored in an array.   
 *
 *  Sources:      Give references for the sources that you used in your 
 *                program if there areany
 **************************************************************************** */
class que4 {  
    static Cipher cipher;  

    public static void main(String[] args) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128); // block size is 128bits
        SecretKey secretKey = keyGenerator.generateKey();

        cipher = Cipher.getInstance("AES");

        String plainText = "AES Symmetric Encryption Decryption";
        System.out.println("Plain Text Before Encryption: " + plainText);

        String encryptedText = encrypt(plainText, secretKey);
        System.out.println("Encrypted Text After Encryption: " + encryptedText);

        String decryptedText = decrypt(encryptedText, secretKey);
        System.out.println("Decrypted Text After Decryption: " + decryptedText);
    }

    public static String encrypt(String plaTex, SecretKey sKey)
            throws Exception {
        byte[] Tb = plaTex.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, sKey);
        byte[] encryptedByte = cipher.doFinal(Tb);
        Base64.Encoder encoder = Base64.getEncoder();
        String encTex = encoder.encodeToString(encryptedByte);
        return encTex;
    }

    public static String decrypt(String encryptedText, SecretKey secretKey)
            throws Exception {
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] encryptedTextByte = decoder.decode(encryptedText);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
        String decTex = new String(decryptedByte);
        return decTex;
    }
}

Upvotes: 0

Views: 6479

Answers (1)

Milgo
Milgo

Reputation: 2767

You could get this error ff you have que2 as a inner class in your tes class file and a separate que2 class file. In this case, you should remove the que2 file.

Upvotes: 1

Related Questions