public double getjiaban(){ return jiaban; } }
2. public abstract class Shape { public String name;
public abstract double GetArea(); public abstract double GetLength();
//将类实例化,参数给到传参方法里,输出面积和长就能实现 public static void main(String[] args) { Shape s=new Circle(2);Shape a=new Rectangle(3,5); System.out.println(\圆面积\+s.GetArea()+\周长\+s.GetLength()); System.out.println(\长方形面积\+a.GetArea()+\周长\+a.GetLength()); } }
class Circle extends Shape{ private double radius; public Circle (double r){ this.radius=r; } public double GetArea() { return (3.14*radius*radius); } public double GetLength() { return (2*3.14*radius); } }
class Rectangle extends Shape{ private double length,width;
public Rectangle (double l,double w){ this.length=l; this.width=w; } public double GetArea() { return (length*width); } @Override public double GetLength() { return 2*(length+width); } }
3.获取class名称:对象名.getClass().getName();
4.
实验七
一、思考题
1. 设int year,month,day分别表示一个日期中的年月日,试编程求
a. 对于任意三个整数,判断其是否为一个合法的日期值。 b. 给定一个日期值,计算若干天后的日期值 c. 如果有两个日期值,计算他们相距的天数。
2. 编写程序,能接收键盘输入的字符串,并将字符串的内容写入文件。
3. 编程将保存在本地机当前文件夹中的java.htm文本文件的内容在屏幕上显示出来,然
后将其另存为java.txt 文件。
4. 创建一个实现Serializable接口的类,其包含3个成员变量bh(编号)、xm(姓
名)、nl(年龄);两个构造方法;一个使用对象流保存对象信息的save方法,一个使用对象流输出的对象信息的display方法。在main主方法中创建该类的对象O1,然后使用其保存与显示的方法。 二、实验内容
1.
2. import java.io.File;
import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class shiyan2 { public static void main(String[] args) throws IOException{ File file=new File(\if(file.exists()){ file.createNewFile(); }
Scanner sc=new Scanner(System.in); System.out.println(\请输入字符串撒\String msg=sc.next(); byte a[]=msg.getBytes();
FileOutputStream fos=new FileOutputStream(file); fos.write(a); fos.close();
System.out.println(\好了 输入完成 自己去文件夹里看\ } }
3. import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class shiyan1 { public static void main(String[] args) throws IOException{ char[] a=new char[500];
FileReader fr=new FileReader(\int num=fr.read(a);
String str=new String(a,0,num);
System.out.println(\此文件的内容为:\
FileWriter fw=new FileWriter(\fw.write(a); fw.write(str); fr.close(); fw.close();
System.out.println(\已将此文件另存为txt格式\ } }
4. package ooo;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;
public class shiyan4 implements Serializable{ public int bh; public String xm; public int nl;
shiyan4(int bh,String xm,int nl){ this.bh=bh; this.xm=xm; this.nl=nl; }
public void save(ObjectOutputStream oos) throws IOException{ oos.writeObject(this); oos.close(); }
public void display(ObjectInputStream ois) throws IOException, ClassNotFoundException{ shiyan4 sy=(shiyan4)ois.readObject(); System.out.println(\类名:\\ System.out.println(\编号 \姓名\年龄\ ois.close(); }
public static void main(String []args) throws IOException, ClassNotFoundException{ shiyan4 o1=new shiyan4(223,\辜朕圆\ FileOutputStream fos=new FileOutputStream(\ ObjectOutputStream oos=new ObjectOutputStream(fos); FileInputStream fis=new FileInputStream(\ ObjectInputStream ois=new ObjectInputStream(fis); o1.save(oos); o1.display(ois); } }
实验八
一、 思考题
1. 编程出一个程序,实现两个线程 A 和 B ,A 和 B 同时启动,A线程每隔500毫秒显示字符?A?,B线程每隔 300 毫秒显示字符?B?
2. 有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC
3. 使用Vector存储一个班的学生信息,实现对学生类的对象进行插入、修改、删除、浏览、统计人数等功能。 二、 实验内容 1.
package pia;
import java.util.Scanner; public class shiyan1 {
public static void main(String[] args) { A a=new A(); B b=new B(); a.start(); b.start(); Scanner sc=new Scanner(System.in);//键盘控制停止运行 String c= sc.next(); a.stop(); b.stop(); } }
class A extends Thread{ public void run(){ boolean f=true; while(f){ try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('A'); }} }
class B extends Thread{ public void run(){ boolean f=true; while(f){ try { sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('B'); }} }
2.测试类
public class test2 { public void s(){ MyThread a=new MyThread(\); MyThread b=new MyThread(\); MyThread c=new MyThread(\); a.start(); try { a.join(); } catch (InterruptedException e) { e.printStackTrace(); } b.start(); try { b.join(); } catch (InterruptedException e) { e.printStackTrace(); } c.start(); try {
相关推荐: