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:...
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--; } } //...
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   ...
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;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT