Contents
- ./Chicken.java
- ./CoinFlip.java
- ./CoinFlipUsingBoolean.java
- ./NotSoRandom.java
- ./NotTheChickenClass.java
./Chicken.java 1/5
[top][prev][next]
/**
* A Java class that represents a Chicken. The state of the chicken is its
* name, height and weight.
*
* @author Sara Sprenkle
*/
public class Chicken {
// ------------ INSTANCE VARIABLES -------------------
private String name;
/** the height of the chicken in centimeters */
private int height;
/** the weight of the chicken in pounds */
private double weight;
/**
* Create a new Chicken object with the charactistics as specified by the
* parameters.
* @param height the height of the chicken in centimeters
* @param weight the weight of the chicken in pounds
*/
public Chicken(String name, int height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
/**
* Default name: "Bubba"; height and weight specified by parameters
* @param height the height of the chicken in centimeters
* @param weight the weight of the chicken in pounds
*/
public Chicken(int height, double weight) {
// if the user doesn't specify a name, let's make it Bubba
this("Bubba", height, weight);
}
//
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* @return the height of the chicken, in centimeters
*/
public int getHeight() {
return height;
}
/**
* @return the weight of the chicken, in pounds
*/
public double getWeight() {
return weight;
}
/**
* @return the name of the chicken
*/
public String getName() {
return name;
}
//
// ------------- MUTATORS -----------
//
/**
* Feeds the chicken
*/
public void feed() {
// Better: create constants for these increment values
weight += .3;
height += 1;
}
//
// ------------- SETTERS ----------
//
/**
* @param n
* the name of the chicken
*/
public void setName(String n) {
name = n;
}
/**
* @param h
* the height of the chicken, in cm
*/
public void setHeight(int h) {
height = h;
}
/**
* @param w
* the weight of the chicken, in pounds
*/
public void setWeight(double w) {
weight = w;
}
/**
* This method is private. It is accessible by the Chicken class
* but NOT by other classes.
*/
private void privateMethod() {
System.out.println("I am private!");
}
/**
* @param args
* the command-line arguments
*/
public static void main(String[] args) {
// "Fred", weight: 2.0, height: 38
// "Sallie Mae", weight: 3.0, height: 45
// "Momma", weight: 6.0, height: 83
int fredHeight = 38;
Chicken chicken = new Chicken("Fred", fredHeight, 2.0);
chicken.feed();
int newFredHeight = chicken.getHeight();
System.out.println(chicken.getName() + " is now " + newFredHeight + " cm tall.");
chicken.feed();
System.out.println("He's a growing boy at " + chicken.getHeight() + " cm tall.");
// Make chicken Bubba, weight: 4.0, height: 50
}
}
./CoinFlip.java 2/5
[top][prev][next]
import java.util.Random;
/**
* This class generates heads or tails at random.
*
* @author CSCI209 Class
*/
public class CoinFlip {
/**
* Called when user runs
* java CoinFlip
*/
public static void main(String[] args) {
Random coin = new Random();
int side = coin.nextInt(2);
if( side == 0 ) {
System.out.println("TAILS!");
} else {
System.out.println("HEADS!");
}
}
}
./CoinFlipUsingBoolean.java 3/5
[top][prev][next]
import java.util.Random;
/**
* This class generates heads or tails at random.
*
* @author CSCI209 Class
*/
public class CoinFlipUsingBoolean {
/**
* Called when user runs
* java CoinFlipUsingBoolean
*/
public static void main(String[] args) {
Random random = new Random();
if ( random.nextBoolean() ) {
System.out.println("HEADS!");
} else {
System.out.println("TAILS!");
}
}
}
./NotSoRandom.java 4/5
[top][prev][next]
import java.util.*;
/**
* Random number generation is not-so-random.
* A starting point for learning more: https://en.wikipedia.org/wiki/Pseudorandom_number_generator
* @author Sara Sprenkle
*/
public class NotSoRandom {
public static void main(String args[]) {
System.out.println("This program prints out random integers using");
System.out.println("two different random number generators");
System.out.println("\nNo hard-coded seed (Time-based seed):");
Random notSeededNumGen = new Random();
for( int i=0; i < 5; i++) {
System.out.println(notSeededNumGen.nextInt());
}
System.out.println("\nUses a seed. So, the same 5 numbers");
System.out.println("will print out each time you run the program.");
Random seededNumGen = new Random(100);
for( int i=0; i < 5; i++) {
System.out.println(seededNumGen.nextInt());
}
System.out.println("\nPlease rerun the program to see what I mean.");
}
}
./NotTheChickenClass.java 5/5
[top][prev][next]
/**
* Demonstrates how we can create Chicken objects
* and call its public methods, but
* we can't call the Chicken's private methods.
* @author Sara Sprenkle
*/
public class NotTheChickenClass {
public static void main(String[] args) {
Chicken myChicken = new Chicken("Fred", 10, 2);
myChicken.feed();
// this will result in a compiler error:
myChicken.privateMethod();
}
}
Generated by GNU Enscript 1.6.6.