-
-
Save bytecodeman/8e838d5f7c0e88af2f2b96c8aebf56ac to your computer and use it in GitHub Desktop.
Chapter 6 Extra Credit in Class Assignment (Decent Numbers)
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: Tony Silvestri | |
* Date: 9/11/19 | |
* Course Number: CSC-220 | |
* Course Name: Data Structures | |
* Problem Number: Chapter 6 Extra Credit in Class Assignment | |
* Email: silvestri@stcc.edu | |
* Decent Number Calculation | |
*/ | |
import java.util.Scanner; | |
public class DecentNumbers { | |
final static String TITLE = "Decent Numbers V1.0"; | |
final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
final static boolean PROCESS_USING_SCANNER = true; | |
//********************************************** | |
// Put as many methods you need here | |
private static String decentNumber(int digits) { | |
int maxGroupof3Fives = digits / 3; | |
while (maxGroupof3Fives >= 0) { | |
int remainingThrees = digits - 3 * maxGroupof3Fives; | |
if (remainingThrees % 5 == 0) | |
break; | |
maxGroupof3Fives--; | |
} | |
if (maxGroupof3Fives < 0) | |
return "-1"; | |
String dn = ""; | |
for (int i = 0; i < maxGroupof3Fives * 3; i++) | |
dn += "5"; | |
for (int i = 0; i < digits - 3 * maxGroupof3Fives; i++) | |
dn += "3"; | |
return dn; | |
} | |
//********************************************** | |
private static void process(Scanner sc, String args[]) { | |
System.out.print("Enter number of digits in DN: "); | |
int digits = sc.nextInt(); | |
String dn = decentNumber(digits); | |
System.out.println("Decent Number of " + digits + " digits = " + dn); | |
} | |
//********************************************** | |
@SuppressWarnings("unused") | |
private static boolean doThisAgain(Scanner sc, String prompt) { | |
System.out.print(prompt); | |
if (PROCESS_USING_SCANNER && sc.hasNextLine()) | |
sc.nextLine(); | |
String doOver = sc.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
//********************************************** | |
public static void main(String args[]) { | |
System.out.println("Welcome to " + TITLE); | |
Scanner sc = new Scanner(System.in); | |
do { | |
process(sc, args); | |
} while (doThisAgain(sc, CONTINUE_PROMPT)); | |
sc.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