locked
Please convert java code into c# code. RRS feed

  • Question

  • Hi. This a java code. Please convert it into c# code.

     

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    package orffinder;

    /**
     *
     * @author Farrukh
     */
    public class Main {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           String sequence="ATTGCTAAGCAA";
           StringBuffer rnasequence= new StringBuffer();
          
           //////////////////////// Rna/////////////////////////////////////////////
           System.out.println("DNA Sequence::"+ sequence);
            for(int x=0; x<sequence.length(); x++)
            {
                if(sequence.charAt(x)=='T')
                {
                
                   
                    rnasequence.append('U');
                }
                else
                {
                    rnasequence.append(sequence.charAt(x));
                }
                
            }
           System.out.println("mRNA Sequence::"+rnasequence);
         
          
          
          
           //////////////////////////////Forward ORF//////////////////////////////////////
                  System.out.print("First ORF:");
           for(int y=0; y<sequence.length()-2;y=y+3)
               {
                  
                   System.out.print(sequence.substring(y,y+3)+"\t");
               }
               System.out.print("\nSecond ORF:");
                  for(int y=1; y<sequence.length()-2;y=y+3)
               {
                  
                   System.out.print(sequence.substring(y,y+3)+"\t");
               }
                System.out.print("\nThird ORF:");
                  for(int y=2; y<sequence.length()-2;y=y+3)
               {
                  
                   System.out.print(sequence.substring(y,y+3)+"\t");
               }
               
          ////////////////////////////////Reverse ORF////////////////////////////////////////
          
                
                StringBuffer sb= new StringBuffer();
                  for(int y=sequence.length()-1; y>=0;y--)
               {
                  sb.append(sequence.charAt(y));
                  
               }
               sb.toString();
                 System.out.print("\nFourth ORF:");
           for(int y=0; y<sequence.length()-2;y=y+3)
               {
                  
                   System.out.print(sb.substring(y,y+3)+"\t");
               }
               System.out.print("\nFifth ORF:");
                  for(int y=1; y<sequence.length()-2;y=y+3)
               {
                  
                   System.out.print(sb.substring(y,y+3)+"\t");
               }
                System.out.print("\nSixth ORF:");
                  for(int y=2; y<sequence.length()-2;y=y+3)
               {
                  
                   System.out.print(sb.substring(y,y+3)+"\t");
               }
               
               
           //////////////////////////////////////////////////////////////////////////
        }

    }

     

     

    thanks

    Regards,

    MFarrukh

    Friday, December 31, 2010 11:03 AM

Answers

  • Hmm ... Homework service? Where do you have the problems? I don't really feel to help ...

    System.out.print is Console.Write, .Net is not using camel case so you have to switch to Uppercase with first characters. And C# has properties for stuff like Length so no () required. And StingBuilder is the class you use to build strings.

    And I am not a Java developer, but sb.toString(); seems like a useless call. It creates a string and do not need an instance of it?

    But ok - the code that I made quickly without checking anything:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleTest
    {
      public class Main
      {
    
        public static void Main(String[] args)
        {
          string sequence = "ATTGCTAAGCAA";
          StringBuilder rnasequence = new StringBuilder();
    
          // Rna
          Console.Write("DNA Sequence::" + sequence);
    
          for (int x = 0; x < sequence.Length; x++)
          {
            if (sequence[x] == 'T')
              rnasequence.Append('U');
            else
              rnasequence.Append(sequence[x]);
    
          }
          Console.Write("mRNA Sequence::" + rnasequence);
    
          // Forward ORF
    
          Console.WriteLine("First ORF:");
    
          for (int y = 0; y < sequence.Length - 2; y = y + 3)
            Console.Write(sequence.Substring(y, y + 3) + "\t");
    
          Console.Write("\nSecond ORF:");
          for (int y = 1; y < sequence.Length - 2; y = y + 3)
            Console.Write(sequence.Substring(y, y + 3) + "\t");
    
          Console.Write("\nThird ORF:");
          for (int y = 2; y < sequence.Length - 2; y = y + 3)
            Console.Write(sequence.Substring(y, y + 3) + "\t");
    
          // Reverse ORF
    
    
          StringBuilder sb = new StringBuilder();
          for (int y = sequence.Length - 1; y >= 0; y--)
            sb.Append(sequence[y]);
    
          string sbstring = sb.ToString();
          Console.Write("\nFourth ORF:");
          for (int y = 0; y < sequence.Length - 2; y = y + 3)
            Console.Write(sbstring.Substring(y, y + 3) + "\t");
    
          Console.Write("\nFifth ORF:");
          for (int y = 1; y < sequence.Length - 2; y = y + 3)
            Console.Write(sbstring.Substring(y, y + 3) + "\t");
    
          Console.Write("\nSixth ORF:");
          for (int y = 2; y < sequence.Length - 2; y = y + 3)
            Console.Write(sbstring.Substring(y, y + 3) + "\t");
        }
      }
    }
    
    
    • Marked as answer by Mio_Miao Thursday, January 6, 2011 9:46 AM
    Friday, December 31, 2010 11:35 AM
  • That's pretty close - but 'Substring' in C# takes length as the second argument, unlike Java which takes 'end index' as the second argument, so all the Substring calls need to be changed.

    e.g., the last line of the conversion should be:

    Console.Write(sb.Substring(y, 3)+"\t");
    

    Also, in the original code I'm pretty sure that the line:

    sb.toString();
    
    

    is a mistake - this has no effect unless you assign it to something (in Java or in C#).


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Marked as answer by Mio_Miao Thursday, January 6, 2011 9:46 AM
    Friday, December 31, 2010 3:53 PM

All replies

  • Hmm ... Homework service? Where do you have the problems? I don't really feel to help ...

    System.out.print is Console.Write, .Net is not using camel case so you have to switch to Uppercase with first characters. And C# has properties for stuff like Length so no () required. And StingBuilder is the class you use to build strings.

    And I am not a Java developer, but sb.toString(); seems like a useless call. It creates a string and do not need an instance of it?

    But ok - the code that I made quickly without checking anything:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleTest
    {
      public class Main
      {
    
        public static void Main(String[] args)
        {
          string sequence = "ATTGCTAAGCAA";
          StringBuilder rnasequence = new StringBuilder();
    
          // Rna
          Console.Write("DNA Sequence::" + sequence);
    
          for (int x = 0; x < sequence.Length; x++)
          {
            if (sequence[x] == 'T')
              rnasequence.Append('U');
            else
              rnasequence.Append(sequence[x]);
    
          }
          Console.Write("mRNA Sequence::" + rnasequence);
    
          // Forward ORF
    
          Console.WriteLine("First ORF:");
    
          for (int y = 0; y < sequence.Length - 2; y = y + 3)
            Console.Write(sequence.Substring(y, y + 3) + "\t");
    
          Console.Write("\nSecond ORF:");
          for (int y = 1; y < sequence.Length - 2; y = y + 3)
            Console.Write(sequence.Substring(y, y + 3) + "\t");
    
          Console.Write("\nThird ORF:");
          for (int y = 2; y < sequence.Length - 2; y = y + 3)
            Console.Write(sequence.Substring(y, y + 3) + "\t");
    
          // Reverse ORF
    
    
          StringBuilder sb = new StringBuilder();
          for (int y = sequence.Length - 1; y >= 0; y--)
            sb.Append(sequence[y]);
    
          string sbstring = sb.ToString();
          Console.Write("\nFourth ORF:");
          for (int y = 0; y < sequence.Length - 2; y = y + 3)
            Console.Write(sbstring.Substring(y, y + 3) + "\t");
    
          Console.Write("\nFifth ORF:");
          for (int y = 1; y < sequence.Length - 2; y = y + 3)
            Console.Write(sbstring.Substring(y, y + 3) + "\t");
    
          Console.Write("\nSixth ORF:");
          for (int y = 2; y < sequence.Length - 2; y = y + 3)
            Console.Write(sbstring.Substring(y, y + 3) + "\t");
        }
      }
    }
    
    
    • Marked as answer by Mio_Miao Thursday, January 6, 2011 9:46 AM
    Friday, December 31, 2010 11:35 AM
  • thanks Konrad. i did not know the C# language but it is very necessary to me.

     

    thanks

    regards,

    MFarrukh 

    Friday, December 31, 2010 1:29 PM
  • That's pretty close - but 'Substring' in C# takes length as the second argument, unlike Java which takes 'end index' as the second argument, so all the Substring calls need to be changed.

    e.g., the last line of the conversion should be:

    Console.Write(sb.Substring(y, 3)+"\t");
    

    Also, in the original code I'm pretty sure that the line:

    sb.toString();
    
    

    is a mistake - this has no effect unless you assign it to something (in Java or in C#).


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Marked as answer by Mio_Miao Thursday, January 6, 2011 9:46 AM
    Friday, December 31, 2010 3:53 PM