按照題目要求編寫的用javaBean規(guī)范設(shè)計(jì)的學(xué)生類Student的Java程序如下
創(chuàng)新互聯(lián)致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,包括成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、SEO優(yōu)化、網(wǎng)絡(luò)推廣、整站優(yōu)化營銷策劃推廣、電子商務(wù)、移動(dòng)互聯(lián)網(wǎng)營銷等。創(chuàng)新互聯(lián)為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制及解決方案,創(chuàng)新互聯(lián)核心團(tuán)隊(duì)十多年專注互聯(lián)網(wǎng)開發(fā),積累了豐富的網(wǎng)站經(jīng)驗(yàn),為廣大企業(yè)客戶提供一站式企業(yè)網(wǎng)站建設(shè)服務(wù),在網(wǎng)站建設(shè)行業(yè)內(nèi)樹立了良好口碑。
需要?jiǎng)?chuàng)建user.java.test包,把Student.java文件和Test.java文件放入包中,編譯Student.java文件并且編譯運(yùn)行Test.java文件得到運(yùn)行結(jié)果
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 "學(xué)號:"+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("這些學(xué)生的平均分:"+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";
//字節(jié)流拷貝
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();
}
}
運(yùn)行代碼
具體的代碼如下:
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'字符的個(gè)數(shù)是:" + count);
}
}
運(yùn)行結(jié)果
1、public static void main(String[] args)
2、public finall static
3、3
4、抽象類
5、implements
6、類:一個(gè)包含屬性、方法,使用class關(guān)鍵字定義
對象:創(chuàng)建對象必須使用new關(guān)鍵字,對象其實(shí)在內(nèi)存中存儲(chǔ)的是類的引用地址。
7、try是可能發(fā)生異常的內(nèi)容;
catch是發(fā)生異常后要處理的代碼;
finally是始終執(zhí)行的,比如關(guān)閉資源等。
8、
public?class?Demo?{
//小明今年12歲,他母親比他大20歲。編程計(jì)算多少年后他母親的年齡是小明的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++;
}
}
}
}