導航:首頁 > 編程語言 > java程序實驗報告

java程序實驗報告

發布時間:2022-10-05 02:29:54

java實驗報告

實驗1
Java開發工具及程序設計初步
實驗目的

能夠搭建Java的開發環境

熟悉常用的JDK操作命令的使用

熟悉Java程序的編寫和編譯過程

了解常用Java開發工具的使用
實驗內容
實驗練習1:搭建和使用JDK
1.JDK的下載和安裝
JDK是SUN公司免費在網上發布的,我們可以進入SUN公司的網站(http://java.sun.com)下載並安裝。JDK安裝完成後,進行如下操作:
(1)查看並寫出JDK的安裝目錄______________________________;
(2)查看主目錄bin文件夾中常用的JDK命令文件,寫出java、javac等JDK命令所在的文件目錄_________________________________;
(3)在Windows中打開一個命令行窗口,打開命令行窗口的方法有兩種:
•選擇「開始」→「所有程序」→「附件」→「命令提示符」
•選擇「開始」→「運行…」命令,在打開的「運行」窗口中輸入「cmd」命令並「確定」。
(4)在打開的命令行窗口中,分別執行java和javac命令,如:C:\>java
查看執行結果,看是否輸出如下的java命令的幫助信息(如圖1-1所示)。
圖1-1:java命令的幫助信息
C:\>javac
查看執行結果,看是否輸出如下的javac命令的幫助信息(如圖1-2所示)。
圖1-2:javac命令的幫助信息
如果上面的信息均能輸出,說明JDK的安裝成功,基本上可正常使用。
2.操作系統環境變數的使用
操作系統的環境變數是在操作系統中定義的變數,可被該操作系統上運行的程序訪問。JDK中主要有兩個相關的操作系統環境變數,即path和classpath。
(1)查看操作系統環境變數
•在命令行窗口中執行set命令
如:C:\>set
查看set命令的輸出結果,在set命令的輸出結果中可看到許多等式,其中等號的左邊是環境變數的名稱,等號的右邊是環境變數的取值。
命令:set
<環境變數名>,可查看指定的某個環境變數的設置情況。
執行命令:C:\>set
path,查看path變數的設置情況。
•在「環境變數」窗口中查看環境變數
用滑鼠右鍵單擊「我的電腦」,選擇「屬性」命令,在打開的「系統屬性」對話框中選擇「高級」→「環境變數」,打開「環境變數」窗口。
在「環境變數」窗口中,有用戶變數和系統變數兩種環境變數。其中,用戶環境變數只對某個用戶起作用;系統環境變數對該系統上的所有用戶都有效。
當我們以某一用戶身份登錄Windows時,它所使用的變數是系統變數和用戶變數的總和。
如果一個變數在系統變數窗口和用戶變數窗口都進行了設置,那麼這個變數的取值在一般情況下為系統變數的取值與用戶變數的取值相加;但有時也會出現用戶變數的取值覆蓋系統變數取值的情況。不管是什麼情況,最終都以在命令行窗口中通過set命令查看到的結果為准。
(2)設置系統的環境變數
•在命令窗口中執行set命令
格式:set
變數名=[變數值]
執行如下命令,查看執行結果。
C:\>set
Xx=aa
//新建一個環境變數並設置其取值
C:\>set
Xx
//查看該環境變數
C:\>set
Xx=
//取消環境變數的設置
C:\>set
//查看環境變數
注意:在命令行窗口中設置的環境變數,只在該命令行窗口中起作用。
•在「環境變數」窗口中進行設置
可使用「新建」或「編輯」按鈕。
注意:①
在「環境變數」窗口中設置的環境變數對以後所有的應用程序和命令行窗口都起作用,但不會影響原來運行的應用程序和啟動的命令行窗口。

在「用戶環境變數」窗口中進行的設置需要在注銷重新登錄後才起作用。
(3)path環境變數的作用和設置
環境變數path的值指定了JDK命令搜索路徑,即當我們要執行某個命令時,操作系統首先會在當前目錄下查找這個命令,如果當前目錄下沒有這個命令程序,操作系統會沿著path環境變數中所指定的目錄依次去查找這個命令文件,並以最先找到的為准。
觀察path變數的取值情況,path變數可以有多個取值,每個值代表一條搜索路徑,它們之間用「;」分隔。
在命令行窗口中執行如下命令:
C:\>set
path
//查看path環境變數
C:\>set
path=
或set
path=C:\
//取消path變數的設置或重新設置path的值
C:\>set
path
C:\>java
或javac
//執行java或javac命令
會出現如下錯誤信息(如圖1-3所示):
圖1-3:錯誤信息
分析錯誤的原因:_____________________________________________________
改正的方法:__________________________________
實驗練習2:Java程序的編寫與編譯過程
1.一個簡單的Java程序的編寫與編譯
第一步:先在磁碟上創建保存程序文件的目錄(文件夾),如:D:\javawork\lesson1。
第二步:在「記事本」中創建並編輯Java源程序文件Test1.java,程序代碼如下:
class
Test1
{
}
保存文件,擴展名為.java。
第三步:在命令行窗口中,對源文件Test1.java進行編譯。
可參考如下命令:
如果當前盤當前目錄為C:\,先將當前目錄設置為D:\javawork\lesson1。
C:\>d:
D:\>cd
ja*w*
D:\javawork>cd
le*
D:\javawork\lesson1>javac
Test1.java
如果編譯成功,則不出現任何提示,回到命令提示符狀態下。打開保存源文件的文件夾,查看編譯生成的位元組碼文件Test1.class。
第四步:運行位元組碼文件Test1.class。
D:\javawork\lesson1>java
Test1
執行結果出現異常,如圖1-4所示。
圖1-4:執行出現異常
出現異常的原因是:缺少執行應用程序的入口main方法。main方法的聲明必須是public
static
void
main(String
args[]){}。修改源程序代碼並重新編譯,源程序代碼如下:
class
Test1
{
public
static
void
main(String
args[])
{
}
}
編譯完成後,執行位元組碼文件。
在main方法中,添加輸出的語句並重新編譯執行,程序代碼如下:
class
Test1
{
public
static
void
main(String
args[])
{
System.out.println("Hello
World!");
}
}
輸出結果如圖1-5所示。
圖1-5:程序執行結果
2.classpath環境變數的作用與設置
環境變數calsspath用於指定Java類的路徑,即設置classpath的作用是告訴Java運行系統的類載入器到哪裡去尋找要執行的類。
在前面打開的命令行窗口中繼續執行以下命令:
D:\>java
Test1
執行結果如圖1-6所示:
圖1-6:改變當前目錄後重新執行的結果
會發現程序執行出現異常:無法找到Test1類。說明在classpath變數中沒有將Test1.class文件所在的目錄設置為搜索路徑,在命令行窗口中執行命令:
D:\>set
classpath=%classpath%;d:\javawork\lesson1
重新執行程序,則運行結果正常。
調試練習
本節中的程序Test2.java不能正確地編譯,修改所有的語法錯誤,使程序能夠成功編譯。程序編譯之後,執行程序,比較輸出結果與示例輸出,然後排除所有可能的邏輯錯誤,示例輸出演示了代碼更正後程序的正確輸出結果。
1.示例輸出
2.錯誤的代碼
//Test2.java
import
javax.swing.JOptionPane;
public
class
test2
{
public
static
void
main(string
args[])
{
JOptionPane.showMessageDialog(null,"Hello")
}
}

㈡ Java實驗報告

我剛做了一個,呵呵

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Calculator extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;
// public static final int EXIT_ON_CLOSE = 3;

private Button plus=new Button("+"); //運算符
private Button minus=new Button("-");
private Button multip=new Button("*");
private Button divise=new Button("/");

private Panel pan=new Panel();
private JLabel opl1 = new JLabel("操作數1");
private JLabel opl2 = new JLabel("操作數2");
private JLabel resl = new JLabel("結果:");

private TextField op1=new TextField(10);
private TextField op2=new TextField(10);
private TextField result=new TextField(10);

public Calculator ()
{
super("Calculator");
initialize();
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try
{
if(e.getSource()==plus)
{

if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作數不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1+oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()==minus)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作數不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1-oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()== multip)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作數不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1*oprt2;
result.setText(temp.toString());
}
}
if(e.getSource()==divise)
{
if(op1.getText().equals("")||op2.getText().equals(""))
result.setText("操作數不完整");
else
{
int oprt1 = Integer.parseInt(op1.getText());
int oprt2 = Integer.parseInt(op2.getText());
Integer temp = oprt1/oprt2;
result.setText(temp.toString());
}
}

}
catch(Exception ee)
{
result.setText(ee.getMessage());
ee.printStackTrace();
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new Calculator();
}

}

㈢ java程序設計實驗報告怎麼寫

實驗報告書寫要求

實驗報告原則上要求學生手寫,要求書寫工整。

若因課程特點需列印的,標題採用四號黑體,正文採用小四號宋體,單倍行距。紙張一律採用A4的紙張。

實驗報告書寫說明

實驗報告中實驗目的和要求、實驗儀器和設備、實驗內容與過程、實驗結果與分析這四項內容為必需項。


㈣ java實驗報告總結

源代碼:
public class Exe1_1 {
public static void main (String args[]){
byte a1=127;
int a2=12345;
long a3=123456789L;
float a4=1.23e-5f;
double a5=1.4567e3;
char a6=65;
boolean a7=true;
System.out.println("a1="+a1+"\na2="+a2+"\na3="+a3+"\na4="+a4+"\na5="+a5+"\na6="+a6+"\na7="+a7);
}
}運行結果:
2.編寫Java小應用程序,輸出兩行字元串:「Java很有趣。」和「努力學習Java編程。」,輸出的起點坐標是(20,20),行距是50像素。源程序代碼:
import java.awt.Graphics;
import java.applet.Applet;
public class Exe1_2 extends Applet{
public void paint(Graphics g){
g.drawString("Java很有趣。",20,20);
g.drawString("努力學習Java編程。",20,70);
}
}運行情況:
3.使用算術運算符得到一個4位十進制數的各位數字並輸出,然後輸出該數的逆序數和各位數字平方後相加的和。
源程序代碼:
public class Exe1_3 {
public static void main (String args[]){
int n=3756,a,b,c,d,n1;
a=n/1000;
b=(n-1000*a)/100;
d=n%10;
c=(n%100-d)/10;
System.out.println("3756的逆序數為:"+d+" "+c+" "+b+" "+a);
System.out.print("各位數字平方後相加的和為:");
System.out.print(a*a+b*b+c*c+d*d);
}
}
運行結果:

三、實驗總結(是否完成實驗、實驗過程中的問題以及解決方法分析等)
本次是我第一次使用JAVA來進行編程,感覺很是不一樣,在前兩節課中我們學習了有關Java的一些簡單知識。然後這次的實驗是對最初的一種練習。
在第一題中,我們重要是認識java的運行環境,並且了解各種變數的數據類型。只要通過簡單的語句就可以通過運行。其中出現了一些單詞的拼寫錯誤,這些是可以修改正確的。第二題我們練習的十一個小的applet應用程序,需要設置一個網頁來顯示,這是一個很不一樣的地方,最後成功測試,裡面需要注意的是顯示位置的問題。第三題我們做的是一個有演算法要求的解決問題,在測試中出現了一點小問題,程序的演算法出了錯。還有一個問題是有關於顯示問題,最後的數據輸出需要用到兩行分別顯示字元和數據,這樣才不會出錯。

㈤ java實驗報告,沒時間寫了 ,一百分

package cn.rain.form;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Moble extends JFrame {

private static final long serialVersionUID = 1L;
private JTextField txtView;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Moble moble = new Moble();
moble.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Create the frame
*/
public Moble() {
setTitle("Moble");
setBounds(100, 100, 200, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);

txtView = new JTextField();
panel.setLayout(null);
panel.add(txtView);

final JButton btn1 = new JButton();
btn1.addActionListener(new ActionListener() {
//加監聽
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("1"));
}
});
btn1.setText("1");
btn1.setBounds(0, 73, 59, 23);
panel.add(btn1);

final JButton btn2 = new JButton();
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//文本顯示
txtView.setText(txtView.getText().concat("2"));
}
});
btn2.setText("2");
btn2.setBounds(68, 73, 59, 23);
panel.add(btn2);

final JButton btn3 = new JButton();
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("3"));
}
});
btn3.setText("3");
btn3.setBounds(133, 73, 59, 23);
panel.add(btn3);

final JButton btn4 = new JButton();
btn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("4"));
}
});
btn4.setText("4");
btn4.setBounds(0, 102, 59, 23);
panel.add(btn4);

final JButton btn5 = new JButton();
btn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("5"));
}
});
btn5.setText("5");
btn5.setBounds(65, 102, 59, 23);
panel.add(btn5);

final JButton btn6 = new JButton();
btn6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("6"));
}
});
btn6.setText("6");
btn6.setBounds(133, 102, 59, 23);
panel.add(btn6);

final JButton btn7 = new JButton();
btn7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("7"));
}
});
btn7.setText("7");
btn7.setBounds(0, 131, 59, 23);
panel.add(btn7);

final JButton btn8 = new JButton();
btn8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("8"));
}
});
btn8.setText("8");
btn8.setBounds(65, 131, 59, 23);
panel.add(btn8);

final JButton btn9 = new JButton();
btn9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("9"));
}
});
btn9.setText("9");
btn9.setBounds(133, 131, 59, 23);
panel.add(btn9);

final JButton btn0 = new JButton();
btn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText(txtView.getText().concat("0"));
}
});
btn0.setText("0");
btn0.setBounds(68, 160, 57, 23);
panel.add(btn0);

txtView = new JTextField();
txtView.setBounds(59, 24, 90, 21);
panel.add(txtView);

final JButton btnSend = new JButton();
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//如果撥過號了,就不能再撥
if(txtView.getText().indexOf("calling...")==-1){
txtView.setText(txtView.getText().concat("calling..."));
}
else{
return;
}
}
});
btnSend.setText("Send");
btnSend.setBounds(40, 189, 127, 23);
panel.add(btnSend);

final JButton btnclear = new JButton();
btnclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
txtView.setText("");
}
});
btnclear.setText("Clear");
btnclear.setBounds(40, 212, 127, 23);
panel.add(btnclear);

final JButton btnOff = new JButton();
btnOff.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
btnOff.setText("Off");
btnOff.setBounds(40, 232, 127, 23);
panel.add(btnOff);
}
}
//這個程序不難的,好像沒什麼注釋可寫,你自己看看應該看得懂,如果哪
//里不明白,再問我吧,程序沒有問題,我運行過了~

㈥ Java實驗報告求大神幫助

publicclassVehicle{
privateintspeed;
privateStringbrand;
privateStringno;

publicVehicle(){
}

publicVehicle(intspeed,Stringbrand,Stringno){
this.speed=speed;
this.brand=brand;
this.no=no;
}

publicintgetSpeed(){
returnspeed;
}

publicvoidsetSpeed(intspeed){
this.speed=speed;
}

publicStringgetBrand(){
returnbrand;
}

publicvoidsetBrand(Stringbrand){
this.brand=brand;
}

publicStringgetNo(){
returnno;
}

publicvoidsetNo(Stringno){
this.no=no;
}

publicStringshowInfo(){
return"speed:"+speed+",brand="+brand+",no="+no;
}

publicstaticvoidmain(String[]args){
Vehiclev1=newVehicle(150,"brand1","123");
Vehiclev2=newVehicle(100,"brand2","976");
Vehiclev3=newVehicle(200,"brand3","231");
intaverSpeed=(v1.speed+v2.speed+v3.speed)/3;
System.out.println("三輛車的平均速度為:"+averSpeed);

}
}

閱讀全文

與java程序實驗報告相關的資料

熱點內容
如何看app限制視頻 瀏覽:980
電影廁所持刀惡人 瀏覽:607
go to the cinema翻譯 瀏覽:843
主人公叫艾米麗是什麼電影 瀏覽:426
文件夾排序怎麼變成橫排了 瀏覽:690
一個男的看女生洗澡電影 瀏覽:292
虛擬幣充值源碼 瀏覽:87
我昨天看了航天電影英文翻譯 瀏覽:175
熙和宇電影高級家庭 瀏覽:236
主角10歲收了母親的小說 瀏覽:544
女獄電影日本 瀏覽:154
二龍湖浩哥最早的作品 瀏覽:699
異界收母入後宮 瀏覽:155
ida反編譯出來的代碼是匯編指令嗎 瀏覽:820
小孩子是天才的電影 瀏覽:450
輿情監控演算法 瀏覽:147
好看123電影 瀏覽:751
成龍主演的五行拳電影名字 瀏覽:954
好看的中文字幕經典 瀏覽:913
android仿qq輸入 瀏覽:117