Java 语言程序设计实践环节(04748)是 Java 语言程序设计(一)专业课的上级测试部分,考核目标是掌握调试、完善和简单设计 Java 程序的能力、掌握 MyEclipse 开发工具的使用(新建项目,新建类,修改与运行程序)、掌握 Java 的基本语句,基本输入输出流、掌握使用类及方法进行 Java 面向对象程序开发的方法。运行环境是 Windows 10 系统下的 MyEclipse 软件。以下为我在学习和实战练习过程中所做的笔记,可供参考。
一、输入、输出语句
使用 Scanner 语句输入时需要引入包 import java.util.Scanner;
,并定义 Scanner 对象:
1 2 3 4 5 6 7 8 9 10
| import java.util.Scanner;
public class Test1 { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String temp = sc.next(); System.out.println("temp:"+ n); } }
|
BufferedReader
由 Reader
类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的 readLine
,读取一个文本行,从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。使用 BufferedReader 流前需要引入 import java.io.Reader;
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.io.* public class Test2 { public static void main(String[] args){ String st; int num; float fnum; try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); st = br.readLine(); num = Integer.parseInt(br.readLine()); fnum = Float.parseFloat(br.readLine()); System.out.println(st + num + snum); }catch(IOException e){ System.out.println("错误"); } } }
|
二、实现常见的基本算法
字符统计程序。编写输入字符行,统计输入字符行中数字符、英文字母个数的 Java 应用程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import java.util.Scanner;
public class Test3 { public static void main(String[] args) { int a = 0 ; int b = 0 ; int c = 0 ; int d = 0 ; Scanner sc = new Scanner(System.in) ; System.out.println("请输入一串字符串"); String s = sc.nextLine() ; char[] sr = s.toCharArray() ; for(int i = 0 ;i<sr.length ; i ++ ) { if('A'<=sr[i] && 'Z'>=sr[i] || 'a'<=sr[i] && 'z'>=sr[i] ) { a++ ; }else if('0'<=sr[i] && '9'>=sr[i]) { b++ ; }else if(sr[i] == ' ') { c++ ; }else { d++ ; } } System.out.println("字母的个数为:" + a); System.out.println("数字的个数为:" + b); System.out.println("空格的个数为:" + c); System.out.println("其他字符的个数为:" + d); } }
|
特殊性质数的判断。例如水仙花数、完数、素数的判断程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| public class Daffodil { public static void main(String[] args) { int sum=0, number; for(number=100;number<=999;number++) { int num1 = number%10; int num10 = number/10%10; int num100 = number/100%10; sum = num1*num1*num1 + num10*num10*num10 + num100*num100*num100; if(sum==number) { System.out.println(number+"是水仙花数"); } } } }
public class WanNumber { public static void main(String[] args) { int i; for(int num=1;num<=1000;num++) { int sum = 0; for( i=1;i<num;i++) { if(num%i==0) { sum+=i; } } if(num==sum) { System.out.println(num); } } } }
public class PrimeDemo { public static void main(String[] args){ System.out.println("请输入一个数"); Scanner sc = new Scanner(System.in); int paime = sc.nextInt(); boolean t=true; int i; for (i=2;i<paime;i++) { if(paime%i==0) { t=false; } } if(t) { System.out.println("是素数"); } else { System.out.println("是素数"); } } }
|
类的继承定义。声明几何形状类,类中定义几何形状的成员变量和方法,然后继承声明几何形状类,创建对象,并显示对象的相关信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| import java.util.Scanner; class GeometricObject { private String col; private boolean fil;
public GeometricObject() { col = "white"; fil = false; }
public GeometricObject(String col, boolean fil) { this.col = col; this.fil = fil; }
public String getCol() { return col; }
public void setCol(String col) { this.col = col; }
public boolean isFil() { return fil; }
public void setFil(boolean fil) { this.fil = fil; } }
class Triangle extends GeometricObject { private double s1; private double s2; private double s3;
public Triangle() { s1 = 1; s2 = 1; s3 = 1; }
public Triangle(String col, boolean fil, double s1, double s2, double s3) { super(col, fil); this.s1 = s1; this.s2 = s2; this.s3 = s3; }
public double gets() { double p = (s1 + s2 + s3) / 2.0; double s = p * (p - s1) * (p - s2) * (p - s3); s = Math.sqrt(s); return s; }
public double getc() { return s1 + s2 + s3; }
public void tos() { String s="Triangle:\n"+"side1 = "+s1+" side2 = "+s2+" side3 = "+s3+"\n" +"Color: "+getCol()+" and filled: "+isFil()+"\n" +"The area is "+gets()+"\n"+"The perimeter is "+getc()+"\n"; System.out.println(s); } }
public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); double a=sc.nextDouble(); double b=sc.nextDouble(); double c=sc.nextDouble(); String s=sc.next(); boolean f=sc.nextBoolean(); sc.close(); Triangle t=new Triangle(s,f,a,b,c); t.tos(); } }
|
数组排序程序。编写输入整数序列、对输入的整数进行排序后输出的程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class Array4{ public static void main(String[] args){ int[] Arr = new int[]{14,52,56,32,17}; printArray(Arr); bubbleSort(Arr); printArray(Arr); }
public static void printArray(int[] arr){ System.out.print("["); for(int x=0; x<arr.length; x++){ if(x!=arr.length-1) System.out.print(arr[x]+","); else System.out.println(arr[x]+"]"); } }
public static void swap(int[] arr, int a, int b){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; }
public static void bubbleSort(int[] arr){ for(int x=0; x<arr.length-1; x++){ for(int y=0; y<arr.length-1-x;y++){ if(arr[y]>arr[y+1]) swap(arr,y+1,y); } } } }
|
提取整数中每个数字的程序:
1 2 3 4 5 6 7 8 9 10 11 12
| public class Test{ public String print(int num){ while(num>0){ System.out.print(num%10+","); num/=10; } } public static void main(String [] args){ Test t = new Test(); t.print(12345); } }
|
三、基于 Swing 和 AWT 的界面程序设计
两个顶级容器的应用:Frame(窗口)、Dialog(弹窗) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| import java.awt.Button; import java.awt.Color; import java.awt.Dialog; import java.awt.Frame; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Test{ public static void main(String[] args) { Myframe frame = new Myframe(); frame.init(); frame.compen(); } }
class Myframe extends Frame{ private static final long serialVersionUID = -3005332394925719672L; public Myframe() { super("测试窗口"); } public void init() { super.setVisible(true); super.setBounds(200, 200, 200, 200); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void compen() { Button button = new Button("按钮"); button.setSize(100, 100); this.setLayout(null);; this.add(button); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { end(); } }); } public void end() { new Mydialog(this,"测试弹窗").increase(); } }
class Mydialog extends Dialog{ private static final long serialVersionUID = 1L;
public Mydialog(Frame owner,String title) { super(owner,title); setVisible(true); setSize(100, 200);
setBackground(Color.BLUE); setLocation(200, 300);
this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void increase() { Label label = new Label("这是一个文本框",1); this.add(label); } }
|
四、JavaApplet 下的图形处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.awt.* ; import java.applet.Applet ; public class AppletImage extends Applet{ private Image img[] ; private int index=0; public void init(){ img=new Image[10]; int num ; for(num=0;num<10;num++){ img[num]=getImage(getDocumentBase(),"T"+(num+1)+".gif") ; } } public void start(){ } public void paint(Graphics g){ g.drawImage(img[index],0,0,this) ; index=++index%10 ; repaint() ; try{ Thread.sleep(500) ; }catch(Exception e){ } } }
|
五、Java 下的多线程程序设计
编写一个有两个线程的程序,第一个线程用来计算 1~100 之间的偶数及个数,第二个线程用来计算 1-100 之间的偶数及个数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package experiment4; class NumberRunnable implements Runnable{ private final int first; NumberRunnable(int first){ this.first=first; } @Override public void run() { for (int i=first;i<=100;i+=2){ System.out.println(i+ " "); } System.out.println("结束!"); } } public class Exp4_1 { public static void main(String[] args){ NumberRunnable runnable1 = new NumberRunnable(1); NumberRunnable runnable2 = new NumberRunnable(2); Thread thread_odd = new Thread(runnable1,"奇数线程"); Thread thread_even = new Thread(runnable2,"偶数线程"); thread_odd.start(); thread_even.start(); } }
|