Skip to content

Instantly share code, notes, and snippets.

@ltagliaferri
Created February 21, 2014 05:55
Show Gist options
  • Save ltagliaferri/9129493 to your computer and use it in GitHub Desktop.
Save ltagliaferri/9129493 to your computer and use it in GitHub Desktop.
/**
* When the javadoc tool is run over this code, it will bring up the
* HTML maintenance manual.
* @author LTagliaferri
* @version 1.0
*/
public class Counter {
/**
* This declares private integers and
* declares initial count and max values
*/
private int count;
private int max;
public Counter() {
int count = 0;
int max = 10;
}
public int getCount() {
return count;
}
//This method returns count.
public int getMax() {
return max;
}
//This method returns max.
public void setCount(int n) {
this.count = n;
}
//This method allows the program to set count.
public void setMax(int n) {
this.max = n;
}
//This method allows the program to set max.
public void increase() {
if (this.count < this.max) {
count = this.count + 1;
}
else {
System.out.println("You cannot go beyond the max.");
}
}
//This method increases the count by 1, and stops the increasing at the upper limit (prints error).
public void increase(int n) {
if (this.count < this.max) {
count = this.count + n;
System.out.println("Now your account is worth: " +this.count);
}
else {
System.out.println("You cannot go beyond the max.");
}
}
//This method increases the count by n, prints, and stops the increasing at the upper limit (prints error).
public void decrease() {
if (this.count > 0) {
count = this.count - 1;
}
else {
System.out.println("You cannot overdraw.");
}
}
//This method decreases the count by 1, and stops the increasing at the upper limit (prints error).
public void decrease(int n) {
if (this.count > 0) {
count = this.count - n;
System.out.println("Now your account is worth: " +this.count);
}
else {
System.out.println("You cannot overdraw.");
}
}
//This method increases the count by n, prints, and stops the increasing at the upper limit (prints error).
public void reset() {
this.count = 0;
System.out.println("Counter reset! Your balance is " + this.count);
}
//This method resets the count to 0, and rints it out.
public String toString(){
return "Your current count: " + this.count + "\n" + "Your max: " + this.max;
}
//This method prints out the created object, converting it into a string.
}
public class CounterTest {
public static void main(String[] args) {
int n = 5;
System.out.println("Counter test: ");
Counter c = new Counter();
//System.out.println(c);
c.setCount(0);
c.setMax(10);
System.out.println(c);
for(int i=0; i<11; i++){
System.out.print("Your account is worth: " + i);
System.out.print("\n");
c.increase();
}
//c.increase();
for(int i=10; i>0; i--){
System.out.print("Your account is worth: " + i);
System.out.print("\n");
c.decrease();
}
c.increase(5);
c.decrease(5);
c.reset();
}
}
/**
* When the javadoc tool is run over this code, it will bring up the
* HTML maintenance manual.
* @author LTagliaferri
* @version 1.0
*/
public class CountUpExample {
/**
* This method counts up from a specified number
* to 5. It will print its progress to the
* command line.
* @param count The number to count from.
*/
public void countForwardTo(int count) {
for (int i = count; i > 0 && i <= 5; i++)
System.out.println(i);
System.out.println("\nAll done!");
}
/**
* Main creates a new instance of my
* program and calls the program's method.
* @param args This program does not use this parameter.
*/
public static void main (String[] args) {
CountUpExample q = new CountUpExample();
q.countForwardTo(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment