一道简单的JAVA编程题 各位大神帮帮忙 在线采纳答案 急急急

2024-12-22 20:48:17
推荐回答(5个)
回答1:

下面写了一个简单的例子,看符合你的要求吗,这个题重点考察的是get、set方法的用法。
年月日set方法没有做范围判断! 有问题可以百度 Hi我或留言!~
public class Date {

private String yyyy; //年
private String mm; //月
private String dd; //日

public String getYyyy() {
return yyyy;
}
public void setYyyy(String yyyy) {
this.yyyy = yyyy;
}
public String getMm() {
return mm;
}
public void setMm(String mm) {
this.mm = mm;
}
public String getDd() {
return dd;
}
public void setDd(String dd) {
this.dd = dd;
}

@Override
public String toString() {
// TODO Auto-generated method stub
/*
* 通过get方法获取三个变量的值,拼接形成
* */
StringBuilder sb_ymd = new StringBuilder();
sb_ymd.append(getYyyy());
sb_ymd.append("年");
sb_ymd.append(getMm());
sb_ymd.append("月");
sb_ymd.append(getDd());
sb_ymd.append("日");

return sb_ymd.toString();
}

/**
*
* @Title: main
* @Description: 测试
* @param @param args
* @return void
* @throws
*/
public static void main(String[] args) {
Date d = new Date();
d.setYyyy("2011");
d.setMm("11");
d.setDd("11");
System.out.println(d.toString()); //输出d的tostring返回值
}

}

回答2:

import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateTest {
private int year;
private int month;
private int day;
public DateTest() {
Calendar cal=new GregorianCalendar();
this.year=cal.get(Calendar.YEAR);
this.month=cal.get(Calendar.MONTH);
this.day=cal.get(Calendar.DAY_OF_MONTH);
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return year+"年"+ month + "月"
+day + "日";
}
}
测试主方法代码:
public class test {
public static void main(String srgs[]){
DateTest dt=new DateTest();
System.out.println(dt.toString());
}

}

回答3:

/**
* 2011-11-9,上午08:33:31
*
*/

/**
* @author Baicai
*
*/
public class Date {
private String yearString;
private String monthString;
private String dayString;
/**
* @return the yearString
*/
public String getYearString() {
return yearString;
}
/**
* @param yearString the yearString to set
*/
public void setYearString(String yearString) {
this.yearString = yearString;
}
/**
* @return the monthString
*/
public String getMonthString() {
return monthString;
}
/**
* @param monthString the monthString to set
*/
public void setMonthString(String monthString) {
this.monthString = monthString;
}
/**
* @return the dayString
*/
public String getDayString() {
return dayString;
}
/**
* @param dayString the dayString to set
*/
public void setDayString(String dayString) {
this.dayString = dayString;
}

@Override
public String toString(){
return this.yearString+"年"+this.monthString+"月"+this.dayString+"日";
}
}

/**
* 2011-11-9,上午08:38:23
*
*/

/**
* @author Baicai
*
*/
public class TestDate {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date date = new Date();
date.setYearString("2011");
date.setMonthString("11");
date.setDayString("9");
System.out.println(date.toString());
}

}

回答4:

public class myData
{
private String day;
private String month;
private String year;

public String getDay()
{
return day;
}

public void setDay(String day)
{
this.day = day;
}

public String getMonth()
{
return month;
}

public void setMonth(String month)
{
this.month = month;
}

public String getYear()
{
return year;
}

public void setYear(String year)
{
this.year = year;
}
@Override
public String toString()
{
return getYear()+"年"+getMonth()+"月"+getDay()+"日";
}
public static void main(String[] args)
{
myData md = new myData();
md.setYear("2011");
md.setMonth("10");
md.setDay("1");
System.out.println(md);
}
}

回答5:

public class SDate
{
private int year;
private int month;
private int date;

.....set, get方法...

public String toString()
{
return “”+year+"年“+month+"月”+date+“日”
}

}