十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于成都定制网页设计,高端网页制作,对宴会酒店设计等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业成都网站营销优化,H5建站,响应式网站。
需要创建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());
}
}
}
你好,代码如下。需要修改的话,你可以根据情况修改:
class Info{ // 定义信息类
private String name = "生产者"; // 定义name属性
private String content = "压入子弹" ; // 定义content属性
private boolean flag = false ; // 设置标志位
public synchronized void set(String name,String content){
if(!flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
this.setName(name) ; // 设置名称
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
this.setContent(content) ; // 设置内容
flag = false ; // 改变标志位,表示可以取走
super.notify() ;
}
public synchronized void get(){
if(flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
System.out.println(this.getName() +
" -- " + this.getContent()) ;
flag = true ; // 改变标志位,表示可以生产
super.notify() ;
}
public void setName(String name){
this.name = name ;
}
public void setContent(String content){
this.content = content ;
}
public String getName(){
return this.name ;
}
public String getContent(){
return this.content ;
}
};
class Producer implements Runnable{ // 通过Runnable实现多线程
private Info info = null ; // 保存Info引用
public Producer(Info info){
this.info = info ;
}
public void run(){
boolean flag = false ; // 定义标记位
for(int i=0;i12;i++){
if(flag){
this.info.set("生产者","压入子弹") ; // 设置名称
flag = false ;
}else{
this.info.set("消费者","射出子弹") ; // 设置名称
flag = true ;
}
}
}
};
class Consumer implements Runnable{
private Info info = null ;
public Consumer(Info info){
this.info = info ;
}
public void run(){
for(int i=0;i24;i++){
this.info.get() ;
}
}
};
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info = new Info(); // 实例化Info对象
Producer pro = new Producer(info) ; // 生产者
Consumer con = new Consumer(info) ; // 消费者
new Thread(pro).start() ;
new Thread(con).start() ;
}
};
此题的Employee类设计有缺陷,缺少员工薪水的属性。根据题意只能写出下面的程序:
abstract class Employee {
public abstract double earnings();
}
class YearWorker extends Employee {
@Override
public double earnings() {
return 0;
}
}
class MonthWorker extends Employee {
@Override
public double earnings() {
return 0;
}
}
class WeekWorker extends Employee {
@Override
public double earnings() {
return 0;
}
}
class Company {
Employee[] employee;
double salaries = 0;
Company(Employee[] employee) {
this.employee = employee;
}
public double salariesPay() {
salaries = 0;
//计算salaries
for(int i=0; iemployee.length; i++) {
salaries += employee[i].earnings();
}
return salaries;
}
}
public class HardWork {
public static void main(String[] args) {
Employee[] employee = new Employee[20];
for(int i=0; iemployee.length; i++) {
if(i % 3 == 0) {
employee[i] = new YearWorker();
}
else if(i % 3 == 1) {
employee[i] = new MonthWorker();
}
else if(i % 3 == 2) {
employee[i] = new WeekWorker();
}
}
Company company = new Company(employee);
System.out.println("公司年工资总额:" + company.salariesPay());
}
}
运行结果:
公司年工资总额:0.0
package xinguan;
abstract class Operation{ //抽象类
public static double numberA= 0;
public static double numberB = 0;
abstract double getResult(); //抽象方法
}
class OperationADD extends Operation{
@Override
double getResult() {
return numberA+numberB;
}
}
class OperationSUB extends Operation{
@Override
double getResult() {
return numberA-numberB;
}
}
class OperationMUL extends Operation{
@Override
double getResult() {
return numberA*numberB;
}
}
class OperationDIV extends Operation{
@Override
double getResult() {
return numberA/numberB;
}
}
class OperationFactory{
public static Operation createOperate(char operate){
Operation oper = null;
switch (operate){
case'+':
oper= new OperationADD();
break;
case'-':
oper= new OperationSUB();
break;
case'*':
oper= new OperationMUL();
break;
case'/':
oper= new OperationDIV();
break;
}
return oper;
}
}
public class CalculateDemo {
/**
* @param args
*/
public static void main(String[] args) {
Operation operADD = OperationFactory.createOperate('+');
Operation operSUB = OperationFactory.createOperate('-');
Operation operMUL = OperationFactory.createOperate('*');
Operation operDIV = OperationFactory.createOperate('/');
operADD.numberA = 15.0;
operADD.numberB = 3;
System.out.println(operADD.getResult());
System.out.println(operSUB.getResult());
System.out.println(operMUL.getResult());
System.out.println(operDIV.getResult());
}
}
因为抽象类是静态方法 所以 给operADD 那个对象赋值一次就能获得所有结果。要是去掉static 那么就需要每个对象 赋值。现在基本满足你的要求了。