- 1、本文档共8页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
河北工业大学java实验二
实验二 面向对象编程
【实验目的】
通过编程和上机实验理解Java语言是如何体现面向对象编程的基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和类的多态性的作用,熟悉包、接口的使用方法,掌握OPP方式进行程序设计的方法。
【实验要求】
编写程序实现类的定义和使用 。
编写不同成员和不同成员方法修饰方法的程序
编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序和多态性(成员方法重载、构造方法重载)的程序。
编写接口的定义和使用的程序
编写包的定义和使用的程序。
【实验内容】
1、类的定义和使用
import java.io.PrintStream;
public class Date
{
private int year,month,day;
public Date(int year,int month,int day)
{
this.set(year,month,day);
}
public void set(int y,int m,int d)
{
this.year = y;
this.month = ((m>=1)&&(m<=12))?m:1;
this.day = ((d>=1)&&(d<=daysOfMonth()))?d:1;
}
public static boolean isLeapYear(int year)
{
return year%400==0||year%100!=0&&year%4==0;
}
public String toString()
{
return this.day+"/"+this.month+"/"+this.year;
}
public void tomorrow()
{
this.day++;
if(day>this.daysOfMonth())
{
day = 1;
month++;
if(month>12)
{
month = 1;
year++;
}
}
}
public static int daysOfMonth(int year,int month)
{
switch(month)
{
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: return isLeapYear(year)?29:28;
default : return 0;
}
}
public int daysOfMonth()
{
return daysOfMonth(this.year,this.month);
}
public static void main(String args[])
{
Date date1 = new Date(2014,11,13);
System.out.println("今天是"+date1.toString());
date1.tomorrow();
System.out.println("明天是"+date1.toString());
}
}
(2)
public class Table
{
String name;
double weight;
double width;
double length;
double height;
public Table(){
name = "方桌1号";
weight = 200;
width = 20;
length = 30;
height = 10;
}
public double Area(){
double area;
area = width * height;
return area;
}
public void Display(){
System.out.println("桌子名称:"+n
文档评论(0)