admin管理员组

文章数量:1534205

2023年12月14日发(作者:)

东北石油大学学生实验报告

实验课程名称

指导教师

所在院(系)

班 级

_______________

王X

X

数学与统计学院

应数161班

学生姓名

学 号

XXX

161X26152X 实验一 java概述

一、 实验目的

1.

掌握如何下载Java安装软件包并了解和使用JDK环境;

2.

熟悉JDK集成环境的基本命令、功能和基本用法,并学会配置Java环境变量;

3.

简述JDK, JRE的区别并简述path和classpath环境变量的作用;

4.

学习并熟练掌握使用JDK环境进行Java程序开发的全过程(包括:源程序的编 辑、编译、调试、运行和结果查看);

二、三、实验内容

1. JDK开发包的下载与安装(JDK版本可选);

2.

根据要求写出统环境变量的配置;

3.

编写一个JAVA程序进行调试,掌握在JDK开发包支持下编译和运行的过程;

实验记录

1. JDK开发包的下载与安装

1)从Oracle官网下载安装包安装,安装路径:C:Program FilesJava

gpa SE D<*volo|^m<*r>r Kit 8 LJpdRo* 131 (G-4

g上 Java

• 〜 •

SC

9 MKMteg *31

QX

立0 M丘人EHBI. WRn皿"MIK■内毛F. OVW1

变里名on:

变星值CV):

2.

配置系统环境变量path和classpath 变里名(N):

变重值(V):

3.

编写java程序,并编译、运行: 程序如下:

public class Students{

public static void main(String[] args){

System, out. printin(,z这是我编写的第一个Java程序! “);

}

}

运行结果:

d:jaual>jauac

d:jaual>jaua Students

陆是我编写的第一个Jaua程序?实验二运算符、流程控制语句

一、 实验目的

1.

了解Java的数据类型

2.

掌握各种变量的声明方式。

3.

理解运算符的优先级。

4.

掌握Java基本数据类型、运算符与表达式。

5.

理解Java程序语法结构,掌握顺序结构、选择结构和循环结构语法的程序 设计方法。

通过以上内容,掌握Java语言的编程规则。

二、 实验内容

1.

编写一个声明Java不同数据类型变量的程序。

2.

编写一个使用运算符、表达式、变量的程序。

3.

编写表达式语句、复合语句的程序。

4.

编写使用不同选择结构的程序。

5.

编写使用不同循环结构结构的程序。

三、 实验记录

1•课堂案例1:键盘录入一个成绩,判断并输出成绩的等级:90-100优;80-89良;

70-79

中;60-69

及;0-59

程序如下:

6.

import java. util. Seanner;

class Exeimple09 { public static void main(String[] args){ Scanner sc=new Scanner(System, in);

System, out. printin (请输入学生的成绩;

int x二sc. nextlnt ();

int y=x/10;

switch(y){ case

10: case 9:

System, out. println(〃优“);

break;

case 8: System, out. printin (“

良〃); break;

case 7: System. out. println("屮〃);

break;

case 6: System. out. printing及〃); break;

defauIt:

Systcm. out. printin

(〃差〃);

break;

}

z,运行结果:

2.课堂案例2:键盘录入x的值,计算出y的并输出x>=3, y二2*x +l;-l〈x〈3, y

2 * x; x<=-l , y

2 * x - 1

程序如下:

import java. util. Scanner; class ExamplelO{

public static void main(String[] args){

Scanner sc=ncw Scanner (Systom. in); System, out. print In

(〃请输入

x

的值:〃);

int x二sc. nextlnt ();

int y=0;

if (x>3) {

y二2*x+l;

}else if (x>~l){

y二2*x;

}else {

y二2*x-l;

}

System, out. printIn

(〃y二〃+y);

}

}

运行结果:

d: jaual>jaua ExanplelS

f青输入X的融

3

3.实验题1:编程实现1! +2! +3!+・・・..+10!的结果并输出结果。 程序如下:

public class Example17{

public static void main(String[] args){ int s=0;

for (int i二1; iU10; i++) {

s二s+i*i;

System, out. println(〃s二〃+s); }

}

运行结果:

C: Useps >)ewe idon g>d:

DD: >cd D: java

□D: jaua.>javac Exanplel7. java

fD:java>java Exanplel?

4.

实验题2:输出1到100之间的所有偶数。并控制每行输出十个偶数。 程序如下:

public class Examplel8{

public static void main(String[] args){ int k=0;

for(int i=l;i<=100;i++) { if(i%2==0) {

System, out. print(i);

k++; if(k%10==0) {

Systcm. out. println();

}

}

运行结果:

tt): jaua>jaua Example 18 24681 p22426283

424446485 626466687 828486889

5•实验题3:分别利用while循环、do-whi 1 e循环、for循环编程。打印出1到

100 Z间能被9整除的所有整数。

wh订e循环程序如下:

public class Example19{

public static void main(String[] args){

int i=l;

while(i<=100) {

if(i%9==0){

System, out. printin(i);

}

i++;

运行结果:

D;java>java Exanplel? 9

18

27

45

3b

54

63

81

72

90

99

do-while循环程序如下:

public class

Example20{ public static void main(String[] args){ int

i=l;

do{

if(i%9=0) {

System, out. println(i);

}

i++;

}wh订e(i<=100);

}

运行结果: D: Xjava>jauac

Exanple2R

9

18

27

36

45

5-1

63

7Z

81

yw

99

for循环编程程序如下:

public class Example21{ public static void main(Stringd args)

{ for(int i=l;i<=100;i++) { if(i%9==0){

System. out. printin(i);

}

}

运行结果:

D:jaua>jauac

D: jaua>jaua Example21

9

18

2?

36

54

§3

72

81

90

99实验三方法数组

一、 实验目的

1.

知道怎么样声明、创建和初始化各种类型的数组

2.

理解二维数组的概念,能够声明、创建和初始化各种类型的二维数组

3.

掌握对一维或二维数组元素的访问方法

4.

掌握方法在程序中的使用方式

5.

利用方法编写程序

二、 实验内容

1.

编写程序,掌握数组的声明和访问数组元素,了解数组对象length的作用

2.

编写程序,掌握二维数组的定义和应用,会调用方法;

三、 实验记录

1、使用冒泡排序法,实现对数组{25, 24, 12, 76, 101,96, 28}的排序•并求出数组 元素的和。

程序如下:

public class Demo5{

public static void main(String[] args){

int[] arr={25, 24, 12, 76, 101,96, 28};

bubblcSort(art);

System, out. print

(〃排序后而结果:〃);

printArray(arr);

int sum=0;

for (int i=0, i

sum+= [i];

} _

System, out. printin (z,数组元素的和二〃+sum);

}

public static void bubbleSort(int[] arr){

for (int i=0;i〈arr・ lengthT;i++) {

for (int j=0; j

{ if(arr[j]>arr[j+1]) { int temp=arr[j];

a[j]=arr[j+l];

arr[j+1]二temp;

public static void bubbleSort(int[] arr){ for (int

i=0;i

[i]+“);

z,}

System・ out. println();

}

}运行结果: d: java>jauac d:Xjava>jaua DemoE

排序后结果

12 24 25 28 76 96 101

数组元素的和£62

:2、编写一个程序,利用整型数组存储星期,利用字符串数组存储季节,然后采用

for循环输出,利用"・length"属性控制。

程序如下:

class Demo6{ public static void main(StringEJ args){ int[] a={l, 2, 3, 4, 5, 6, 7};

for (int i二0;i〈;i++){

Systcm. out. print (a[i]+ ”);

,z}

String b[口〃春〃,〃夏〃,〃秋〃,〃冬〃};

for(int i=0;i<;i++){

System, out. print java (b[i]+“);

z,}

}

}

运行如下:

rt:jaua>javac Demo6-jaua

d: jaua>java DenoG ^234567春夏秋冬

d: jaua>

*r+3

3.

r>0

已知、・ = ‘

0 r=0函数 请设计一个方法实现此函数,根据传入的

/-I r<0

x的值返回对应的y值

(书后77页编程题第2题)

程序如下:

public class Demo8{

public static void main(StringEJ args){

int y二function(0);

Systcm. out. println(y);

}

public static int function(int x){

int y;

if(x>0) {

y=x+3;

}else if (x==0){

y

二0;

}else{

y二x*x_l; }

return y;

}

}

运行结果:

d:jaua>javac

d:jaua>java DemoS b

4.

编程实现计算一个长方形的对角线的长度,其中show_length()方法可接收长 方形的宽与高,计算后返回对角线面积

程序如下:

public class Demo3{ public static void main(String[] args){

int arca=gctArea(3, 5);

System, out. print In (The area is+area);

z,z/}

public static int getArea(int x, int y){

int temp二x*y; retUTn tcmp;

}

运行结果:

d = '•xjciua> jauac DcmoO - jaua

a: jaua>jaua Demo3

The 15:1 S实验四类对象

一、 实验目的

1.

理解类与像的概念,掌握类的定义和像的创建;

2.

掌握成员变量和局部变量的区别;

3.

理解匿名对象的调用方法和掌握匿名对象的使用

二、 实验内容

1.

编程利用类和对像描述成员的属性和行为;

2.

编程创建匿名对象描述多个对象的属性和行为;

三、 实验记录

1.

根据所学程序知识完成以下程序

手机类

*属性:品牌(brand)价格(price)

*行为:打屯话(cal 1),发信息(sendMessage)玩游戏(pl ayGame)

*品牌:苹果,价格:7000兀

程序如下:

public class Demi{

publ ic static void niain( Str in g[] args) {

Mo p=new Mo ();

p. brand二〃苹果〃;

=7000;

System, out. println(p. brand+〃+p. price);

p. call ();

p. sendMessageO ;

p. playGame ();

}

}

class Mo{

String brand;

int price;

public void call () {

System, out. print In

(〃打电话〃);

}

public void sendMessage(){

Systeni. out. println

(〃发信,息、”);

}

z,public void playGame(){

System, out. printin(玩游戏〃);z,>j00

aua?0苹打发玩曹心戏

运行结果:

2.

根据学生类或者手机类,发挥自己的才艺分析一个生活中的事物, 要求有属性和行为,并创建5个对象调用其中的方法.

程序如下:

public class Dem2{

public sta tic void mgiin( Str in g[] args) {

Compe pl=ncw Compe();

pl. dec (pl);

Compe pl=new Compe();

p2. dec (p2);

Compe pl=new Compe();

p3. dec (p3);

Compe pl=new Compe();

p4. dec (p4);

Compe pl二new Compe();

p5. dec (p5);

}

}

class Come{

String brand;

int price;

public void dec (Come p){

p. bra nd

二〃戴尔〃;

二5499;

System, out. printin(p. brand+〃

}

}

■a■.7

u 5a

4>9

De

丈尔

4^4<^4

尔a u a j 94 4尔9 9 99 9 9

丈尔

4^尔

运行结果:

+p. price);

〃 3.

匿名对象可以当参数传递吗?不看PPT上的例子,自己编写其他程序举例说明(创建多部手机,手机的颜色白色,屏幕尺寸:7寸。使用匿名对象) 程序如下:

public class Dem3{

public static void main(String[] args){

Mo pl=new Mo();

method (pl);

Mo p2=new Mo ();

method (p2);

}

public static void method (Mo p) {

p. color二〃white〃;

=7;

p. run ();

}

}

class Mo{

String color;

int side;

public void run() {

System, out. print In (color+

〃+side);

}

}

,z运行结果:

d: jaua>jaua De m3 white ?

white 7实验五 private、this

一、 实验目的

1.

利用private关键字修饰成员变量;

2.

掌握this关键字的使用方法

二、 实验内容

1、编写程序利用private关键字和this关键字

三、 实验记录

1.

有一个手机类,属性是颜色,品牌,(两个都私有)行为是:打电话,发信息, 玩微信。比如:苹果手机,白色;三星手机,黑色用学过的知识给属性赋值,并输出 它的属性和行为。

程序如下:

class Dcmol{

public static void main(String[] args){

Mo pl二new Mo();

pl. setName(〃苹果");

pl. setColor

("白色〃);

System. out. printin (pl. getNamc ()+z,...

〃+pl・ getColor ());

pl. call ();

pl. sendMessage();

pl. playWeixin();

Mo p2=new Mo ();

p2・ sctNamc

("三星〃);

p2. setColor (黑色〃);

System・ out. println(p2. getName () +

〃…〃+p2. getColor ());

p2. cal 1 ();

p2. sendMessage ();

p2・ playWeixinO ;

}

}

class Mo{

private String name;

private String color;

public void setName(String name){

this. name二name;

}

public String getName(){

return name;

}

public void setColor(String color) {

=color;

}

public String getColor(){

return color;,z publ ic void cal 1 () {

System・ out・ println

("打电话"); }

public void sendMessage(){

System, out. printin

(〃发信息〃);

public void playWeixin(){

}

运行结果:

苹打发玩三打发一机

果一电话色信息微信色星一电话白信息微信黑De,z

System, out. println(玩微信〃);

2.

编程创建一个Box类,在其屮定义三个私有成员变量表示一个立方体的长、宽和 高,在定义一个方法,求出立方体体积,创建一个对象,求给定尺寸的立方体的 体积(用两种方法输出结果)。

程序如下:

import java. util. Scanner;

class Dem4{ public static void main(String[] args) { Scanner sc=new Scanner (System, in);

Box s二new Box ();

System, out. println(z,请输入长/宽/高:〃);

int x=sc. nextlnt ();

int y二sc. ncxtlnt ();

int z=sc. nextlnt ();

s. setLength(x);

s. setWide (y);

s. setlligh(z); System, out. println(,z立方体体积 二〃+s. get Leng th () *s. get Wide

() *s. get High ());

s. printe ();

}

}

class Box{

private int length;

private int wide;

private int high;

public void setLength(int length) { this. length=length;

F:

e

jau >

}

public int getLengthO { return ;

} public void setWide(int wide){ this. widc=wide;

}

public int getWide(){ return ;

}

public void setHigh(int high){ this. high=high;

}

public int getHighO { return ;

}

public void printe(){ int v二length*wide*high;

System, out. printin(立方体体积二〃+v);

}

,z运行结果:

F:>jauac

F:>jaua Dem4

请输入长/宽/高:

4 5 6

立方体体积叮20

立方体体积叮20

3. this关键字是什么?this关键字的应用场景?

【答】[this关键字]this是一个保留字,仅限于构造函数和方法成员中使用; 在类的构造函数中岀现表示对正在构造的对象本身的引用,在类的方法中出现表示对调 用该方法的对象的引用,在结构的构造上函数中出现表示对正在构造的结构的引用在结 构的方法中岀现表示对调用该方法的结果的引用;

[this关键字的应用场景]用来区分成员变量和局部变量重名

实验六构造方法、方法重载

一、 实验目的

1•掌握构造方法的格式及特点,理解其作用;

2.

掌握构造方法的重载及其注意事项;

3.

理解给成员变量赋值的两种方式的区别;

二、 实验内容

1.

利用构造方法编写学生类手机类代码并测试;

2.

利用构造方法的重载给成员变量赋值;

三、 实验记录

1・模仿学生类完成手机类代码及测试

class Deml{

public static void main (String[] args){ Mo pl=new Mo();

pl. setNameC苹果〃);

se (7000);

System, out. println(pl. getName() +

〃…〃+pl. getPrise()); pl. printeO ;

//输出语句

Mo p2=new Mo

("三星",6500);

System, out. println(p2・ gctNamc()+〃••• 〃+p2. getPrise()); p2. printe ();

} } class Mo{

privatc String name;

private int prise;

public Mo() { }

public Mo (String name, int prise){

this. namc=namc;

this. prise=prise;

return;

}

public void setName(String name){

this. namc=namc;

}

public String getNameO {

return this, name;

}

public void setPrise(int prise) {

this. prise=prise;

publ ic int getPriseO { return

this, prise;

} public void printe(){

System・ out. printin (name+. . .+prise); }

}

z/,z运行结果:

F:>jauac

F:>jaua Demi

苹.7oee

.7000

三皇・・・65G8

三星...6500

2.

编程创建一个Box类,在其中定义三个私有成员变量表示一个立方体的长、宽 和高,在定义一个方法,求出立方体体积,创建一个对象,求给定尺寸的立方体的体积 (用两种方法输岀结果)。

程序如下:

import java. util. Scanner;

class Dem2{

public static void main(String[] args){

Scanner sc=new Scanner(System, in);

System, out. print In (,z请输入长/宽:〃);

int x=sc. ncxtlnt ();

int y=sc. nextint ();

Rect pl二new Rect ();

pl. setLength(x);

pl. setWide(y);

Sys tem. out. print 1 n(2*(pl・ get Leng th()+pl・ get WidcO));

System, out. println(pl. getLengthO^pl. getWideO);

pl. printe ();

System, out. printin(2*(p2. getLength() +p2. getWide()));

Sys tem. out. printl n( p2. get Length ()*p2. get Wide ()); p2. printe ();

}

} 〃自定义类 class Rect{

private int length; private int wide; public Rect () {

} public Rect (int length, int wide){ =length;

this. wide二wide; return;

} public void setLength(int length) { =length;

} public int getLcngth() { return ;

} public void setWide(int wide) { this. wide二wide;

} public int getWide() { return this. wide;

} public void printe() {

int m=2^1cngth+2*widc;

int n=length^wide;

System, out. pri周长二〃+m); System,

out. printin(面积二〃+n);

}

}

z,F:>jaua Dom2

运行结果:

请输入长/宽:

3 4

14

12

周长叮4

面积訂2

14

12

周长訂4

3•定义一个员工类,自己分析几个成员,然后给出成员变量(姓名name,工号id

工资salary)构造方法:(空参有参)以及一个显示所有成员信息的方法

程序如下:

class Dem3{

public static void main(String[] args){

Employee pl二new Employee(); pl. setName(〃张三〃);

pl. setTd(125);

pl. setSalary(5000);

pl. work ();

Employee p2=new Employee(”李四”,234,6000);

p2. work ();

}

}

class Employee{

private String name;

private int id;

private double salary; public Employee(){

}

public Employee (String name, int id,double salar)0 {

=name;

this. id=id;

this. salary=salary;

return;

}

public void setName(String name){

=name;

}

public String gctNamc(){

return this, name;

}

public void setTd(int id) {

this. id=id;

} public int getld(){ feturn this, id;

}

public void setSalary(double salary){

this. salary=salary;

}

public double getSalary(){

return ;

}

public void work(){

System, out. print In (员工姓名}

}

z,z,name+员工编号:〃+id+"薪水:〃+〃

+salary);

z,z/F:>jaua Dem3

运行结果:员工姓名:张三员工编号:125薪水:5000.0

员工姓名:李四员工编号:234薪水:600C. 0

实验七

static、代码块

1.

了解static关键字的特点及作用

2.

了解成员变量和静态变量的区别;

3.

区分局部代码块,构造代码块及静态代码块;

4.

了解Java帮助文档的使用;

一、 实验目的

二、 实验内容

1.

编写学生类程序使用static关键字;

2.

读含有代码块的程序写结果;

三、 实验记录

1.

声明一个名为Person的类,含有三个私有属性:name、age> city。声明

Person类的一个构造方法,此构造方法分别为各属性赋值。要求ci ty属性都赋值为美 国声明一

talk()方法,此方法用于返冋用户信息 声明一个测试类,创建并实例化三 个Person对象。在测试类中调用talk()方法,输出用户信息。

程序如下:

class Dem2{

publ ic static void niain( Str in g[] args) {

Person pl二new Person();

pl. set Name

(〃张三〃);

pl. setAge(23);

pl. setCity

(〃美国〃);

pl. talk();

Person p2=new Person (李四〃,25);

p2. talk ();

Person p3二new Person(〃王二〃,24);

p3. talk();

}

}

class Person{

private String name;

private int age;

static private String city;

public Person() {

}

public Person(String na, int ag){

name=na;

z,age=ag; } public void setName(String na){

ncime=na;

} public String

getName(){ return name;

}

public void set Age (int cig) {

age=ag;

} public int

getAge(){ return age;

}

public void sctCity(String ci) { city=ci;

}

publ

ic

String

getCity ()

{

return city;

} public

void

talk() {

System, out. print In (z/

输出用户信息〃);

System, out. println(name+〃

+age+〃 〃+city);

}

}

二Ort-羣au

毫De a1J 7耳皇自

岀“国三户息岀国户息输四国户运行结果:

王岀3

35

>J

2•定义一个计算矩形面积、立方体和球体体积的类。该类完成计算的的方法用静实现

程序如下:

import java, uti1. Scanner;

class Dem3{

public static void main(String[] args){

Scanner sc=ncw Scanner (Systcm. in);

〃 态方法Ma pl=new Ma();

System, out. print In (请输入矩形长/宽:〃);

int x二sc. nextTnt ();

int y二sc. nextint ();

pl. set Leng th (x);

pl. setWide (y);,? pl. runl ();

System, out. printin(z,请输入立方体的边长:〃);

int z=sc• nextint();

Ma p2=ncw Ma(z);

p2. run2 ();

System, out. print In (/z

请输入球体半径:〃);

double r=ubleO ;

Ma p3=new Ma(r);

p3. run3 ();

class

Ma{ static

private int

xstatic static

privatc int

static public

}

private int

y;

〃长

//宽

public private

double

Zr;

Ma () {

〃高

//球体半径

Ma(int a) {

z=a;

}

public Ma(double d) { r=d;

}

public static void setLength(int b){ x二

b;

}

public static int getLength() {

return x;

}

public static void setWide(int c) {

y=c;

}

public static int getWidcO {

return y;

}

publ ic static void runl () {

System, out. print In (矩形面积,z=,/+Ma. x*Ma. y);

}

public static void run2(){

System, out. println(立方体体积二〃+z*z*z);

z,}

public static void run3() {

System, out. print In (球体体积二〃+4*3. 14159*r*r*r/3);z, 运行结果:

C: MUter*'sSevc UonirM : b:>cd “ = 7・"

匕:JavA>J*vac DeibJ

・J w

胸入飓宪:长如

3.

编个小游戏:获取一个1-100之间的随机数,猜对为止

3 4

程序如下:

import . Scanner;

class Deml{

public static void main(String[] args) { Seanner sc=new Sea

rm er (System, in);

int y=(int) (Math, random()*100) +1; for(int

i=l;i<=100;i++){

System, out. print In C请输入你猜的数字:〃);

int

x二sc. nextint ();

if (x>y) {

System, out. println(猜大了");

} if (x

System. out. pr、intln(〃猜小了〃);

} if (x==y){

System, out. print In (z,恭喜你,答对了〃);

break;

}

System, out. printing别灰心,再猜猜看〃);

}

d: jaual>jauac

d: jaual>java Denol

,z请端入你猜的数字:

46

运行结果:

taoB:

猜大了

39

猜犬了

*青輪入你猜的数字:

慵小了

显喜你,答对了实验八继承

一、 实验目的

1•了解继承的概念,特点,好处及弊端;

2.

掌握关键字this和Super的使用方式;

3.

理解继承中构造方法及成员方法的关系;

4.

掌握继承中方法重写,区分方法重写及方法重载;

二、 实验内容

1•仿照老师学生类编程猫狗案例;

2.仿照老师学生类编程不同车型的案例;

三、 实验记录

猫狗案例:

*猫狗案例继承版

*属性:毛的颜色,腿的个数

*行为:吃饭

*猫特有行为:抓老鼠catchMouse

*狗特有行为:看家1 ookHome

程序如下:

class Demi{ publ ic static void niain( Str in g[] args) {

/zCat cat二new Cat (); cat. setColor (

白色〃);

cat. setLeg(4);

cat. print () ;

//输出猫的颜色/腿数

cat. run ();

cat. catchMouse () ;

dog. print () ;

dog. run ();

dog. 1 ookHome () ;

}

}

class Aniaml{

String color;

int leg;

public Aniaml () {}

public Aniaml(String color,int leg) {//行为-吃饭

//输出猫的颜色/腿数

//行为-吃饭

Dog dog=new Dog

C黑色",4); this, color二color; =leg;

}

public void setColor(String color){

=color;

} publ ic String getColorO { return color;

}

public void setLeg(int leg) {

二leg;

} public int getLegO { return leg;

} public void print(){

System, out. println(color+〃 〃+leg);

}

public void run() { System, out. printin (吃饭〃);

}

}

class Cat extends Aniciml

{ public void run() { super.

run();

System, out. println(z,喝酒〃);//方法重写特有行为

}

public void catchMouse()

{ System, out. println(,z抓老,z鼠〃);//新增行为

}

} class Dog extends Aniaml {

public Dog() {} public Dog(String color,int leg) { super (color, leg);

public void lookllome() {

Systcm. out. println

(〃看家〃);

运行结果:

4Deni 2.定义一个名为Vehicles

(交通工具)的基类,该类中应包含String类型的成员 属性brand

(商标)和color

(颜色),

还应包含成员方法nm

(行驶,在控制台显示“我已经开动了”)和showinfo

(显 示信息,在控制台显示商标和颜色),并

编写构造方法初始化其成员属性。编写Car

(小汽车)类继承于Vehicles类,增 加int型成员属性seats

(座位),还应增加成员方法showCar

(在控制台显示小汽车 的信息),并编写构造方法。编写Truck

(卡车)类继承于Vehicles类,增加float

型成员属性load

(载重),还应增加成员方法showTruck

(在控制台显示卡车的信息), 并编写构造方法。在main方法中测试以上各类。

程序如下:

class Dem2{

public static void main(String[] args){

Car c二new Car ();

c. setBrand

C奥迪〃);

c. setColor (银白〃);

c. setSeats (5);

c. showCar ();

System, out. printin (,z车辆信息:〃+c. getBrandO +,z

〃+c. getColor ()); Truck

t=new Truck (沃尔沃〃,〃海蓝",18);

t. showTruck ();

System, out. printin

("车辆信息:〃+t・ getBrand () +"

〃+t. getColor ());

}

}

class Vehicles {

privatc String brand;

private String color;

int seats;

float load;

public Vehicles() {

}

public Vehicles (String brand, String color,float load){

this. brand=brand;

=color; this> load=load;

}

public void setBrand(String brand){

=brand;

,zz,}

public String getBrand(){

return brand;

}

public void setColor(String color) {

=color;

}

public String getColor () { return color;

} public void setSeats(int seats){ 二seats;

} public int getSeats(){ return seats;

}

public void run() {

System, out. printin(我已经开动了 !");

}

public void show!nfo(){ System, out. printin

(〃车辆信息:〃+brand+" +color);

}

} class Car extends Vehicles{

public void showCar(){ super, run (); super, showinfo ();

System, out. printin (载客量〃+seats);

}

}

class Truck extends Vehicles {

public Truck() {}

public Truck (String brand, String color, float load){ super(brand, color, load);

} public void showTruckO { super, run (); super, showinfo (); System, out. printin

(载重量}

z,z,z,,zzz+load);

}

运行结果:

Jjaua>java Dm2

丁己经开动了「 邀信息唄迫银白 嫡書L翱很白 [辆倩息:沃尔沃海蓝 直童18』 辅時息氓尔沃海蓝

实验九多态、final

一、 实验目的

1.

掌握Final关键字的使用方法及注意事项;

2.

理解多态的概念及代码实现;

3.

掌握多态中成员访问的特点及方式;

4.

体会多态的好处及弊端;

5.

掌握多态中向上转型和向下转型的方法及代码实现过程;

二、 实验内容

1.

编写程序创建Person类,定义两个子类Student, Teacher,用多态作形式参数

2.

掌握多态的相关概念;

三、 实验记录

1.

多态是什么?前提是什么? [答案]多态是事物存在的多种形态;

前提:(1)要有继承关系;(2)要有方法重写;(3)要有父类引用指向子类对象

2.

:多态的好处及弊端?

好处:(1)提高了代码的维护性(继承保证);(2)提高了代码的扩展性(由多态保证) 弊端:不能直接使用子类的特有属性和行为

3.

用多态思想自己编写一个父类Person类程序,他有两个子类分别为student

和teacher类,自己定义方法,完成Person p

(父类引用)做为参数传递,调用子类 方法

的程序。

程序如下:

class Demi{

public static void main(String[] args){

method(new Student ());

method(new Teacher ());

}

public static void method(Person p){

i f(p i nstanceof Student){ Student s=(Student)p;

s. eat ();

s. StuO ;

//定义方法学习

}else if(p instanceof Teacher) {

Teacher t二(Teacher)p;

t. eat ();

t. tea() ;

}else {

p. eat ();

//定义方法讲课

class Person{ public void eat () {

System, out. printin

C吃饭〃);

}

} class Student extends Person{ public void eat () {

System, out. printin

C学生吃饭〃); }

publ ic void stu() { System, out. printinC学生学习〃);

}

:

class Teacher extends Person{ publ ic void eat () {

System, out. printin

(〃老师吃饭〃);

} public void tea0 {

System, out. printin (z,老师讲课〃);

}

}

生吃学生师吃讲师运行结果:

一、 实验目的

1.

掌握抽象类的定义方法;

2.

了解抽象类的特点;

3.

掌握抽象类成员的特点;

4.

明白abstract不能和哪些关键字共存;

二、 实验内容

1.

利用多态性和抽象类编写程序;

au7a

・小饭习饭课•円Den6

实验十抽象类 三、 实验记录

1•定义一个抽象父类Shape,它包含一个抽象方法getAreaO ,从Shape类派生出

Rectangle和Circle类,这两个类都用getAreaO方法计算对象的面积。要求用多态 性编写该程序实现其功能。

程序如下:

import java. util. Scanner; public class Dem6{

public static void main(String[] args){ Scanner sc= new Scanner(Systcm. in); System, out.

printin (请输入矩形长/宽:");

double x二sc. nextDoubleO ;

double y=ubleO ;

Shape al=new Rectangle (x,y,0);

al. get Area.();

System, out. printin

(请输入圆形半z,z,径:");

double z二sc.

nextDoubleO ; Shape

a2二new Circle(0, 0,

z);

a2. get A rea();

} } abstract class Shape{

private double length;

private double wide;

privatc double rad; public Shape() {} public Shape(double length,double wide, double rad)

{ this, length=lenglh;

this. wide二wide;

this. ra.d=ra.d;

} public void setLength(double length){ this, length=lenglh;

} public double get Leng th(){ return length;

public void setWide(double wide){

this. wide二wide;

} public double getWide(){ return wide;

}

public void set Reid (double rad) {

=rad;

}

public double getRad(){

return rad;

}

public abstract void getArea();

} class Rectangle extends Shape{

publ ic Rectangle() {} public Rectangle(double length,double wide, double rad)

{ super(length,wide, rad);

} public void getAreaO {

System, out. println(矩形面积:〃+this. getLength()*this. getWideO);

}

} class Circle extends Shape{

public CircleO {} public Circle(double length,double wide, double rad){ super(length,

wide, rad);

}

public void getAreaO { System, out. println(圆的面积:〃+3. 14*this. getRad()*this.

getRad());

}

}

z,z,运行结果:

d: jaual>jaua Den)6

情输入矩形长/宽:

3 4

3

陰形面积江2 0

情縮入齒形半倍

圆的面积:28.259999999999998

2.

(练习老师案例)

*具体事物:基础班老师,就业班老师 *共性:姓名,年龄,讲课。

程序如下:

class Deml{

public static void main(String[] args){ Teachc c二new Teachc();

c. setName (,z韩磊〃);

c. setAge (27);

System, out. printin (c. getName ()

+〃 〃+c. getAge ()); c. tea();

//特有

Teachs s=new Teachs

(〃李芳28);

System, out. printin (s. getName ()

+〃 〃+s. getAge ()); s. tea();

S. emp() ;

//特有

c. com() ;

}

} abstract class Person{ private String name;

private int age; public Person() {} public Person(String name, int age){ this. name=name;

this, age二age;

}

public void set Name (S tring neime) {

this. namc=nanic;

}

public String getName(){

return name; }

public void setAge(int age) {

二age;

}

public int getAge(){

return age;

} public abstract void tea();

} class Teachc extends Person{

public Teachc() {}

public Teachc(String name, int age){ super (name, age);

}

publ ic void tea() {

System, out. print In (,z

基础班讲课〃);

}

public void com() {

System, out. print In (,?基础化教学〃);

}

}

class Teachs extends Person!

public Teachs() {}

public Teachs(String name, int age){ super (name, age);

}

public void tea() {

System, out. printin

C就业班讲课〃); }

publ ic void emp() {

System, out. println(就业指导教学〃);

}

}

z,运行如下:

d : jaua>jaua Deni

3.

抽象类练习员工案例

1

•程序员:姓名、工号以及工资。

工作内容:敲代码

2.项目经理:姓名、工号以及工资、奖金 工作内容:管理

程序如下:

class Dem2{

public static void main(String[] args){

Ch c二new Ch (); c. sctNamc

C韩磊〃);

c. setld (27);

c. setWide(8000);

System, out. println(c. getName() +,z

〃+c. getId()+〃 〃+〃工资〃+c. getWideO);

//工作

Xm s=ncw Xm(〃李芳〃,28, 10000);

c. act () ;

s. setAd (5000);

System, out. println(s. getName()+〃 〃+s. getId()+〃 〃+〃工资〃+s. getWideO+"奖 金〃+s・ getAdO);

s. act () ;

//工作

abstract class Person{ private String name; private int id; privatc int wide; public Person()

{} public Person(String name, int id, int wide){ this. name=name;

this, id二id; this . widc=wide;

} public void setName(String name){ this. name=name;

} public String getName(){ return

name;

} public void setTd(int id){ this, id二id;

} public int getld(){ return id;

} public void setWide(int wide) { this. widc=widc;

} public int getWideO { return wide;

} public abstract void act () ;

} class Ch extends Person{

publ ic Ch () {} public Ch (String neime, int id, int wide) { super (name, id, wide);

} public void act(){

System, out. printin(z,敲代码〃);

}

} class Xm extends Person{ private int ad;

publ ic Xm() {} public Xm(String neime, int id, int wide) { super (name, id, wide);//抽象方法 public void setAd(int ad){ this. ad=ad;

}

public

int gctAdO

{ return ad;

System, out. printin (,z管理〃);

} public

void act () {

运行结果:

d:jaua>jaua Dem2

韩磊27工资8000

敲代码

季芳28工资10000奖金50盹 脅理实验^一接口

一、 实验目的

1•掌握4种访问修饰符的区别;

2.掌握接口声明及使用;

二、 实验内容

1

•使用接口的定义方法编写程序

三、 实验记录

l.

模仿动物类,写一个person类,其中有student和teacher

类继承

person类,学生类中部分人会跳舞,老师类部分人会唱歌。

程序如下:

class Deml{

public static void main(String[] args){

Student s=new Student();

s. setName

C学生〃);

s. setNum(23);

s. print ();

s. eat ();

Student m=new Studcntl();

m. dance ();

Teacher t二new Teacher

(〃老师〃,5);

t. print ();

t> eat ();

Teacher n=new Teacher1();

n. sing();

}

}

abstract class Person{

private String name;

private int num;

public Person() {}

public Person(String name, int rmm) {

this. ncime=name;

this. num二num;

}

public void setName(String name){

this, name二name;

}

public String getName(){

return name;

}

public void setNum(int num) { this. num二num; }

public int getNum(int rmm) { return num;

public void print (){

System. out. println (name+z/

〃+nuni+"人〃);

}

void eat ();

interface Peoplel{ public

abstract }

interface People2{ public

abstract }

class Student extends void singO

Person!

publ ic void eat () {

System, out. printin (z,学生吃饭〃);

public abstract

void dance ();

public void dance() {};

}

class

Teacher extends Person{ public Teacher() {}

public Teacher (String name, int num){ super

(name, num);

}

publ ic void eat () {

System, out. printin

(〃老师吃饭〃);

public void singO {};

}

class Studentl extends Student implements Peoplel { public

void dance(){

System, out. print In (部分学生会跳舞〃);

zz} }

class Teacherl extends Teacher implements People2{ public void

singO {

System, out. print In (zz部分老师会唱歌");

}

}

*笙23

运行结果:

javal>java Deml

II

老师5人 老师吃t

会跳舞

3.

编写一个Java应用程序,除了主类(TestTwo)夕卜,该程序还有Shape接口、 三角形类Triangle类、

矩形Rectangle类。该程序具体要求如下:

1) Shape

接口

public abstract double computcArca(double a, double b)扌巾象 方法。

2) Triangle类实现Shape接口,完成计算面积的功能:用a代表三角形的底、b

代表三角形的高。

3) Rectangle类实现Shape接口,完成计算面积的功能:有a代表矩形的底、b

代表矩形的高。

4)

在TestTwo类的主方法中分别创建各子类的对象并调用各自的计算面积的方法, 并打印输出信息面积信息。

(要求使用到多态性)

import java. util. Sea rm er;

class DcmlO{ public static void main(String[] args){ Scanner sc二new Scanner (System, in);

System, out. printin(z,请输入

a/b

的数值:〃);

double x二sc. nextDouble (); double y=sc.

nextDouble(); method(new Triangle (x,y)); method(new Rectangle (x,y));

} public static void method(TuXing c){ if(c instanceof Triangle) { Triangle t=

(Triangle)c;

System, out. println(t. computerArea(t. getA(), t. getB()));

}else if(c instanceof Rectangle){

Rectangle r=(Rcctangle)c; System, out. print In (r. computerArea (r.

getA(), r. getB()));

}

}

} abstract class TuXing{ private double a; private double b; publ ic TuXing() {} public

TuXing(double a, double b){ this. a=a; this.b=b;

} public void setA(double a){ this. a=a;

} public double getA() {

return a;

} public void setB(double b){

this.b=b;

}

public double getB(){

return b; }

}

interface Shape{

public abstract double computerArea(double a, double b); }

class Triangle extends TuXing implements Shape{

public Triangle() {}

public Triangle(double a, double b) {

super (a, b);

}

public double computerArea(double a, double b){ System, out. printing三角形面积:");

double si二a*b/2;

return si;

}

}

class Rectangle extends TuXing implcments Shape{

public Rectangle() {}

public Rectangle (double a, double b){

super (a, b);

}

public double computcrArca (double a, double b){ System, out. printin (矩形面积:〃);

double s2二a*b;

return s2;

}

}

,z运行结果:

d: jaua>jaua Deml0

请输入R/論数值:

3

4

三角形面积:

6.0

矩形面积:

12.0成绩:

指导教师签字:

本文标签: 方法成员程序掌握实验