JAVA实验代码(粘贴)

发布时间:2014-05-12 21:17:39

实验二
GISMap类:

import java.io.*;

import java.text.SimpleDateFormat;

import java.util.*;

/**

* 类名: GISMap

* 说明:设计一个GISMap类,要求进行简单定义、实现,输入输出。

*/

/**

* @author dlxy

*

*/

public class GISMap {

/**

* 地图名

*/

private String strName;//地图名

private String strAuthor;//作者

private String strNotes;//注释

private Date dtProductDate;//制作日期

public GISMap()

{

strName = "Untitled";

strAuthor = "NA";

strNotes = "";

dtProductDate = new Date();

}

public void finalize()

{

strName = "";

strAuthor = "";

strNotes = "";

dtProductDate = null;

}

/**

* 设置地图信息

* @param strMapName 地图名字

* @param strMapAuthor 地图制作者

* @param strMapNotes 地图注释

* @param dtMapProductDate 地图制作日期

*/

public void setMapInfo(String strMapName, String strMapAuthor,String strMapNotes,Date dtMapProductDate)

{

strName = strMapName;

strAuthor = strMapAuthor;

strNotes = strMapNotes;

dtProductDate = dtMapProductDate;

}

/**

* 获取地图名字

* @return 地图名

*/

public String getMapName()

{

return strName;

}

public String getMapAuthor()

{

return strAuthor;

}

public String getMapNotes()

{

return strNotes;

}

public Date getMapProductDate()

{

return dtProductDate;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

try

{

GISMap oneMap = new GISMap();

System.out.println("请输入地图名称");

String strMapName = Keyboard.getString();

System.out.println("请输入制作者");

String strAuthor = Keyboard.getString();

System.out.println("请输入地图说明");

String strNotes = Keyboard.getString();

System.out.println("请输入制作日期");

String strDate = Keyboard.getString();

@SuppressWarnings("deprecation")

java.util.Date oneDate = new Date(strDate);

oneMap.setMapInfo(strMapName, strAuthor, strNotes, oneDate);

System.out.println(oneMap.getMapProductDate().toString());//.toLocaleString());

//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//System.out.println(df.format(new Date()));

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

}

Keyboard

import java.io.*;

public class Keyboard

{

public Keyboard()

{

}

static BufferedReader inputStream = new BufferedReader

(new InputStreamReader(System.in));

public static char getChar()

{

try

{

return ((char)inputStream.read());

}

catch(Exception e)

{

return '0';

}

}

//获取整数

public static int getInteger()

{

try

{

return (Integer.valueOf(inputStream.readLine().trim()).intValue());

}

catch(Exception e){

e.printStackTrace();

return 0;

}

}

//使用byte数组转化获取键盘输入的整数值

public static int getInt()

{

byte buf[] = new byte[50]; //50个字节的数组用于存字符串

int anInt = 0; //局部变量要初始化

try

{

System.in.read(buf); //从键盘读一个数字串保存于buf

String str = new String(buf); //buf 转换成String对象 str

//(ASCII字符串转换成Unicode码串)

anInt = Integer.parseInt(str.trim()); //数字串转换成整数

}

catch (Exception e) {

e.printStackTrace();

return 0;

} //catch 语句,Exception为异常类

return anInt;

}

public static int getInt2()

{

try

{

return Integer.parseInt(inputStream.readLine());

}

catch(IOException e)

{

e.printStackTrace();

return 0;

}

}

//获取字符串

public static String getString()

{

try

{

return (inputStream.readLine());

}

catch(IOException e)

{

e.printStackTrace();

return "0";

}

}

public static void flush()

{

System.out.flush();

}

public static void main(String[] args)

{

Keyboard keyboard = new Keyboard();

}

}

实验三

/**

*

Title:

*

*

Description:

*

设计思路:

*

1. 设计ShapefileGeoDatabase格式的地图文档类,继承自GISDocument

*

2. 或者设计Layer(图层)作为基类,设计FeatureLayer(要素图层)和TinLayertin图层)

*

*

Copyright: Copyright (c) 2010

*

*

Company:

*

* @author not attributable

* @version 1.0

*/

GISDocument类:

public abstract class GISDocument {

public String docName;

public String docFormat;

public abstract void Save();

public abstract void SaveAs();

public abstract void Print();

public GISDocument() {

}

public static void main(String[] args) {

//GISDocument gisdocument = new GISDocument();

}

}

GeodatabaseDocument类:

public class GeodatabaseDocument extends GISDocument {

public GeodatabaseDocument() {

}

/**

* Print

*

* @todo Implement this exp_03_mg.GISDocument method

*/

public void Print() {

System.out.println("GeodatabaseDocument Printing....");

}

/**

* Save

*

* @todo Implement this exp_03_mg.GISDocument method

*/

public void Save() {

}

/**

GISMap类:

import java.util.*;

import java.lang.*;

import java.awt.*;

/**

*

Title: 设计地图类

*

*

Description: 实现地图的基本功能特性抽象、模拟实现

*

设计思路

* 1.抽象地图基本方法编辑、保存、显示并提供地图信息输出

* 2.根据公有的方法设计基本字段

*

Copyright: Copyright (c) 2010

*

*

Company: geo.sicnu.edu.cn

*

* @author mg

* @version 0.1

*/

public class GISMap {

//编辑地图

public void EditMap()

{

System.out.println("Editing Map");

}

//保存地图

public void SaveMap()

{

System.out.println("Save Map");

}

//显示地图

public void DisplayMap()

{

System.out.println("Display Map");

}

//输出地图基本信息

public void ShowMapInfo()

{

System.out.println("Show Map Info:");

System.out.println(title);

System.out.println(author);

System.out.println(productDate.toString());

System.out.println("Height: " + scale.height + " Width: " + scale.width);

System.out.println(CoordinateSystem);

}

private String title;

private String author;

private Date productDate;

private Rectangle scale;//地图区域

private String CoordinateSystem;//坐标系统

public GISMap() {

title = "Untitled Map";

author = "NA";

productDate = new Date();

scale = new Rectangle();

CoordinateSystem = "WGS 1984";

}

public GISMap(String strTitle,

String strAuthor,

String strCoordinateSystem)

{

title = strTitle;

author = strAuthor;

productDate = new Date();

scale = new Rectangle();

CoordinateSystem = strCoordinateSystem;

}

protected void finalize() throws Throwable

{

productDate = null;

scale = null;

super.finalize();

}

public static void main(String[] args) {

GISMap gismap = new GISMap("Chengdu Map","mg","WGS 1984");

gismap.EditMap();

gismap.SaveMap();

gismap.DisplayMap();

gismap.ShowMapInfo();

}

}

Keyboard类:

public class Keyboard {

public Keyboard() {

}

static BufferedReader inputStream = new BufferedReader

(new InputStreamReader(System.in));

public static char getChar()

{

try{

return ((char)inputStream.read());

}

catch(Exception e)

{

return '0';

}

}

//获取整数

public static int getInteger()

{

try{

return (Integer.valueOf(inputStream.readLine().trim()).intValue());

}catch(Exception e){

e.printStackTrace();

return 0;

}

}

//获取double

public static double getDouble()

{

try{

return Double.parseDouble(inputStream.readLine());

}catch(Exception e){

e.printStackTrace();

return 0;

}

}

//使用byte数组转化获取键盘输入的整数值

public static int getInt() {

byte buf[] = new byte[50]; //50个字节的数组用于存字符串

int anInt = 0; //局部变量要初始化

try {

System.in.read(buf); //从键盘读一个数字串保存于buf

String str = new String(buf); //buf 转换成String对象 str

//(ASCII字符串转换成Unicode码串)

anInt = Integer.parseInt(str.trim()); //数字串转换成整数

} catch (Exception e) {

e.printStackTrace();

return 0;

} //catch 语句,Exception为异常类

return anInt;

}

public static int getInt2()

{

try{

return Integer.parseInt(inputStream.readLine());

}catch(IOException e){

e.printStackTrace();

return 0;

}

}

//获取字符串

public static String getString()

{

try{

return (inputStream.readLine());

}catch(IOException e){

e.printStackTrace();

return "0";

}

}

public static void flush()

{

System.out.flush();

}

public static void main(String[] args) {

Keyboard keyboard = new Keyboard();

}

}

shapefileDocument类:

public class ShapefileDocument extends GISDocument {

public ShapefileDocument() {

}

/**

* Print

*

*/

public void Print() {

System.out.println("ShapefileDocument Printing....");

}

/**

* Save

*

*/

public void Save() {

}

/**

* SaveAs

*

*/

public void SaveAs() {

}

public static void main(String[] args) {

ShapefileDocument shapefiledocument = new ShapefileDocument();

}

}

Test类:

public class Test {

static void MyPrint(GISDocument doc)

{

doc.Print();

}

public static void main(String[] args) {

MyPrint(new GeodatabaseDocument());

MyPrint(new ShapefileDocument());

}

}

实验四

public interface IFeatureWorkspace {

String GetName();

}

public interface IWorkspaceFactory {

public IFeatureWorkspace OpenFromFile();

}

public class RasterWorkspaceClass implements IFeatureWorkspace {

@Override

public String GetName()

{

return "RasterWorkspace";

}

}

public class RasterWorkspaceFactroyClass

implements IWorkspaceFactory{

public RasterWorkspaceFactroyClass() {

}

public IFeatureWorkspace OpenFromFile()

{

IFeatureWorkspace pFW = new RasterWorkspaceClass();

System.out.println("Calling RasterWorkspaceFactroyClass.OpenFromFile()");

return pFW;

}

}

public class ShapefileWorkspaceClass implements IFeatureWorkspace {

@Override

public String GetName() {

// TODO Auto-generated method stub

return "ShapefileWorkspace";

}

}

public class ShapefileWorkspaceFactoryClass implements IWorkspaceFactory {

public ShapefileWorkspaceFactoryClass() {

}

/**

* OpenFromFile

*

* @return IFeatureWorkspace

* @todo Implement this exp_04_mg.IWorkspaceFactory method

*/

public IFeatureWorkspace OpenFromFile() {

IFeatureWorkspace pFW = new ShapefileWorkspaceClass();

System.out.println("Calling ShapefileWorkspaceFactory.OpenFromFile()");

return pFW;

}

public static void main(String[] args) {

}

}

public class TestWSF {

public static void main(String[] args) {

IWorkspaceFactory pWSF =

new RasterWorkspaceFactroyClass();

IFeatureWorkspace pFWS =

pWSF.OpenFromFile();

System.out.println(pFWS.GetName());

pWSF = new ShapefileWorkspaceFactoryClass();

pFWS = pWSF.OpenFromFile();

System.out.println(pFWS.GetName());

}

}

实验五

TestWindouws

import javax.swing.JFrame;

import java.awt.FlowLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class TestWindows extends JFrame {

public TestWindows() {

getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

JButton btnNewButton_3 = new JButton("ShowMessage");

btnNewButton_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

javax.swing.JOptionPane.showMessageDialog

(null, "Test");

}

});

getContentPane().add(btnNewButton_3);

JButton btnNewButton_2 = new JButton("New button");

getContentPane().add(btnNewButton_2);

JButton btnNewButton_4 = new JButton("New button");

getContentPane().add(btnNewButton_4);

JButton btnNewButton_1 = new JButton("New button");

getContentPane().add(btnNewButton_1);

JButton btnNewButton = new JButton("New button");

getContentPane().add(btnNewButton);

JButton btnNewButton_5 = new JButton("New button");

getContentPane().add(btnNewButton_5);

}

}

UseWindows

public class UseWindow {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

TestWindows myWin = new TestWindows();

myWin.setBounds(100, 100, 400, 300);

myWin.setVisible(true);

}

}

实验六

SimpleGui

import javax.swing.*;

import javax.swing.JTextField;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.*;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.MouseWheelEvent;

import java.awt.event.MouseWheelListener;

/**

*

Title: 窗口设计

*

*

Description: 使用两种布局模式设计窗口程序

*

*

Copyright: Copyright (c) 2010

*

*

Company:

*

* @author not attributable

* @version 1.0

*/

public class SimpleGui extends JFrame {

public SimpleGui() {

try {

jbInit();

} catch (Exception ex) {

ex.printStackTrace();

}

}

public static void main(String[] args) {

SimpleGui simplegui = new SimpleGui();

simplegui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

simplegui.setBounds(200,200,400,300);

simplegui.setVisible(true);

simplegui.setSize(100,100);

}

private void jbInit() throws Exception {

jButton1.addActionListener(new SimpleGui_jButton1_actionAdapter(this));

this.getContentPane().setLayout(null);

jTextField1.setMaximumSize(new Dimension(100, 20));

jTextField1.setMinimumSize(new Dimension(50, 20));

jTextField1.setDisabledTextColor(Color.black);

jTextField1.setSelectedTextColor(Color.white);

jTextField1.setBounds(new Rectangle(53, 18, 90, 20));

jPanel1.setBackground(SystemColor.inactiveCaptionBorder);

jPanel1.setToolTipText("");

jPanel1.setBounds(new Rectangle(106, 9, 197, 150));

jPanel1.addMouseWheelListener(new SimpleGui_jPanel1_mouseWheelAdapter(this));

jPanel1.setLayout(null);

jPanel2.setBackground(SystemColor.inactiveCaptionBorder);

jPanel2.setBounds(new Rectangle(28, 176, 349, 102));

jPanel2.setLayout(flowLayout1);

jButton2.setText("jButton2");

jTextField3.setText("jTextField3");

jTextField4.setText("jTextField4");

jTextField2.setBounds(new Rectangle(60, 103, 77, 20));

jButton1.setBounds(new Rectangle(58, 54, 88, 23));

jPanel1.add(jButton1);

jPanel1.add(jTextField1);

jPanel1.add(jTextField2);

this.getContentPane().add(jPanel2);

jPanel2.add(jTextField3);

jPanel2.add(jButton2);

jPanel2.add(jTextField4);

this.getContentPane().add(jPanel1);

jButton1.setText("Commit");

}

JTextField jTextField1 = new JTextField();

JButton jButton1 = new JButton();

JTextField jTextField2 = new JTextField();

JPanel jPanel1 = new JPanel();

JPanel jPanel2 = new JPanel();

FlowLayout flowLayout1 = new FlowLayout();

JButton jButton2 = new JButton();

JTextField jTextField3 = new JTextField();

JTextField jTextField4 = new JTextField();

int i = 0;

//按钮点击操作

//这里实现把上面的文本框jTextField1输入的内容显示在下面的对话框jTextField2

public void jButton1_actionPerformed(ActionEvent e) {

jTextField2.setText(jTextField1.getText());

jTextField2.setBackground(Color.GREEN);

}

public void jPanel1_mouseWheelMoved(MouseWheelEvent e) {

i++;

jPanel1.setBackground(Color.getColor("test",i));

}

}

class SimpleGui_jPanel1_mouseWheelAdapter implements MouseWheelListener {

private SimpleGui adaptee;

SimpleGui_jPanel1_mouseWheelAdapter(SimpleGui adaptee) {

this.adaptee = adaptee;

}

public void mouseWheelMoved(MouseWheelEvent e) {

adaptee.jPanel1_mouseWheelMoved(e);

}

}

class SimpleGui_jButton1_actionAdapter implements ActionListener {

private SimpleGui adaptee;

SimpleGui_jButton1_actionAdapter(SimpleGui adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton1_actionPerformed(e);

}

}

TestThread

import javax.swing.JFrame;

import javax.swing.JButton;

import java.awt.Rectangle;

import javax.swing.JTextArea;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.JScrollPane;

import java.lang.*;

/**

*

Title:

*

*

Description:

*

*

Copyright: Copyright (c) 2010

*

*

Company:

*

* @author not attributable

* @version 1.0

*/

public class TestThread extends JFrame {

private boolean bStart = false;

private boolean bPause = false;

public TestThread() {

try {

jbInit();

} catch (Exception ex) {

ex.printStackTrace();

}

}

public static void main(String[] args) {

TestThread testthread = new TestThread();

testthread.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

testthread.setBounds(200,200,400,300);

testthread.setVisible(true);

}

private void jbInit() throws Exception {

this.getContentPane().setLayout(null);

jButton1.setBounds(new Rectangle(49, 19, 83, 23));

jButton1.setText("Start");

jButton1.addActionListener(new TestThread_jButton1_actionAdapter(this));

jButton2.addActionListener(new TestThread_jButton2_actionAdapter(this));

jButton3.addActionListener(new TestThread_jButton3_actionAdapter(this));

jTextArea1.setLineWrap(true);

jScrollPane1.getViewport().setBackground(Color.lightGray);

jScrollPane1.setBounds(new Rectangle(44, 70, 308, 171));

jTextArea1.setText("计数");

jButton3.setBounds(new Rectangle(270, 20, 69, 22));

jButton3.setText("Stop");

this.getContentPane().add(jScrollPane1);

jScrollPane1.getViewport().add(jTextArea1);

this.getContentPane().add(jButton1);

this.getContentPane().add(jButton3);

this.getContentPane().add(jButton2);

jButton2.setBounds(new Rectangle(160, 21, 86, 23));

jButton2.setText("Pause");

jButton3.setEnabled(false);

}

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

JButton jButton3 = new JButton();

JTextArea jTextArea1 = new JTextArea();

JScrollPane jScrollPane1 = new JScrollPane();

Thread firstThread = null;

Thread secondThread = null;

JPanel jPanel1 = new JPanel();

public void jButton1_actionPerformed(ActionEvent e) {

//开始启动线程

jTextArea1.setText("");

bStart = true;

firstThread = new Thread(new MyThread());

firstThread.start();

secondThread = new Thread(new MyThread());

secondThread.start();

jButton1.setEnabled(false);

jButton3.setEnabled(true);

}

public void jButton2_actionPerformed(ActionEvent e) {

bPause = !bPause;

String strMsg = bPause ? "Continue":"Pause";

jButton2.setText(strMsg);

try {

if(bPause)

{

firstThread.wait();

secondThread.wait();

}

else

{

firstThread.notify();

secondThread.notify();

}

}

catch (InterruptedException ex)

{

ex.printStackTrace();

}

}

public void jButton3_actionPerformed(ActionEvent e) {

bStart = false;

bPause = false;

jButton1.setEnabled(true);

jButton3.setEnabled(false);

firstThread.interrupt();

secondThread.interrupt();

}

class MyThread implements Runnable{

private Integer i = 0;

public MyThread() {

}

public void run() {

while(bStart)

{

if(!bPause)

{

i++;

jTextArea1.append(i.toString() + "\n");

}

}

}

public int getI() {

return i;

}

}

}

class TestThread_jButton3_actionAdapter implements ActionListener {

private TestThread adaptee;

TestThread_jButton3_actionAdapter(TestThread adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton3_actionPerformed(e);

}

}

class TestThread_jButton2_actionAdapter implements ActionListener {

private TestThread adaptee;

TestThread_jButton2_actionAdapter(TestThread adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton2_actionPerformed(e);

}

}

class TestThread_jButton1_actionAdapter implements ActionListener {

private TestThread adaptee;

TestThread_jButton1_actionAdapter(TestThread adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton1_actionPerformed(e);

}

}

实验七

* @param x 被加数

* @param y 加数

* @return xy的和

*/

public float plus(float x, float y)

{

return x + y;

}

/** *//**

* 减法运算

* @param x 被减数

* @param y 减数

* @return xy之差

*/

public float minus(float x, float y)

{

return x-y;

}

/** *//**

* 乘法运算

* @param x 被乘数

* @param y 乘数

* @return xy的乘积

*/

public float multiply(float x, float y)

{

return x * y ;

}

/** *//**

     * 除法运算

     * @param x 被除数

     * @param y 除数

     * @return xy的商

     */

public float divide(float x, float y)

{

return x / y ;

}

JAVA实验代码(粘贴)

相关推荐