JA V A程序设计实验报告
班级:1191301班
姓名:徐鑫
学号:2013213051
实验一、Java程序的编辑、编译和运行
一、实验目的:
1.掌握使用JDK开发Java程序的步骤(编辑、编译、运行)。
2.掌握Java程序的基本结构。
二、实验要求:
编写一个简单的Java应用程序,输出两句话:
“Hello, World!”
“This is the first Java program.”
三、实验内容:
1.创建目录D:\你的学号。把这个目录作为我们的工作目录。我们的Java
源程序、编译后的字节码文件和用来运行Java Applet的html文件都放在
这个目录中。
2.从ftp服务器上下载jdk并安装。
3.启动附件中记事本工具。
4.在记事本编辑如下源程序:
public class Ex1
{
public static void main( String args[] )
{
System.out.println("Hello, World!");
System.out.println("This is the first Java program.");
}
}
5.保存程序。注意,保存源程序时,程序名要与主类名一致。即要用Ex1.java
作为本程序的文件名。(记事本默认的扩展名是*.txt,要改为*.java)。把
该文件保存到第一步所建的目录中(即D:\你的学号)。
6.编译程序。启动MS_DOS窗口,键入如下命令:(假如你的学号是410808,
即你建的目录为D:\410808)
C:\WINDOWS>d:
D:\>cd 410808
D:\410808>javac Ex1.java
如果编译成功,则在D:\410808目录中生成字节码文件Ex1.class。
7.运行程序。
D:\410808>java Ex1
就可以运行程序。
注意:Ex1是上一步编译生成的Ex1.class,文件名有大小写区别。
8.观察程序输出的结果是否与实验要求相符。
9.输入和调试一下程序。
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate{
public static void main(String[] args){
System.out.println(myDate.toString());
SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss ");
System.out.println(sDateFormat.format(myDate));
}
}
实验截图如下:
实验心得:
第一次接触java,MS_DOS窗口启动了java运行,成功创建一个java类。掌握使用JDK开发Java程序的步骤(编辑、编译、运行)。因为之前做的截的图没了,之后在机房配置又出现了极大的问题,所以就没有用实验中要求的方法,直接用的eclipse,觉得还是挺神奇的。
实验二、java控制语法
一、实验目的:
1.熟练掌握if语句和switch语句。
2.学会使用合适的表达式描述问题。
3.熟练使用while、do-while、和for语句实现循环的方法。
4.掌握在程序设计中用循环的方法实现各种算法。
二、实验要求:
编写一个含有分支控制结构的Java应用程序。
三、实验内容:
3.1 分支控制结构
1.编写一个成绩转换的java程序。其对应关系如下:
分数转换后的输出结果
0~59 不及格
60~69 及格
70~79 中
80~89 良
90~100 优
其它错误
2.在记事本编辑如下源程序:
public class Ex2
{
public static void main( String args[] )
{
int score =70;
if((score>=0)&&(score<=59))
System.out.println("不及格");
else if((score>=60)&&(score<=69))
System.out.println("及格");
else if((score>=70)&&(score<=79))
System.out.println("中");
else if((score>=80)&&(score<=89))
System.out.println("良");
else if((score>=90)&&(score<=100))
System.out.println("优");
else
System.out.println("错误");
}
3.保存程序Ex2.java,编译程序,并运行程序,查看结果。
4.修改score变量的初始值(0、59、60、79、80、99、105),重新编
译运行,查看结果是否正确。
5.switch语句替代if-else语句,并重新编译运行程序,再重复第4步,
测试程序是否正确。
实验截图:
在换成0、59、60、79、80、99、105时的情况:
switch语句替代if-else语句:
源程序:
public class Ex6{
public static void main( String args[] ) {
int i;
int score[]={0,59,60,79,80,99,105};
for(i=0;i<7;i++){
if(score[i]==100)
System.out.println("优");
if(score[i]==0)
System.out.println("不及格");
if(score[i]>100||score[i]<0)
System.out.println("错误");
switch(score[i]/10){
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("不及格");
break;
case 6:
System.out.println("及格");
break;
case 7:
System.out.println("中");
break;
case 8:
System.out.println("良");
break;
case 9:
System.out.println("优");
break;
}} }
3.2循环控制结构
1.编写一个成绩统计的java程序。先在数组中定义5个学生的成绩,再求
出平均成绩,最后找出最高分。
2.在记事本编辑如下源程序:
public class Ex3
{
public static void main( String args[] )
{
int score[]={60,76,90,48,80};
int sum =0;
int max =0;
for(int i=0;i<5;i++)
{
sum+=score[i];
if(score[i]>max)
max =score[i];
}
double average =(double)sum /5;
System.out.println("Average ="+average);
System.out.println("Max ="+max);
}
}
3.保存程序Ex3.java,编译运行程序,观察程序的运行结果,体会for循
环语句的执行流程。
4.用while语句替代for语句,找出最低分,并重新编译运行程序。
5.用do-while语句替代for语句,找出最低分,并重新编译运行程序。
6.用循环语句对5个学生的成绩排序,并按从小到大的顺序输出。
程序截图:
用while语句替代for语句,找出最低分:
源程序:
public class Ex6{
public static void main( String args[] )
{
int score[]={60,76,90,48,80};
int sum =0;
int max =0;
int i=0;
while (i<5)
{
sum+=score[i];
if(score[i]>max)
max =score[i];
i++;
}
double average =(double)sum /5;
System.out.println("Average ="+average);
System.out.println("Max ="+max);}}
用do-while语句替代for语句,找出最低分:
源程序:
public class Ex6{
public static void main( String args[] )
{
int score[]={60,76,90,48,80};
int sum =0;
int max =0;
int i=0;
do
{
sum+=score[i];
if(score[i]>max)
max =score[i];
i++;
}while (i<5);
double average =(double)sum /5;
System.out.println("Average ="+average);
System.out.println("Max ="+max);}}
用循环语句对5个学生的成绩排序,并按从小到大的顺序输出:
源程序:
public class Ex6
{
public static void main( String args[] )
{
int score[]={60,76,90,48,80};
int sum =0;
int max =0;
int i=0,t;
do
{
sum+=score[i];
if(score[i]>max)
max =score[i];
i++;
}while (i<5);
for(int j=0;j<4;j++)
for(i=0;i<4-j;i++)
if(score[i]>score[i+1])
{
t=score[i];
score[i]=score[i+1];
score[i+1]=t;
}
for(i=0;i<5;i++){
System.out.println(score[i]);
}
double average =(double)sum /5;
System.out.println("Average ="+average);
System.out.println("Max ="+max);}}
实验心得:
通过这次上机实验让我像又重新复习了一遍C语言的内容似的,对于其中的控制语法if-else与switch的转换以及do-while,for,while的转换与区别都与C语言的语法很相似,所以我在学习上以及编程上都相对轻松些,但是在看题的时候可能出现了一些偏差,题中的“找出最小值”我都找成了最大值,因为都是在基础程序上改的,偷懒造成的失误。最重要的是,明白了JA V A程序中,定义方法中的局部变量时一定要赋初始值否则编译不能通过会报错;但类中的成员变量则不一定需要。
实验三、java的类
一、实验目的:
1.掌握如何定义类以及类的成员变量、类的方法。
2.掌握对象的创建、对象属性的引用和方法的调用。
3.理解成员的访问权限。
4.掌握如何定义和使用构造方法。
5.掌握this的使用。
6.掌握关键字static、final的使用方法。
7.掌握构造方法的重载。
8.掌握派生子类的方法。
9.理解关键字super的含义。
10.理解继承中属性的隐藏和方法的覆盖机制。
11.掌握instanceof运算符的用法。
12.区别重载和覆盖。
13.理解在继承关系中构造方法的调用过程
二、实验要求:
1.编写一个含有简单类定义的Java应用程序。
2. 编写一个含有多个构造方法的类的Java应用程序。
三、实验内容:
3.1 简单类
1.定义一个学生类,它包括几个属性:学号(ID)、姓名(Name)、年龄(Age)、
性别(Sex)。在main方法中创建一个学生类的对象,然后给这个对象赋
初值,最后输出该学生的属性。
2.在记事本编辑如下源程序:
class Student {
public int ID;
public String StuName;
public int Age;
public boolean Sex;
}
class Ex4 {
public static void main(String[] args) {
// TODO: Add your code here
Student stu =new Student();
stu.ID =410808;
stu.StuName ="王杰";
stu.Age =21;
stu.Sex =true;
System.out.println("学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"性别");
System.out.println(stu.ID+"\t"+stu.StuName+"\t"+stu.Age+"\t"+(stu.Sex ?"男":"女"));
}
}
3.保存程序Ex
4.java,编译程序,并运行程序,查看结果。
4.把Student类中所有的成员变量的访问权限由public分别改为private、
private protected、protected,保存并重新编译程序,看会出现什么结果。答:程序均出现错误,无法运行。
5.改写Student类,把Student类中所有的成员变量的访问权限由public改
为private,并为每个成员变量定义两个方法,(如对于StuName属性,分别定义SetName(String n)、GetName()),使之通过Student类的方法来访问其成员变量。修改main方法的内容,使修改后的程序输出相同的结果程序截图:
改写成Student类:
源程序:
class Student {
private int ID;
private String StuName;
private int Age;
private boolean Sex;
public void SetID(int num){
ID=num;
}
public int GetID(){
return ID;
}
public void SetName(String n){
StuName=n;
}
public String GetName(){
return StuName;
}
public void SetAge(int a){
Age=a;
}
public int GetAge(){
return Age;
}
public void SetSex(boolean S){
Sex=S;
}
public boolean GetSex(){
return Sex;
}
}
class Ex4 {
public static void main(String[] args) {
Student stu =new Student();
stu.SetID(410808);
stu.SetName("王杰");
stu.SetAge(21);
stu.SetSex(true);
System.out.println("学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"性别");
System.out.println(stu.GetID()+"\t"+stu.GetName()+"\t"+stu.GetAge()+"\t"+(stu.GetSex()?"男":"女"));
}
}
3.2 类的构造
1.定义一个点(Point)类,它包含横坐标x和纵坐标y两个属性,再给
Point定义两个构造方法。
2.定义一个圆(Circle)类,它包含圆心(Center)和半径(Radius)两个
属性,再给圆定义4个构造方法和一个打印圆的面积的方法。
3.在main方法中分别用4种不同的方法构造圆,并打印出圆的面积。
4.在记事本编辑如下源程序:
class Point{
private int x;
private int y;
Point(){
SetXY(0,0);
}
Point(int a,int b)
{
SetXY(a,b);
}
public int GetX() {
return x;
}
public int GetY() {
return y;
}
public void SetXY(int a, int b) {
x =a;
y =b;
}
}
class Circle{
final double PI =3.1415926;
private Point Center;
private int Radius;
Circle() {
Center =new Point();
Radius =5;
}
Circle(Point Center) {
Radius =5;
this.Center =Center;
}
Circle(int Radius) {
this.Radius =Radius;
Center =new Point();
}
Circle(Point Center, int Radius) {
this(Center);
this.Radius =Radius;
}
public void PrintArea() {
double area =PI*Radius*Radius;
System.out.println("**************************************");
System.out.println("Center =("+Center.GetX()+", "+
Center.GetY()+"), "+"Radius ="+Radius+", "+"Area ="+area);
System.out.println("**************************************");
}
}
public class Ex5 {
public static void main(String[] args) {
// TODO: Add your code here
Point center =new Point(2,2);
int radius =3;
Circle cir1 =new Circle();
cir1.PrintArea();
Circle cir2 =new Circle(center);
cir2.PrintArea();
Circle cir3 =new Circle(radius);
cir3.PrintArea();
Circle cir4 =new Circle(center,radius);
cir4.PrintArea();
}
}
5.保存程序Ex5.java,编译程序,并运行程序,查看分析结果。
6.在main方法中语句“cir1.PrintArea();”后面加入一条语句“cir1.PI =2;”,
保存程序,并重新编译,会出现什么问题?
答:程序出错,PI不能继续复制。
7.将语句“this(Center);”改为“Circle(Center);”,保存程序,并重新编译,会出现什么问题?理解this关键字的不同用法。
答:并没有定义circle()的方法
程序截图:
3.3 类的继承
1.定义一个点(Point)类,它包含横坐标x和纵坐标y两个属性,再给
Point定义两个构造方法和一个打印点坐标的方法(Show())。
2.定义一个圆(Circle)类,它继承Point类(它是一个点,圆心(Center)),
除此之外,还有属性半径(Radius),再给圆定义2个构造方法、一个
打印圆的面积的方法(PrintArea())和一个打印圆中心、半径的方法
(Show())。
3.在记事本编辑如下源程序:
class Point{
protected int x;
protected int y;
Point(){
SetXY(0,0);
}
Point(int a,int b)
{
SetXY(a,b);
}
public int GetX() {
return x;
}
public int GetY() {
return y;
}
public void SetXY(int a, int b) {
x =a;