Between Stanford CS106A and 'Art and Science of Java' (the course textbook.

Just showing people some of the code, not all of it though. Only when I feel like it.

Tuesday, August 6, 2013

Write a program that simulates the decay of a sample that contains 10,000 atoms of radioactive material, where each atom has a 50 percent chance of decaying in a year. The output of your program should show the number of atoms remaining at the end of each year
 import acm.program.*;  
 import acm.util.*;  
 public class atoms extends ConsoleProgram {  
   public void run() {  
        int total = 10000;  
        int count = 0;  
        while (total > 0) {  
             count++;  
             if (total == 10000) {  
                  println("There are " + total + " atoms initially.");  
             }  
             total = (halfMinus(total));  
             println("There are " + total + " atoms at the end of year " + count);  
        }//ends while  
   } //ends run  
   private int halfMinus (int number) {  
        int difference;  
        if (number < 30) {  
              difference = rgen.nextInt(1,3);  
        } else {  
              difference = rgen.nextInt(12,31);  
        }  
        int choice = rgen.nextInt(1,2);  
        if (choice == 1) {  
             number -= difference;  
        } else {  
             number += difference;  
        }  
        number /= 2;  
        return number;  
   }  
   RandomGenerator rgen = new RandomGenerator();  
 }//ends class  
While this does give me the result I'm looking for, I am not satisfied with it 100% of the time. The issue is when it gets down to the lower numbers, sometimes when it hits 2,1 or 0, it will go for a few more rounds. Anyway, this serves its purpose.

Monday, August 5, 2013

Write a program that displays the name of a card randomly chosen from a complete deck of 52 playing cards. Each card consists of a rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and a suit (Clubs, Diamonds, Hearts, Spades). Your program should display the complete name of the card
 import acm.program.*;  
 import acm.util.*;  
 public class Exercises extends ConsoleProgram {  
   public void run() {  
        println("This program displays a randomly chosen card.");  
        println(card(rgen.nextInt(1,13)) + " of " + suit(rgen.nextInt(1,4)));  
   } //ends run  
   private String card(int number) {  
        switch (number) {  
        case 1: return "Ace";  
        case 2: return "2";  
        case 3: return "3";  
        case 4: return "4";  
        case 5: return "5";  
        case 6: return "6";  
        case 7: return "7";  
        case 8: return "8";  
        case 9: return "9";  
        case 10: return "10";  
        case 11: return "Jack";  
        case 12: return "Queen";  
        case 13: return "King";  
        default: return null;  
        }//ends switch  
   }//ends method  
   private String suit(int number) {  
        switch (number) {  
        case 1: return "Clubs";  
        case 2: return "Diamonds";  
        case 3: return "Hearts";  
        case 4: return "Spades";  
        default: return null;  
        }  
   }  
   private RandomGenerator rgen = new RandomGenerator();  
 }//ends class  

Monday, July 29, 2013

Draws a target.
1:  /*  
2:   * File: Target.java  
3:   * Name:  
4:   * Section Leader:  
5:   * -----------------  
6:   * This file is the starter file for the Target problem.  
7:   */  
8:  import acm.graphics.*;  
9:  import acm.program.*;  
10:  import java.awt.*;  
11:  public class Target extends GraphicsProgram {       
12:       public void run() {  
13:            drawCircle1(getWidth() / 2 - 72, getHeight() / 2 - 72, 144, 144); //tells the program to render the first circle  
14:            drawCircle2(getWidth() / 2 - 46.8, getHeight() / 2 - 46.8, 93.6, 93.6);  //tells the program to render the second circle  
15:            drawCircle3(getWidth() / 2 - 21.6, getHeight() / 2 - 21.6, 43.2, 43.2);  //tells the program to render the last circle  
16:            //all these circles pass the 4 required numbers to the methods  
17:       }  
18:       public void drawCircle1(double x, double y, double w, double h) { //outer Red  
19:            GOval circle1 = new GOval(x, y, w, h); //creates the circle with the above dimensions  
20:            circle1.setColor(Color.RED); //makes it red first  
21:            circle1.setFilled(true); //fills it (with the red)  
22:            add(circle1); //draws it  
23:       }  
24:       //very similar things for the next two methods.  
25:       public void drawCircle2(double x, double y, double w, double h) { //middle white  
26:            GOval circle2 = new GOval(x, y, w, h);  
27:            circle2.setColor(Color.WHITE);  
28:            circle2.setFilled(true);  
29:            add(circle2);  
30:       }  
31:       public void drawCircle3(double x, double y, double w, double h) { //inner red  
32:            GOval circle3 = new GOval(x, y, w, h);  
33:            circle3.setColor(Color.RED);  
34:            circle3.setFilled(true);  
35:            add(circle3);  
36:       }  
37:  }  
Code for letting the user enter a bunch of numbers, then when 0 is hit, stops, reads back the highest and lowest numbers that were entered.
1:  /*  
2:   * File: FindRange.java  
3:   * Name:  
4:   * Section Leader:  
5:   * --------------------  
6:   * This file is the starter file for the FindRange problem.  
7:   */  
8:  import acm.program.*;  
9:  public class FindRange extends ConsoleProgram {  
10:       public void run() {  
11:            int number, small = Integer.MAX_VALUE, large = 0;  
12:            println("This program finds the largest and smallest numbers.");  
13:            number = readInt("?");  
14:            while (true) {  
15:                 number = readInt("?");  
16:                 if (number == 0) {  
17:                      break;  
18:                 }  
19:                 if (number > large) {  
20:                      large = number;  
21:                 }  
22:                 if (number < small) {  
23:                      small = number;  
24:                 }  
25:            }  
26:            println("smallest was " + small + " and largest was " + large);  
27:       }  
28:  }