Question

In: Computer Science

CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...

CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT

import java.util.*;

public class TestPaperFolds
{
   public static void main(String[] args)
   {
       for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds
       {
           String fold_string = paperFold(i);   //call paperFold to get the String for i folds
           System.out.println("For " + i + " folds we get: " + fold_string);
       }
   }

   public static String paperFold(int numOfFolds)   //recursive function that returns the FoldSequence string
   {
       if(numOfFolds == 1){       //if numOfFolds = 1, the return "v"
           return "v";
       }
       else{                       //Otherwise make a recursive call to the paperFold(numOfFolds - 1)
           String s = paperFold(numOfFolds -1);
           return flipString(reverseString(s)) + "v" + s;    //reverse the string s, then flip it, then append "v" and s
       }

   }

   public static String reverseString(String s)   //this helper function reverses a string
   {
       String out = "";
       for(int i = s.length()-1; i >= 0; i--)
           out = out + s.charAt(i);
       return out;
   }

   public static String flipString(String s)   //this helper function flips a string
   {
       String out = "";
       for(int i = 0; i < s.length(); i++){
           if(s.charAt(i) == 'v'){               //converts v into ^
               out = out + '^';
           }
           else if(s.charAt(i) == '^'){       //converts ^ into v
               out = out + 'v';
           }
       }
       return out;
   }
}

-----------
Sample Output:
For 1 folds we get: v
For 2 folds we get: ^vv
For 3 folds we get: ^^vv^vv
For 4 folds we get: ^^v^^vvv^^vv^vv

Solutions

Expert Solution

Dear Student ,

As per requirement submitted above kindly find below solution.

Here new console application in C# is created using visual studio 2019 with name "TestPaperFoldsApp". This application contains a program with name "TestPaperFolds.cs".Below is the details of this class.

TestPaperFolds.cs :

//namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//application namespace
namespace TestPaperFoldsApp
{
class TestPaperFolds //C# class
{
//Main() method
static void Main(string[] args)
{
//using for loop
for (int i = 1; i <= 4; i++) //loop for i = 1 to 4 folds
{
string fold_string = paperFold(i); //call paperFold to get the String for i folds
Console.WriteLine("For " + i + " folds we get: " + fold_string);
}
}

public static string paperFold(int numOfFolds) //recursive function that returns the FoldSequence string
{
if (numOfFolds == 1)
{ //if numOfFolds = 1, the return "v"
return "v";
}
else
{ //Otherwise make a recursive call to the paperFold(numOfFolds - 1)
String s = paperFold(numOfFolds - 1);
return flipString(reverseString(s)) + "v" + s; //reverse the string s, then flip it, then append "v" and s
}
}
public static string reverseString(string s) //this helper function reverses a string
{
string outVar = "";
for (int i = s.Length - 1; i >= 0; i--)
outVar = outVar + s[i];
return outVar;
}

public static string flipString(string s) //this helper function flips a string
{
string outvar = "";
for (int i = 0; i < s.Length; i++)
{
if (s[i] == 'v')
{ //converts v into ^
outvar = outvar + '^';
}
else if (s[i] == '^')
{ //converts ^ into v
outvar = outvar + 'v';
}
}
return outvar;
}
}

}

==================================

Output :Run application using F5 and will get the screen as shown below


Related Solutions

Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public...
Please convert this java program to a program with methods please. import java.io.*; import java.util.*; public class Number{ public static void main(String[] args) {    Scanner scan = new Scanner(System.in); System.out.println("Enter 20 integers ranging from -999 to 999 : "); //print statement int[] array = new int[20]; //array of size 20 for(int i=0;i<20;i++){ array[i] = scan.nextInt(); //user input if(array[i]<-999 || array[i]>999){ //check if value is inside the range System.out.println("Please enter a number between -999 to 999"); i--; } } //...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for...
Can someone convert this to C++ Please! import java.util.*; // for Random import java.util.Scanner; // for Scanner class game{    public static void main(String args[])    {        // generating a random number        Random rand = new Random();        int code = rand.nextInt(99999) + 1, chances = 1, help, turn, i,match, sum;               // for input        Scanner sc = new Scanner(System.in);               // running for 10 times   ...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;        count = 0;    } // end Dersop3 class constructor   ...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT