重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
创新互联致力于互联网品牌建设与网络营销,包括成都网站设计、成都做网站、外贸网站建设、SEO优化、网络推广、整站优化营销策划推广、电子商务、移动互联网营销等。创新互联为不同类型的客户提供良好的互联网应用定制及解决方案,创新互联核心团队十多年专注互联网开发,积累了丰富的网站经验,为广大企业客户提供一站式企业网站建设服务,在网站建设行业内树立了良好口碑。
需要创建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());
}
}
}
百度好像不支持代码格式了,下面的代码供题主参考。
public class CopyFile {
public static void main(String[] args) throws IOException {
String srcFilePath = "D:/src.txt";
String destFilePath1 = "D:/des1.txt";
String destFilePath2 = "D:/des2.txt";
//字节流拷贝
copyFileWithStream(srcFilePath,destFilePath1);
//字符流拷贝
copyFileWithReader(srcFilePath,destFilePath2);
}
public static void copyFileWithStream(String srcFilePath,String destFilePath) throws IOException {
InputStream is = new FileInputStream(srcFilePath);
FileOutputStream os = new FileOutputStream(destFilePath);
byte[] buff = new byte[1024];
int len;
while((len=is.read(buff)) != -1){
os.write(buff);
}
os.flush();
is.close();
os.close();
}
public static void copyFileWithReader(String srcFilePath,String destFilePath) throws IOException {
Reader reader = new FileReader(srcFilePath);
BufferedReader buffReader = new BufferedReader(reader);
Writer wirter = new FileWriter(destFilePath);
BufferedWriter buffWriter = new BufferedWriter(wirter);
String line;
while((line=buffReader.readLine()) != null) {
buffWriter.write(line);
}
buffWriter.flush();
reader.close();
wirter.close();
}
}
运行代码
具体的代码如下:
import java.util.HashMap;
public class SearchDemo {
public HashMapInteger,Integer countMap(char[] arr){
HashMap countMap = new HashMapInteger, Integer();
for (int i = 0; i arr.length; i++) {
if(!countMap.containsKey(arr[i])){
countMap.put(arr[i],1);
}else{
int value = (int)countMap.get(arr[i]) + 1;
countMap.put(arr[i],value);
}
}
return countMap;
}
public static void main(String[] args) {
char[] arr={'A','1','R','!','e','','A','5','A','g','#','D'};
int count = new SearchDemo().countMap(arr).get('A');
System.out.println("其中'A'字符的个数是:" + count);
}
}
运行结果
1、public static void main(String[] args)
2、public finall static
3、3
4、抽象类
5、implements
6、类:一个包含属性、方法,使用class关键字定义
对象:创建对象必须使用new关键字,对象其实在内存中存储的是类的引用地址。
7、try是可能发生异常的内容;
catch是发生异常后要处理的代码;
finally是始终执行的,比如关闭资源等。
8、
public class Demo {
//小明今年12岁,他母亲比他大20岁。编程计算多少年后他母亲的年龄是小明的2倍。
public static void main(String[] args) {
System.out.println(getYeah());
}
public static int getYeah(){
int xmAge = 12,mqAge = 20,yeah = 0;
while(true){
if(mqAge / 2 == xmAge){
return yeah;
}
mqAge++;
yeah++;
}
}
}
9、
public class Demo {
public static void main(String[] args) {
new Thread(new Piao()).start();
new Thread(new Piao()).start();
new Thread(new Piao()).start();
new Thread(new Piao()).start();
}
}
class Piao implements Runnable{
private static int piao = 1;
public void run(){
while(true){
synchronized(Piao.class){
if(piao 100){
System.exit(0);
}
System.out.println(Thread.currentThread().getName()+"出票:" + piao + "号");
piao++;
}
}
}
}