JAVA实验报告1.DOC1

JAVA实验报告1.DOC1
JAVA实验报告1.DOC1

Java与面向对象程序设计上机报告(第六周)

第一题:

PP4.4设计并实现类Book,所包含的实例数据表示书名、作者、出版社及版权日期。定义Book构造方法接收和初始化这些数据,并定义接收和设置这些数据的方法。定义toString 方法返回多行且格式美观的描述书的字符串。创建驱动类Bookshelf,该类的main方法实例化并更新若干个Book对象。

private String publisher;

private String publishDate;

public Book()

{

bookName="Java程序设计教程";

author="John Lewis and William Loftus";

publisher="电子工业出版社";

publishDate="2010年12月第3版";

}

public Book(String bookName,String author,String publisher,String publishDate)//构造方法

{

this.setBookName(bookName);

this.setAuthor(author);

this.setPublisher(publisher);

this.setPublishDate(publishDate);

}

public void setBookName(String bookName)

{

this.bookName=bookName;

}

public String getBookName()

{

return bookName;

}

public void setAuthor(String author)

{

this.author=author;

}

public String getAuthor()

{

return author;

}

public void setPublisher(String publisher)

{

this.publisher=publisher;

}

第二题:

PP4.12修改本章中Fahrenheit程序,增加一个按钮,当该按钮按下时触发温度转换计算。也就是说,用户现在可以选择操作方式,在单行文本框中按回车键或点击按钮都可以进行温

import javax.swing.JFrame;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Fahrenheit

{

public static void main(String[]args)

{

JFrame frame=new JFrame("Fahrenheit");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new FahrenheitPanel());

frame.pack();

frame.setVisible(true);

}

}

class FahrenheitPanel extends JPanel

{

private JLabel inputLabel,outputLabel,resultLabel;

private JTextField fahrenheit;

private JButton push;

public FahrenheitPanel()

{

inputLabel=new JLabel("请输入一个华氏温度值:");

fahrenheit=new JTextField(5);

push=new JButton("点击转换温度");

outputLabel=new JLabel("与其等价的摄氏温度值为:");

resultLabel=new JLabel("---");

push.addActionListener(new ButtonListener());

add(inputLabel);

add(fahrenheit);

add(push);

add(outputLabel);

add(resultLabel);

setPreferredSize(new Dimension(350,75));

setBackground(Color.yellow);

}

private class ButtonListener implements ActionListener

运行情况及结果截图:

相关主题
相关文档
最新文档