资源描述
信 息 工 程 学 院实验报告的内容与格式按任课教师的要求书写。
Java程序设计 实验/实习报告
学院:
班级:地信
姓名:
学号:2
成绩:A
面向对象编程
一、 实验/实习过程
(一)实验目的
掌握Java程序面向对象编程的基本架构,会运用面向对象的思想编写Java程序。
(二)实验过程
【实验题1】 完成如下程序,完成思考题
[基本要求] 运行程序并观察运行结果。
[思考问题] 试述程序中主要语句的作用
运行结果:
主要程序语句的作用已标注为注释:
class CCircle // 类的声明
{ double pi; //申明成员变量
double radius; //申明成员变量
double getRadius() //创建方法
{ return radius; //返回 radius
}
void setCircle(double r, double p) //创建方法
{ pi=p; //初始化数据成员
radius=r; //初始化数据成员
}
}
public class Ex2_1 //类的声明
{ public static void main(String args[]) //主方法
{ CCircle cir1=new CCircle(); //创建对象
cir1.setCircle(2.0,3.1416); //调用对象
System.out.println("radius="+cir1.getRadius()); //输出结果
}
}
【实验题2】 设计一个用来描述汽车的类,使用类的静态成员变量来表示汽车的车主姓名、当前的速率和当前方向盘的转向角度,使用类的非静态成员方法来表示改变汽车的速率和停车两个操作。
package zz1;
public class Car{
String name;
int speed;
int jiaodu;
public Car(String name,int speed,int jiaodu){
this.name=name;
this.speed=speed;
this.jiaodu=jiaodu;
System.out.println("车主:"+this.name);
System.out.println("车速为:"+this.speed);
System.out.println("方向盘角度:"+this.jiaodu);
}
void changename(String name){
this.name=name;
System.out.println("当前车主为:"+this.name);
}
void changespeed(int speed){
this.speed=speed;
System.out.println("当前速度为:"+this.speed);
}
void changejiaodu(int jiaodu){
this.jiaodu=jiaodu;
System.out.println("当前方向盘角度为:"+this.jiaodu);
}
void stop(){
this.speed =0;
System.out.println("停车后,车速为:"+this.speed);
}
public static void main(String args[]){
Car car=new Car("张震宇张三",0,0);
car.changename("好友");
car.changespeed(20);
car.changejiaodu(30);
car.stop();
}
}
结果:
【实验题3】 定义一个类MyProgram,包含两个属性:一个是private的整型属性data、一个是private的String类型的属性str,封装这两个属性的四个方法setData()和getData()、setStr( )和getStr();将这两个属性转变为字符串的方法是toStr()。编写程序,使用MyProgram类,实现数据的访问和修改,并调用toStr()方法显示该类的属性。
[基本要求] 编写完整程序。
[思考问题] 试述程序中各个方法的作用。
代码如下:
package zz1;
public class Myprogram {
private int data;
private String str;
void setdata(int data){
this.data=data;
}
int getdata(){
return data;
}
void setstr(String str){
this.str=str;
}
String getstr(){
return str;
}
String toStr(){
String a=data+" ";
a=str;
return a;
}
public static void main(String args[]){
Myprogram m=new Myprogram();
m.data=1000;
m.str="jgfldjsglkdj";
System.out.println("转换之前:"+m.data);
System.out.println("转换之后:"+m.str);
}
}
结果如下:
[思考问题] 试述程序中各个方法的作用。
setData()和setStr()方法的作用是获得数据和资料,getData()、和getStr()作用为设置数据和资料。toStr()函数用来将其他类型转化为字符串类型。
【实验题4】 定义一个类实现银行帐户的概念,包括的变量有"帐号"和"存款余额",包括的方法有"存款"、"取款"、"查询余额"和”显示帐号”。定义主类,创建帐户类的对象,并完成相应操作。
提示:关键代码如下:
public int getleftmoney(){
return leftmoney;
}
public void savemoney(double money){
leftmoney+=money;
}
public void getmoney(double money){
if(money<=leftmoney)
leftmoney-=money;
else
System.out.println("只能取:"+leftmoney);
}
…
bankaccount ba=new bankaccount(888123,1000);
ba.savemoney(21000);
System.out.println("存入21000元后余额为:"+ba.getleftmoney());
ba.getmoney(11500);
System.out.println("11500元后余额为:"+ba.getleftmoney());
代码如下:
package zz1;
public class Bankaccount {
int acc;
int leftmoney;
Bankaccount(int acc,int leftmoney){
this.acc=acc;
this.leftmoney=leftmoney;
}
public int getleftmoney(){
return leftmoney;
}
public void savemoney(double money){
leftmoney+=money;
}
public void getmoney(double money){
if(money<=leftmoney)
leftmoney-=money;
else
System.out.println("只能取:"+leftmoney);
}
public static void main(String args[]){
Bankaccount ba=new Bankaccount(888123,1000);
ba.savemoney(21000);
System.out.println("存入21000元后余额为:"+ba.getleftmoney());
ba.getmoney(11500);
System.out.println("取出11500元后余额为:"+ba.getleftmoney());
}
}
结果如下:
【实验题5】 完成课本4.13(102页)jar文件的生成并产生Use类的运行结果。
【实验题6】 对象的组合。现有一个Rectangle类,请设计一个柱形类cuboid。
1. 此类具有两个成员变量,(1)Rectangle类型的成员变量rect(长方体以长方形做底)和(2)height(柱形的高)。
2. 此类应具有构造方法。
3. (1)此类应具有求面积的方法getVolme()。
(2)此类应具有设置和获得底的长度和宽度的方法:getBottomWidth(),
setBottomWidth(),
getBottomLength(),
setBottomLength()。
Rectangle的代码如下:
public class Rectangle {
double width,length;
public void setWidth(double w) {
if(w > 0)
this.width=w;
}
public double getWidth(){
return width;
}
public void setLength (double h) {
if(length > 0)
this.length =h;
}
public double getLength () {
return length;
}
}
代码如下:
package zz1;
public class Cuboid {
double rect,height;
double width,length;
Cuboid(double height){
this.height=height;
}
public void getBottomWidth(double width){
if(width > 0)
this.width=width;
}
public void setBottomWidth(){
System.out.println("底面宽是:"+width);
}
public void getBottomLength(double length) {
if(length > 0)
this.length =length;
}
public void setBottomLength (){
System.out.println("底面长是:"+length);
}
public void getVolme(){
double V;
V=width*length*height;
System.out.println("柱形的体积是:"+V);
}
public static void main(String args[]){
Cuboid cub=new Cuboid(20);
cub.getBottomWidth(15);
cub.getBottomLength(17);
cub.setBottomWidth();
cub.setBottomLength();
cub.getVolme();
}
}
结果如下:
【实验题5】 有图形类的父类Shape,参照圆Circle类补充完整正方性Square和三角形Triangle类,并分析运行结果。
class Shape {
void draw() {}
void erase() {}
}
class Circle extends Shape {
void draw() { System.out.println("Circle.draw()");}
void erase() { System.out.println("Circle.erase()");}
}
class Square extends Shape {
void draw() { }
void erase() { }
}
class Triangle extends Shape {
void draw() { }
void erase() { }
}
public class Shapes {
public static Shape randShape() {
switch((int)(Math.random() * 3)) {
default: // To quiet the compiler
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
}
}
public static void main(String[] args) {
Shape[] s = new Shape[3]; // Fill up the array with shapes:
for(int i = 0; i <s.length; i++)
s[i] = randShape(); // Make polymorphic method calls:
for(int i = 0; i <s.length; i++)
s[i].draw();
}
}
补充完整的代码如下:
package zz1;
class Shape {
void draw() {}
void erase() {}
}
class Circle extends Shape {
void draw() { System.out.println("Circle.draw()");}
void erase() { System.out.println("Circle.erase()");}
}
class Square extends Shape {
void draw() { System.out.println("Square.draw()"); }
void erase() { System.out.println("Square.erase()");}
}
class Triangle extends Shape {
void draw() {System.out.println("Triangle.erase()"); }
void erase() {System.out.println("Triangle.erase()"); }
}
public class Shapes {
public static Shape randShape() {
switch((int)(Math.random() * 3)) {
default: // To quiet the compiler
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
}
}
public static void main(String[] args) {
Shape[] s = new Shape[3]; // Fill up the array with shapes:
for(int i = 0; i <s.length; i++)
s[i] = randShape(); // Make polymorphic method calls:
for(int i = 0; i <s.length; i++)
s[i].draw();
}
}
运行结果:
【实验题6】 有两个类:MobileManagement和Mobile,分别描述如图所示两部手机名称及价格,类MobileManagement在包cn.edu.nwsuaf.jp.p3中,而Mobile在包cn.edu.nwsuaf.jp.p3.data中。它们代码如下。运行MobileManagement.java,你应该看到如图所示结果。
[基本要求] 在空白处填写相关代码并修改上面程序,使程序能够显示两部手机的价格和数量,运行结果如图所示。
E365, 1780 RMB M330, 1450 RMB
手机及价格图
程序Mobile.java源代码:
public class Mobile {
/** Holds the name of the mobile. */
private String name;
/** Holds the price of the mobile. */
private float price;
/** Creates a new mobile object. */
public Mobile(String name, float price) {
this.name = name;
this.price = price;
}
/** Gets the name of the mobile. */
public String getName() {
return name;
}
/** Gets the price of the mobile. */
Public float getPrice() {
return price;
}
}
程序MobileManagement.java源代码:
import javax.swing.JOptionPane;
public class MobileManagement {
/** Defines the entry point of the application. */
public static void main(String[] args) {
// Creates two mobile phone objects.
Mobile mobile1 = new Mobile("E365", 1780);
Mobile mobile2 = new Mobile("M330", 1450);
// Displays the two mobile phones in a dialog box.
JOptionPane.showMessageDialog(null, "Mobile phones:\n\n"+mobile1.getName()+"\n"+mobile2.getName());
}
}
实验结果如下截图:
【实验题7】有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类,Product和Store代码如下:
Store.java源代码:
package cn.edu.nwsuaf.jp.p4;
import java.util.Arrays;
import javax.swing.JOptionPane;
import cn.edu.nwsuaf.jp.p4.data.Mobile;
import cn.edu.nwsuaf.jp.p4.data.Mp3Player;
import cn.edu.nwsuaf.jp.p4.data.Product;
public class Store {
//** Defines the entry point of the application. */
public static void main(String[] args) {
// Creates two mobile phone objects.
Mobile mobile1 = new Mobile("China Mobile", "E365",1780);
Mobile mobile2 = new Mobile("China Mobile", "M330",1450);
Mp3Player player1 = new Mp3Player("Meizo X3", 256, 399);
Mp3Player player2 = new Mp3Player("Meizo E5", 512, 580);
Mp3Player player3 = new Mp3Player("Xlive XM MP3 Play",256, 930);
Product[] products={mobile1,mobile2,player1,player2, player3};
Arrays.sort(products);
String text = "";
for(int index = 0; index <products.length; ++index)
text += products[index].toString()+"\n";
// Displays the two mobile phones in a dialog box.
JOptionPane.showMessageDialog(null,"The products
are:\n\n"+text+"\nThere are "+Product.getCount()
+" products.");
}
}
Mp3Player的完整代码如下:
package cn.edu.nwsuaf.jp.p4.data;
public class Mp3Player extends Product
{
private int capacity;
public Mp3Player(String name,int capacity,float price)
{
super(name,price);
this.capacity=capacity;
}
public float getPrice()
{
return price;
}
public String name()
{
return name;
}
public static int count()
{
return count;
}
public String toString()
{
return name+" "+"("+capacity +" "+"MB"+")"+","+" "+price+" "+"RMB";
}
}
Mobile的完整代码如下:
package cn.edu.nwsuaf.jp.p4.data;
public class Mobile extends Product
{
private String string;
public Mobile(String string,String name,float price)
{
super(name,price);
this.string=string;
}
public String getName()
{
return name;
}
public float getprice()
{
return price;
}
public static int getCount()
{
return count;
}
public String toString()
{
return name+" "+"on"+" "+string+","+" "+String.valueOf(price)+" "+"RMB";
}
}
结果如下:
【实验题8】实现一个名为Person的类和它的子类Employee, Manager是Employee的子类,设计一个接口Add用于涨工资,接口中有一个抽象方法addVage()。普通员工一次能涨10%,经理能涨20%。
具体要求如下:
(1)Person类中的属性有:姓名name(String类型),地址address(String类型)并写出该类的构造方法;
(2)Employee类中的属性有:工号ID(String型),工资wage(double类型),工龄(int型),写出该类的构造方法;
(3)Manager类中的属性有:级别level(String类型),写出该类的构造方法;
编写一个测试类,产生一个员工和一个经理并输出其具有的信息, 之后
给这两个人增加工资,然后再次输出信息。
未做
二、 实验/实习总结
展开阅读全文