实验目的:1.掌握定义类、创建对象、使用类与对象。
2.掌握类及成员的修饰符的使用。
3.掌握构造函数的使用。
4.掌握如何定义方法和调用方法。
5.掌握形式参数与实际参数的结合过程。
实验步骤与内容:
1.编写一个Application程序MyDate.java,建立日期类,接受用户输入的年、月、日,完成日期增加若干天得到新的日期,日期推前若干天得到新的日期,日期与日期间相差多少天的计算。
2.编写一个彩票中奖模拟程序,实现下述功能:创建六个筛子,每个筛子可以定制有多少个面(一般6-30);投掷六个筛子,得出六个开奖数值;模拟若干参与者,进行赌采,根据开奖数值,给出猜对的成绩。
主要代码:
第一题:
import java.util.Scanner;
public class DateTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int y, m, d;
System.out.println("请输入年、月、日:");
y = scan.nextInt();
m = scan.nextInt();
d = scan.nextInt();
y = scan.nextInt();
m = scan.nextInt();
d = scan.nextInt();
Date my_date = new Date(y, m, d);
System.out.println("当前日期是:" + my_date);
System.out.println("当前日期是:" + my_date);
System.out.println("请输入要增加的天数:");
int days = scan.nextInt();
my_date.plusDay(days);
System.out.println("增加后的日期是:" + my_date);
int days = scan.nextInt();
my_date.plusDay(days);
System.out.println("增加后的日期是:" + my_date);
System.out.println("请输入要减少的天数:");
days = scan.nextInt();
my_date.minusDay(days);
System.out.println("减去后的日期是:" + my_date);
days = scan.nextInt();
my_date.minusDay(days);
System.out.println("减去后的日期是:" + my_date);
System.out.println("请输入要比较的日期的年月日:");
y = scan.nextInt();
m = scan.nextInt();
d = scan.nextInt();
Date my_date2 = new Date(y, m, d);
System.out.println("两日期相差天数是:" + my_date.differDay(my_date2));
}
}
y = scan.nextInt();
m = scan.nextInt();
d = scan.nextInt();
Date my_date2 = new Date(y, m, d);
System.out.println("两日期相差天数是:" + my_date.differDay(my_date2));
}
}
class Date {
private int year;
private int month;
private int day;
private int year;
private int month;
private int day;
public Date(int year, int month, int day){
this.year = year;
this.month = month;
this.day = day;
}
this.year = year;
this.month = month;
this.day = day;
}
public void setYear(int year) {
this.year = year;
}
public int getYear(){
return year;
}
public void setMonth(int month) {
this.month = month;
}
public int getMonth() {
return month;
}
public void setDay(int day) {
this.day = day;
}
public int getDay() {
return day;
}
this.year = year;
}
public int getYear(){
return year;
}
public void setMonth(int month) {
this.month = month;
}
public int getMonth() {
return month;
}
public void setDay(int day) {
this.day = day;
}
public int getDay() {
return day;
}
public String toString() {
return year + "年" + month + "月" + day + "日";
}
return year + "年" + month + "月" + day + "日";
}
public int dayOfMonth(int m) {
switch(m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if((year%4 == 0 && year%4 != 0) || (year%400 == 0))
return 29;
else
return 28;
}
return -1;
}
switch(m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if((year%4 == 0 && year%4 != 0) || (year%400 == 0))
return 29;
else
return 28;
}
return -1;
}
public void plusDay(int d) {
if(day+d <= dayOfMonth(month)) {
day += d;
}else {
d -= dayOfMonth(month) - day + 1;
day = 1;
if(day+d <= dayOfMonth(month)) {
day += d;
}else {
d -= dayOfMonth(month) - day + 1;
day = 1;
if(month < 12) {
month++;
}else {
year++;
month = 1;
}
plusDay(d);
}
}
month++;
}else {
year++;
month = 1;
}
plusDay(d);
}
}
public void minusDay(int d) {
if(day-d > 0) {
day -= d;
}else {
d -= day;
if(month > 1) {
month--;
}else {
month = 12;
year--;
}
day = dayOfMonth(month);
minusDay(d);
}
}
if(day-d > 0) {
day -= d;
}else {
d -= day;
if(month > 1) {
month--;
}else {
month = 12;
year--;
}
day = dayOfMonth(month);
minusDay(d);
}
}
public int dayOfDate(Date date) {
int days = 0;
int y = date.getYear();
int m = date.getMonth();
int d = date.getDay();
days = 365*(y-1)+(y-1)/4-(y-1)/100+(y-1)/400;
for(int i=1; i<m; i++)
days += dayOfMonth(m);
days += d;
return days;
}
int days = 0;
int y = date.getYear();
int m = date.getMonth();
int d = date.getDay();
days = 365*(y-1)+(y-1)/4-(y-1)/100+(y-1)/400;
for(int i=1; i<m; i++)
days += dayOfMonth(m);
days += d;
return days;
}
public int differDay(Date date) {
int days1 = dayOfDate(this);
int days2 = dayOfDate(date);
return (days1>days2) ? (days1-days2) : (days2-days1);
}
}
int days1 = dayOfDate(this);
int days2 = dayOfDate(date);
return (days1>days2) ? (days1-days2) : (days2-days1);
}
}
第二题:
import java.util.Random;
import java.lang.Math;
import java.util.Scanner;
import java.lang.Math;
import java.util.Scanner;
public class DieTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Die[] die = new Die[6];
int number;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Die[] die = new Die[6];
int number;
for(int i=0; i<6; i++) {
die[i] = new Die(6);
System.out.print("请输入你猜的数值:");
number = scan.nextInt();
die[i].guess(number);
}
}
}
die[i] = new Die(6);
System.out.print("请输入你猜的数值:");
number = scan.nextInt();
die[i].guess(number);
}
}
}
class Die {
private int face;
private int value;
private int face;
private int value;
public Die(int face) {
this.face = face;
value = (int)(Math.random() * face) + 1;
}
this.face = face;
value = (int)(Math.random() * face) + 1;
}
public int getValue() {
return value;
}
return value;
}
public void setValue(int v) {
value = v;
}
value = v;
}
public void guess(int v) {
if(v == value)
System.out.println("恭喜你,猜中了!");
else
System.out.println("很遗憾,你没有猜中");
}
}
if(v == value)
System.out.println("恭喜你,猜中了!");
else
System.out.println("很遗憾,你没有猜中");
}
}