java程序设计实验报告

更新时间:2023-12-09 11:50:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Java程序设计实验报告

学号: 姓名: 座位号: 实验日期: 【实验名称】: JDK配置与开发工具的使用 【实验目的】:

1. 熟悉JDK开发环境。

2. 熟悉EditPlus编辑器或Eclipse等开发环境的使用。

3. 掌握Java Application的程序结构和开发过程。 【实验内容及要求】:

1. JDK安装。

2. 设置环境变量。

3. 分别运用EditPlus和Eclipse编写并运行一个简单的“Hello World!”应用程序。

【程序输出结果与结果分析】: Helloworld

【自评分及理由,自己的体会和收获】:

主要是熟悉jdk的开发环境,环境变量的设置。 Editplus需要设置环境变量,eclipse不用

Path的环境变量设置C:\\EditPlus\\jdk1.6.0\\bin

Classpath的环境变量的设置.;C:\\EditPlus\\jdk1.6.0\\bin

【程序代码】: Editplus: class Helloworld {

public static void main(String[] args) {

System.out.println(\ } }

Eclipse:

public class HelloWorld {

public static void main(String[] args) {

System.out.println(\); } }

Java程序设计实验报告

学号: 姓名: 座位号: 实验日期: 【实验名称】: 类和对象的应用 【实验目的】:

1. 掌握各种数据类型及其使用方法。

2. 掌握分支语句if、switch和循环语句for、while、do-while的应用。 3. 掌握类的声明和对象的创建。

4. 掌握方法的定义、调用和构造器的使用。 【实验内容及要求】:

1. 分别使用if-else-if语句和switch语句编程,确定某一月在哪个季节。

2. 分别使用while、do-while和for语句编程,求1~100的和。

3. 使用break语句实现记数:从1~100,当数到78时程序终止。

4. 编程创建一个Box类,在其中定义三个变量表示一个立方体的长、宽和

高,再定义一个方法setDemo对这三个变量进行初始化,然后定义一个方法求立方体的体积。创建一个对象,求给定尺寸的立方体的体积。 【程序输出结果与结果分析】: 一

***☆ 确定季节 ☆**** * 1.if-else-if * * 2.switch * * 3.exit * ****************** 请选择:1

请输入当前月份:2 该月为春季

***☆ 确定季节 ☆**** * 1.if-else-if * * 2.switch * * 3.exit * ****************** 请选择:2

请输入当前月份:6 该月为夏季

***☆ 确定季节 ☆**** * 1.if-else-if * * 2.switch * * 3.exit * ****************** 请选择: 二

1到100的数字和:5050 三 1 2 3 4 5 6 7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

请输入Box的长:2 请输入Box的宽:5 请输入Box的高:4

Box的长为:2 Box的宽为:5 Box的高为:4 Box的体积为:40

【自评分及理由,自己的体会和收获】: Editplus当有输入时无法运行,Eclipse可行

【程序代码】: 1

import java.util.Scanner;

public class Test1 {

/**

* 季节:

* 1.if-else-if语句 * 2.switch语句 */

public static void main(String[] args) { while (true) {

Scanner scan = new Scanner(System.in);

System.out.println(\☆ 确定季节 ☆****\); System.out.println(\); System.out.println(\); System.out.println(\); System.out.println(\); System.out.print(\请选择:\); int select = scan.nextInt(); switch (select) { case 1:

IfSeason.decide(); break; case 2:

SwitchSeason.decide(); break; case 3:

System.exit(0); break; default:

System.out.println(\您输入的选项有误\);

//调用工具类utils中的reset函数判断是否重新输入 boolean select1 = utils.reset(); if (select1) { break; } else {

System.exit(0); } } } } }

//if-else-if class IfSeason{

public static void decide(){ while (true) {

//调用工具类utils中的input函数输入月份 int month = utils.input();

if (month==2||month==3||month==4) { System.out.println(\该月为春季\\n\\n\); break;

} else if (month==5||month==6||month==7) { System.out.println(\该月为夏季\\n\\n\); break;

} else if (month==8||month==9||month==10) { System.out.println(\该月为秋季\\n\\n\); break;

} else if (month==11||month==12||month==1) { System.out.println(\该月为冬季\\n\\n\); break; } else {

System.out.println(\您输入的月份有误\);

//调用工具类utils中的reset函数判断是否重新输入 boolean select = utils.reset(); if (select) { //跳出本次循环 continue; } else {

//跳出while循环 break; } } } } }

//switch

class SwitchSeason{

public static void decide(){ out: while (true) {

}

}

}

//调用工具类utils中的input函数输入月份 int month = utils.input();

switch (month) { case 2: case 3: case 4:

System.out.println(\该月为春季\\n\\n\); break out; case 5: case 6: case 7:

System.out.println(\该月为夏季\\n\\n\); break out; case 8: case 9: case 10:

System.out.println(\该月为秋季\\n\\n\); break out; case 11: case 12: case 1:

System.out.println(\该月为冬季\\n\\n\); break out; default:

System.out.println(\您输入的月份有误\);

//调用工具类utils中的reset函数判断是否重新输入 boolean select = utils.reset(); if (select) { //跳出本次循环 continue out; } else {

//跳出while循环 break out; } }

//工具类

class utils{

//输入当前月份

public static int input(){

Scanner scan = new Scanner(System.in);

}

System.out.print(\请输入当前月份:\); int month = scan.nextInt(); return month; }

//是否重新输入

public static boolean reset(){ }

System.out.println(\是否重新输入?(Y/N):\); Scanner scan = new Scanner(System.in); String select = scan.next().trim(); if (\.equals(select)) { return true; }else{

return false; }

2(1)

class Sum {

public static void main(String[] args) {

int i=1; int s=0;

while(i<=100) {s=s+i; i++; }

System.out.println(\到100的数字和:\+s); } }

(2)

class Sum1 {

public static void main(String[] args) {

int s=0;

for(int i=1;i<=100;i++){ s=s+i; }

System.out.println(\到100数字的和:\+s); } }

(3)

class Sum2 {

public static void main(String[] args) {

int i=1; int s=0; do{

s=s+i; i++;

}while(i<=100);

System.out.println(\到100的数字的和:\+s); } }

3

public class BreakTest {

/**

* @param args */

public static void main(String[] args) { for (int i = 1; i <= 100; i++) {

System.out.println(i);//print(i+\

if (i==78) { break; } } }

}

4

import java.util.Scanner;

public class BoxTest {

/**

* @param args */

public static void main(String[] args) { Scanner scan = new Scanner(System.in); Box box = new Box();

System.out.print(\请输入Box的长:\); box.setLength(scan.nextInt()); System.out.print(\请输入Box的宽:\); box.setWidth(scan.nextInt()); System.out.print(\请输入Box的高:\); box.setHeight(scan.nextInt());

System.out.println(\的长为:\+box.getLength()); System.out.println(\的宽为:\+box.getWidth()); System.out.println(\的高为:\+box.getHeight()); System.out.println(\的体积为:\+box.getVolume()); } }

class Box{

private int length; //长 private int width; //宽 private int height; //高 public Box() { }

public Box(int length,int width,int height){ this.length=length; this.width=width; this.height=height; }

public int getLength() { return length; }

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

public int getWidth() { return width; }

public void setWidth(int width) { this.width = width; }

public int getHeight() { return height; }

public void setHeight(int height) { this.height = height; }

public int getVolume(){

return this.height*this.length*this.width; }

}Java

程序设计实验报告

学号: 姓名: 座位号: 实验日期: 【实验名称】: 继承与多态的应用 【实验目的】:

1.掌握类的继承方法。 2.掌握变量的继承和覆盖。

3.掌握方法的继承、重载和覆盖。 4.掌握扩展类中构造器的使用。 【实验内容及要求】:

1. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的成员属性有:车

轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含属性载人数passenger_load。卡车Truck是Vehicle的子类,其中包含载人数passenger_load和载重量payload。要求每个类都有相关数据的输出方法。 2. 运行程序,理解成员变量的继承与隐藏。

【程序输出结果与结果分析】: ---------- java ----------

轮子1 重量1

汽车乘客人数2 卡车价钱3 汽车轮子1 卡车轮子1 卡车乘客人数3

【自评分及理由,自己的体会和收获】: 主要学习了类的继承 Car继承了vehicle

c.getWeight()实际调用了vehicle的weight 【程序代码】:

class Vehicle {

int wheels=1; int weight=1;

public Vehicle(){}

public Vehicle(int wheels,int weight) {

this.wheels=wheels; this.weight=weight; }

public int getWheels() {

return this.weight; }

public int getWeight() {

return this.wheels; } }

class Car extends Vehicle {

int passenger_load=2; public Car(){}

public Car(int passenger_load) {

this.passenger_load=passenger_load; }

public int getPassenger_load() {

return this.passenger_load; } }

class Truck extends Vehicle {

int passenger_load=3; int payload=3; public Truck(){}

public Truck(int passenger_load,int payload) {

this.passenger_load=passenger_load; this.payload=payload; }

public int getPassenger_load() {

return this.passenger_load; }

public int getPayload() {

return this.payload;

}

}

class VehicleTest {

public static void main(String[] args) {

Vehicle v=new Vehicle(); Car c=new Car();

Truck t=new Truck();

System.out.println(\轮子\+v.getWheels()); System.out.println(\重量\+c.getWeight());

System.out.println(\汽车乘客人数\+c.getPassenger_load()); System.out.println(\卡车价钱\+t.getPayload()); System.out.println(\汽车轮子\+c.getWheels()); System.out.println(\卡车轮子\+t.getWheels());

System.out.println(\卡车乘客人数\+t.getPassenger_load()); } }

设计实验报告

学号: 姓名: 座位号: 实验日期: 【实验名称】: 接口的使用 【实验目的】:

1.掌握接口的含义和声明。 2.掌握单继承和多继承的概念。 【实验内容及要求】:

1.定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea(),然后设计MyCircle和MyRectangle两个类都实现这个接口中的方法calculateArea(),分别计算圆和矩形的面积。 2.运行程序,理解使用接口的环境和方法。 【程序输出结果与结果分析】: ---------- java ----------

圆的面积:1256.6370614359173

本文来源:https://www.bwwdw.com/article/c3y5.html

Top