JAVA实验报告Y.Daniel Liang第二次实验

合集下载

java实验报告_2

java实验报告_2

实验一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实验报告 2

java实验报告 2

实验报告课程: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实验二报告

java实验二报告

中国矿业大学计算机学院实验报告课程名称 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.使用类编写程序(在程序中定义类,然后在主方法中创建类的对象,并调用该类中的方法,观察所得结果。

java实验二试验报告

java实验二试验报告

姓名学号:班级:说明:实验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程序设计》实验报告 --实验2

《Java程序设计》实验报告 --实验2
2.import java.util.*;
3.class Point2D{
4.double X; double Y;
5.public Point2D()//2D
6.{
7.X=0;Y=0;}
8.Point2D(double x, double y){
9.X=x;Y=y;}
10.void offset(double a, double b){
38.Point2D[] P=new Point2D [9];
39.//double Y;
40.//Y=Math.random();System.out.println(Y);
41.for(int i=0;i<P.length;i++)
42.{P[i]=new Point2D();}
43.for(int i=0;i<P.length;i++){P[i].setPoint2D(Math.random(),Math.random());}
36.distance = Math.sqrt(Math.pow((p3d1.X-p3d2.X),2)+Math.pow((p3d1.Y-p3d2.Y),2)+Math.pow((p3d1.Z-p3d2.Z),2));
37.System.out.println("Distance Between Point3D P3P1 and P3P2 is "+ distance);
(2)Point2D有一个void型成员方法offset(int a, int b),它可以实现Point2D的平移。
(3)Point3D是Point2D的直接子类,它有有三个整型成员变量x,y,z (分别为三维空间的X,Y,Z方向坐标),Point3D有两个构造方法:Point3D(int x,int y,int z)和Point3D(Point2D p,int z),两者均可实现对Point3D的成员变量x, y,z的初始化。

java实验二附答案

java实验二附答案

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实验2实验报告

java实验2实验报告

reader=new Scanner(System.in); standard=reader.nextLine(); }while(!"no".equals(standard)); } } 四、运行结果与测试分析 1.程序分析: 首先,在 TestException 这个类中除了 main 方法和一个无参数构造函数之外,还有另外 3 个方法:testEx()、testEx1()和 testEx2()。它们的返回值都是布尔型,且都可能抛出异 常。程序从 main 方法开始执行,首先创建了一个 TestException 类的对象 testException1, 然后进入 try 语句块,用对象 testException1 调用函数 testEx,程序开始执行 testEx 函 数的函数体。在函数 testEx 的 try 块中调用了函数 testEx1,于是又转向执行 testEx1 的 函数体,但在它的 try 块中又调用了 testEx2(),于是程序转向执行 testEx2 的函数体。 在 testEx2 的 try 块中的 for 循环中,前两次循环正常执行,分别输出 i=2 和 i=1,但第 三次循环时出错(出现了除数 i 为 0 的情况),抛出异常并被本函数中的 catch 语句捕获, 输出“testEx2,catch exception”,把 ret 的值赋成 false,并抛出原异常,但在离开本 函数之前还要执行 finally 中的语句,于是输出“testEx2,finally; return value=false” 并返回 ret 的值 false,由于 finally 语句中使用了 return 语句,所以导致异常丢失,接 着程序返回 testEx2 的调用者 testEx1 中,由于 testEx2 的返回值为 false,故 testEx1 中的 ret 为 false,if 语句条件为真,返回 false,但在离开 testEx1 之前,要执行 finally 中的语句,于是输出“testEx1,finally; return value=false”并返回 ret 的值 false (由于 finally 语句中使用了 return 语句,所以 try 区域中 return 语句的返回值被覆盖)。 流程转到 testEx 中,由于 testEx1 返回 false,故 testEx 中的 ret 为 false,接着执行 finally 中的语句,输出“testEx,finally;return value=false”并返回 ret 的值 false。 接着流程返回到 main 函数中,程序正常结束。运行结果截图如下:

JAVA实验报告_实验2__

JAVA实验报告_实验2__

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。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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的特色,更熟练的使用。

相关文档
最新文档