您的当前位置:首页正文

Java习题答案

2023-01-26 来源:易榕旅网
6.2 修改本章student类,每个学生对象包含三次成绩测验,利用参数设置所有实例值,通过重载构造方法,将每次测验的初始值设置为零,在提供哟个setTestScore方法,接收两个参数,测验编号(1-3)与成绩,同时在提供一个getTestScore方法,接受测验编号并返回对应成绩。提供一个average方法,计算并返回该学生的平均成绩。修改tostring方法,返回的字符串因包含该学生的测验成绩与平均成绩。设置main(): public class Address { private String streeAddress,city,state; private long zipCode; public Address(String a,String b,String c,long d){ streeAddress=a; city=b; state=c; zipCode=d; } public String toString(){ String str; str=streeAddress+\"\\n\"+city+\ return str; } }

public class Student { private String firstName,lastName; private Address homeAddress,schoolAddress; private float score1,score2,score3; public Student (String a,String b,Address c,Address d){ firstName=a; lastName=b; homeAddress=c; schoolAddress=d; score1=0; score2=0; score3=0; } public void setTestScore(int x,float y){ switch(x){ case 1:score1=y;break; case 2:score2=y;break; case 3:score3=y;break; } } public float getTestScore(int x){ if(x==1) return score1;

else if(x==2) return score2; else return score3; } public float average(){ float ave; ave=(score1+score2+score3)/3; return ave; } public String toString(){ String str; str=firstName+\" \"+lastName+\"\\n\"; str+=\"Home Address:\\n\"+homeAddress+\"\\n\"+\"School Address:\\n\"+schoolAddress+\"\\n\"; str+=\"TextScore(1~3): \"+score1+\" ; \"+score2+\" ; \"+score3+\"\\n\"+\"Average: \"+average(); return str; } }

import javax.swing.JOptionPane; public class StudentTest { public static void main(String[] args) { String frist=JOptionPane.showInputDialog(null,\"Frist name:\"); String last=JOptionPane.showInputDialog(null,\"Last name:\"); JOptionPane.showMessageDialog(null,\"Input the home address:\"); String a=JOptionPane.showInputDialog(null,\"Street address:\"); String b=JOptionPane.showInputDialog(null,\"City:\"); String c=JOptionPane.showInputDialog(null,\"State:\"); String input1=JOptionPane.showInputDialog(null,\"Zip code:\"); long d=Integer.parseInt(input1); Address home=new Address(a,b,c,d); JOptionPane.showMessageDialog(null,\"Input the school address:\"); String e=JOptionPane.showInputDialog(null,\"Street address:\"); String f=JOptionPane.showInputDialog(null,\"City:\"); String g=JOptionPane.showInputDialog(null,\"State:\"); String input2=JOptionPane.showInputDialog(null,\"Zip code:\"); long h=Integer.parseInt(input2); Address school=new Address(e,f,g,h); Student student=new Student(frist,last,home,school); int again=0; while(again==0){ String input3=JOptionPane.showInputDialog(null,\"测验次数:\"); int i=Integer.parseInt(input3); if(i>3){

JOptionPane.showMessageDialog(null,\"次数应低于3!\"); continue; } String input4=JOptionPane.showInputDialog(null,\"the score:\"); float j=Float.parseFloat(input4); student.setTestScore(i, j); again=JOptionPane.showConfirmDialog(null,\"Again?\"); } again=JOptionPane.showConfirmDialog(null,\"查询测验成绩?\"); if(again==0){ while(again==0){ String input3=JOptionPane.showInputDialog(null,\"测验次数:\"); int i=Integer.parseInt(input3); if(i>3){ JOptionPane.showMessageDialog(null,\"次数应低于3!\"); continue; } String str=\"第 \"+i+\" 次成绩为: \"+student.getTestScore(i); JOptionPane.showMessageDialog(null,str); again=JOptionPane.showConfirmDialog(null,\"Again?\"); } } JOptionPane.showMessageDialog(null,student.toString()); } }

6.3 设计并实现一个表示在校课程的Course类,一个课程对象至少掌握五名学生的学习情况,可以使用上一个student类,Course类的构造方法只接受课程名臣。提供一个addStudent的方法,接受一个Student参数(Course对相应记录选修此课程全部有效学生)。提供一个averrage方法,计算并返回全部学生的平均分。在提供一个roll方法,打印选修这门课程的所有学生。main方法,创建一个这样课程,再添加几名学生,最后打印出学生名册和测验成绩。

import javax.swing.JOptionPane;

public class Course { private String classname; private Student[] student=new Student[30]; int i=0; public Course(String a,Student b){ classname=a; student[i]=b; i++; }

public void addStudebt(Student a){ student[i]=a; i++; } public float average(){ float ave=0,sum=0; for(int j=0;jimport javax.swing.*; public class Coursetest { public static void main(String[] args) { Student student; Course course; int again=0; JOptionPane.showMessageDialog(null,\"输入课程名称:\"); String name=JOptionPane.showInputDialog(null,\"Course name:\"); JOptionPane.showMessageDialog(null,\"输入学生信息:\"); String frist=JOptionPane.showInputDialog(null,\"Frist name:\"); String last=JOptionPane.showInputDialog(null,\"Last name:\"); JOptionPane.showMessageDialog(null,\"Input the home address:\"); String a=JOptionPane.showInputDialog(null,\"Street address:\"); String b=JOptionPane.showInputDialog(null,\"City:\"); String c=JOptionPane.showInputDialog(null,\"State:\"); String input1=JOptionPane.showInputDialog(null,\"Zip code:\"); long d=Integer.parseInt(input1); Address home=new Address(a,b,c,d); JOptionPane.showMessageDialog(null,\"Input the school address:\"); String e=JOptionPane.showInputDialog(null,\"Street address:\"); String f=JOptionPane.showInputDialog(null,\"City:\"); String g=JOptionPane.showInputDialog(null,\"State:\");

String input2=JOptionPane.showInputDialog(null,\"Zip code:\"); long h=Integer.parseInt(input2);

Address school=new Address(e,f,g,h);

student=new Student(frist,last,home,school); course=new Course(name,student);

again=JOptionPane.showConfirmDialog(null,\"增加学生?\"); if(again==0){ while(again==0){ JOptionPane.showMessageDialog(null,\"输入学生信息:\"); frist=JOptionPane.showInputDialog(null,\"Frist name:\"); last=JOptionPane.showInputDialog(null,\"Last name:\"); JOptionPane.showMessageDialog(null,\"Input the home address:\"); a=JOptionPane.showInputDialog(null,\"Street address:\"); b=JOptionPane.showInputDialog(null,\"City:\"); c=JOptionPane.showInputDialog(null,\"State:\");

input1=JOptionPane.showInputDialog(null,\"Zip code:\"); d=Integer.parseInt(input1); home=new Address(a,b,c,d); JOptionPane.showMessageDialog(null,\"Input the school address:\"); e=JOptionPane.showInputDialog(null,\"Street address:\"); f=JOptionPane.showInputDialog(null,\"City:\"); g=JOptionPane.showInputDialog(null,\"State:\"); input2=JOptionPane.showInputDialog(null,\"Zip code:\"); h=Integer.parseInt(input2); school=new Address(e,f,g,h); student=new Student(frist,last,home,school); course.addStudebt(student); again=JOptionPane.showConfirmDialog(null,\"Again?\"); } }

again=0;

for(int k=0;k3){ JOptionPane.showMessageDialog(null,\"次数应低于3!\"); continue; } String input4=JOptionPane.showInputDialog(null,\"the score:\"); float j=Float.parseFloat(input4); course.setScore(k,i,j); again=JOptionPane.showConfirmDialog(null,\"Again?\");

} } String str=\"所有学生平均成绩: \"+course.average(); JOptionPane.showMessageDialog(null,str); course.roll(); } }

8.1 设计并实践从coin中导出MonetaryCoin类。储存硬币面值,同时添加返回面值的方法。通过创建main实例多个对象并计算面值总和。 public class Coin { private final int HEADS=0; private final int TAILS=1; private int face; public Coin(){ flip(); } public void flip(){ face=(int)(Math.random()*2); } public boolean isHeads(){ return(face==HEADS); } public String toString(){ String faceName; if(face==HEADS) faceName=\"Hedas\"; else faceName=\"Tails\"; return faceName; } }

class MonetaryCoin extends Coin{ private int money; public MonetaryCoin(int a){ money=a; flip(); } public int get(){ return money; } }

import javax.swing.*; public class Cointest {

public static void main(String[] args) { String input=JOptionPane.showInputDialog(null,\"输入要投的硬币数:\"); int n=Integer.parseInt(input); MonetaryCoin[] coin=new MonetaryCoin[n]; String str=\"投掷结果:\\n\"; for(int i=0;i8.2设计代表医院职工的类,有医生,护士,管理人员等。依据不同员工定义相应的方法,并打印相关信息。 import javax.swing.*;

public abstract class Hospital { String name,sex; int age; abstract void print(); public String get_name(String a){ if(a.equals(name)) return name; else return null; } public int get_age(int a){ if(age==a) return age; else return 0; } public String get_sex(String a){ if(a.equals(sex)) return sex; else return null; }

public void set_name(String a){ name=a; } public void set_age(int a){ age=a; } public void set_sex(String a){ sex=a; } }

class Doctor extends Hospital{ String keshi; public Doctor(String a,int b,String c,String d){ name=a; age=b; sex=c; keshi=d; } public void print(){ JOptionPane.showMessageDialog(null, \"医生\\n姓名: \"+name+\"\\n年龄: \" +age+\"\\n性别: \" +sex+\"\\n科室: \"+keshi); } public String get_keshi(String a){ if(a.equals(keshi)) return keshi; else return null; } public void set_keshi(String a){ keshi=a; } }

class Nurse extends Hospital{ String bingf; public Nurse(String a,int b,String c,String d){ name=a; age=b; sex=c; bingf=d; } public void print(){ JOptionPane.showMessageDialog(null, \"护士\\n姓名: \"+name+\"\\n年龄: \" +age+\"\\n性别: \" +sex+\"\\n负责病房: \"+bingf); }

public String get_bingf(String a){ if(a.equals(bingf)) return bingf; else return null; } public void set_keshi(String a){ bingf=a; } }

class Administrator extends Hospital{ String xiangm; public Administrator(String a,int b,String c,String d){ name=a; age=b; sex=c; xiangm=d; } public void print(){ JOptionPane.showMessageDialog(null, \"管理人员\\n姓名: \"+name+\"\\n年龄: \" +age+\"\\n性别: \" +sex+\"\\n管理项目: \"+xiangm); } public String get_keshi(String a){ if(a.equals(xiangm)) return xiangm; else return null; } public void set_keshi(String a){ xiangm=a; } }

import javax.swing.JOptionPane; public class Hospitaltest { public static void main(String[] args) { Hospital worker; String input=JOptionPane.showInputDialog(null,\"输入职工岗位:\\n(doctor~1; nurse~2; administrator~3)\"); int a=Integer.parseInt(input); String name=JOptionPane.showInputDialog(null,\"输入姓称:\"); input=JOptionPane.showInputDialog(null,\"输入年龄:\"); int age=Integer.parseInt(input); String sex=JOptionPane.showInputDialog(null,\"输入性别:\"); if(a==1){

String keshi=JOptionPane.showInputDialog(null,\"输入工作科室:\"); worker=new Doctor(name,age,sex,keshi); worker.print(); } else if(a==2){ String bingf=JOptionPane.showInputDialog(null,\"输入负责病房:\"); worker=new Nurse(name,age,sex,bingf); worker.print(); } else if(a==3){ String xiangm=JOptionPane.showInputDialog(null,\"输入管理项目:\"); worker=new Administrator(name,age,sex,xiangm); worker.print(); } else JOptionPane.showMessageDialog(null,\"ERROR!!!\"); } }

8.3 设计代表书的类,有小说,杂志,教科书等。类中有不同的数据,并打印相关信息。 import javax.swing.*;

public abstract class Books { String name; int pages; abstract void print(); int get_name(String a){ if(name.equals(a)) return 1; else return 0; } int get_pages(int a){ if(pages==a) return 1; else return 0; } void set_name(String a){ name=a; } void set_pages(int a){ pages=a; } }

class Novels extends Books{

String author; String primary; public Novels(String a,int b,String c,String d){ name=a; pages=b; author=c; primary=d; } void print(){ JOptionPane.showMessageDialog(null,\"书名: \"+name+\"\\n页数: \"+pages+\"\\n作者: \"+author+\"\\n主人公: \"+primary,\"书的信息\ } int get_auther(String a){ if(author.equals(a)) return 1; else return 0; } int get_primary(String a){ if(primary.equals(a)) return 1; else return 0; } void set_auther(String a){ author=a; } void set_primary(String a){ primary=a; } }

class Magazines extends Books{ int num; public Magazines(String a,int b,int c){ name=a; pages=b; num=c; } void print(){ JOptionPane.showMessageDialog(null,\"书名: \"+name+\"\\n页数: \"+pages+\"\\n期号: \"+num,\"书的信息\TION_MESSAGE); } int get_num(int a){ if(num==a)

return 1; else return 0; } void set_num(int a){ num=a; } }

class Textbooks extends Books{ String classes; public Textbooks(String a,int b,String c){ name=a; pages=b; classes=c; } void print(){ JOptionPane.showMessageDialog(null,\"书名: \"+name+\"\\n页数: \"+pages+\"\\n科目: \"+classes,\"书的信息\ } int get_classes(String a){ if(classes.equals(a)) return 1; else return 0; } void set_classes(String a){ classes=a; } }

import javax.swing.*; public class Booktest { public static void main(String[] args) { Books book; String input=JOptionPane.showInputDialog(null,\"输入书的种类:\\n(novel~1;magazine~2;textbook~3)\"); int a=Integer.parseInt(input); String name=JOptionPane.showInputDialog(null,\"输入书的名称:\"); input=JOptionPane.showInputDialog(null,\"输入书的页数:\"); int page=Integer.parseInt(input); if(a==1){ String author=JOptionPane.showInputDialog(null,\"输入书的作者:\"); String primary=JOptionPane.showInputDialog(null,\"输入书的主人公:\"); book=new Novels(name,page,author,primary); book.print();

}

}

}

else if(a==2){ input=JOptionPane.showInputDialog(null,\"输入书的期号:\"); int num=Integer.parseInt(input); book=new Magazines(name,page,num); book.print(); }

else if(a==3){ String classes=JOptionPane.showInputDialog(null,\"输入书的科目:\"); book=new Textbooks(name,page,classes); book.print(); } else JOptionPane.showMessageDialog(null,\"ERROR!!!\");

因篇幅问题不能全部显示,请点此查看更多更全内容