收藏 分销(赏)

java实验考.doc

上传人:仙人****88 文档编号:12012306 上传时间:2025-08-27 格式:DOC 页数:7 大小:91.50KB 下载积分:10 金币
下载 相关 举报
java实验考.doc_第1页
第1页 / 共7页
java实验考.doc_第2页
第2页 / 共7页


点击查看更多>>
资源描述
1.(25分)产生一个int数组a,长度为100,并向其中随机填充三位的整数,然后再从该100个随机数中任意抽取10形成一个10个数的数组b,再将数组b输出。 代码: /** * 1.(25分)产生一个int数组a,长度为100,并向其中随机填充三位的整数, * 然后再从该100个随机数中任意抽取10形成一个10个数的数组b,再将数组b输出。 * @version 1.0 */ package wangshuai;//数组a,100 import java.util.Random; public class RandomArray { public static int[] getArray(int length, int downLimit, int upLinit) { int[] originalArr = new int[length]; int[] result = new int[10]; Random r = new Random(); for (int i = 0; i < originalArr.length; i++) { originalArr[i] = r.nextInt(upLinit - downLimit + 1) + downLimit; } int point = originalArr.length; for (int i = 0; i < 10; i++) { int temp = r.nextInt(point); result[i] = originalArr[temp]; originalArr[temp] = originalArr[point - 1]; point--; } return result; } public static void main(String[] args) { int[] result = getArray(100, 100, 999); System.out.print("结果数组:"); for (int i : result) { System.out.print(i + " "); } } } 2. (25分)请产生一个6字符的随机字符串,要求 (1)必须包含数字和字母(a-z或者A-Z); (2)不能以数字开头 (3)字母和数字的个数随机(即为5字母+1数字或者4字母+2数字,...); 再对该字符串按照字母顺序进行排序。 package wangshuai; import java.util.Random; public class RandomString { public char[] dict; //随机产生6字符的随机字符串 public RandomString(char[] initDict) { dict = new char[initDict.length]; for (int i = 0; i < initDict.length; i++) { this.dict[i] = initDict[i]; } } public String getRandomString(int length) { int len = dict.length; Random r = new Random(); char[] result = new char[length]; do { for (int i = 0; i < length; i++) { result[i] = dict[r.nextInt(len)]; } } while (!checkString(result)); System.out.println(new String(result)); sort(result); return new String(result); } public static boolean checkString(char[] arr) { if (arr == null) { return false; } if (arr[0] >= '0' && arr[0] <= '9') { return false; } for (int i = 1; i < arr.length; i++) { if (arr[i] >= '0' && arr[i] <= '9') { return true; } } return false; } //对随机产生的字符串进行排序 public void sort(char[] result) { int len = result.length; for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { if (result[i] >= '0' && result[i] <= '9') { continue; } if (result[j] >= '0' && result[j] <= '9') { continue; } if (result[i] < result[j]) { char temp = result[i];//采用中间变量进行调换 result[i] = result[j]; result[j] = temp; } } }//两个for循环通过比较ASCII实现排序 } public static void main(String[] args) { char[] dict = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K','L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 'Y', 'Z' }; RandomString rs = new RandomString(dict); String result = rs.getRandomString(6); System.out.print(result); } } 二、 面向对象题 1. 定义接口AreaInterface,该接口有一个双精度浮点型的变量PI,它的值等于Math.PI;含有一个求面积的方法 double area();再定义一个类Rectangle,实现接口AreaInterface,包含成员左上角点、宽和高,并且定义一个新的方法double length(),该方法返回矩形的周长;定义接口MoveAble,包含方法void move(int delx,int dely);再定义类Circle,要求该类实现AreaInterface和MoveAble接口,包含了成员圆心和半径。请编写主程序,在主程序中,需要完成: (1) 定义一个左上角在(10,10)的矩形,宽度为600,高度为480的矩形对象,然后输出该矩形对象的面积以及周长; (2) 定义5个圆对象,每个圆的圆心是(0.0)-(100,100)矩形区域中的一个随机点,半径为100-200之间的随机整数,这5个圆用一个ArrayList存放,请输出他们的面积并将他们随机的沿着x方向移动20-50距离,沿着y方向移动同样范围的随机距离;然后再将这存放这5个圆的ArrayList采用DataOutputStream保存到文件circles.dat中。 源代码: //定义接口AreaInterface,含有一个求面积的方法 double area() public interface AreaInterface { public final double PI = Math.PI; public double area(); } //定义接口MoveAble,包含方法void move(int delx,int dely) public interface MoveAble { void move(int delx, int dely); } //定义类Rectangle,实现接口AreaInterface,包含成员左上角点、宽和高,并且定义一个新的方法double length(),该方法返回矩形的周长 public class Rectangle implements AreaInterface { private int leftTopX; private int leftTopY; private double high; private double width; public Rectangle() { super(); } public Rectangle(int leftTopX, int leftTopY) { super(); this.leftTopX = leftTopX; this.leftTopY = leftTopY; } public Rectangle(int leftTopX, int leftTopY, double hight, double width) { super(); this.leftTopX = leftTopX; this.leftTopY = leftTopY; this.high = hight; this.width = width; } public int getLeftTopX() { return leftTopX; } public void setLeftTopX(int leftTopX) { this.leftTopX = leftTopX; } public int getLeftTopY() { return leftTopY; } public void setLeftTopY(int leftTopY) { this.leftTopY = leftTopY; } public double getHight() { return high; } public void setHight(double hight) { this.high = hight; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } @Override public double area() { // TODO Auto-generated method stub return high * width; } public double length() { return 2 * (high + width); } } //定义类Circle,实现AreaInterface和MoveAble接口,包含了成员圆心和半径 public class Circle implements AreaInterface, MoveAble { private int centerX; private int centerY; private double radius; public Circle() { super(); } public Circle(int centerX, int centerY, double radius) { this.centerX = centerX; this.centerY = centerY; this.radius = radius; } public int getCenterX() { return centerX; } public void setCenterX(int centerX) { this.centerX = centerX; } public int getCenterY() { return centerY; } public void setCenterY(int centerY) { this.centerY = centerY; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } @Override public double area() { return PI * Math.pow(radius, 2); } @Override public void move(int delx, int dely) { this.centerX += delx; this.centerY += dely; } } //定义类ExamTest01 public class ExamTest01 { public ExamTest01() { Rectangle rec = new Rectangle(10, 10, 800, 600); System.out.println("矩形的面积为:" + rec.area()); System.out.println("矩形的周长为:" + rec.length()); } public static void main(String[] args) { // TODO Auto-generated method stub new ExamTest01(); } } //定义类ExamTest02 import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Random; public class ExamTest02 { public ExamTest02() throws IOException { List<Circle> list = new ArrayList<>(); Circle circle = null; Random r = new Random(); for (int i = 0; i < 5; i++) { int centerX = r.nextInt(101); int centerY = r.nextInt(101); double radius = r.nextDouble() * 100 + 100; circle = new Circle(centerX, centerY, radius); int delX = r.nextInt(31) + 20; int delY = r.nextInt(31) + 20; circle.move(delX, delY); System.out.println("第" + (i+1) + "个圆的面积为:" + circle.area()); list.add(circle); } saveResult("circles.dat", list); } public void saveResult(String src,List<Circle> result) throws IOException{ Iterator<Circle> iter = null; Circle circle = null; File file = new File(src); if(!file.exists()){ file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); DataOutputStream dos = new DataOutputStream(fos); iter = result.iterator(); while(iter.hasNext()){ circle = iter.next(); dos.writeInt(circle.getCenterX()); dos.writeInt(circle.getCenterY()); dos.writeDouble(circle.getRadius()); dos.writeDouble(circle.area()); } dos.close(); fos.close(); } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub new ExamTest02(); } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服