按照題目要求編寫的用javaBean規(guī)范設(shè)計(jì)的學(xué)生類Student的Java程序如下
創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供石阡網(wǎng)站建設(shè)、石阡做網(wǎng)站、石阡網(wǎng)站設(shè)計(jì)、石阡網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、石阡企業(yè)網(wǎng)站模板建站服務(wù),10多年石阡做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
需要?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é)號(hào):"+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());
}
}
}
下面是WINDOW界面的代碼,用JAVA SWING寫的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlusWhile {
//主面板
private JFrame mainTop = null;
private JPanel inPanel = null; //輸入面板
private JPanel outPanel = null;//輸出面板
private JButton count = null; //按鈕
private JTextField inText = null; // 輸入文本框
private JTextField result = null;//結(jié)果文本框
private JLabel resultOut = null;//輸出提示
private JLabel remindIn = null;//輸入提示
//初始化方法
private void init() {
//組件實(shí)例化
mainTop = new JFrame("階乘計(jì)算器");
inPanel = new JPanel();
outPanel = new JPanel();
count = new JButton("計(jì)算");
inText = new JTextField(10);
result = new JTextField(10);
resultOut = new JLabel("計(jì)算的結(jié)果:");
remindIn = new JLabel("請(qǐng)輸入要計(jì)算的正整數(shù):");
//添加輸入組件
inPanel.add(remindIn);
inPanel.add(inText);
inPanel.add(count);
//添加輸出組件
outPanel.add(resultOut);
outPanel.add(result);
result.setEnabled(false);
//設(shè)置布局樣式
mainTop.setLayout(new BorderLayout());
//主面板添加組件
mainTop.add(inPanel, BorderLayout.NORTH);
mainTop.add(outPanel, BorderLayout.SOUTH);
//設(shè)置監(jiān)聽
count.addActionListener(new ButtonListener());
//設(shè)置屏顯位置
mainTop.setLocation(350 , 300);
mainTop.pack();
mainTop.setVisible(true);
mainTop.setAlwaysOnTop(true);
//設(shè)置關(guān)閉事件
mainTop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//監(jiān)聽類
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int inputNum = 0;
String input = inText.getText();
//若輸入為非數(shù)字,提示錯(cuò)誤
try {
inputNum = Integer.parseInt(input);
}catch(Exception e) {
inputNum = -1;
}
//輸入數(shù)值小于0,提示錯(cuò)誤
if(inputNum 0) {
inText.setText("請(qǐng)輸入正確的數(shù)值!");
result.setText("");
}
else if(inputNum == 0) {//輸入值為0處理
result.setText("1");
}
else {//輸入值為正整數(shù),計(jì)算階乘
int temp = inputNum;
while(inputNum 1) {
temp *= --inputNum;
}
result.setText(temp + "");
}
}
}
//入口方法
public static void main(String[] args) {
new PlusWhile().init();
}
}
有什么不明白的繼續(xù)補(bǔ)充,呵呵。 加點(diǎn)兒分不。
創(chuàng)建一個(gè)名字為“ReportCard”的類,然后用下邊的內(nèi)容全部替換掉,你會(huì)成為全班最亮的仔。
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() + "語文分?jǐn)?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é)號(hào)
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;
}
}
java課程設(shè)計(jì)題目及代碼分別是:
1、題目:計(jì)算器。設(shè)計(jì)內(nèi)容是設(shè)計(jì)一個(gè)圖形界面(GUI)的計(jì)算器應(yīng)用程序,完成簡單的算術(shù)運(yùn)算。
設(shè)計(jì)要求是設(shè)計(jì)的計(jì)算器應(yīng)用程序可以完成家法、減法、乘法、除法和取余運(yùn)算。且有小數(shù)點(diǎn)、正負(fù)號(hào)、求倒數(shù)、退格和清零功能。
2、代碼:
數(shù)字按鈕NumberButton類如下:
import java.awt.
import java.awt.event.
import javax.swing.
public class NumberButton extends Button.
{
int number.
public NumberButton(int number).
{
super(""+number).
this.number=number.
setForeground(Color.blue).
}
public int getNumber().
{
return number;
}
}
其它java課程設(shè)計(jì)題目及代碼是:
題目:華容道。編寫一個(gè)按鈕的子類,使用該子類創(chuàng)建的對(duì)象代表華容道中的人物。通過焦點(diǎn)事件控制人物顏色,當(dāng)人物獲得焦點(diǎn)時(shí)顏色為藍(lán)色,當(dāng)失去焦點(diǎn)時(shí)顏色為灰色。
通過鍵盤事件和鼠標(biāo)事件來實(shí)現(xiàn)曹操、關(guān)羽等人物的移動(dòng)。當(dāng)人物上發(fā)生鼠標(biāo)事件或鍵盤事件時(shí),如果鼠標(biāo)指針的位置是在人物的下方(也就是組件的下半部分)或按下鍵盤的“↓“鍵,該人物向下移動(dòng)。向左、向右和向上的移動(dòng)原理類似。
代碼是:
String name[]={"曹操","關(guān)羽","張","劉","馬","許","兵","兵","兵","兵"}.
for(int i=0;iname.length;i++).
{
person[i]=new Person(i,name[i]).
person[i].addKeyListener(this).
person[i].addMouseListener(this).
//? ? ?person[i].addFocusListener(new Person).
add(person[i]).
}
person[0].setBounds(104,54,100,100).
person[1].setBounds(104,154,100,50).
person[2].setBounds(54,154,50,100).
person[3].setBounds(204,154,50,100).
person[4].setBounds(54,54,50,100).
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);