关于“类、对象”的题?应该怎么做啊?
来源:百度知道 编辑:UC知道 时间:2024/07/02 10:21:02
老师要求小菜使用面向对象的思想编写一个计算器,可以实现两个数的加、减、乘、除运算。如果你是小菜,如何实施?写出你的思路。
还有一题是:“假设当前的时间是2010年8月8日22点12分35秒,编写一个Demo类,设置属性为改时间,并包括toString()方法,可以显示改时间。
小妹今天才学“对象和类”,这两道题杂个做啊?在线等啊!
还有一题是:“假设当前的时间是2010年8月8日22点12分35秒,编写一个Demo类,设置属性为改时间,并包括toString()方法,可以显示改时间。
小妹今天才学“对象和类”,这两道题杂个做啊?在线等啊!
(1)计算器:
public class Calculator {
private int a;
private int b;
public int plus() {
return a + b;
}
public int minus() {
return a - b;
}
public int multiply() {
return a * b;
}
public int divide() {
return a / b;
}
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
public void setB(int b) {
this.b = b;
}
public int getB() {
return b;
}
}
(2)Demo
package test;
import java.util.Date;
public class Demo {
private Date date;
public Demo(Date date) {
this.setDate(date);
}
public String toString() {
return date.toString();
}
@SuppressWarnings("deprecation")
public void setDate(int date) {
this.date.setD