南京信息工程大学 实验(实习)报告
实验课程 java程序设计 实验名称 第三次实验 实验日期 2016-4-11 指导老师
专业 年级 姓名 学号 得分
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
实验目的:对java的类的熟悉与练习。
实验内容:
定义一个类,它包含了一个 int 类型的变量 x、若干个构造函数(根据用户自己的需要)
和一个输出方法 show( )。编程:从键盘输入一个两位以上的数,将这个数传递给这个类的 变量x,采用方法 show( )逆序输出这个数。
程序如下:
package reversenum;
/**
*
* @author Administrator
*/
public class Reversenum {
private int x;
Reversenum (int x){
this.x = x;
}
public void show() {
String str;
str = Integer.toString(x);
char array[] = new char[str.length()];
array = str.toCharArray();
for(int i = str.length()-1; i >= 0;i--)
System.out.print(array[i]);
}
public static void main(String[] args) {
Reversenum temp = new Reversenum(325425);
temp.show();
}
}运行结果如下:
run:
524523成功构建 (总时间: 0 秒)
2、定义一个复数类complex,它的内部具有两个实例变量:realPart 和 imagPart,分别代表复数的实部和虚部,编程实现要求的数学运算。
(1)实现两个复数相加。复数加运算的原则是:复数的实部和虚部分别相加。
(2)实现两个复数相减。复数减运算的原则是:复数的实部和虚部分别相减。
(3)输出运算结果,判断是否正确。
程序如下:
package complex;
/**
*
* @author Administrator
*/
public class Complex
{
private double realPart;
private double imagePart;
public Complex()
{
this.realPart = 0.0;
this.imagePart = 0.0;
}
public Complex(double real, double image)
{
this.realPart = real;
this.imagePart = image;
}
public void show()
{
if ((this.realPart == 0.0) && (this.imagePart == 0.0))
System.out.println("0");
else if (this.realPart == 0.0)
System.out.println(this.imagePart + "i");
else if (this.imagePart == 0.0)
System.out.println(this.realPart);
else
System.out.println(this.realPart + "+" + this.imagePart + "i");
}
public void add(Complex x, Complex y) {
this.realPart = x.realPart + y.realPart;
this.imagePart = x.imagePart + y.imagePart;
}
public void sub(Complex x, Complex y) {
this.realPart = x.realPart - y.realPart;
this.imagePart = x.imagePart - y.imagePart;
}
public static void main(String arg[]) {
Complex c1 = new Complex(1, 2);
Complex c2 = new Complex(2, 2);
Complex c3 = new Complex();
c1.show();
c2.show();
c3.add(c1, c2);
System.out.print("add : ");
c3.show();
c3.sub(c1, c2);
System.out.print("sub : ");
c3.show();
}
}
运行结果:
1.0+2.0i
2.0+2.0i
add : 3.0+4.0i
sub : -1.0
成功构建 (总时间: 0 秒)
3、定义一个圆类 circle,它有一个变量 radius(半径)。从键盘输出数据,通过构造函数的参数传递给 radius,编程计算并输出圆的周长和面积。但是,你必须确保输入的数据不为负数。
程序如下:
package Circle;
import java.awt.*; import java.applet.*;
public class Circle extends Applet{
TextField banjing;
int r=0;
round Circle;
public void init(){
Label prompt;
Circle=new round();
prompt = new Label("半径:");
banjing=new TextField(20);
add( prompt );
add( banjing );
}
public void paint(Graphics g){
Circle.setRound(r);
g.drawString("面积:"+Circle.area() ,50,80);
g.drawString("周长:"+Circle.lengthCircle() ,50,100);
}
public boolean action(Event e,Object o)
{
if (e.target == banjing)
{
r = Integer.parseInt( banjing.getText() );
if(r<0)
{
showStatus("半径不能为负数!请重新输入数据。");
return false;
}
repaint();
}
return true;
}
}
class round{
final float PI=3.14f;
int r;
void setRound(int t){
r=t;
}
float area(){
return PI*r*r;
}
float lengthCircle(){
return 2*PI*r;
}
}
运行结果截图:
4、定义一个日期类 date,确保具有如下功能:
输出日期的格式具有如下几种:
YYYY MM DD
MM DD YYYY
DD MM YYYY
采用重载构造函数,以上述日期格式数据为参数,创建 date 类型的对象。
程序如下:
package date;
/**
*
* @author Administrator
*/
public class Date {
int YYYY;
int MM;
int DD;
public Date(int y, int m, int d)
{
this.YYYY = y;
this.MM = m;
this.DD = d;
}
public Date( )
{
this.YYYY = 0;
this.MM = 0;
this.DD = 0;
}
public boolean isValid() {
return ( this.YYYY > 0&& this.MM > 0 && this.DD > 0 && this.MM < 13 && this.DD < 32);
}
public void showYMD() {
System.out.println(this.YYYY+" "+this.MM+" "+this.DD);
}
public void showMDY() {
System.out.println(this.MM+" "+this.DD+" "+this.YYYY);
}
public void showDMY() {
System.out.println(this.DD+" "+this.MM+" "+this.YYYY);
}
public static void main(String args[]){
Date day1=new Date();
Date day2=new Date(2003,12,1);
if(day1.isValid()){
day1.showYMD();
day1.showMDY();
day1.showDMY();
}
if(day2.isValid()){
day2.showYMD();
day2.showMDY();
day2.showDMY();
}
}
}
运行结果如下:
run:
2003 12 1
12 1 2003
1 12 2003
成功构建 (总时间: 0 秒)