JAVA(課后練習(xí))部分拓展實(shí)踐代碼_第1頁(yè)
JAVA(課后練習(xí))部分拓展實(shí)踐代碼_第2頁(yè)
JAVA(課后練習(xí))部分拓展實(shí)踐代碼_第3頁(yè)
JAVA(課后練習(xí))部分拓展實(shí)踐代碼_第4頁(yè)
JAVA(課后練習(xí))部分拓展實(shí)踐代碼_第5頁(yè)
已閱讀5頁(yè),還剩9頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Chap 2實(shí)踐2-1class Ex2_1public static void main(String args) float sum=0.0f,count=1.0f;for(int i=1;i=20;+i)count=1.0f;for(int j=1;j=i;+j)count*=j;sum+=count;System.out.println(1+2!+3!+.+20!的值為:+sum);實(shí)踐2-2public class Ex2_2 public static void main(String args) System.out.println(* 菲波拉挈數(shù)列的前20個(gè)數(shù)為:*); lon

2、g f1 = 1, f2 = 1; for (int i = 1; i = 10; i +) System.out.print(f1 + + f2 + ); if (i % 2 = 0) System.out.println(); f1 = f1 + f2; f2 = f2 + f1; Chap 3實(shí)踐3-1public class Student static String name=張三; static int age; public Student() age=18; public static void main(String args) System.out.println(姓名:+

3、name+,年齡:+age); 實(shí)踐3-2class People String name; People(String name) =name ; /對(duì)成員變量name 初始化 class Student extends People String stuID; Student(String name,String stuID) super(name) ; /對(duì)繼承自父類的成員變量name初始化 this.stuID=stuID ; /對(duì)成員變量stuID 初始化 class PeopleDemo public static void main(String arg) St

4、udent stu=new Student(張三,20080601) ; /定義對(duì)象 System.out.println(+ +stu.stuID) ; /輸出對(duì)象的姓名和學(xué)號(hào) Chap 4實(shí)踐4-1public class Ex4_1 String str; public Ex4_1(String str) this.str = str; int getlength() return(str.length(); public static void main(String args) Ex4_1 test = new Ex4_1(Hello); System.out.pri

5、ntln(字符串是:+test.str+ 長(zhǎng)度為:+test.getlength(); 實(shí)踐4-2import java.util.*;import java.io.*;public class Ex4_2 public static void main(String args) String str1 = new String(); String str2 = new String(); char ch; Scanner reader= new Scanner(System.in); System.out.println(輸入字符串:); str1=reader.next(); /輸入字符串

6、 System.out.println(輸入要?jiǎng)h除的字符:); str2=reader.next(); /輸入穿要?jiǎng)h除的字符 ,以字符串的形式輸入 ch=str2.charAt(0); /將字符串轉(zhuǎn)換為字符 str2=str1.replace(ch, ); /用空格替代指定字符 System.out.println(刪除字符后的字符串 +str2); Chap 5實(shí)踐5-1public class Ex5_1 public static void main(String args) try int num = new int 10; System.out.println(num10 is +

7、num10); catch (ArithmeticException ex) System.out.println(ArithmeticException); catch (RuntimeException ex) System.out.println(RuntimeException); catch (Exception ex) System.out.println(Exception); 實(shí)踐5-2public class Ex5_2 static double cal(double a, double b) throws IllegalArgumentException double v

8、alue; if ( b = 0 ) / 拋出IllegalArgumentException異常 throw new IllegalArgumentException(除數(shù)不能為0); else value = a/b; if ( value 0 ) / 拋出IllegalArgumentException異常 throw new IllegalArgumentException(運(yùn)算結(jié)果小于0); return value; public static void main(String args) double result; try double a = Double.parseDoub

9、le(args0); double b = Double.parseDouble(args1); result = cal(a, b); System.out.println(運(yùn)算結(jié)果是: + result); catch( IllegalArgumentException e ) / 處理llegalArgumentException異常 System.out.println(異常說明: +e.getMessage(); Chap 6實(shí)踐6-1import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.Font;

10、 public class ex6_1 public static void main(String args) JFrame frame=new JFrame(); JLabel label1=new JLabel(JAVA Programming); Font font1=label1.getFont(); font1=new Font(Courier, font1.getStyle(), 20); label1.setFont(font1); frame.getContentPane().add(label1); frame.pack(); frame.setVisible(true);

11、 實(shí)踐6-2import javax.swing.*;public class ex6_2 extends JFrame public ex6_2() super(第一個(gè)窗體); setDefaultCloseOperation(DISPOSE_ON_CLOSE); setSize(300, 150); setLocationRelativeTo(null); setVisible(true); public static void main(String args) new ex6_2(); Chap 7實(shí)踐7-1import javax.swing.*;import java.awt.*;

12、public class ex7_1 extends JFrame ex7_1()super(程序調(diào)試);setLayout(new FlowLayout();JButton jbtn1=new JButton(確定);JButton jbtn2=new JButton(取消);this.getContentPane().add(jbtn1); this.getContentPane().add(jbtn2); this.setVisible(true);this.setSize(300,150); public static void main( String args) new ex7_1

13、 (); Chap 8實(shí)踐8-1import java.awt.event.*;import javax.swing.*;import java.awt.*;public class Ex8_1 extends JFrame implements WindowListener public Ex8_1() Container con=this.getContentPane(); con.setLayout(new FlowLayout(); setBounds(0,0,200,200); setVisible(true); addWindowListener(this); public sta

14、tic void main(String args)new Ex8_1(); public void windowClosing(WindowEvent e)System.out.println(The window closing); System.exit(0); public void windowClosed(WindowEvent e) public void windowOpened(WindowEvent e)System.out.println(window opened); public void windowIconified(WindowEvent e) public v

15、oid windowDeiconified(WindowEvent e) public void windowDeactivated(WindowEvent e) System.out.println(The window deactived); public void windowActivated(WindowEvent e)System.out.println(The window Activated); 實(shí)踐8-2 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Ex8_2 e

16、xtends JFrame implements ActionListener static Ex8_2 frm=new Ex8_2(); static JButton btn1=new JButton(Yellow); static JButton btn2=new JButton(Green); public static void main(String args) btn1.addActionListener(frm); / 把事件監(jiān)聽者frm向btn1注冊(cè) btn2.addActionListener(frm); / 把事件聆聽者frm向btn2注冊(cè) frm.setTitle(Act

17、ion Event); frm.setLayout(new FlowLayout(FlowLayout.CENTER); frm.setSize(200,150); frm.getContentPane().add(btn1); frm.getContentPane().add(btn2); frm.setVisible(true); public void actionPerformed(ActionEvent e) JButton btn=(JButton) e.getSource(); / 取得事件源對(duì)象 if(btn=btn1) / 如果是按下btn1按鈕 frm.getContent

18、Pane().setBackground(Color.yellow); else if(btn=btn2) / 如果是按下btn2按鈕 frm.getContentPane().setBackground(Color.green); Chap 9實(shí)踐9-1import javax.swing.*;import java.awt.*;import javax.swing.event.*;public class Ex9_1 extends JFrame implements ListSelectionListener String pro = 軟件專業(yè), 網(wǎng)絡(luò)專業(yè), 動(dòng)漫專業(yè); JPanel p

19、1; JComboBox courseCombo; JList proList; Ex9_1() proList = new JList(pro); proList.addListSelectionListener(this); courseCombo = new JComboBox(); p1 = new JPanel(); add(p1); p1.add(proList); p1.add(courseCombo); add(p1); setSize(400,200); setVisible(true); public void valueChanged(ListSelectionEvent

20、 e) Object obj = e.getSource(); if(obj=proList)courseCombo.removeAllItems();int nIndex = proList.getSelectedIndex();switch(nIndex )case 0: courseCombo.addItem (Java程序設(shè)計(jì)) ; courseCombo.addItem (J2ee項(xiàng)目開發(fā)) ; break;case 1:courseCombo.addItem (網(wǎng)絡(luò)基本原理) ; courseCombo.addItem (局域網(wǎng)技術(shù)與組網(wǎng)工程) ; courseCombo.addI

21、tem (網(wǎng)絡(luò)操作系統(tǒng)) ; break; case 2: courseCombo.addItem (動(dòng)漫造型) ; courseCombo.addItem (漫畫制作) ; break; public static void main(String args) new Ex9_1(); 實(shí)踐9-2import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Ex9_2 extends JFrame implements ItemListener JRadioButton rad1=new JRadioB

22、utton(說法正確,false); JRadioButton rad2=new JRadioButton (說法錯(cuò)誤,false); ButtonGroup btg=new ButtonGroup (); JTextArea ta=new JTextArea(2,10); JTextField tf=new JTextField(4); JLabel lb=new JLabel(你的選擇:); JPanel jp=new JPanel(); String text=BoxLayout是由Swing 提供的布局管理器,功能上同 GridBagLayout 一樣強(qiáng)大,而且更加易用。; publi

23、c Ex9_2() Container c=this.getContentPane(); setLayout(new FlowLayout(); btg.add(rad1); btg.add(rad2); ta.setText(text); c.add(ta); rad1.addItemListener(this); rad2.addItemListener(this); c.add(rad1); c.add(rad2);c.add(lb); c.add(tf); this.setSize(520,150); this.setVisible(true); public void itemSta

24、teChanged(ItemEvent e) if(e.getSource()=rad1) rad2.setSelected(false); tf.setText(正確!); else rad1.setSelected(false); tf.setText(錯(cuò)誤!); public static void main(String args) new Ex9_2(); Chap 10實(shí)踐10-1import java.io.*;import java.util.*;public class ex10_1 public static void main(String args) String s=; try RandomAccessFile f1=new RandomAccessFile(a.txt,rw); RandomAccessFile f2=new RandomAccessFile(b.txt,rw); s=f1.readLine()+f2.readLine(); char c=s.toCharArray(); Arrays.sort(c); FileOutputStream out= new FileOutputStream(c.txt); for(int i=0;ic.length;i+) out

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論