Java Task: Please help me to write a series of command line arguments that, when
ID: 3704666 • Letter: J
Question
Java Task:
Please help me to write a series of command line arguments that, when correctly specified, will cause the program to run to completion. This program is about a Puzzle.
The command line arguments must make the program to run to completion. Please read and help me. Thank you
Here is the task and example:
A Programming Puzzle
The file debugme/DebugMe.java is provided in the code pack and contains a puzzle. The main() method in this program accepts a series of command line arguments that, when correctly specified, will cause the program to run to completion. If the command line arguments are not specified correctly, the program will fail prematurely. The purpose of this problem is to determine the correct inputs to cause the program to run to completion.
DebugMe is arranged in "stages" and passing stages earns points. After each run the program prints the points earned based on the stages "passed". Example:
Notice that the first argument is always your UserID. DebugMe uses your UserID to create a unique set of conditions which you must pass so that your experience with the program will likely be different from other students.
This task is actually kinder to you - if you fail one phase, you are able to continue to the next.
The stages in DebugMe are intentionally obscure. This is code that is meant to be a puzzle rather than easily readable and to that end, you are strongly encouraged to use a debugger while analyzing the program. Debuggers allow you to stop execution at any point and examine variables, step forward line by line, and ultimately identify which kinds of command line arguments will make it through a stage and which will cause the dreaded failure() method to be invoked ending the run. A primary goal of this problem is to gain experience with debuggers as they are incredibly useful tool for a programmer to have in their utility belt. Credit for the problem is entirely based on how many stages are passed.
Command Line Arguments
The only artifact of your efforts will be the specific command-line invocation of the program, as your only inputs are given as command-line arguments.
the first command-line argument must be your UserID. Many of the phases will use this to randomize and personalize the code for you - you've got your very own unique project!
each phase (method) will read the next few arguments (however many it needs), and use them in more and more quirky ways.
towards the end of each phase, there will be some check and a guarded call to failure(). Your goal is to figure out what command-line arguments will cause these failures to never happen.
if you need to include spaces in a single argument, please use double-quotes.
Running DebugMe through the Debugger
How can we use the debugger with command-line arguments? It's surprisingly easy in DrJava:
Compile DebugMe.java
Set a breakpoint in the phase you're working on
Turn on debugging mode under the debugging menu
In the Interactions Pane, run a command like this to launch DebugMe with command line arguments.
If you've turned on debugging, though, you get to step through and see what's happening.
Most IDEs have a means of running a program through a debugger. Learn about your tool if you are not using DrJava.
For the brave, there is also a command line debugger, command jdb which should be installed with every JDK. This is somewhat less convenient as it provides only limited source code display but can work with some classic unix tools.
Regardless of how you access the debugger, you will want to familiarize yourself with features such setting breakpoints to stop the running program, stepping forward line by line, and displaying the values of variables.
DebugMe is not a "real" program as it is intentionally obscure. However, as an exercise to learn how to analyze running programs it will provide invaluable experience. Happy bug squashing.
------Here is the java codes----
import java.util.*;
public class DebugMe {
public static void main(String[] args){
int argsNeeded = 29;
if (args.length<argsNeeded){
String[] temp = new String[argsNeeded];
for (int i=0; i<argsNeeded; i++){
if (i<args.length){ temp[i] = args[i]; }
else { temp[i] = ""; }
}
args = temp;
}
String userID = args[0];
int hash = Math.abs(userID.hashCode());
hash %= 20000;
int score = 0;
int[] points = {5,5,5,6,6,6,7,7,8};
int pi = 0;
int maxScore = 0; for (int p : points) { maxScore += p; }
boolean lost = false;
try {
System.out.println("Let the games... BEGIN!");
System.out.println("Phase One, let's have some fun!");
phase1(hash, args,1);
score+=points[0];
}
catch (Exception e){
lost = true;
}
try {
if (!lost){
System.out.println("Good for you, time for phase two!");
}
phase2(hash, args,4);
score+=points[1];
}
catch (Exception e){
lost = true;
}
try {
if (!lost){
System.out.println("O-M-G, here's phase three!");
}
phase3(hash, args,7);
score+=points[2];
}
catch (Exception e){
lost = true;
}
try {
if (!lost){
System.out.println("time for more, here's phase four.");
}
phase4(hash, args,11);
score+=points[3];
}
catch (Exception e){
lost = true;
}
try {
if (!lost){
System.out.println("still alive? Here's phase five.");
}
phase5(hash, args,15);
score+=points[4];
}
catch (Exception e){
lost = true;
}
try {
if (!lost){
System.out.println("watch out, here comes a wall of bricks! It's time for you to solve phase six.");
}
phase6(hash, args,17);
score+=points[5];
}
catch (Exception e){
lost = true;
}
try {
if (!lost){
System.out.println("next it's phase eleven! oops, seven.");
}
phase7(hash, args,21);
score+=points[6];
}
catch (Exception e){
lost = true;
}
try {
if (!lost){
System.out.println("youre doing great - now try phase eight!");
}
phase8(hash, args,24);
score+=points[7];
}
catch (Exception e){
lost = true;
}
try {
if (!lost){
System.out.println("finally, the finish line - I hope that you can solve phase nine!");
}
phase9(hash, args,27);
score+=points[8];
}
catch (Exception e){
lost = true;
}
if (!lost){
System.out.println("Hey, neat - the task's complete!");
}
else {
System.out.println("oops! Game Over... >:( ");
}
System.out.printf(" * Score: %s/%s pts * ",score,maxScore);
}
// 3 args
public static void phase1(int hash, String[] args, int argStart){
int a = Integer.parseInt(args[argStart+0]);
int b = Integer.parseInt(args[argStart+1]);
int c = Integer.parseInt(args[argStart+2]);
a += hash % 40;
if (a<b && b>c && a<c) { return; }
failure("Double debugger burger, order up!");
}
// 3 args
public static void phase2(int hash, String[] args, int argStart){
int a = Integer.parseInt(args[argStart+0]);
int b = Integer.parseInt(args[argStart+1]);
int c = Integer.parseInt(args[argStart+2]);
a += hash%26;
int v = a+b;
v *= a;
v /= b;
v += 14;
if (c!=v){ failure("these are not the ints you're searching for..."); }
}
// 4 args
public static void phase3(int hash, String[] args, int argStart){
int a = Integer.parseInt(args[argStart+0]);
int b = Integer.parseInt(args[argStart+1]);
int c = Integer.parseInt(args[argStart+2]);
int d = Integer.parseInt(args[argStart+3]);
if (hash%13 > 4) {
c += hash%13;
}
else {
a *= hash%5 + 1;
}
int temp = 0;
do {
temp += a;
temp *= b;
} while (c --> 0);
if (temp != d) failure("count around, products abound; what's that there - an arrow? uh-oh...");
}
// 4 args
public static void phase4(int hash, String[] args, int argStart){
String s = args[argStart+0];
int a = Integer.parseInt(args[argStart+1]);
int b = Integer.parseInt(args[argStart+2]);
int c = Integer.parseInt(args[argStart+3]);
String temp1 = s.substring(a,b);
String temp2 = s.substring(b,c);
int temp3 = 0;
while (temp3<temp1.length() && temp3<temp2.length()){
if (temp1.charAt(temp3) != temp2.charAt(temp3)){
break;
}
temp3++;
}
if (temp3!=6) failure("over here and over there, I need a certain kind of pair...");
}
// helperfor phase 5.
public static String collatzString(int n){
String s = "[";
while (n!=1){
s += n+", ";
if (n%2==0){
n/=2;
}
else {
n = n*3+1;
}
}
s += "1]";
return s;
}
// 2 args
public static void phase5(int hash, String[] args, int argStart){
int a = Integer.parseInt(args[argStart+0]);
int b = Integer.parseInt(args[argStart+1]);
String s = collatzString(a);
if (b != s.length()) {
failure("collecting strings and lengths of things in one big batch; so what should match?");
}
}
// 4 args
public static void phase6(int hash, String[] args, int argStart){
int a = Integer.parseInt(args[argStart+0]);
int b = Integer.parseInt(args[argStart+1]);
int c = Integer.parseInt(args[argStart+2]);
int v = 0;
int temp = 6;
switch (a + hash%11){
case 1: v = helper3 (hash,a+12,b,temp++); break;
case 2: v = helper3 (hash,a*20,b-a,5); break;
case 3: v = helper3 (hash,a-310,a-b,5); break;
case 4: v = helper3 (hash,(int)Math.pow(a,2),b,5); break;
case 5: v = helper3 (hash,(int)Math.pow(2,a),b,5); break;
case 6: v = helper3 (hash,b,a,temp--); break;
case 7: v = helper3 (hash,a,b+a,temp*2); break;
case 8: v = helper3 (hash,a+temp,b-temp,temp%temp); break;
case 9: case 10: case 11:
v = helper3 (hash,a,b,temp+10); break;
default: v = -1;
}
if ( ! secretMessages[c].equals(args[v])) { failure("wrong message!"); }
}
private static String[] secretMessages = {
"a closed mouth gathers no feet",
"a waist is a terrible thing to mind",
"out of my mind - back in five minutes",
"the harder I work, the luckier I become",
"with great power comes a big electricity bill",
"time flies like an arrow; fruit flies like a banana",
"five exclamation marks, the sure sign of an insane mind",
"I do not like green eggs and ham, I do not like them, Sam-I-am.",
"CS majors need to take ENGH302*N*, not just any section of ENGH302",
"+++Divide By Cucumber Error. Please Reinstall Universe And Reboot +++",
"there are 10 kinds of people in the world, those who understand binary and those who don't"
};
public static int helper3(int hash, int x, int y, int z){
for (int j=0; j<y; j++){
if (prime(j)){
z+=1;
}
}
if (z<10 || z>20){ failure("uh-oh!"); }
return z;
}
public static boolean prime(int n){
if (n<2) return false;
for (int i=2; i<n; i++){
if (n%i==0) { return false; }
}
return true;
}
public static int sumRange(int start, int stop){
if (start > stop) { return 0; }
return start + (sumRange(start+1, stop));
}
// 3 args
public static void phase7(int hash, String[] args, int argStart){
int a = Integer.parseInt(args[argStart+0]);
int b = Integer.parseInt(args[argStart+1]);
int c = Integer.parseInt(args[argStart+2]);
int d = sumRange(a,b);
if (c!=d) { failure("round and round and round she goes, but where she stops, nobody knows!"); }
}
// With all this randomness, surely you want to step through with the
// debugger, yes?
// 3 args
public static void phase8(int hash, String[] args, int argStart) {
Random r = new Random(hash);
int a = Integer.parseInt(args[argStart+0]);
int b = Integer.parseInt(args[argStart+1]);
int c = Integer.parseInt(args[argStart+2]);
int s = 0;
for (int i=0; i<a; i+= b){
if (r.nextInt(hash)%c==0){
s += 1;
}
}
if (s!=10) { failure("level one, as yet undone! "+s); }
}
// Generating so much data, how can you find a reliable answer?
// Remember, you can't change the source code for your submitted
// solution...
// 2 args
public static void phase9(int hash, String[] args, int argStart){
createWaldoPage(args,argStart);
}
private static String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// hmmm, what happens if this is true??? not that you can change it
// for the final submission...
public static final boolean DEBUG = false;
public static void debug(Object msg){ if (DEBUG) { System.out.print(""+msg); } }
public static void debugln(Object msg){ debug(msg+" "); }
public static String[] createWaldoPage(String[] args, int argStart){
int hash = Math.abs(args[0].hashCode()) ;
debugln("hash = "+hash);
int n = Integer.parseInt(args[argStart+0]);
int maxChanges = Integer.parseInt(args[argStart+1]);
debug(String.format("n=%s, maxChanges=%s ",n,maxChanges));
String[] crowd = new String[n];
for (int i = 0; i<n; i++){
crowd[i] = mutate("waldo", maxChanges, hash++);
}
checkWaldoPage(crowd);
return crowd;
}
public static void checkWaldoPage(String[] crowd){
ArrayList<Integer> indexes = new ArrayList<Integer>();
for (int i=0; i<crowd.length; i++){
debugln("c[i] = "+crowd[i]);
if (crowd[i].equals("waldo")){
debugln(i);
indexes.add(i);
}
}
if (indexes.size() < 1) DebugMe.failure("couldn't find waldo!");
else if (indexes.size() > 1) DebugMe.failure("highlander rule: there can be only one (waldo)! "+indexes.size());
}
public static String mutate(String s, int n, int seed){
debug(String.format("mutation(%s#): %s (hash=%s) ",n,s,seed));
Random r = new Random(seed);
for (int i=0; i<n; i++){
s = attemptChange(s, seed++);
}
debugln(" --> "+s);
return s;
}
public static String attemptChange(String s, int seed){
debug(String.format(" attempt(seed=%d): %s ",seed,s));
Random r = new Random(seed);
int i = r.nextInt(s.length()+5);
switch(r.nextInt(3)){
case 0:
try{
debug(String.format(" %s(%s)%s ",s.substring(0,i),letters.charAt(i),s.substring(i)));
s = String.format("%s%s%s",s.substring(0,i),letters.charAt(i),s.substring(i));
} catch (StringIndexOutOfBoundsException e) { }
break;
case 1:
try{
debug(String.format(" %s(%s/%s)%s ",s.substring(0,i),letters.charAt(i),s.charAt(i),s.substring(i+1)));
s = String.format("%s%s%s",s.substring(0,i),letters.charAt(i),s.substring(i+1));
} catch (StringIndexOutOfBoundsException e) { }
break;
case 2:
try{
s = s.substring(0,i)+s.substring(i+1);
} catch (StringIndexOutOfBoundsException e) { }
break;
default:
debugln(" --> DEFAULT! ");
}
return s;
}
// any time the program has wandered into the wrong spot, we call this
// to indicate the phase was not successfully passed.
public static void failure(String msg){
throw new RuntimeException("Failure! "+msg);
}
}
Here is an additional link to the codes: https://paste.ee/p/RIDxP
Please help if you can. Thank you.
Explanation / Answer
Find the below list of arguments for the program to complete all 9 phases
1 1 41 40 1 2 326 1 2 3 32766 aaaaaaaaaaaa 0 6 12 2 6 5 10 3 "the harder I work, the luckier I become" 1 5 15 10 1 1 1 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.