Java实例代码.doc_第1页
Java实例代码.doc_第2页
Java实例代码.doc_第3页
Java实例代码.doc_第4页
Java实例代码.doc_第5页
已阅读5页,还剩87页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

例1.1 / import java.awt.*;import java.applet.Applet;public class SmileFace extends Applet /一个Applet public void paint(Graphics g) g.setColor (Color.yellow); g.fillOval (35, 15, 50, 50); / head g.setColor (Color.black); g.fillOval (50, 30, 5, 5); / left eye g.fillOval (65, 30, 5, 5); / right eye g.drawArc (50, 40, 20, 10, 190, 160); / smile 例2.4(3)public class FunnyNumbers public static void main(String args) double largeNum = (int)Math.exp(4000.0); double posDivZero = 10.0/0; double negDivZero = 10.0 / 0.0; double zeroDivZero = 0.0/0.0; System.out.println(largeNum); System.out.println(posDivZero); System.out.println(negDivZero); System.out.println(zeroDivZero); double x=largeNum+posDivZero; double y=largeNum+negDivZero; double z=largeNum+zeroDivZero; System.out.println(x=+x); System.out.println(y=+y); System.out.println(z=+z); 例2.5public class IncDecTest public static void main(String args) int j, k; System.out.println(第一组:); k = 1; j = k+; System.out.println(j); System.out.println(k); System.out.println(第二组:); k = 1; j = +k; System.out.println(j); System.out.println(k); System.out.println(第三组:); k = 1; System.out.println(k+); System.out.println(+k); System.out.println(k); System.out.println(k); 例2.12if (score = 90) System.out.println(Letter grade: A);else if (score = 80.0) System.out.println(Letter grade: B);else if (score = 70.0) System.out.println(Letter grade: C);else if (score = 60.0) System.out.println(Letter grade D);else System.out.println(Letter grade: F);例2.13switch (grade) case A: case a: System.out.println(90分或更好); break; case B: case b: System.out.println(80到89.99分之间); break; case C: case c: System.out.println(70到79.99分之间); break; case D: case d: case F: case f: System.out.println(少于70分); break; default: System.out.println(无效等级);例2.16public class ArrayParameters public static void changeOrNot(int i, double x) i = -1; x0 = -2.0; double y = x; y1 = -3.0; double z = -4,-4,-4; x = z; public static void main(String args) int k = 1; double A = 1.0, 2.0, 3.0; changeOrNot(k, A); System.out.println(k: + k); System.out.println(A0: + A0); System.out.println(A1: + A1); System.out.println(A2: + A2); 例2.19if (name1.equals(name2) System.out.println(names are the same );else System.out.println(names are different );public class StrangeStrings public static void main(String args) String name1 = 比尔 盖茨;String first = 比尔;String last = 盖茨; String name2 = first + + last; System.out.println(name1); System.out.println(name2); if (name1.equals(name2)/不是if (name1 = name2) System.out.println(names are the same); else System.out.println(names are different); 例2.21import java.io.*;public class Console public static double readDouble() try return Double.valueOf(readString().trim().doubleValue(); catch(NumberFormatException ne) System.err.println(Console.readDouble: Not a double.); System.exit(-1); return 0.0; public static int readInt() try return Integer.valueOf(readString().trim().intValue(); catch(NumberFormatException ne) System.err.println(Console.readInt: Not an integer.); System.exit(-1); return -1; public static String readString() String string = new String(); BufferedReader in=new BufferedReader(new InputStreamReader(System.in); try string = in.readLine(); catch(IOException e) System.out.println(Console.readString: Unknown error.); System.exit(-1); return string; 例3.4class Address / 成员变量 String firstName; String lastName; String email; / 构造方法 Address(String _firstName, String _lastName, String _email) firstName = _firstName; lastName = _lastName; email = _email; / 成员方法 void showAddress() /* 具体实现 */ 例3.5class Parents / 成员变量 String LABELS = father, mother; Address parents = new Address2; / 构造方法 Parents() for (int i = 0; i = 0) & (scale.equals(meter) | (scale.equals(feet) isValid = true; public Length convertToFeet() if (scale.equals(feet) | (!isValid) return this; else return new Length(value * 3.2809, feet); public Length convertToMeter() if (scale.equals(meter) | (!isValid) return this; else return new Length(value / 3.2809, meter); public void showLength() if (isValid) System.out.println(value + + scale); else System.out.println(invalid length); 例3.14public class Length private double value = 0; private String scale = none; private boolean isValid = false; public Length(double _value, String _scale) /* 参考前面例子 */ public Length convertToFeet() /* 参考前面例子 */ public Length convertToMeter() /* 参考前面例子 */ public void showLength() /* 参考前面例子 */ public double getValue() return value; public String getScale() return scale; public boolean isValid() return isValid; public void setValue(double newValue) if (newValue = 0) value = newValue; else isValid = false; public class SafeLengthTest public static void main(String args) Length length = new Length(2, meter); System.out.print(Current: ); length.showLength(); System.out.println(Value: + length.getValue(); System.out.println(Scale: + length.getScale(); System.out.println(Valid: + length.isValid(); System.out.print(To 4 meter: ); length.setValue(4); length.showLength(); 例3.151public class ConcentricCirclesTester2 public static void main(String args)3 ConcentricCircles.x += 100; ConcentricCircles.y += 100;4 ConcentricCircles t1 = new ConcentricCircles();5 ConcentricCircles t2 = new ConcentricCircles(); 6 t1.x += 100;7 t1.r = 50;8 t2.y += 100;9 t2.r += 150;10 System.out.println(T1: x = + t1.x + , y = + t1.y + , r = + t1.r);11 System.out.println(T2: x = + t2.x + , y = + t2.y + , r = + t2.r);12 13例3.17public class CountingObject public static int counter = 0; public CountingObject() counter+; System.out.println(= 创建第 + counter +个对象); public void finalize() System.out.println(= 清除第 + counter+个对象); counter-; public class ObjectCounter public static String showMenu() System.out.println(reate object); System.out.println(un garbage collector); System.out.println(Exit the program); System.out.print(Enter choice: ); return Console.readString(); public static void main(String args) CountingObject obj = null; String choice = showMenu(); while (!choice.equalsIgnoreCase(x) if (choice.equalsIgnoreCase(c) obj = new CountingObject(); else if (choice.equalsIgnoreCase(r) System.gc(); choice = showMenu(); 例3.19public class Student private String name; private double GPA; public Student() this(无名氏, -1.0); /一个不知姓名和GPA的学生对象 public Student(String _name) this(_name, -1.0); /知道姓名不知GPA的学生对象 public Student(double _GPA) this(无名氏, _GPA); /知道GPA但不知姓名的学生对象 public Student(String _name, double _GPA) name = _name; GPA = _GPA; /知道姓名和GPA的学生对象 public void showStudent() System.out.print(Student: + name); if (GPA = 0.0) System.out.println( (GPA: + GPA + ); else System.out.println(); 例3.21public class Shape protected String name; protected double area, perimeter; public Shape() name = undetermined; area = perimeter = 0; public void display() System.out.println(Name: + name); System.out.println(Area: + area); System.out.println(Perimeter: + perimeter); public class Rectangle extends Shape protected double length, width; public Rectangle(double _length, double _width) name = Rectangle; length = _length; width = _width; public void computeArea() area = length * width; public void computePerimeter() perimeter = 2*(length + width); public class Circle extends Shape protected double radius; public Circle(double _radius) name = Circle; radius = _radius; public void computeArea() area = Math.PI * radius * radius; public void computePerimeter() perimeter = 2 *Math.PI * radius; public class ShapeTester public static void main(String args) Shape s = new Shape(); Rectangle r = new Rectangle(2.0, 3.); Circle c = new Circle(4.0); puteArea(); putePerimeter(); puteArea(); putePerimeter(); r.display();c.display(); s.display(); 例3.25ublic class Shape protected String name; protected double area, perimeter; public Shape() name = undetermined; area = perimeter = 0; public void display() System.out.println(* GEOMETRIC SHAPE *); System.out.println(name+, area +area+, perimeter +perimeter); 例3.27public class Rectangle extends Shape protected double length, width; public Rectangle(double _length, double _width) name = Rectangle; length = _length; width = _width; final public void computeArea() area = length * width; public void computePerimeter() perimeter = 2*(length + width); public final class Circle extends Shape protected double radius; public Circle(double _radius) name = Circle; radius = _radius; final public void computeArea() area = Math.PI * radius * radius; final public void computePerimeter() perimeter = 2 *Math.PI * radius; public final class Square extends Rectangle public Square(double _side) super(_side, _side); name = Square; 例328public class ShapeTester / main 方法 public static void main(String args) Rectangle r = new Rectangle(2.0, 3.); Circle c = new Circle(4.0); puteArea(); putePerimeter(); puteArea(); putePerimeter(); System.out.println(r); System.out.println(c); 例3.29public class ObjectArray public static void display(Object obj) if (obj instanceof Rectangle) System.out.println( (Rectangle)obj).toString(); else if (obj instanceof Circle) System.out.println( (Circle)obj).toString(); else if (obj instanceof Double) System.out.println( (Double)obj).toString(); else if (obj instanceof Integer) System.out.println( (Integer)obj).toString(); public static void main(String args) Object objects = new Object4; objects0 = new Rectangle(); objects1 = new Circle(); objects2 = new Double(1.0); objects3 = new Integer(2); for (int i = 0; i objects.length; i+) display(objectsi); 例3.30public class SuperSuper public SuperSuper() System.out.println(SuperSuper Class); public class Super extends SuperSuper public Super() System.out.println(Super Class); public class Sub extends Super public Sub() System.out.println(SubClass); public class Test public static void main(String args) Sub c = new Sub(); 例3.32public abstract class Shape protected String type; protected double area, perim; public abstract void computeArea(); public abstract void computePerimeter(); public String toString() return type + :ntArea = + area + ntPerimeter = + perim; public class Rectangle extends Shapepublic class Circle extends Shape protected double width, height; protected double radius; public Rectangle(double _width, public Circle(double _radius) double _height) type = Circle; type = Rectangle; radius = _radius; width = _width; height = _height; public void computeArea() area = Math.PI * radius * radius; public void computePerimeter() public void computeArea() perim = 2.0 * Math.PI * radius; area = width * height; public String toString() public void computePerimeter() return super.toString() + perim = 2*width + 2*height; ntRadius = + radius; public String toString() return super.toString() + ntWidth = + width + ntHeight = + height; public class Square extends Rectangle public Square(double _side) super(_side, _side); type = Square; 例3.33package graphicPackage;/在Graphic.java中的文件的第一行public abstract class Graphic public class Circle extends Graphic implements Draggable public class Rectangle extends Graphic implements Draggable public interface Draggable /* File: ErsBox.java * Describe: 俄罗斯方块的 Java 实现 */import java.awt.*;class ErsBox implements Cloneable private boolean isColor;private Dimension size = new Dimension();/* 方格类的构造方法 * param isColor 是不是用前景色来为此方格着色 * true前景色,false用背景色 */public ErsBox(boolean isColor) this.isColor = isColor;/* 此方格是不是用前景色显示 * return boolean, true用前景色显示,false用背景色显示 */public boolean isColorBox() return isColor;/* 设置方格的颜色, * param isColor boolean, true用前景色显示,false用背景色显示 */public void setColor(boolean isColor) this.isColor = isColor;/* 获得此方格的尺寸 * return Dimension,方格的尺寸 */public Dimension getSize() return size;/* 设置方格的大小 * param size Dimension,方格的大小 */public void setSize(Dimension size) this.size = size;/* 覆盖Object的Object clone()方法,实现克隆 * return Object,克隆的结果 */public Object clone() /如前所示例4.1import java.io.File;public class FileMethodDemostatic void print(String s) System.out.print(s);public static void main(String args)File f = new File(C:/jdk/bin/java.exe);/建立文件类print(文件名称: + f.getName() + n);/取得文件名称print(文件路径: + f.getPath() + n);/取得文件路径print(绝对路径: + f.getAbsolutePath() + n);/取得绝对路径print(上级目标: + f.getParent() + n);/取得上级目录名称print(文件大小: + f.length() + bytesn);/取得文件大小print(最后修改时间: + f.lastModified() + n);/取得最后修改时间print(是否存在:);print(f.exists() ? 存在n : 不存在n);/检查是否存在print(是否可读取:);print(f.canRead() ? 可读取n : 不可读取n);/检查是否可读取print(是否可写入:);print(f.canWrite() ? 可写入n : 不可写n);/检查是否可写入 例4.2import java.io.File;public class DirectoryDemostatic v

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论