按照題目要求編寫的用javaBean規(guī)范設(shè)計的學(xué)生類Student的Java程序如下
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、君山網(wǎng)站維護、網(wǎng)站推廣。
需要創(chuàng)建user.java.test包,把Student.java文件和Test.java文件放入包中,編譯Student.java文件并且編譯運行Test.java文件得到運行結(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?Product?{
private?String?productName;
private?double?costPrice;
private?double?price;
private?java.util.Date?outDate;
private?String?batch;
public?String?getProductName()?{
return?productName;
}
public?void?setProductName(String?productName)?{
this.productName?=?productName;
}
public?double?getCostPrice()?{
return?costPrice;
}
public?void?setCostPrice(double?costPrice)?{
this.costPrice?=?costPrice;
}
public?double?getPrice()?{
return?price;
}
public?void?setPrice(double?price)?{
this.price?=?price;
}
public?java.util.Date?getOutDate()?{
return?outDate;
}
public?void?setOutDate(java.util.Date?outDate)?{
this.outDate?=?outDate;
}
public?String?getBatch()?{
return?batch;
}
public?void?setBatch(String?batch)?{
this.batch?=?batch;
}
/**
?*?判斷產(chǎn)品是否過期
?*?@param?product
?*?@return?是否過期
?*?@returnType?boolean
?*/
public?boolean?isOutDate(Product?product){
return?new?java.util.Date().getTime()??product.getOutDate().getTime();
}
/**
?*?計算毛利潤
?*?@param?product
?*?@return?毛利潤
?*?@returnType?double
?*/
public?double?getGrossProfit(Product?product){
return?product.getPrice()?-?product.getCostPrice();
}
/**
?*?修改產(chǎn)品價格
?*?@param?product
?*?@param?price
?*/
public?void?updateProductPrice(Product?product,?double?price){
product.setPrice(price);
}
/**
?*?打折出售
?*?@param?product
?*?@param?price
?*/
public?void?discount(Product?product,?double?discount){
product.setPrice(product.getPrice()?*?discount);
}
}
import?java.io.BufferedReader;
import?java.io.InputStreamReader;
public?class?Demo?{
public?static?void?main(String[]?args)?throws?Exception?{
BufferedReader?in?=?new?BufferedReader(new?InputStreamReader(System.in));
System.out.print("請輸入密碼:");
try{
String?pass?=?in.readLine();
if(pass.length()??6?||?pass.length()??4){
System.err.println("長度錯誤,密碼長度必須是4-6位");
return;
}
int?password?=?Integer.parseInt(pass);
System.out.println("信息合法");
}catch(Exception?e){
System.err.println("錯誤,輸入的密碼必須為數(shù)字");
}
}
}
運行代碼
具體的代碼如下:
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'字符的個數(shù)是:" + count);
}
}
運行結(jié)果
創(chuàng)建一個名字為“ReportCard”的類,然后用下邊的內(nèi)容全部替換掉,你會成為全班最亮的仔。
import java.util.HashMap;
/**
* 學(xué)生成績單
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("張三", "070602213");
reportCard.set("語文", 80.0);
reportCard.set("數(shù)學(xué)", 59.5);
reportCard.set("英語", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("數(shù)據(jù)庫", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "語文分數(shù):" + reportCard.get("語文"));
System.out.println(reportCard.getStudentName() + "數(shù)學(xué)考核結(jié)果:" + (reportCard.isPassed("數(shù)學(xué)") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否掛科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 學(xué)生姓名
private String studentName;
// 學(xué)生學(xué)號
private String studentNumber;
// 成績單
private HashMapString, CourseResult cards = new HashMap();
public ReportCard() {
}
public ReportCard(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public Double get(String courseName){
CourseResult courseResult = cards.get(courseName);
return courseResult == null ? Double.NaN : courseResult.getStudentScore();
}
public void set(String courseName, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, studentScore);
cards.put(courseName, courseResult);
}
public void set(String courseName, double passMark, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, passMark, studentScore);
cards.put(courseName, courseResult);
}
public boolean isPassed(String courseName){
return cards.get(courseName).isPassed();
}
public boolean isAllPassed(){
for(CourseResult cr : cards.values()){
if ( ! cr.isPassed()) {
return false;
}
}
return true;
}
public String getStudentName() {
return studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
/**
* 課程
*/
class Course{
// 課程名稱
protected String courseName;
// 及格分
protected double passMark = 60;
public Course(String courseName, Double passMark) {
this.courseName = courseName;
if ( passMark != null) {
this.passMark = passMark;
}
}
}
/**
* 課程成績
*/
class CourseResult extends Course{
// 學(xué)生成績
private Double studentScore;
public CourseResult(String courseName, Double studentScore) {
this(courseName, null, studentScore);
}
public CourseResult(String courseName, Double passMark, Double studentScore) {
super(courseName, passMark);
this.studentScore = studentScore == null ? Double.NaN : studentScore;
}
public boolean isPassed(){
return studentScore = passMark;
}
public String getCourseName() {
return courseName;
}
public double getPassMark() {
return passMark;
}
public Double getStudentScore() {
return studentScore;
}
}