Created
November 15, 2021 18:36
-
-
Save jananpatel2002/7a897ee40568f75fa1c9262a14232b8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Janan Patel | |
* Date: 11/15/2021 | |
* Course Number: CSC 220/Data Structures | |
* Course Name: CSC 220/Data Structures | |
* Problem Number: Bunny ear counter | |
* Email: [email protected] | |
* Short Description of the Problem: This program counts the amount of ears of bunnies with recursive code | |
*/ | |
import java.util.Scanner; | |
public class BunnyEars { | |
private final static String TITLE = "The bunny ears program"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
// ********************************************** | |
// Put as many methods you need here | |
public static int bunnyEars(int bunnies) { | |
if (bunnies == 0) | |
return 0; | |
return 2 + bunnyEars(bunnies - 1); | |
} | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
System.out.println("Enter the amount of bunnies: "); | |
int amount = input.nextInt(); | |
System.out.println(amount + " Bunnies will have " + bunnyEars(amount) + " Ears"); | |
input.nextLine(); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Janan Patel | |
* Date: 11/15/2021 | |
* Course Number: CSC 220/Data Structures | |
* Course Name: CSC 220/Data Structures | |
* Problem Number: Digit adder | |
* Email: [email protected] | |
* Short Description of the Problem: This program adds up each digit of the number | |
*/ | |
import java.util.Scanner; | |
public class SumDigits { | |
private final static String TITLE = "The digits' sum program"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
// ********************************************** | |
// Put as many methods you need here | |
public static int sumDigits(int n) { | |
if (n == 0) | |
return 0; | |
return ((n % 10) + sumDigits(n / 10)); | |
} | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
System.out.println("Enter one numbers as a integar that you would like to sum up: "); | |
int x = input.nextInt(); | |
System.out.println("The sum of the numbers of that number is: " + sumDigits(x)); | |
input.nextLine(); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment