大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
创新互联公司专业为企业提供石阡网站建设、石阡做网站、石阡网站设计、石阡网站制作等企业网站建设、网页设计与制作、石阡企业网站模板建站服务,10多年石阡做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
需要创建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());
}
}
}
下面是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;//结果文本框
private JLabel resultOut = null;//输出提示
private JLabel remindIn = null;//输入提示
//初始化方法
private void init() {
//组件实例化
mainTop = new JFrame("阶乘计算器");
inPanel = new JPanel();
outPanel = new JPanel();
count = new JButton("计算");
inText = new JTextField(10);
result = new JTextField(10);
resultOut = new JLabel("计算的结果:");
remindIn = new JLabel("请输入要计算的正整数:");
//添加输入组件
inPanel.add(remindIn);
inPanel.add(inText);
inPanel.add(count);
//添加输出组件
outPanel.add(resultOut);
outPanel.add(result);
result.setEnabled(false);
//设置布局样式
mainTop.setLayout(new BorderLayout());
//主面板添加组件
mainTop.add(inPanel, BorderLayout.NORTH);
mainTop.add(outPanel, BorderLayout.SOUTH);
//设置监听
count.addActionListener(new ButtonListener());
//设置屏显位置
mainTop.setLocation(350 , 300);
mainTop.pack();
mainTop.setVisible(true);
mainTop.setAlwaysOnTop(true);
//设置关闭事件
mainTop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//监听类
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int inputNum = 0;
String input = inText.getText();
//若输入为非数字,提示错误
try {
inputNum = Integer.parseInt(input);
}catch(Exception e) {
inputNum = -1;
}
//输入数值小于0,提示错误
if(inputNum 0) {
inText.setText("请输入正确的数值!");
result.setText("");
}
else if(inputNum == 0) {//输入值为0处理
result.setText("1");
}
else {//输入值为正整数,计算阶乘
int temp = inputNum;
while(inputNum 1) {
temp *= --inputNum;
}
result.setText(temp + "");
}
}
}
//入口方法
public static void main(String[] args) {
new PlusWhile().init();
}
}
有什么不明白的继续补充,呵呵。 加点儿分不。
创建一个名字为“ReportCard”的类,然后用下边的内容全部替换掉,你会成为全班最亮的仔。
import java.util.HashMap;
/**
* 学生成绩单
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("张三", "070602213");
reportCard.set("语文", 80.0);
reportCard.set("数学", 59.5);
reportCard.set("英语", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("数据库", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "语文分数:" + reportCard.get("语文"));
System.out.println(reportCard.getStudentName() + "数学考核结果:" + (reportCard.isPassed("数学") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否挂科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 学生姓名
private String studentName;
// 学生学号
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{
// 学生成绩
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课程设计题目及代码分别是:
1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。
设计要求是设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。
2、代码:
数字按钮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课程设计题目及代码是:
题目:华容道。编写一个按钮的子类,使用该子类创建的对象代表华容道中的人物。通过焦点事件控制人物颜色,当人物获得焦点时颜色为蓝色,当失去焦点时颜色为灰色。
通过键盘事件和鼠标事件来实现曹操、关羽等人物的移动。当人物上发生鼠标事件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半部分)或按下键盘的“↓“键,该人物向下移动。向左、向右和向上的移动原理类似。
代码是:
String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"}.
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);