JAVA实验报告Y.Daniel Liang第二次实验
- 格式:doc
- 大小:165.00 KB
- 文档页数:10
实验一1.编写一个程序,在屏幕上显示如下信息:**************************welcome<你的名字>**************************想一想:怎样让用户在运行程序的时候指定“你的名字”public class Experiment1_1{public static void main(String[]args){System.out.println("*************************");System.out.println("welcome宁");System.out.println("*************************");}}2.写一个Java程序打印出下列信息:☐姓名☐性别☐年龄☐学号☐系和专业☐兴趣爱好public class Experiment1_2{public static void main(String args[]){System.out.println("姓名");System.out.println("性别");System.out.println("年龄");System.out.println("学号");System.out.println("系和专业");System.out.println("兴趣爱好");}}3.编写一个程序,使用while循环计算1~1000之间能被3和7同时整除的整数之和public class Experiment1_3{public static void main(String[]args){int i=1;int sum=0;while(i<=1000){if(i%7==0&&i%3==0)sum+=i;i++;}System.out.print("sum="+sum);}}实验二1.有一函数,编写一个程序,从键盘输入一个x 值,程序输出y 的值import java.util.Scanner;public class Experiment2_1{public static void main(String[]args){Scanner reader=new Scanner(System.in);int x=reader.nextInt();if(x<0)System.out.println(-1+2*x);else if(x==0)System.out.println(-1);elseSystem.out.println(-1+3*x);}}2.编写一个程序,使用for 循环计算8+88+888+8888+…的前十项之和要求:在上述程序中设置断点和观察点进行单步调试public class Experiment2_2{public static void main(String[]args){int sum=0;int temp=8;for(int i=1;i<=10;i++){sum+=temp;temp=temp*10+8;}System.out.println("sum="+sum);}}3.利用for 循环打印9*9表1*1=11*2=22*2=41*3=32*3=63*3=91*4=42*4=83*4=124*4=161*5=52*5=103*5=154*5=205*5=251*6=62*6=123*6=184*6=245*6=306*6=36⎪⎩⎪⎨⎧>+-=-<+-=03101021x x x x x y1*7=72*7=143*7=214*7=285*7=356*7=427*7=491*8=82*8=163*8=244*8=325*8=406*8=487*8=568*8=641*9=92*9=183*9=274*9=365*9=456*9=547*9=638*9=729*9=81要求:对上述程序中的方法和语句加上注释public class Experiment2_3{public static void main(String[]args){for(int i=1;i<10;i++){for(int j=1;j<=i;j++)System.out.printf("%4d*%d=%d",i,j,j*i);System.out.print('\n');}}}4.从键盘输入一个百分制的成绩,输出相应的等级。
实验报告课程:Java 编程技术班级:网络1203班学号:姓名:实验4 面向对象编程一、实验目的通过编程和上机实验理解Java 语言是如何体现面向对象编程基本思想,了解类的封装方法,以及如何创建类和对象,了解成员变量和成员方法的特性,掌握OOP 方式进行程序设计的方法,了解类的继承性和多态性的作用。
二、实验内容1.创建一个名为Dog的类,它具有重载的bark()方法。
bark()方法应根据不同的基本数据类型的参数来进行重载,bark()方法可根据参数输出狗吠(barking)、咆哮(howling)等信息。
编写main()方法来调用不同的bark()方法。
源代码如下:public class Dog {void f(int m){System.out.println("barking!");}void f(double n){System.out.println("hoeling!");}public static void main(String[] args){Dog d=new Dog();d.f(2);d.f(2.2);}}运行界面如下:2.创建Rodent(啮齿动物)类,其子类有Mouse(老鼠)、Mole(鼹鼠)、Hamster(大颊鼠)。
在父类中,提供对所有的Rodent通用的方法。
在子类中,根据该子类特定的行为习性来覆盖这些方法。
例如老鼠属杂食(omnivorous),鼹鼠主食昆虫(insect),大颊鼠主食植物种子(plant seed)。
创建一个Rodent 数组,填充不同的数据类型,然后调用父类的方法,观察会发生什么情况。
源代码如下:public class Rodent {void eat(){System.out.println("zhushi");}public static void main(String[] args){Rodent r[]=new Rodent[4];Rodent rodent=new Rodent();Mouse mouse=new Mouse();Mole mole=new Mole();Hamster hamster=new Hamster();r[0]=rodent;r[1]=mouse;r[2]=mole;r[3]=hamster;r[0].eat();r[1].eat();r[2].eat();r[3].eat();}}class Mouse extends Rodent{void eat(){System.out.println("omniovrous!");}}class Mole extends Rodent{void eat(){System.out.println("insect!");}}class Hamster extends Rodent{void eat(){System.out.println("plant seed!");}}运行界面如下:3.3.修改上述第9题中的Rodent类,使其成为一个抽象类。
中国矿业大学计算机学院实验报告课程名称 java实验实验名称实验二面向对象编程班级_____ 姓名_____学号___ 实验日期_2013_-5-27____实验报告要求:1.实验目的 2.实验内容 3.实验步骤4.运行结果5.流程图6.实验体会一、实验代码及演示结果1. 编一程序,求两个正整数m、n的最大公约数。
要求程序中有两个方法,分别使用循环和递归,最后在主方法中两次求解并输出最大公约数。
package com;import java.util.Scanner;public class ex21{public static int num1,num2,temp1,temp2;public static void main(String args[]){System.out.print("请输入两个数字:");Scanner in=new Scanner(System.in);num1=in.nextInt();num2=in.nextInt();if(num1>num2){temp1=solution1(num1,num2);temp2=solution2(num1,num2);}else{temp1=solution1(num2,num1);temp2=solution2(num2,num1);}System.out.println("循环法得到"+num1+"和"+num2+"的最大公约数为:"+temp1);System.out.println("递归法得到"+num1+"和"+num2+"的最大公约数为:"+temp2);}private static int solution1(int x,int y){int r;while(y!=0){r=x%y;x=y;y=r;}return x;}private static int solution2(int m,int n){if(n==0){return m;}else{return solution2(n,m%n);}}}结果演示:2.使用类编写程序(在程序中定义类,然后在主方法中创建类的对象,并调用该类中的方法,观察所得结果。
姓名学号:班级:说明:实验2(类的继承,与接口的实现)1. 编一程序,求两个正整数m、n的最大公约数。
要求程序中有两个方法,分别使用循环和递归,最后在主方法中两次求解并输出最大公约数。
截图:代码:public class gcd {public static void main(String[] args) {int a = 6, b = 9;System.out.printf("two nums:%d %d\n", a, b);System.out.println("递归: " + gcd1(a, b));System.out.println("辗转相除: " + gcd2(a, b));}public static int gcd1(int m, int n){return m % n == 0 ? n : gcd1(n, m%n);}public static int gcd2(int m, int n){while(n != 0){int t = m;m = n;n = t % n;}return m;}}2. 使用类编写程序(在程序中定义类,然后在主方法中创建类的对象,并调用该类中的方法,观察所得结果。
)截图:代码:public class Date {public static void main(String[] args) {Date d = new Date();System.out.println(d);d.setYear(2011);d.setMonth(10);d.setDay(3);System.out.println(d);}public Date(){}public Date(int y, int m, int d){year = y; month = m; day = d;}public void setYear(int new_year){ this.year = new_year;} public void setMonth(int m){ this.month = m; }public void setDay(int d){ this.day = d; }public String toString(){return new String("" + year + "/" + month + "/" + day);}public int year = 2000, month = 1, day = 1;}3. 编写一个包含圆类的程序,并为圆类设计几个构造方法,编译并运行它。
Java实验二实验二类与对象实验目的1、掌握面向对象程序设计的方法和Java作为面向对象程序设计语言的特点;2、掌握修饰符和构造方法的使用规则;3、掌握接口的特点、结构、调用和继承;4、掌握如何创建包,通过包如何管理类;5、掌握Java的继承机制和实现多态的方法实验内容(1)定义一个类Student,属性为学号、姓名和成绩;方法为增加记录SetRecord和得到记录GetRecord。
SetRecord赋值学号、姓名和成绩,GetRecord通过学号得到考生的成绩。
通过实例验证编程无误。
(2)定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea(),然后设计Circle和Rectan两个类实现这个接口的方法calculateArea(),分别计算圆和矩形的面积。
通过实例验证编程无误。
(3)假定根据学生的3门学位课程的分数决定其是否可以拿到学位,对于本科生,如果3门课程的平均分数超过60分即表示通过,而对于研究生,则需要平均分超过80分才能够通过。
根据上述要求,请完成以下Java类的设计:(i)设计一个基类Student描述学生的共同特征。
(ii)设计一个描述本科生的类Undergraduate,该类继承并扩展Student类。
(iii)设计一个描述研究生的类Graduate,该类继承并扩展Student类。
(iv)设计一个测试类StudentDemo,分别创建本科生和研究生这两个类的对象,并输出相关信息附实验一名称是Java语言基础实验报告分为以下几个部分一实验名称二实验目的三实验仪器四实验步骤(把你的操作一步一步写清楚,java 程序代码要写)五实验结果(程序运行后的结果就是DOS环境下运行出来的结果写在实验报告上)六实验讨论(实验过程中的错误及如何改正,你的心得体会等)答案:(1)定义一个类Student,属性为学号、姓名和成绩;方法为增加记录SetRecord和得到记录GetRecord。
Java Object-Oriented Programming Experimental ReportStudent ID Name ClassLocation Teacher Time1.Experimental purposes and requirements1.1 Print and println, String literals, String concatenation, Escape sequences1.2 Variables, Constants, Assignment, Integers and Floating point, Arithmetic Expressions, Operator Precedence, Input using the Scanner class1.3 HTML, Applets, Graphics, Colors2.Experimental equipment (environment) and requirements2.1 Eclipse2.2 Window XPb Exercises(Please see Lab Manual)3.1 Names and Places(Choose to do)3.1.13.1.23.1.33.2 Table of Student Grades(Choose to do)/\\\\\\\\\\\\\\\\\\\\\\\ (1)(2)(3)3.3 Two Meanings of Plus(1)(2)(3)解释语句一:第一个输出结果是85,没加括号从左到右运算前第一句为字符串则8和5为字符串,有85(4)解释语句二:第二个输出结果为13,在输出语句中默认优先进行带括号的运算,8和5为整型,变成数字之后在从左到右运算,左面第一个为字符串则运算完的13也为字符型(5)解释语句三:无括号,从左到右运算,其中第三句为字符串则从左到右全识别为字符型则853.4 Pre-lab Exercises(1)常量是静态变量,不会更改,全程序共用同一个常量名变量可以在不同类中同时存在不属于本类的变量名,值可以不同(2)第一句:定义变量x第二句:定义整型变量x值为3第三句:啥都不是(3)(4)A 73B -14C 0.3D 3.333E 1.57F 4.03G 0.248H 4.29I 0.31J 1K 1(5)修改语法错误(已修改完)import java.util.Scanner;public class errors{public static void main (String[] args){String Name; // Name of the userint number;int numSq;Scanner scan = new Scanner(System.in);System.out.print ("Enter your name, please: ");Name = scan.nextString();System.out.print ("What is your favorite number?“);number = scan.nextInt();numSq = number * number;System.out.println (Name +", the square of your number is "+numSquared);}}3.5 Area and Circumference of a Circle(1)(2)(3)3.6 Painting a Room(Choose to do) (1)(2)3.7 Ideal Weight(1)(2)3.8 Lab Grades(Choose to do)3.9 Base Conversion(Choose to do)3.10 Introduction to HTML(Choose to do) 3.11 Drawing Shapes(2)x,y修改为0(3)高和宽改为200 300(4)400 40 50 200(4)矩形打印(5)椭圆3.12 The Java Coordinate System(Choose to do)3.13 Drawing a Face(Choose to do)3.14 Creating a Pie Chart(Choose to do)3.15 Colors in Java(Choose to do)4.Experimental results and data processing5.Analysis and discussionScore: 6.Teacher ReviewsExperimental Report List1 Names and Places1) Source code list3.1package test_java_01;//*************************************************************** //Names.java////Prints a list of student names with their hometowns//and intended major//*************************************************************** public class Test_java_01_1{// ------------------------// main prints the list// ------------------------public static void main (String[] args){System.out.println ();System.out.println ("\tName\t\tHometown");System.out.println ("\t====\t\t========");System.out.println ("\tprince\t\tblack_dragon_river_province"); System.out.println ("\tcircle\t\tWashington");System.out.println ();}}package test_java_01;//*************************************************************** //Names.java////Prints a list of student names with their hometowns//and intended major//*************************************************************** public class Test_java_01_1{// ------------------------// main prints the list// ------------------------public static void main (String[] args){System.out.println ();System.out.println ("\tName\t\tHometown\t\tmajor\t");System.out.println ("\t====\t\t========\t\t==========="); System.out.println ("\tprince\t\tblack_drass\t\tcomputer");System.out.println ("\tcircle\t\tWashington\t\tghhhh");System.out.println ();}}3.2public class Test_java_01_1{// ------------------------// main prints the list// ------------------------public static void main (String[] args){System.out.println("\t//////////////////////\t"+"\t\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\ \t");System.out.println ("\t======\t\tStudent Points\t\t=====\t ");System.out.println ("\tName\t\tLab\t\tBonus\t\tTotal ");System.out.println ("\t----\t\t---\t\t-----\t\t----- ");System.out.println ("\tJoe\t\t43\t\t7\t\t50\t ");System.out.println ("\tWiam\t\t50\t\t8\t\t58 ");System.out.println ("\tMaue\t\t3\t\t1\t\t4 ");System.out.println ("\tMarSue\t\t9\t\t10\t\t9 ");System.out.println ("\tMSue\t\t39\t\t10\t\t49 ");}}3.3(3)package test_java_01;import java.util.Scanner;public class PlusTest {public static void main (String[] args){int vail1,vail2,vail3;double average;Scanner scan=new Scanner(System.in);vail1=scan.nextInt();vail2=scan.nextInt();vail3=scan.nextInt();average=((double)vail1+(double)vail2+(double)vail3)/3;System.out.println("Please enter three integers and " +"I will compute their average :"+average);}}3.43.5(1)package test_java_01;public class sdd_3_5 {public static void main(String[] args){final double PI = 3.14159;int radius = 10;double area = PI * radius * radius;double line = PI * 2 * radius;System.out.println("The area of a circle with radius " + radius + " is " + area);radius = 20;area = PI * radius * radius;area = PI * radius * radius;System.out.println("The area of a circle with radius " + radius + " is " + area);}}(2)package test_java_01;public class sdd_3_5 {public static void main(String[] args){final double PI = 3.14159;int radius = 10;double area = PI * radius * radius,area2=4*area,area3;double line = PI * 2 * radius,line2=2*line,line3;double a,a1;a=line2-line;a1=area2/area;System.out.println("The area of a circle with radius " + radius + " is " + area);System.out.println("The line of a circle with radius " + radius + " is " + line);System.out.println(a+" "+a1);radius = 20;line3 = PI * 2 * radius;area3 = PI * radius * radius;System.out.println("The line of a circle with radius " + radius + " is " + line3);System.out.println("The area of a circle with radius " + radius + " is " + area3);}}(3)package test_java_01;import java.util.Scanner;public class sdd_3_5 {public static void main(String[] args){final double PI = 3.14159;Scanner scan=new Scanner(System.in);System.out.println("put in two number:");int a=scan.nextInt(),b=scan.nextInt();int c=a*b;System.out.println("number :"+c);}}3.6(1)package test_java_01;import java.util.Scanner;public class Area_jisuan_2 {public static void main(String[] args) {final int COVERAGE = 350;Scanner scan = new Scanner(System.in);System.out.print("put in length width and height:");int length = scan.nextInt(),width = scan.nextInt(),height =scan.nextInt();double totalSqFt,paintNeeded;paintNeeded = height*width*2 + width*length*2 + length*height*2;totalSqFt = paintNeeded/COVERAGE;System.out.print("the paintNeeded:"+ paintNeeded+" the totalSqFt:" +totalSqFt);}}(2)package test_java_01;import java.util.Scanner;public class Area_jisuan_2 {public static void main(String[] args) {final int COVERAGE = 350,doorsarea = 20,windowarea = 15 ;Scanner scan = new Scanner(System.in);System.out.println("put in length width and height:");int length = scan.nextInt(),width = scan.nextInt(),height =scan.nextInt();int doors,window;System.out.println("windows number and doors number:");window=scan.nextInt();doors=scan.nextInt();char i;double totalSqFt,paintNeeded;paintNeeded = height*width*2 + width*length*2 + length*height*2 - doors*doorsarea - windowarea*window;totalSqFt = paintNeeded/COVERAGE;System.out.print("the paintNeeded:"+ paintNeeded+" the totalSqFt:" +totalSqFt);}}3.7package test_java_01;import java.util.Scanner;public class weight {public static void main(String[] args) {int male1 = 14, female1 = 5;int weightreall,i=0,l;System.out.print("put in male or female:");Scanner scan = new Scanner(System.in);String c=scan.next();String b=new String("male");System.out.print("put in your height:");double height=scan.nextDouble(),ol,k;ol=height%1;ol=(2*ol*100+1)/20;if(b.equals(c)){weightreall = male1*(int)ol + 100;}else{weightreall = female1*(int)ol + 100;}System.out.print(weightreall);}}(2)package test_java_01;import java.util.Scanner;public class weight {public static void main(String[] args) {int male1 = 14, female1 = 5;int weightreall,i=0,l,weightreallf;System.out.print("put in male or female:");Scanner scan = new Scanner(System.in);String c=scan.next();String b=new String("male");System.out.println("put in your height:");double height=scan.nextDouble(),ol,k;ol=height%1;ol=(2*ol*100+1)/20;weightreall = male1*(int)ol + 100;double wei=weightreall,min,max;min=wei*0.85;max=wei*1.15;System.out.println("最佳体重范围男"+min+"-----"+max);weightreallf =female1*(int)ol +100;wei = weightreallf;min=wei*0.85;max=wei*1.15;System.out.println("最佳体重范围男"+min+"-----"+max);if(b.equals(c)){System.out.print("你的体重"+weightreall);}else{System.out.print("你的体重"+weightreallf);}}}3.8(1)四矩形打印//********************************************//Hello.java////Print a Hello, World message.//********************************************package test_java_01;import javax.swing.JApplet;import java.awt.*;public class hello extends JApplet{public void paint (Graphics page){final int MAX_SIZE = 300;final int PAGE_WIDTH = 600;final int PAGE_HEIGHT = 400;int x, y;int width, height;setBackground (Color.yellow);page.setColor (Color.blue);x = 400;y = 400;width = 150;height = 150;// Draw the rectanglepage.fillRect(x, y, width, height);page.setColor (Color.cyan);page.fillRect(x, y, width-50, height-50);page.setColor (Color.green);page.fillRect(x, y-40, width-100, height+50);page.fillRect(x-200, y-200, width-100, height+50);}}(2)来两个椭圆//********************************************//Hello.java////Print a Hello, World message.//******************************************** package test_java_01;import javax.swing.JApplet;import java.awt.*;public class hello extends JApplet{public void paint (Graphics page){final int MAX_SIZE = 300;final int PAGE_WIDTH = 600;final int PAGE_HEIGHT = 400;int x, y;int width, height;setBackground (Color.yellow);page.setColor (Color.blue);x = 400;y = 400;width = 150;height = 150;// Draw the rectanglepage.fillOval(x, y, width, height);page.setColor (Color.cyan);page.fillRect(x, y, width-50, height-50);page.setColor (Color.green);page.fillRect(x, y-40, width-100, height+50);page.fillOval(x-200, y-200, width-10, height+50);}}2) Results data processing3)Answer questions4)Error Analysis and Discussion。
§2.2 Java程序设计结构§2.1.1实验目的、内容及性质1、掌握Java语言基础知识如变量、编程基本结构。
2、数组的使用实验性质:验证、必做实验学时:4学时§2.2.2问题及思考1、字符串和字符如何区别?常见的转义字符有那些?2、变量按作用域可以分为那几类?3、说明java 语言中的数据类型以及基本数据类型的默认值。
类型转换规则。
4、数组如何定义,如何初始化?基本数组的一些算法?如查找、排序等§2.2.3实验指导1、选择结构示例(1)if语句(2)switch结构2、循环结构示例(1)for语句(2)while语句3、数组示例§2.2.4实践编程编写以下程序1、显示星期几对应的英文字符串。
用week表示星期几,可用switch语句将week转换成对应英文字符串,week为整型数据由用户输入。
用户输入参考:import java.util.Scanner;public class StandardIO{public static void main(String[] args) {Scanner scan=new Scanner(System.in);String s=scan.next(); //输入一个字符串int x=scan.nextInt();//输入一个整形数字double d=scan.nextDouble();//输入一个double}}程序:import java.util.Scanner;public class Word1 {public static void main(String[] args) {Scanner scan =new Scanner(System.in);System.out.println("请输入一个数字:");int week=scan.nextInt();//输入一个整形数字switch (week){case 1: System.out.println("Monday");break;case 2: System.out.println("Tuesday");break;case 3: System.out.println("Wedensday");break;case 4: System.out.println("Thurday");break;case 5: System.out.println("Friday");break;case 6: System.out.println("Saturday");break;case 7: System.out.println("Sunday");break;}scan.close();}截图:2、计算输出Fibonacci前20序列。
实验二熟悉Applet,GUI编程实验目的:本实验旨在巩固同学们对上课所讲Applet,异常处理,java的控制加深理解,图形用户界面基本组件窗口、按钮、文本框、选择框、滚动条等的使用方法,对java的语法和编程有一个更加深入的理解,为同学们以后的能够用Java进行独立地编写桌面程序打下一定的基础。
了解如何使用布局管理器对组件进行管理,以及如何使用Java的事件处理机制。
实验内容:创建一个java项目,命名为experiment2。
(如何创建项目参考《实验一》的项目创建方法。
)(一)创建图形用户界面图形用户界面(Graphic User Interface ,简称GUI)是为方便用户使用设计的窗口界面,在图形用户界面中用户可以看到什么就操作什么,取代了在字符方式下知道是什么后才能操作什么的方式。
组件(Component)是构成GUI的基本要素,通过对不同事件的响应来完成和用户的交互或组件之间的交互。
组件一般作为一个对象放置在容器(Container)内,容器是能容纳和排列组件的对象,如Applet、Panel(面板)、Frame(窗口)等。
通过容器的add方法把组件加入到容器中。
1.在Applet中添加标签、按钮并使用网格布局(1)程序功能在Applet 容器中添加组件标签、按钮,并使用网格布局管理器排列组件在容器中的位置。
(2)编写LX6_1.java 程序文件,源代码如下:import java.awt.*;import java.applet.Applet;public class LX6_1 extends Applet{Label l1;Button b1, b2, b3, b4, b5, b6;public void init(){setLayout(new GridLayout(3,3)); // 设置网格布局(3 行3 列共9 个网格)l1=new Label("标签1");b1 = new Button("按钮1");b2 = new Button("按钮2");b3 = new Button("按钮3");b4 = new Button("按钮4");add(l1);add(b1);add(b2);add(b3);add(new Label());add(b4);add(new Button("按钮5"));add( new Button("按钮6"));add(new Label("标签2"));}}(3)编译程序LX6_1.java。
2014-2015学年第一学期实验报告
课程名称:Java SE 平台技术
实验名称:JA VA编程(基础练习)姓名:
学号:
成绩:
指导教师:
日期:
目录
一、实验目的 (3)
二、实验内容 (3)
2.1 构建一个继承类。
(3)
2.2 抽象类和接口的使用 (3)
2.3 图像类编程和事件驱动程序。
(3)
三、实验环境 (3)
四、实验结果 (3)
4.1构建继承类 (3)
4.2抽象类和接口的使用 (3)
4.3 事件驱动程序 (3)
五、附录 (4)
5.2 设计方案 (4)
5.1构建继承类 (4)
5.2抽象类和接口的使用 (4)
5.3 事件驱动程序 (4)
5.2 设计方案 (4)
5.1构建继承类 (4)
5.2抽象类和接口的使用 (4)
5.3 事件驱动程序 (4)
5.3 算法 (4)
5.3.1 三角形继承类 (4)
5.3.2 抽象类与接口 (5)
5.3.3 画出小车 (5)
5.4. 设计图 (6)
5.4.1 三角形类UML设计图 (6)
5.4.2.1 Colorable接口UML设计图 (7)
5.4.2.2 CompareTo与Clone接口UML设计图 (8)
5.4.3 小车UML设计图 (9)
5.5 仿真结果 (9)
5.5.1构建继承类 (9)
5.5.2抽象类和接口的使用 (9)
5.5.3 事件驱动程序 (10)
5.6 调试心得 (10)
5.6.1 错误分析 (10)
5.6.2 心得与收获 (10)
一、实验目的
通过使用JA V A语言进行基本程序的开发,掌握JA V A通用IDE,练习类的封装使用、JA V A基本类库的使用、利用UML进行简单建模。
二、实验内容
2.1 构建一个继承类。
2.2 抽象类和接口的使用
2.3 图像类编程和事件驱动程序。
三、实验环境
Eclipse、Win 8.1
四、实验结果
4.1构建继承类
输出了三角形的三边长,面积,周长。
4.2抽象类和接口的使用
4.2.1执行Colorable接口,输出了一条how to color的信息
4.2.2执行Comparable 和Cloneable接口,输出了创建的五边形和克隆五边形的周
长和面机的信息。
4.3 事件驱动程序
输入不同的数值,四个小车的速度不一样。
五、附录
5.2 设计方案
5.1构建继承类
构建三角形类输出周长面积,只需要在该类中增加周长函数和面机函数。
5.2抽象类和接口的使用
4.2.1创建Square类执行Colorable接口覆盖howToColor函数。
4.2.2Comparable 和Cloneable接口本身就有,可以直接用,但是本程序自己覆盖
了一下。
5.3 事件驱动程序
改程序涉及到较为深入的GUI编程,小车的绘制,坐标的选择难度较大,但可以参考课后习题,根据比例计算坐标。
5.2 设计方案
5.1构建继承类
先写好基类,三角形类中定义好构造函数,周长和面积函数即可。
5.2抽象类和接口的使用
4.2.1执行Colorable接口覆盖howToColor函数,在主程序中创建五个Square测
试。
4.2.2覆盖了Comparable 和Cloneable接口,创建了一个八边型,另一个通过克隆
创建,用CompareTo比较两函数。
5.3 事件驱动程序
创建Car类存坐标,速度,MFrame类画出文本框,得到键盘读入信息,以及设定基本布局。
5.3 算法
5.3.1 三角形继承类
public double getArea(){
double m = (side1+side2+side3)/2;
double s = Math.sqrt(m*(m-side1) * (m-side2) * (m-side3) );
return s;
}
通过海伦公式得到面积函数。
5.3.2 抽象类与接口
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
public int compareTo(Object o) {
if (this.getArea() == ((Octagon)o).getArea() )
return 1;
else
return 0;
}
重载Colorable接口和clone接口。
5.3.3 画出小车
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
xCoordinate += speed;
if (xCoordinate > width)
xCoordinate = 0;// 如果超出宽度则从头开始
// 画小车
g.setColor(Color.BLACK); // 画轮子
g.fillOval(xCoordinate + 12, yCoordinate - 12, 12, 12);
g.fillOval(xCoordinate + 36, yCoordinate - 12, 12, 12);
g.setColor(Color.GREEN); // 画车身
g.fillRect(xCoordinate, yCoordinate - 24, 60, 12);
g.setColor(Color.RED); // 画车顶
int[] xPoints = new int[4];
int[] yPoints = { yCoordinate - 24, yCoordinate - 36,
yCoordinate - 36, yCoordinate - 24 };
for (int i = 1; i <= 4; i++)
xPoints[i - 1] = xCoordinate + i * 12;
g.fillPolygon(xPoints, yPoints, 4);
// 画线
g.setColor(Color.black);
g.drawLine(0, yCoordinate, width, yCoordinate);
}
5.4. 设计图
5.4.1 三角形类UML设计图
5.4.2.1 Colorable接口UML设计图
5.4.2.2 CompareTo与Clone接口UML设计图
5.4.3 小车UML设计图
5.5 仿真结果
5.5.1构建继承类
5.5.2抽象类和接口的使用
5.2.2.1运行自定义howToColor函数。
5.5.2.2Comparable 和Cloneable接口
输出八边型信息并进行比较,信息相等输出相等信息。
5.5.3 事件驱动程序
本程序小车可以实现暂停,继续,重置等操作,由于效果不好展示所以此处没放截图。
5.6 调试心得
5.6.1 错误分析
使用布局管理器时,因为BorderLayout默认CENTER模块最大,所以排版的时候若把开始、重置、暂停、继续按钮放在第二排会出现排版问题。
适用布局管理器不够熟练不能了解其精髓。
5.6.2 心得与收获
经过这次练习,深入掌握了Java语言的使用,也了解了它的特点,这次实验总体算法不难,主要是练习了GUI的使用,抽象类和接口等。
但是其中还会有些细节问题需要注意,相信随着学习的深入,我们会更加深刻的了解了Java的特色,更熟练的使用。