JAVA LAB3 问题3-答案与解析

在这里插入图片描述


```java

```java

```java

```java

```java

```java
package question3;

/**
 * 
 * @author wang
 * @version 1.0
 *
 */

public class Counter
{
	private int count;
	private int max;

//question 1 create a constructor
	/**
	 * Description: this is a constructor with no parameter and it assign 0->count
	 * and 10->max
	 */
	public Counter()
	{
		this.count = 0;
		this.max = 10;
	}

//question 2 create getter and setter for count and max
	/**
	 * getter of count
	 * 
	 * @return int
	 */
	public int getCount()
	{
		return count;
	}

	/**
	 * getter of max
	 * 
	 * @return int
	 */

	public int getMax()
	{
		return max;
	}

	/**
	 * setter of cout
	 * 
	 * @param n
	 */

	public void setCount(int n)
	{
		this.count = n;
	}

	/**
	 * setter of max
	 * 
	 * @param n
	 */

	public void setMax(int n)
	{
		this.max = n;
	}
//question 3 create main method to test the things discussed above

	public static void main(String[] args)
	{
		Counter counter = new Counter();
		System.out.println(counter.getCount());
		System.out.println(counter.getMax());
		counter.setCount(5);
		counter.setMax(15);
		System.out.println(counter.getCount());
		System.out.println(counter.getMax());
		counter.increase();
		System.out.println(counter.getCount());
		System.out.println(counter.getMax());
		counter.decrease();
		System.out.println(counter.getCount());
		System.out.println(counter.getMax());
		counter.doubler();
		System.out.println(counter.getCount());
		System.out.println(counter.getMax());
		counter.reset();
		System.out.println(counter.getCount());
		System.out.println(counter.getMax());
//testing of method overloading~
		System.out.println("testing of method overloading~");
		counter.increase(50);
		counter.decrease(500);

		System.out.println(counter.getCount());
		System.out.println(counter.getMax());
		System.out.println(counter.toString());

	}

//question4  add four methods to counter class
	/**
	 * description: increase the count value by 2
	 */
	public void increase()
	{
		count = count + 2;
		if (count == this.max)
		{
			reset();
		}

	}

	/**
	 * description: decrease the count value by 1
	 */

	public void decrease()
	{

		if (count >= 1)
		{
			count--;

		} else
		{
			System.out.println("error");
		}
	}

	/**
	 * description: double the count
	 */

	public void doubler()
	{
		count = count * 2;
	}

	/**
	 * description: make the count to 0
	 */

	public void reset()
	{
		count = 0;
		System.out.println("Counter Reset");
	}

//question5 toString method--display the current value of count and max
	public String toString()
	{
		return ("count = " + count + " max = " + max);

	}

//question7 method overloading 
	/**
	 * 
	 * @param n
	 */
	public void increase(int n)
	{
		count = count + n;
	}

	/**
	 * 
	 * @param n
	 */

	public void decrease(int n)
	{
		count = count - n;
	}

}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值