JAVA编程题 求助
2个回答
展开全部
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener {
private JTextArea area;
private JTextField field;
private JButton button;
private JScrollPane pane;
public Test() {
field = new JTextField();
field.setPreferredSize(new Dimension(300, 50));
area = new JTextArea();
button = new JButton();
button.addActionListener(this);
pane=new JScrollPane(area);
pane.setPreferredSize(new Dimension(300, 300));
button.setText("点击显示文件类容");
button.setPreferredSize(new Dimension(100, 100));
this.setLayout(new FlowLayout());
this.add(field);
this.add(pane);
this.add(button);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400, 500);
}
public static void main(String[] args) {
new Test().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String string = field.getText();
File file = new File(string);
if (!file.exists()) {
JOptionPane.showMessageDialog(null, "请输入正确的文件路径");
return;
}
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
if (fileInputStream != null) {
FileChannel channel = fileInputStream.getChannel();
MappedByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0,
channel.size());
byte[] bs = new byte[(int) channel.size()];
while(buffer.position()<buffer.limit()){
bs[buffer.position()]=buffer.get();
}
channel.close();
area.setText(new String(bs));
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
//这是第二题
public class Test2 {
public static void main(String[] args) {
Changfangxing changfangxing=new Changfangxing();
changfangxing.setHeight(100);
changfangxing.setWidth(50);
System.out.println("长方形 面积"+changfangxing.mianJi());
System.out.println("长方形 周长"+changfangxing.zhouChang());
//使用多态的性质
Changfangxing zhengfangxing=new Zhengfangxing();
zhengfangxing.setWidth(26);
System.out.println("正方形 面积"+changfangxing.mianJi());
System.out.println("正方形 周长"+changfangxing.zhouChang());
Yuan yuan=new Yuan();
yuan.setR(15);
System.out.println("圆形 面积"+yuan.mianJi());
System.out.println("圆形 周长"+yuan.zhouChang());
}
}
//长方形
class Changfangxing{
protected double width;
protected double height;
/**
* 计算周长 (长+宽)的二倍
* @return
*/
public double zhouChang(){
return (width+height)*2;
}
/**
* 计算面积 长*宽
* @return
*/
public double mianJi(){
return width*height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
//正方形 继承长方形
class Zhengfangxing extends Changfangxing{
/**
* 正方形计算周长 边长*4就可以了
*/
@Override
public double zhouChang(){
return width*4;
}
/**
* 正方形计算面积 边长的平方就可以了
*/
public double mianJi(){
return width*width;
}
/**
* 正方形不需要 这个方法 故注释掉
*/
@Deprecated
public double getHeight() {
return height;
}
@Deprecated
public void setHeight(double height) {
this.height = height;
}
}
//圆
class Yuan{
private double r;
/**
* 计算周长 π*2*r
* @return
*/
public double zhouChang(){
return Math.PI*2*r;
}
/**
* 计算面积 π*r*r
* @return
*/
public double mianJi(){
return Math.PI*r*r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
//这是第三题
展开全部
第一题:
public class Test
{
public static void main(String[] args)
{
int[] array = {10, 33, 2, 11, 4, 5};
int arrayLength = array.length;
int maxValue = array[0];
int minValue = array[0];
int sum = 0;
for (int i=0; i<arrayLength; i++)
{
if (maxValue < array[i])
{
maxValue = array[i];
}
if (minValue > array[i])
{
minValue = array[i];
}
}
System.out.println("最小数" + minValue);
System.out.println("最大数" + maxValue);
}
}
public class Test
{
public static void main(String[] args)
{
int[] array = {10, 33, 2, 11, 4, 5};
int arrayLength = array.length;
int maxValue = array[0];
int minValue = array[0];
int sum = 0;
for (int i=0; i<arrayLength; i++)
{
if (maxValue < array[i])
{
maxValue = array[i];
}
if (minValue > array[i])
{
minValue = array[i];
}
}
System.out.println("最小数" + minValue);
System.out.println("最大数" + maxValue);
}
}
追问
谢谢 后面两题呢,关键是后面的T T 大神 快出现T T
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询