java期末大作业代码 java大作业题目

java练习题求完整代码按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果
Student.java文件代码如下
package user.java.test;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String no;
private String name;
private double score;
public Student(){}
public Student(String no,String name,double score){
this.no=no;
this.name=name;
this.score=score;
}
public String getNo(){ return no;}
public void setNo(String no){ this.no=no;}
public String getName(){ return name;}
public void setName(String name){ this.name=name;}
public double getScore(){ return score;}
public void setScore(double score){ this.score=score;}
public String toString(){
return "学号:" no ",姓名:" name ",成绩:" score;
}
public static double getAvg(Student[] sArray){
double sum=0,avg;
for(int i=0;isArray.length;i){
sum=sum sArray[i].getScore();
}
avg=sum/sArray.length;
return avg;
}
}
Test.java文件代码如下
package user.java.test;
public class Test{
public static void main(String[] args){
Student[] sArray=new Student[5];
sArray[0]=new Student("001","张三",89.5);
sArray[1]=new Student("002","李四",82.5);
sArray[2]=new Student("003","王五",93);
sArray[3]=new Student("004","赵六",73.5);
sArray[4]=new Student("005","孙七",66);
System.out.println("这些学生的平均分:" Student.getAvg(sArray));
for(int i=0;isArray.length;i){
System.out.println(sArray[i].toString());
}
}
}
求大神帮忙编两个java代码(学生java作业)第一题: 元素的复制
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
int[] scores = {91,85,98,62,78,93};
int[] newScores=Arrays.copyOfRange(scores, 0, 5);//复制元素, 左开右闭区间[0,5)
System.out.println(Arrays.toString(newScores));//调用数组工具类的方法转成字符串并打印
}
}
第二题: 这题虽然使用集合更方便 ,但却是非常好的一维数组的训练题目.
解法一: 集合解决 随机产生7个不重复的数字很简单
import java.util.HashSet;
import java.util.Random;
public class NumberTest {
public static void main(String[] args) {
HashSetInteger set=new HashSetInteger();//元素不可重复的无序集合
Random rd=new Random();//随机产生器
while(set.size()7) {
set.add(rd.nextInt(36) 1);//产生1~36的随机数
//如果元素重复, 那么添加不上去
}
System.out.println(set);
}
}
解法二:一维数组 ,解决产生7个数字, 并升序排列
int[]nums 数组存储1~36个数组
boolean[] flags 数组存储的是和nums数组一一对应的true或者false,如果使用了就标记为true.,如果没有使用标记为false,
例如 随机产生了一个下标0,那么查看flags[0] ,如果是true, 那么说明该元素已经使用了,重新产生一个随机数, 如果是false ,那么表示nums[0]没有被使用
具体代码如下(稍微留个尾巴, 就是中不中的判断, 可以把两个数组都升序排序,然后元素一一比较,全部相同就是中了)
import java.util.Arrays;
import java.util.Random;
public class NumberDemo {
public static void main(String[] args) {
int[] nums= new int[36];//长度为36的数组 ,默认全是0
for (int i = 0; inums.length; i) {//利用for循环赋值1~36
nums[i]=i 1;
}
boolean[] flags=new boolean[nums.length];//长度和nums相同的数组,默认值全是false ,表示全部没有使用过
//用boolean值表示对应的nums里的元素是否被使用
int[] result=new int[7];//存储结果
Random rd = new Random();
for (int i = 0; iresult.length; i) {
int temp=rd.nextInt(nums.length);//随机产生下标
//System.out.println(Arrays.toString(result));
if(flags[temp]) {//如果已经被使用,那么i-1,并在此循环
i--;
//System.out.println("号码" nums[temp] "已经存在.再次循环");
}else {
result[i]=nums[temp];
flags[temp]=true;//标记true表示已经使用了
}
}
System.out.println("原始排序:" Arrays.toString(result));
Arrays.sort(result);//升序排列
System.out.println("升序排列:" Arrays.toString(result));
}
}
需要一份500行的java程序,期末大作业 , 最好带详细注释 。Java生成CSV文件简单操作实例
CSV是逗号分隔文件(Comma Separated Values)java期末大作业代码的首字母英文缩写java期末大作业代码,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件 。在 CSV文件中,数据“栏”以逗号分隔 , 可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏 。如java期末大作业代码:
1231,张三,男2,李四,男3,小红,女
Java生成CSV文件(创建与导出封装类)
package com.yph.omp.common.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
/**
* Java生成CSV文件
*/
public class CSVUtil {
/**
* 生成为CVS文件
*
* @param exportData
*源数据List
* @param map
*csv文件的列表头map
* @param outPutPath
*文件路径
* @param fileName
*文件名称
* @return
*/
@SuppressWarnings("rawtypes")
public static File createCSVFile(List exportData, LinkedHashMap map,
String outPutPath, String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
file.mkdir();
}
// 定义文件名格式并创建
csvFile = File.createTempFile(fileName, ".csv",
new File(outPutPath));
// UTF-8使正确读取分隔符","
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(csvFile), "GBK"), 1024);
// 写入文件头部
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
csvFileOutputStream
.write("\""(String) propertyEntry.getValue() != null ? (String) propertyEntry
.getValue() : """\"");
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
csvFileOutputStream.newLine();
// 写入文件内容
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
/*-------------------------------*/
//以下部分根据不同业务做出相应的更改
StringBuilder sbContext = new StringBuilder("");
if (null != BeanUtils.getProperty(row,(String) propertyEntry.getKey())) {
if("证件号码".equals(propertyEntry.getValue())){
//避免:身份证号码,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey())"\t");
}else{
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()));
}
}
csvFileOutputStream.write(sbContext.toString());
/*-------------------------------*/
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
if (iterator.hasNext()) {
csvFileOutputStream.newLine();
}
}
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 下载文件
*
* @param response
* @param csvFilePath
*文件路径
* @param fileName
*文件名称
* @throws IOException
*/
public static void exportFile(HttpServletRequest request,
HttpServletResponse response, String csvFilePath, String fileName)
throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/csv;charset=GBK");
response.setHeader("Content-Disposition", "attachment; filename="
new String(fileName.getBytes("GB2312"), "ISO8859-1"));
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer))0) {
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e1) {
System.out.println(e1);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
}
/**
* 删除该目录filePath下的所有文件
*
* @param filePath
*文件目录路径
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; ifiles.length; i) {
if (files[i].isFile()) {
files[i].delete();
}
}
}
}
/**
* 删除单个文件
*
* @param filePath
*文件目录路径
* @param fileName
*文件名称
*/
public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; ifiles.length; i) {
if (files[i].isFile()) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
return;
}
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void createFileTest() {
List exportData = https://www.04ip.com/post/new ArrayListMap();
Map row1 = new LinkedHashMapString, String();
row1.put("1", "11");
row1.put("2", "12");
row1.put("3", "13");
row1.put("4", "14");
exportData.add(row1);
row1 = new LinkedHashMapString, String();
row1.put("1", "21");
row1.put("2", "22");
row1.put("3", "23");
row1.put("4", "24");
exportData.add(row1);
LinkedHashMap map = new LinkedHashMap();
map.put("1", "第一列");
map.put("2", "第二列");
map.put("3", "第三列");
map.put("4", "第四列");
String path = "d:/export";
String fileName = "文件导出";
File file = CSVUtil.createCSVFile(exportData, map, path, fileName);
String fileNameNew = file.getName();
String pathNew = file.getPath();
System.out.println("文件名称:"fileNameNew );
System.out.println("文件路径:"pathNew );
}
}
//注:BeanUtils.getProperty(row,(String) propertyEntry.getKey())"\t" ,只为解决数字格式超过15位后,在Excel中打开展示科学记数问题 。
【java期末大作业代码 java大作业题目】关于java期末大作业代码和java大作业题目的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读