Simplifying code
Suppose that you want to roll many different sizes of dice (because you are playing Dungeons and Dragons or some other role playing games).
Currently you would code it like this:
public class random
{
public static void main (String args [])
{
int num = 0;
num = ((int) (Math.random () * 4)) + 1;
System.out.println ("4 Sided Die Rolled: " + num);
num = ((int) (Math.random () * 6)) + 1;
System.out.println ("6 Sided Die Rolled: " + num);
num = ((int) (Math.random () * 8)) + 1;
System.out.println ("8 Sided Die Rolled: " + num);
num = ((int) (Math.random () * 10)) + 1;
System.out.println ("10 Sided Die Rolled: " + num);
num = ((int) (Math.random () * 20)) + 1;
System.out.println ("20 Sided Die Rolled: " + num);
}
}
These lines are repeated, just changed slightly:
num = ((int) (Math.random () * 20)) + 1;
System.out.println ("20 Sided Die Rolled: " + num);
We can put them in a method and then call the method in main:
public class random
{
public static void main (String args [])
{
roll (4);
roll (6);
roll (8);
roll (10);
roll (20);
}
public static void roll (int lg)
{
int num = ((int) (Math.random () * lg)) + 1;
System.out.println (lg + " Sided Die Rolled: " + num);
}
}
Simplify the following programs:
1. Enter numbers in certain ranges:
public class num
{
public static void main (String args [])
{
int num = -1;
while (num < 1 || num > 4)
{
num = IBIO.inputInt ("Enter a number between 1 and 4: ");
}
num = -1;
while (num < 1 || num > 6)
{
num = IBIO.inputInt ("Enter a number between 1 and 6: ");
}
num = -1;
while (num < 1 || num > 8)
{
num = IBIO.inputInt ("Enter a number between 1 and 8: ");
}
num = -1;
while (num < 1 || num > 10)
{
num = IBIO.inputInt ("Enter a number between 1 and 10: ");
}
num = -1;
while (num < 1 || num > 20)
{
num = IBIO.inputInt ("Enter a number between 1 and 20: ");
}
}
}
2. Cash Register:
public class cashregister
{
public static void main (String [] args)
{
String cont = "y";
while (cont.equals ("y"))
{
double total = IBIO.inputDouble ("Total cost?:");
double paid = IBIO.inputDouble ("Amount paid?:");
double change2 = paid - total;
int change = (int) (change2 * 100);
System.out.println (" ");
System.out.println ("Total change is " + change2 + "");
int num = 0;
System.out.println ("You will need:");
if ((change / 200) > 0)
{
num = change / 200;
change = change - num * 200;
System.out.println ("" + num + " twoony(twoonies)");
}
if ((change / 100) > 0)
{
num = change / 100;
change = change - num * 100;
System.out.println ("" + num + " loony(loonies)");
}
if ((change / 25) > 0)
{
num = change / 25;
change = change - num * 25;
System.out.println ("" + num + " quarter(s)");
}
if ((change / 10) > 0)
{
num = change / 10;
change = change - num * 10;
System.out.println ("" + num + " dime(s)");
}
if ((change / 5) > 0)
{
num = change / 5;
change = change - num * 5;
System.out.println ("" + num + " nickel(s)");
}
if (change > 0)
{
System.out.println ("" + change + " penny(pennies)");
}
cont = IBIO.inputString ("Do you want to continue? y/n:");
if (!cont.equals ("y"))
{
System.out.println ("goodbye!=)");
}
} //end while
} // end main
} // end cashregister